Java programming Implement the Rectangle class as described in the book and expl
ID: 3813980 • Letter: J
Question
Java programming
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
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Namburi Ramesh
*/
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public Rectangle() {
length=0;
width=0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
if(length>=0){
this.length = length;
}else{
this.length=0;
}
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
if(this.width>=0){
this.width = width;
}else{
this.width=0;
}
}
public double computeArea(){
return length*width;
}
public double computePerimeter(){
return length+width;
}
@Override
public String toString() {
return "The length is: " + length + " The width is: " + width;
}
public void displayRectangle(){
System.out.println(toString());
}
public boolean equals(Rectangle other){
if(other.getLength()== length && other.getWidth() == width){
return true;
}else{
return false;
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.Scanner;
/**
*
* @author Namburi Ramesh
*/
public class Applications {
public static void main(String args[]){
double length,width;
Scanner s=new Scanner(System.in);
System.out.println("Enter the values of length and width");
length=s.nextDouble();
width=s.nextDouble();
Rectangle box=new Rectangle(length, width);
Rectangle box2=new Rectangle();
box2.setLength(5);
box2.setWidth(6);
box.displayRectangle();
box2.displayRectangle();
boolean b=box.equals(box2);
System.out.println("box equals box2: "+b);
Rectangle box3=new Rectangle();
length=box.getLength();
width=box.getWidth();
box3.setLength(length);
box3.setWidth(width);
boolean b2=box.equals(box3);
System.out.println("box equals box3: "+b2);
System.out.println("box area: "+box.computeArea()+" box perimeter: "+box.computePerimeter()+" box2 area: "+box2.computeArea()+" box2 perimeter: "+box2.computePerimeter()+" box3 area: "+box3.computeArea()+" box3 perimeter: "+box3.computePerimeter());
}
}
/*
*console output for displayRectangle() on box. It equals the user entered input
The length is: 3.0
The width is: 4.0
*console output for displayRectangle() on box2
The length is: 5.0
The width is: 6.0
*console output for returned value of equals() on box with box2 as parameter. As both length and width are not equal It returned false
box equals box2: false
*console output for returned value of equals() on box with box3 as parameter. As both length and width are equal It returned true
box equals box3: true
*console output for computeArea() and computePerimeter() on box,box2,box3
box area: 12.0
box perimeter: 7.0
box2 area: 30.0
box2 perimeter: 11.0
box3 area: 12.0
box3 perimeter: 7.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.