Results 1 to 5 of 5
- 04-07-2011, 06:13 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 44
- Rep Power
- 0
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
- 04-07-2011, 06:31 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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:
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.Java Code:Assign8_Roberts a8r = new Assign8_Roberts(); Rectangle rec1 = a8r.new Rectangle(3,5); ...
- 04-07-2011, 06:33 PM #3
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 - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-07-2011, 06:41 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
Your constructor name also must match your class name.
- 04-07-2011, 08:07 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Java Generics assistance and confusion
By sideswipe091976 in forum Advanced JavaReplies: 9Last Post: 03-30-2011, 08:24 AM -
Learning Generics in Java. Need Assistance
By codex in forum New To JavaReplies: 1Last Post: 03-25-2011, 05:00 PM -
Help w/ java programming assignment counting number of spaces
By clemsontigers in forum New To JavaReplies: 9Last Post: 03-06-2011, 03:00 AM -
help..stuck w/ a java programming assignment
By clemsontigers in forum New To JavaReplies: 15Last Post: 02-24-2011, 09:31 PM -
Help with implement class programming assignment
By ALH813 in forum EclipseReplies: 1Last Post: 10-02-2009, 01:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks