Rectangle class to test for options...

Posted on 2004-11-16
1 Endorsement
Last Modified: 2010-03-31
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:


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!
Question by:adidas7fire
    LVL 86

    Expert Comment

    There is already a Rectangle class
    LVL 86

    Expert Comment

    .. so you could just extend it (java.awt.Rectangle) to give it an area method
    LVL 13

    Accepted Solution

    yes ok:

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

     protected static Rectangle rectangle = new Rectangle();

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

    Author Comment


    With the RectangleDriver, do I need to include an .java packages?  For instance, import* or import java.awt.Rectangle?  Thank you for your help as well as CEHJ!
    LVL 86

    Expert Comment

    You need to read keyboard input:

    and i would use java.awt.Rectangle with an extra area method
    LVL 13

    Expert Comment

    agree with CEHJ
    LVL 15

    Expert Comment

    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.



    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now