Question

In: Computer Science

Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...

Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement a class called Square that represents a square. Class Square must be derived from Rectangle. Make sure you override toString().

Solutions

Expert Solution

Here is code:

Rectangle.java:

class Rectangle {

protected double width;

protected double height;

public Rectangle() {

this.width = this.height = 0;

}

public Rectangle(double width, double height) {

this.width = width;

this.height = height;

}

public double getArea() {

return width * height;

}

public double getPerimeter() {

return 2 * (width + height);

}

public String toString() {

return " area " + String.format("%.2f", getArea()) +

" and perimeter " + String.format("%.2f", getPerimeter());

}

}

Square.java:

class Square extends Rectangle {

public Square(double side) {

this.width = this.height = side;

}

}

ShapeTester.java:

public class ShapeTester{

public static void main(String[] args) {

Rectangle r = new Rectangle(5,9);

Square s = new Square(9);

System.out.println("Rectangle : ");

System.out.println(r.toString());

System.out.println("Square :");

System.out.println(s.toString());

}

}

Output:


Related Solutions

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...
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...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output: 6.0...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor overloading) for initializing objects with different parameters. The employee class should have different functions for saving and updating bio-data, educational background, and current status like experience, Salary, position & travel history. The employee class should have a function which asks user what do he likes to see about employee (.i.e., Bio-data, current status....) and only show the respective data. Create two objects of interns...
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the...
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle, GeometricObject, and Comparable. Also include explain paragraph.
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT