Question

In: Computer Science

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

• A method named getHeight() returns value of height

• A method named setWidth(double) set the width with given value

• A method named setHeight(double) set the height with given value

• A method named getArea() that returns the area of this rectangle

• A method getPerimeter() that returns the perimeter

Write a test program that creates two rectangle objects – one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area and perimeter of each rectangle in this order.

Solutions

Expert Solution

Here is the Java code for given question.

Output screenshot is added at the end.

Code:

Rectangle.java:

public class Rectangle {

    double width,height;      /*data fields*/


    //no-arg constructor
    public Rectangle(){
        this.width=1;
        this.height=1;
    }

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

    //getter for width
    public double getWidth() {
        return width;
    }

    //getter for height
    public double getHeight() {
        return height;
    }

    //setter for width
    public void setWidth(double width) {
        this.width = width;
    }

    //setter for height
    public void setHeight(double height) {
        this.height = height;
    }

    //method to get area of rectangle
    public double getArea(){

        return this.getWidth()*this.getHeight();

    }

    //method to return perimeter of rectangle
    public double getPerimeter(){

        return this.getWidth()+this.getHeight();

    }
}

Test program(RectangleDriver.java):

public class RectangleDriver {

    public static void main(String[] args){

        //creates a new Rectangle object
        Rectangle rectangle1=new Rectangle(4,40);


        //craetes a new Rectangle object
        Rectangle rectangle2=new Rectangle(3.5,35.9);

        //prints details of rectangle 1
        System.out.println("Rectangle 1:\nWidth: "+rectangle1.getWidth()+"\nHeight: "+rectangle1.getHeight()+"\nArea: "+rectangle1.getArea()+"\nPerimeter: "+rectangle1.getPerimeter());

        //prints details of rectangle 2
        System.out.println("\nRectangle 2:\nWidth: "+rectangle2.getWidth()+"\nHeight: "+rectangle2.getHeight()+"\nArea: "+rectangle2.getArea()+"\nPerimeter: "+rectangle2.getPerimeter());

    }
}

Output:


Related Solutions

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 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...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
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, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
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...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments)...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments) // declare the constructor that sets the type and rate based in the sqft parm        // set values based on sqft <1000 is small with $100 per night, // sqft between 1000 and 2000, mid-sized $200 per night, and // over 2000 as a large cabin with $300 per night        //declare getRate        //declare getType        //declare setRate with int rate parm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT