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...
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...
In java, (include javadoc comments for each method) design a class named Contacts that has fields...
In java, (include javadoc comments for each method) design a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contacts objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to...
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...
In java, design a class named Motorcycle that has the following fields: • year – The...
In java, design a class named Motorcycle that has the following fields: • year – The year field is an int that holds the motorcycle’s year • make – The make field references a String object that holds the make of the motorcycle. • speed – The speed field is an int that holds the motorcycle’s current speed The class should have the following constructor and other methods: • Constructor – the constructor should accept the motorcycle’s year and make...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT