
Lavi M.
asked 10/17/20How 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
}
}
1 Expert Answer

Patrick B. answered 10/17/20
Math and computer tutor/teacher
//******** Shapes.txt *************//
abstract class Shape
{
private String color;
public Shape( String Color) { color = new String(Color); }
public String GetColor() { return(color); }
public abstract double getArea() ;
public abstract String toString();
}
//********** Rectangle.java ************//
class Rectangle extends Shape
{
protected double length;
protected double width;
public Rectangle( double L, double w, String color )
{
super(color);
length = L;
width = w;
}
public double getArea() { return(length*width); }
@Override public String toString()
{
return( new String (( length + "," + width )));
}
double GetLength() { return(length); }
double GetWidth() { return(width); }
}
//******* Triangle.java ***************//
class Triangle extends Shape
{
protected double base;
protected double height;
Triangle ( double B, double h, String color)
{
super(color);
base = B;
height = h;
}
public double GetBase() { return(base); }
public double GetHeight() { return(height); }
public double getArea() { return( (base*height)/2 ); }
@Override public String toString()
{
return ( new String( ( base + "," + height)));
}
}
//********* Circle.java *****************//
import java.math.*;
class Circle extends Shape
{
protected double radius;
public Circle ( double R, String color)
{
super(color);
radius=R;
}
public double GetRadius() { return(radius); }
public double GetDiameter() {return(radius*2); }
public double getArea() { return( Math.PI * radius*radius); }
@Override public String toString()
{
return( (new String(" "+radius )).trim());
}
}
//*********** main *****************//
class MainShape
{
public static void main( String args[])
{
Rectangle rectangle = new Rectangle( 6,5,"ORANGE");
Triangle triangle = new Triangle( 10,5,"PURPLE");
Circle circle = new Circle( 10,"GREEN");
System.out.println(" Rectange has area " + rectangle.getArea()
+ " with dimensions " + rectangle.toString()
+ " and color " + rectangle.GetColor()
);
System.out.println(" Triangle has area " + triangle.getArea()
+ " with dimensions " + triangle.toString()
+ " and color " + triangle.GetColor()
);
System.out.println(" Circle has area " + circle.getArea()
+ " with dimensions " + circle.toString()
+ " and color " + circle.GetColor()
);
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
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.java10/17/20