Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default I need some help, for some reason I am getting an error at the very end.

    Here are the instructions.
    Click the title of the assignment to see Due Date and submission features.

    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 2x15, second rectangle should be 3x10 and the third rectangle should be 4x6.

    After you get all this working, do these steps :
    > go back to class Rectangle and add a compareArea method. This method is a Boolean method to compare the areas of two rectangles.
    > main should be expanded to compare the areas of Rectangle 1 and Rectangle 2 by calling compareArea of Rectangle1 passing to it only a pointer of / name of Rectangle2
    > main should be expanded to compare the areas of Rectangle 2 and Rectangle 3 by calling compareArea of Rectangle2 passing to it only 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.

    Use Assign8_{your last name} as the name of the program.

    Your program should have comments towards the top of the program. Copy sample run(s) and paste as a comment at the bottom of the program.

    Submit one file, the Java file only.

    As a general rule; no credit is given to work submitted late. If any consideration is given for partial credit; such decision will be made at the end of the semester; at the point in time when the Course Letter grade is determined.





    Here is my code:
    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
    Last edited by Clemson; 04-11-2014 at 06:52 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: I need some help, for some reason I am getting an error at the very end.

    You haven't asked a question. You need to explain what the error is and show the error message. Also, please wrap your code between [CODE][/CODE] tags.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Do you have any specific questions?
    If you are getting any errors, copy the full text and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    See post#3

    Add one line before the code:
    [code]

    add one line after the code:
    [/code]

    The code is between those two lines.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Here is my code:
    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
    I get this error message: Assign8_abbott.java:44: error: reached end of file while parsing
    } // end program
    ^
    1 error

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    The code has lost all of its indentations making it hard to see where a } matches with its pairing {.
    Please post properly formatted code.

    error: reached end of file while parsing
    That is usually caused by improper matching {}. Formatting the code will make that easier to find where the problem is.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: I need some help, for some reason I am getting an error at the very end.

    You need to add a closing brace to your first class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    When I add the closing brace, I get these errors:
    Assign8_abbott.java:3: error: class, interface, or enum expected
    public static void main( String [] args ) {
    ^
    Assign8_abbott.java:5: error: class, interface, or enum expected
    Rectangle rec2 = new Rectangle(6,4);
    ^
    Assign8_abbott.java:6: error: class, interface, or enum expected
    Rectangle rec3 = new Rectangle( 15 );
    ^
    Assign8_abbott.java:9: error: class, interface, or enum expected
    System.out.println( "Rectangle 1 is bigger than Rectangle 2 ? " + rec1.compareArea(rec2));
    ^
    Assign8_abbott.java:10: error: class, interface, or enum expected
    System.out.println( "Rectangle 2 is bigger than Rectangle 3 ? " + rec2.compareArea(rec3));
    ^
    Assign8_abbott.java:11: error: class, interface, or enum expected
    System.out.println("The perimeter of Rectangle 3 is: " + rec3.getPerimeter() );
    ^
    Assign8_abbott.java:12: error: class, interface, or enum expected
    } // end main()
    ^
    7 errors

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Looks like you're making progress now.

    Where did you add the }?

    Format the code and post it if you need further help.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Here is my code:
    Java Code:
    public class Assign8_Roberts {} < This is where I added the }
    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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Please format the code. It has lost all indentations which are important for solving this kind of problem.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    What do you mean format it? I showed you where I posted the brace.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Sorry, I don't bother trying to read unformatted code. This problem has to do with matching {}s and it is very hard to scan unformatted code to check for matching {}s.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    This is what I did, and these are the errors. (I added the brace beside abbott to make it {}

    Here is my code:
    Java Code:
    public class Assign8_abbott {}
    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

    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:3: error: class, interface, or enum expected
    public static void main( String [] args ) {
    ^
    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:5: error: class, interface, or enum expected
    Rectangle rec2 = new Rectangle(6,4);
    ^
    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:6: error: class, interface, or enum expected
    Rectangle rec3 = new Rectangle( 15 );
    ^
    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:9: error: class, interface, or enum expected
    System.out.println( "Rectangle 1 is bigger than Rectangle 2 ? " + rec1.compareArea(rec2));
    ^
    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:10: error: class, interface, or enum expected
    System.out.println( "Rectangle 2 is bigger than Rectangle 3 ? " + rec2.compareArea(rec3));
    ^
    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:11: error: class, interface, or enum expected
    System.out.println("The perimeter of Rectangle 3 is: " + rec3.getPerimeter() );
    ^
    C:\Users\Bradley Abbott\Documents\Assign8_abbott.java:12: error: class, interface, or enum expected
    } // end main()
    ^
    7 errors

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    The code needs to be formatted with indentations to show the nesting levels of statements.
    A sample:
    Java Code:
       public void aMethod()
       {
           for(int i = 0 ; i < num.length ; i++)
           {
               for(int j = 0 ; j > i ; j++)
               {
                   if(num[i] == num[j])
                       count++;
               }
              for( i = 0 ; i < num.length ; i++)
               System.out.println(num[i] + " occurs " + count + " times ");
           }
       }  // end aMethod()
    Notice how the }s are in the same column as the { they pair with. Makes it easy to see problems.
    Last edited by Norm; 04-11-2014 at 07:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    I don't understand what you mean by indentations? Is that what the error is saying? I need to space it out?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    Our posts crossed. See post#15 for an example of indentations.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    So the error it is giving me is that they are not lined up correctly in the same columns?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: I need some help, for some reason I am getting an error at the very end.

    No, the compiler doesn't care about extra lines or spaces.
    It's humans that need to see where the {}s are so they can find the problem.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Clemson is offline Member
    Join Date
    Feb 2014
    Posts
    49
    Rep Power
    0

    Default Re: I need some help, for some reason I am getting an error at the very end.

    To be honest, I am not good at all at making my code neat. I know that's going to kill me in the future. I just need to know what the problem is.

Page 1 of 2 12 LastLast

Similar Threads

  1. Cannot figure out reason for error..
    By xKxCx in forum New To Java
    Replies: 5
    Last Post: 07-21-2013, 10:54 AM
  2. Replies: 9
    Last Post: 05-28-2013, 09:14 AM
  3. Replies: 6
    Last Post: 05-26-2013, 02:53 AM
  4. [SOLVED] Can't pinpoint reason for this error..
    By Reiyn in forum New To Java
    Replies: 6
    Last Post: 03-30-2009, 06:39 PM

Posting Permissions

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