Learn how to a build a cloud-first strategyRegister Now


Rectangle class to test for options...

Posted on 2004-11-16
Medium Priority
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:

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

Expert Comment

ID: 12595989
There is already a Rectangle class
LVL 86

Expert Comment

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

Accepted Solution

petmagdy earned 2000 total points
ID: 12596064
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:
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12600115

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!
LVL 86

Expert Comment

ID: 12600136
You need to read keyboard input:


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

Expert Comment

ID: 12600676
agree with CEHJ
LVL 15

Expert Comment

ID: 12601272
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 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