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!
adidas7fireAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
There is already a Rectangle class
0
CEHJCommented:
.. so you could just extend it (java.awt.Rectangle) to give it an area method
0
petmagdyCommented:
yes ok:

Create a Class called for Example RectangleDriver may be like this:

RectangleDriver
{
 protected static Rectangle rectangle = new Rectangle();

     public static void main(String[] args) {
       
                while(true)
                {
                  //Present the menu
                  // then read the option from user
                  //then based on action
                  switch(action)
                   {
                        case 1: //means set length
                            doSetLenght(int len);
                       ....
                      case 0:
                         return;
                       
                   }
    }}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

adidas7fireAuthor Commented:
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!
0
CEHJCommented:
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
0
petmagdyCommented:
agree with CEHJ
0
JavatmCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.