Add two Java classes in order named as Applications and Rec
- 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
- 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
I- mplement 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
Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Application
{
public static void main (String[] args) throws java.lang.Exception
{
double len,wid;
Scanner s = new Scanner(System.in);
//System.out.println(\"Please enter the sides of the recangle:\");
//len = s.nextDouble();
//wid = s.nextDouble();
Rectangle box=new Rectangle(5,6);
Rectangle box2=new Rectangle();
box2.setLength(8);
box2.setWidth(7);
box.displayRectangle();
box2.displayRectangle();
/*
OUTPUT:
The length is:5.0
The width is:6.0
The length is:8.0
The width is:7.0
*/
boolean r=box.equals(box2);
System.out.println(\"Equals result:\"+r);
// OUTPUT: Equals result:false
Rectangle box3=new Rectangle();
len=box.getLength();
wid=box.getWidth();
box3.setWidth(wid);
box3.setLength(len);
boolean r2=box.equals(box3);
System.out.println(\"Equals result:\"+r2);
// OUTPUT: Equals result:true
double area1=box.computeArea();
double peri1=box.computePerimeter();
double area2=box2.computeArea();
double peri2=box2.computePerimeter();
double area3=box3.computeArea();
double peri3=box3.computePerimeter();
System.out.println(\"Area1:\" + area1 +\" Perimeter1:\" +peri1);
System.out.println(\"Area2:\" + area2 +\" Perimeter2:\" +peri2);
System.out.println(\"Area3:\" + area3 +\" Perimeter3:\" +peri3);
/*
OUTPUT:
Area1:30.0 Perimeter1:22.0
Area2:56.0 Perimeter2:30.0
Area3:30.0 Perimeter3:22.0
*/
}
}
class Rectangle
{
double length;
double width;
Rectangle(double len, double wid){
length = len;
width = wid;
}
Rectangle(){
}
double getLength()
{
return length;
}
double getWidth()
{
return width;
}
void setLength(double l)
{
if(l>=0) length=l;
else length=0;
}
void setWidth(double w)
{
if(w>=0) width=w;
else width=0;
}
double computeArea()
{
return length*width;
}
double computePerimeter()
{
return 2*(length+width);
}
String toString(int c)
{
if(c==1) return \"The length is:\"+length;
else return \"The width is:\"+width;
}
void displayRectangle()
{
System.out.println(toString(1));
System.out.println(toString(2));
}
boolean equals(Rectangle other)
{
if(this.length == other.getLength() && this.width==other.getWidth()) return true;
else return false;
}
}
OUTPUT:
The length is:5.0
The width is:6.0
The length is:8.0
The width is:7.0
Equals result:false
Equals result:true
Area1:30.0 Perimeter1:22.0
Area2:56.0 Perimeter2:30.0
Area3:30.0 Perimeter3:22.0



