In: Computer Science
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width.
2. Write a java program (a driver application) that tests the above class by providing the users with the following Console based menu options: 1 - to set the length 2 - to set the width 3 - to get the length 4 - to get the width 5 - to get the perimeter 6 - to get the area 0 - to quit Your 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 inputs from the above menu. Use Command Line Interface or Console for all input/output.
import java.util.*;
class Rectangle
{
double length,width;
Rectangle() //parameter less constructor
{
length = 0;
width = 0;
}
Rectangle(double l,double w) //constructor with parameter
{
length = l;
width = w;
}
public void setLength(double l) //setLength method
{
length = l;
}
public void setWidth(double w) //setWidth method
{
width = w;
}
public double getLength() //getLength method
{
return length;
}
public double getWidth() //getWidth method
{
return width;
}
public void area() //area method
{
double ans;
ans = length * width; //calculating area
System.out.println("Area of rectangle is: "+ans); //printing
area
}
public void perimeter() //perimeter method
{
double ans;
ans = 2 * (length + width); //printing perimeter
System.out.println("Perimeter of rectangle is: "+ans);
}
}
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Rectangle r = new Rectangle(); //creating object
Rectangle r2 = new Rectangle(3,6); //creating
object
int ch;
char ch2='y';
while(ch2=='y' || ch2=='Y'){
System.out.println("1.Set Length");
System.out.println("2.Set Width");
System.out.println("3.Get Length");
System.out.println("4.Get Width");
System.out.println("5.Get Area");
System.out.println("6.Get Perimeter");
System.out.println("0.Exit");
System.out.println("Enter your choice: ");
ch = sc.nextInt(); //taking input from user
switch (ch) {
case 0:System.exit(0);
break;
case 1:System.out.println("Enter the Length: ");
double l=sc.nextDouble(); //taking input from
user
r2.setLength(l);
break;
case 2:System.out.println("Enter the width: ");
double w=sc.nextDouble(); //taking input from
user
r2.setWidth(w);
break;
case 3:System.out.println("The length is:
"+r2.getLength());
break;
case 4:System.out.println("The width is:
"+r2.getWidth());
break;
case 5:r2.area();
break;
case 6:r2.perimeter();
break;
default:System.out.println("Enter valid
choice");
}
System.out.println("Do you want to continue: ");
ch2 = sc.next().charAt(0); //taking input from
user
}
}
}
Output:-