Question

In: Computer Science

1. Write a class called Rectangle that maintains two attributes to represent the length and width...

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.

Solutions

Expert Solution

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:-


Related Solutions

(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and...
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method)...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double area = 0;     area = width * length;     cout << "\n***maxArea called" << endl;          cout << "***       rectangleCount = " << Rectangle::rectangleCount << endl << endl;    } Compete this code to find the maximum area of rectangle between a,b,c
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT