87.9k views
4 votes
Design a new Triangle class that extends the abstract

GeometricObject class. Draw the UML diagram for the classes Triangle
and GeometricObject and then implement the Triangle class. Write a test
program that prompts the user to enter three sides of the triangle, a color, and a
Boolean value to indicate whether the triangle is filled. The program should create
a Triangle object with these sides and set the color and filled properties using
the input. The program should display the area, perimeter, color, and true or false
to indicate whether it is filled or not.
..
I don't understand this one. I have the GeometricObject class. I just the rest along with the UML diagram. Please no JOptionPane please...
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
so, the get method name is isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\\color: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

1 Answer

5 votes

Answer:

See explaination

Step-by-step explanation:

The GeometricObject

public class GeometricObject {

private String color = " white ";

private boolean filled;

private java.util.Date dateCreated;

public GeometricObject() {

dateCreated = new java.util.Date();

}

public GeometricObject(String color, boolean filled) {

dateCreated = new java.util.Date();

this.color = color;

this.filled = filled;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean filled) {

this.filled = filled;

}

public java.util.Date getDateCreated() {

return dateCreated;

}

public String toString() {

return "Created on " + dateCreated + "\\ color: " + color + " and filled ";

}

}

The Triangle program

public class Triangle extends GeometricObject {

private double side1 = 1.0;

private double side2 = 1.0;

private double side3 = 1.0;

public Triangle() {

}

public Triangle(double side1, double side2, double side3) {

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

}

public double setSide1() {

return side1;

}

public double setSide2() {

return side2;

}

public double setSide3() {

return side3;

}

public void setSide1(double side1) {

this.side1 = side1;

}

public void setSide2(double side2) {

this.side2 = side2;

}

public void setSide3(double side3) {

this.side3 = side2;

}

public double getArea() {

return (side1 + side2 + side3) / 2;

}

public double getPerimeter() {

return side1 + side2 + side3;

}

public String toString() {

return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2

+ " Side 3 = " + side3;

}

}

The Testprogram.

import java.util.Scanner;

public class TestTriangle {

private double side1 = 1.0;

private double side2 = 1.0;

private double side3 = 1.0;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter three sides of the Triangle");

double side1 = input.nextDouble();

double side2 = input.nextDouble();

double side3 = input.nextDouble();

System.out.println("Enter the color of the Triangle");

String color = input.next();

System.out.println(" Is the Triangle filled? Reply with 'True' or 'False' ");

String filled = input.next();

}

{

new Triangle(side1, side2, side3);

//How do i get the information into theTriangle?

System.out.println("The Triangle Sides are \\ side 1: " + side1 + "\\ Side 2: " + side2 + "\\ Side 3: " + side3);

System.out.println("The Triangle's Area is " + (side1 + side2 + side3) / 2);

System.out.println("The Triangle's Perimeter is "

+ (side1 + side2 + side3));

System.out.println("The Triangle's Color is " + //what goes here?);

System.out.println("Is the Triangle filled? " + //what goes here?);

}

}

You need to create a new Triangle object like this, so that you have a reference

Trangle triangle = new Triangle(side1, side2, side3);

// ^^^^^^ This is the most important thing you're missing. You need a reference

// point for your object. That's the only way you can access it's

// properties.

You also need to set it's filled and color properties

triangle.setFilled(filled);

triangle.setColor(color);

Then, you can invoke its methods like this:

System.out.println("The Triangle Sides are \\ side 1: "

+ triangle.getSide1() + "\\ Side 2: " + triangle.getSide2()

+ "\\ Side 3: " + triangle.getSide3());

System.out.println("The Triangle's Area is " + triangle.getArea());

System.out.println("The Triangle's Perimeter is " + triangle.getPerimeter();

System.out.println("The Triangle's Color is " + triangle.getColor());

System.out.println("Is the Triangle filled? " + triangle.isFilled());

You're able to access the GeometricObject's isFilled(), setFilled(), getColor(), and setColor() because a Triangle is a GeometricObject (extends), so it inherits all its methods.

By the way, this is not how to calculate the area of a triangle:

public double getArea() {

return (side1 + side2 + side3) / 2; // This is so wrong

}

Check out this link for correct formula

Edit: With another problem with code

public double setSide1() {

return side1;

}

public double setSide2() {

return side2;

}

public double setSide3() {

return side3;

}

/**** Should Be ******/

public double getSide1() {

return side1;

}

public double getSide2() {

return side2;

}

public double getSide3() {

return side3;

}

Edit: Triangle Formula

public double getArea() {

int p = getPerimeter() / 2

return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3));

}

User Fraser Speirs
by
5.3k points