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 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...
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 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...
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....
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT