Question

In: Computer Science

PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...

PUT IN JAVA PROGRAMMING LANGUAGE

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 this rectangle.
• A method named findPerimeter() that finds the perimeter of this rectangle.
• Create a client (test) class (program) to test and use your Rectangle class.

In the client program, you need to create objects of the Rectangle type and use those

objects to perform some meaningful and valid rectangle operations (finding area and perimeter).

The test/client class should display all results.

Solutions

Expert Solution

Explanation:I have written both the classes Rectangle and Client.I have implemented all the methods and attributes as mentioned in the question.I have also shown the output, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//Rectangle class


public class Rectangle {
   double width;
   double height;

   public Rectangle() {
       this.width = 1;
       this.height = 1;
   }

   public Rectangle(double width, double height) {
       this.width = width;
       this.height = height;
   }

   public double findArea() {
       return width * height;
   }

   public double findPerimeter() {
       return (2 * width + 2 * height);
   }
}

//Client class


public class Client {

   public static void main(String[] args) {
       Rectangle rectangle1 = new Rectangle(12.5, 7.5);
       System.out
               .println("The area of rectangle 1 is " + rectangle1.findArea());
       System.out.println("The perimeter of rectangle 1 is "
               + rectangle1.findPerimeter());
       Rectangle rectangle2 = new Rectangle(5, 7);
       System.out
               .println("The area of rectangle 2 is " + rectangle2.findArea());
       System.out.println("The perimeter of rectangle 2 is "
               + rectangle2.findPerimeter());

   }

}

Output:


Related Solutions

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...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle 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 the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle 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 the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle 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 the rectangle. The default values are 1 for both width and height. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with specified width and height • A method name getWidth() return the value...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT