Results 1 to 5 of 5
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default assistance w/ java programming assignment

    im getting an error on the last curly brace and can figure out what is wrong
    idk if im missing something or what but please help...also if you have any input on something to make the program better please advise

    here is my instructions and code

    Create a class named Rectangle, to have two integer variables width and height. Create a constructor method. Create a getArea method to compute and return the area of the Rectangle ( an integer ). Create a getPerimeter method to compute and return the Perimeter.

    Create a class with the name Assign8_{your last name} , this class would contain the main method. The main method should create 3 rectangles. First rectangle should be 5x6, second rectangle should be 3x10 and the third rectangle should be 4x5.

    After you get all this working, go back to class Rectangle and add a compareArea method. This method is a Boolean method to compare the areas of two rectangles. Next, main should be expanded to compare the areas of Rectangle 1 and Rectangle 2 by calling compareArea of Rectangle1 passing to it a pointer of / name of Rectangle2. Next main should be expanded to compare the areas of Rectangle 2 and Rectangle 3 by calling compareArea of Rectangle2 passing to it a pointer of / name of Rectangle3.

    I expect the output of the program to look something like this :
    Rectangle 1 and rectangle 2 have equal areas
    Rectangle 2 and rectangle 3 not have equal areas
    The method getPerimeter will end-up being not used, that is OK.

    Java Code:
    public class Assign8_Roberts {
    public static void main( String [] args ) {
    Rectangle rec1 = new Rectangle( 3,5);
    Rectangle rec2 = new Rectangle(6,4);
    Rectangle rec3 = new Rectangle( 15 );
    System.out.println( "Rectangle 1 is bigger than Rectangle 2 ? " + rec1.compareArea(rec2));
    System.out.println( "Rectangle 2 is bigger than Rectangle 3 ? " + rec2.compareArea(rec3));
    System.out.println("The perimeter of Rectangle 3 is: " + rec3.getPerimeter() );
    } // end main()
    //private class
    private class Rectangle {
    int height;
    int width;
    // constructor #1
    public Rectangle( int w, int h ) {
    height = h;
    width = w;
    // constructor #2
    public Rectangle( int side ) {
    height = side;
    width = side;
    // method1
    public boolean compareArea( Rectangle anotherRectangle ) {
    return this.getArea() > anotherRectangle.getArea();
    // method2
    public double getArea() {
    return (double)height*width;
    // method3
    public int getPerimeter() {
    return (width + height) * 2;
    } // end program

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Rep Power


    You need to put another closing curly brace after your private class. You'll still get errors after doing that, though - since Rectangle is private, you need to instantiate it through an instance of its enclosing class, for example:

    Java Code:
    Assign8_Roberts a8r = new Assign8_Roberts();
    Rectangle rec1 = Rectangle(3,5);
    Wouldn't you be better off using the default access level for Rectangle (ie, not private)? From the assignment you posted, it doesn't seem like there's any need to make it private.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power


    What error are you getting? I can't really read your code because it's not indented.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  4. #4
    snotmare is offline Member
    Join Date
    Apr 2011
    Rep Power


    Your constructor name also must match your class name.

  5. #5
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Rep Power

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 05:16 AM
  2. Java Generics assistance and confusion
    By sideswipe091976 in forum Advanced Java
    Replies: 9
    Last Post: 03-30-2011, 09:24 AM
  3. Learning Generics in Java. Need Assistance
    By codex in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 06:00 PM
  4. Replies: 9
    Last Post: 03-06-2011, 04:00 AM
  5. Replies: 1
    Last Post: 10-02-2009, 02:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts