Question

In: Computer Science

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



public class Rectangle
{
   /**Instance variables */
   double width ;
   double height;
   /*Name of the constructor must match with
   * the name of the class, Rectangle*/
   public Rectangle()
   {
       width=1;
       height=1;
   }
   /*Name of the constructor must match with
   * the name of the class, Rectangle*/
   public Rectangle(double newWidth, double newHeight)
   {
       width = newWidth;
       height = newHeight;
   }
   /**Returns area */
   public double FindArea()
   {
       return width * height;
   }
   /**Returns perimeter */
   public double FindPerimeter()
   {
       return 2*(width + height);
   }
}/**End of Rectangle class*/


Create another file named client.java

import java.text.DecimalFormat;
public class client
{
   public static void main (String[] args)
   {
       Rectangle rectangle = new Rectangle(4,40);


       /**Create an instace of DecimalFormat class*/
       DecimalFormat df=new DecimalFormat("#,##.##");
      
       System.out.println("The details of the given rectangle");
       System.out.println("Width of Rectangle:" + rectangle.width);
       System.out.println("Height of Rectangle:" + rectangle.height);
       System.out.println("Area of Rectangle:" + df.format(rectangle.FindArea()));
       System.out.println("Perimeter of Rectangle:"+ rectangle.FindPerimeter());
       System.out.println();

   
   }
}

Output:


Related Solutions

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