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

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...
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...
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...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT