Lavi M.

asked • 10/17/20

How to make these 3 classes? (Java)

Class1 Shape

-color:String

Methods:

+getArea() : double

+toString(): String ---> Superclass defines the expected behaviors(public intertace) of all subclasses.

Program at the public interface.


Class2 Rectangle

-length: int

-width:int

Methods:

+getArea(): double

+toString ():String


Class3 Triangle

-base: int

-height:int

Methods:

+getArea():double

+tostring):StringB---->Subclasses provide the

actual Implementations.



••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••


Hint :

**

* Superclass Shape maintain the common properties of all shapes

*/

public class Shape {

// Private member variable

private String color;


/** Constructs a Shape instance with the given color */

public Shape (String color) {

______________________;

}

/** Returns a self-descriptive string */

@Override

public String toString() {

_____________________;


/** All shapes must provide a method called getArea() */

public double getArea() {

// We have a problem here!

// We need to return some value to compile the program.

__________________________________;

return 0;

}

}

/**

* The Rectangle class, subclass of Shape

*/

public class Rectangle extends Shape {

// Private member variables

private int length, width;


/** Constructs a Rectangle instance with the given color, length and width */

public Rectangle(String color, int length, int width) {

super(_________);

this._______ = _______;

this._______ = _______;

}

/** Returns a self-descriptive string */

@Override

public String toString() {

return "Rectangle[length=" + _____ + ",_______=" + ______ + "," + super.________() + "]";

/** Override the inherited getArea() to provide the proper implementation for rectangle */

@Override

public double getArea() {

return _______*_______;

}

}

/**

* The Triangle class, subclass of Shape

*/

public class Triangle extends Shape {

// Private member variables

private int base, height;


/** Constructs a Triangle instance with the given color, base and height */

public Triangle(String color, int base, int height) {

super(______);

this._____ = ______;

this._______ = _______;

}


/** Returns a self-descriptive string */

@Override

public String toString() {

return "Triangle[base=" + ______ + ",_______=" + _______ + "," + super.______() + "]";

/** Override the inherited getArea() to provide the proper implementation for triangle */

@Override

public double getArea() {

return 0.5*______*_______;

}

}

public class TestShape {

public static void main(String[] args) {

// Constructing a Shape instance poses problem!

Shape s3 = new _______("______");

System.out.println(____);

//Shape[color=green]

System.out.println("Area is " + s3.______()); // Invalid output

//Shape unknown! Cannot compute area!

//Area is 0.0

}

}

Patrick B.

source code uploaded in RESOURCES section under this link... Shapes.txt contains the following source code files: Shape.java Rectangle.java Triangle.java Circle.java ShapeMain.java
Report

10/17/20

1 Expert Answer

By:

Patrick B. answered • 10/17/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.