Q: Assistance in creating a class Rectangle with double attributes length and wi
ID: 3811869 • Letter: Q
Question
Q: Assistance in creating a class Rectangle with double attributes length and width. The default constructor should set these attributes to 1. Provide methods that calculate the rectangle's perimeter and area, as well as accessors/getters and mutators/setters for both data fields. The mutator methods for length and width should verify that the number being passed in is larger than 0.0 and less than 20.0 -- if it doesn't fit those criteria, the value of the field should not be changed. Additionally implement two methods called makeCopy() and fromRectangle(). - makeCopy() is class method: takes a rectangle as input parameter and return the copy of that rectangle - fromRectangle() is an instance method that takes a rectangle as input and copies the data attributes of rectangle into the calling object. Write a function called main() in the same file to test your Rectangle class . It should prompt the user to enter a length and width of a rectangle, and then print out the area and perimeter of the rectangle. (Use the mutators to set the length and width of the rectangle, not the constructor .)
Explanation / Answer
Rectangle.java
import java.util.Scanner;
public class Rectangle implements Cloneable{
//Declaring instance variables
private double length;
private double width;
//Zero argumented constructor
public Rectangle() {
super();
this.length=1;
this.width=1;
}
//Setters and getters
public double getLength() {
return length;
}
public void setLength(double length) {
if(length>0.0 && length<20.0)
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
if(width>0.0 && width<20.0)
this.width = width;
}
//Method which calculates the perimeter of the rectangle
public double perimeter()
{
return 2*(length+width);
}
//Method which calculates the area of the rectangle
public double area()
{
return length*width;
}
public Rectangle makeCopy(Rectangle rect)
{
Rectangle copyr=new Rectangle();
try {
copyr=(Rectangle)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return copyr;
}
public Rectangle fromRectangle(Rectangle rect)
{
Rectangle r=new Rectangle();
r.length=rect.length;
r.width=rect.width;
return r;
}
public static void main(String args[])
{
double length,width;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the inputs entered by the user
System.out.print("Entee the Length :");
length=sc.nextDouble();
System.out.print("Entee the Width :");
width=sc.nextDouble();
//creating the rectangle class object
Rectangle rect=new Rectangle();
//calling the setter methods on the Rectangle class object
rect.setLength(length);
rect.setWidth(width);
//Displaying the area of the Rectangle
System.out.println("Area of the Rectangle :"+rect.area());
//Displaying the Perimeter of the Rectangle
System.out.println("Perimeter of the Rectangle :"+rect.perimeter());
Rectangle rect1=rect.makeCopy(rect);
System.out.println("Perimeter of the Rectangle#1:"+rect1.perimeter());
}
}
___________________
Output:
Entee the Length :6.6
Entee the Width :7.7
Area of the Rectangle :50.82
Perimeter of the Rectangle :28.6
Perimeter of the Rectangle#1:28.6
_________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.