Link to home
Start Free TrialLog in
Avatar of adidas7fire
adidas7fire

asked on

Rectangle class to test for options...

What I am trying to do is write a class called Rectangle that maintains two attributes to represent the length and breadth of a rectangle. I've got to provide suitable get, set, and toString methods plus two methods that return the perimeter and area of the rectangle. Also, I must include two constructors for this class. One a parameterless constructor that initializes both the length and breadth to 0, and the second one that takes two parameters to initialize the length and breadth.  So far, this is what I have:


import java.io.*;

public class Rectangle
{
  protected long length;
  protected long width;
      
//....constructors go here

  public long get_length(){return length;}
  public long get_width(){return width;}
  public void set_length(long len){length = len;}
  public void set_width(long wid){width = wid;}
  public long area(){return width * length;}
  public long perimeter(){return 2 * (width + length);}
}

-----------------------------------------
//2 Constructors

public Rectangle()
   {
     length = 0;
     width = 0;
   }
      
  public Rectangle(long len, long wid)
   {
     length = len;
     width = wid;
   }
------------------------------------------
Then, after this, I've got to write a program (a driver application) that tests the above class by providing the users with the following menu options:
1 - to set the length
2 - to set the breadth
3 - to get the length
4 - to get the breadth
5 - to get the perimeter
6 - to get the area
7 - to print the object as string
0 - to quit
 
Eventually,  this program should create one Rectangle object at the beginning using the default constructor, and then repeatedly call the appropriate method for that object depending on the user selection from the above menu.

I'm not sure where to implement the code for this information or what I am missing.  Could you help me out please?  Thanks in advance!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

There is already a Rectangle class
.. so you could just extend it (java.awt.Rectangle) to give it an area method
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adidas7fire
adidas7fire

ASKER

Petmagdy,

With the RectangleDriver, do I need to include an .java packages?  For instance, import java.io.* or import java.awt.Rectangle?  Thank you for your help as well as CEHJ!
You need to read keyboard input:

http://javaalmanac.com/egs/java.io/ReadFromStdIn.html

and i would use java.awt.Rectangle with an extra area method
agree with CEHJ
You can create the frame and call your underlying class for Triangle and you can also integrate your driver class. So that you'll have
a complete program. This depends on your needs. Some comments was already made by other experts, let me know if I could add
help to your problem thanks.

Javatm