Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add two Java classes in order named as Applications and Rectangle in your project; when using the NetBeans editor add the Applications class first and

Add two Java classes in order named as Applications and Rectangle in your project; when using the NetBeans editor add the Applications class first and mark it as your main class

Exercises

Implement the Rectangle class as described in the book and explained in lectures and also you add more properties as described below. The requirements are as follows.

The class has

Two private data fields of type double, named length and width

Each field has an associated accessor method (getter) and a mutator method (setter); the mutator method takes a parameter and sets the field by passing the parameter, but only if the parameter is not negative. For negative parameter the field is assigned zero

Implement a method named computeArea( ) (replacing getArea of the book) such that using the fields it computes and returns the area of the rectangle

Implement a method named computePerimeter( ) such that using the fields it computes and returns the perimeter of the rectangle

Implement a method named toString( ) which returns a string message. The message is built in the method such that it reveals the field values with references The length is: and The width is: . This method does NOT print the message! A sample display when the message is printed may look like this:

The length is: 29.66

The width is: 17.03

Implement a method named displayRectangle( ). The method is void.

The method calls the toString( ) method and prints the returned value to the console

Implement a method named equals( ) which returns a boolean value. The method takes a Rectangle type parameter, and compares the class fields to that of the parameter Rectangle. Returns true if the fields are equal and false otherwise. The header shall be

public boolean equals(Rectangle other){

The method uses a boolean expression built from

length == other.length

and the like for width.

Or, you may apply an if-else logic to determine the return value.

The class contains an initializer constructor and the default constructor:

public Rectangle(double len, double w){

length = len;

width = w;

}

public Rectangle(){

}

The Applications class contains the main method, no other methods or fields. In the method

Declare local variables length and width of type double

Using a Scanner object solicit two number from the console and save the values in the variables length and width

Declare and instantiate a Rectangle object named box. Use the initializer constructor; the parameters are the length and width

Declare and instantiate another Rectangle object named box2. Use the default constructor.

Use box2 to call the setLength() and setWidth() methods. Choose numbers for parameters at will.

Call the display( ) method with respect to box as well as to box2. Copy the console output into a comment block placed after the class. Check and comment if the field values correspond to the input.

Use box to call the equals( ) method, use box2 for the parameter. Print the returned boolean to the console. Comment on the output.

Declare and instantiate a third Rectangle object named box3. Use the default constructor.

Use box to call the getter methods and assign the local variables width and length the values returned by the getters.

Use box3 to call the setters (like above for box2), the setters this time take the local variables for parameter.

Use box to call the equals method and use box3 for parameter. Print the returned boolean to the console. Comment on the output in the comment bock you already created above.

Call the computeArea( ) and computePerimeter( ) methods with respect to all your boxes. Print the return values to the console together with a short explanation for each, and copy the output into the comment block

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

More Books

Students also viewed these Databases questions