Welcome to Dream.In.Code
Become a Java Expert!

Join 150,382 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,185 people online right now. Registration is fast and FREE... Join Now!




making rectangles

 
Reply to this topicStart new topic

making rectangles, how to construct a rectangle in java

alata28
13 Feb, 2008 - 07:15 PM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 5

hello i'm new to this and i really need help!
im taking a java class and i am supposed to do the following but im getting stuck because i dont know where im going wrong or what exactly im doing wrong.
Problem 1: Write program (Rectangle.java) that contains appropriate methods to
calculate area and perimeter of a rectangle.
Also provide a tester class (RectangleTester.java) that contains a main method to
construct three objects (given below), apply methods to the objects for calculating the
area and perimeter, and finally display the results.
rectangle1: Length= 25 Width = 20
rectangle2: Length= 15 Width = 10
rectangle3: Length= 20 Width = 20
The output shall be of the following format:
For rectangle with length = xx and width = yy,
Area = aa.
Perimeter = bb

CODE

public class Rectangle
{
   // instance variables - replace the example below with your own

   int length=0;
   int width=0;
   public static void main (String[] args)
   {
       Rectangle rectangle1 = new Rectangle(25,20);
       Rectangle rectangle2 = new Rectangle(15,10);
       Rectangle rectangle3 = new Rectangle(20,20);
   }

   /**
    * Constructor for objects of class Rectangle
    */
   public Rectangle()
   {
       // initialise instance variables

   }
   public Rectangle(int length1,int width1);
   {
           length=length1;
           width=width1;
       }
       public int Area();
       {
           Area=(length * width);
           return Area;
       }
       public int Perimeter();
       {
           Perimeter = 2*(length) + 2*(width);
           return Perimeter;
       }

}
edit: added code tags PB

This post has been edited by PennyBoki: 13 Feb, 2008 - 08:24 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Making Rectangles
13 Feb, 2008 - 08:36 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Ok, you're on your way, but the assignment requires that you create another class called RectangleTester.java, that means you should create another file called RectangleTester.java where there will be just the main(). So remove the main() from Rectangle.java and write it in RectangleTester.java

And in your Area() and Perimeter() functions you need to declare the variables Area and Perimeter, that means write say int in front of them.

Also you need to print the rects, well that's easy just print them in the main().

So try that and see what happens. Hope this helps.

another thing, remove the semicolons ; from the function definitions.
User is offlineProfile CardPM
+Quote Post

alata28
RE: Making Rectangles
19 Feb, 2008 - 06:33 PM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 5

okay so i fixed up a lot of things but somehow im getting errors and i dont know what to do... here is my code. i have a file thats Rectangle.java and the other one is RectangleTester.java
CODE



/**
* Write a description of class RectangleTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RectangleTester
{
public static void main (String [] args)
{
Rectangle rectangle1 = new Rectangle(25,20);
int x = rectangle1.getLength();
int y  = rectangle1.getWidth();
int area   = rectangle1.getArea();
int perimeter = rectangle1.getPerimeter();
    

System.out.print("For rectangle with length "+x);
System.out.prinln(" and width " +y );
System.out.println("Area = "+area);
System.out.println("Perimeter = "+perimeter);

}


{
Rectange rectangle2 = new Rectangle (15,10);
int x = rectangle2.getLength();
int y  = rectangle2.getWidth();
int area   = rectange2.getArea();
int perimeter = rectangle2.getPerimeter();
    
System.out.println("For rectangle with length " +x);  
System.out.prinln(" and width " +y );
System.out.println("Area = "+area);
System.out.println("Perimeter = "+perimeter);

}


{
Rectange rectangle3 = new Rectangle (20,20);
int x = rectangle3.getLength();
int y  = rectangle3.getWidth();
int area   = rectange3.getArea();
int perimeter = rectangle3.getPerimeter();
  
System.out.println("For rectangle with length "+x);
System.out.prinln(" and width " +y );
System.out.println("Area = "+area);
System.out.println("Perimeter = "+perimeter);


}

}


i am getting like constructor errors and i dont know how to fix them... some please help what i can do to change this.
User is offlineProfile CardPM
+Quote Post

Mavirick
RE: Making Rectangles
19 Feb, 2008 - 08:30 PM
Post #4

D.I.C Head
**

Joined: 18 Feb, 2008
Posts: 59



Thanked: 7 times
My Contributions
The first thing I notice is that your class is called Rectangle, with a capital 'R'. I'm not 100% sure, maybe you can overwrite it, but Rectangle is reserved by Java, and by the standard you name classes with the first word beginning in lower case and subsequent words with capitals, so that your two classes would be rectangle.java and rectangleTester.java. But in this I may be wrong.

Now with that out of the way some of the other mistakes are easy to fix. One is that you're enclosing each different object of Rectangle (rectangle 1,2,3) in curly braces ( { } ). That first closing curly brace after your initialization and work with rectangle1 is actually closing your main() class, so that rectangle 1 and 2 are in free space. Remove the curly braces after rectangle1, around 2, and before 3 so that you have your opening brace before rectangle1 and your closing after rectangle3.

Next is the three methods that each object of Rectangle (rectangle 1,2,3) calls:
CODE
int x = rectangle1.getLength();
int y  = rectangle1.getWidth();
int area   = rectangle1.getArea();
int perimeter = rectangle1.getPerimeter();


Each method here is called getLength() or getWidth(), but in your actual Rectangle class, there is no method to return length or width, and the return methods for area and perimeter don't have the same name:
CODE
       public int Area();
       {
           Area=(length * width);
           return Area;
       }
       public int Perimeter();
       {
           Perimeter = 2*(length) + 2*(width);
           return Perimeter;
       }


Change those methods to "public int getArea()" and "public int getPerimeter()" and add in a "public int getLength()" and "public int getWidth()" and you'll be a good deal closer to finishing the program.

Good luck!
-Mav
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month