Design a class named Triangle that extends GeometricObject. The GeometricObject
ID: 3779225 • Letter: D
Question
Design a class named Triangle that extends GeometricObject.
The GeometricObject class contains:
• Private data fields named
o color - String
o filled - Boolean
• No-arg constructor and constructor with two arguments (color and filled).
• Get and set methods for the data field color.
• isFilled and setFilled methods to get and set the values of the field filled
• toString() method that returns a string description of the GeometricObject.
o The color of the triangle
o The information whether the triangle is filled or not.
The Triangle class contains:
• Three double private data fields named side1, side2, and side3.
• A no-arg constructor that creates a default triangle with all three sides equal to 1
• A constructor with three arguments that creates a triangle with the specified side1, side2, and side3.
• The accessor and mutator methods for all three data fields.
• A public method named calculatePerimeter() that returns the perimeter of this triangle.
• toString() that overrides the one in the GeometricObject class and returns a string description for the Triangle. The description should contain
o The values of the three sides.
o The color of the triangle
o The information whether the triangle is filled or not.
o The area perimeter of the triangle
Note:
1. The perimeter of the triangle is equals to the sum of the three sides.
Specifications
1. Write the GeometricObject class
2. Write the Triangle class
3. Test using the DemoTriangle class
**************************************************************************************************************
public class DemoTriangle
{
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
System.out.println(triangle);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
}
}
***Here's what I have so far. It appears to work but it prints the results twice. What am I doing wrong?***
*************************************************************************************************
public class GeometricObject
{
private String Color;
private boolean isFilled;
public String getColor()
{
return Color;
}
public void setColor(String Color)
{
this.Color = Color;
}
public void setFill(boolean fill)
{
isFilled = fill;
}
public boolean isFilled()
{
return isFilled;
}
}
*******************************************************************************
public class Triangle extends GeometricObject
{
private double side1;
private double side2;
private double side3;
public double getSide1()
{
return side1;
}
public void setSide1(double side1)
{
this.side1 = side1;
}
public double getSide2()
{
return side2;
}
public void setSide2(double side2)
{
this.side2 = side2;
}
public double getSide3()
{
return side3;
}
public void setSide3(double side3)
{
this.side3 = side3;
}
public Triangle()
{
side1 = 1;
side2 = 1;
side3 = 1;
}
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double calculatePerimeter()
{
return side1 + side2 +side3;
}
public double getArea()
{
double a = calculatePerimeter()/2;
double Area = a * (a-side1)*(a-side2)*(a-side3);
return Area;
}
public String toString()
{
return "Value of Side 1: " + side1 + " Value of side 2: " + side2 + " Value of side 3: " + side3 + " Color of triangle: " + getColor() + " Filled: " + isFilled() + " Area of triangle: " + getArea() + " Perimeter of triangle: " + calculatePerimeter();
}
}
****************************************
Results of running DemoTriangle:
Value of Side 1: 1.0
Value of side 2: 1.5
Value of side 3: 1.0
Color of triangle: null
Filled: false
Area of triangle: 0.24609375
Perimeter of triangle: 3.5
Value of Side 1: 1.0
Value of side 2: 1.5
Value of side 3: 1.0
Color of triangle: yellow
Filled: true
Area of triangle: 0.24609375
Perimeter of triangle: 3.5
*******************Edit*******************************
After reading through the instructions again, I realized that I don't have the GeomotricObject class written right.
I didn't include:
"toString() that overrides the one in the GeometricObject class and returns a string description for the Triangle. The description should contain"
Though I believe I can figure that one out, please provide a solution so I can double check my code. Thank you!
Explanation / Answer
Hey, Do check out the comments in code. It was printing twice, because you wrote System.out.println twice.
Also, I have created toString() method in Geometric Object, and I have overriden the same in Triangle class.
Do let me know if you still have any doubts.
DemoTriangle.java:
public class DemoTriangle {
public static void main(String[] args) {
Triangle triangle = new Triangle(1.0, 1.5, 1.0);
//System.out.println(triangle);
//It was printing twice because you wrote the printing command twice.
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
}
}
Triangle.java
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public double getSide1()
{
return side1;
}
public void setSide1(double side1)
{
this.side1 = side1;
}
public double getSide2()
{
return side2;
}
public void setSide2(double side2)
{
this.side2 = side2;
}
public double getSide3()
{
return side3;
}
public void setSide3(double side3)
{
this.side3 = side3;
}
public Triangle()
{
side1 = 1;
side2 = 1;
side3 = 1;
}
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double calculatePerimeter()
{
return side1 + side2 +side3;
}
public double getArea()
{ //This method is calculating the wrong area, and it isn't even asked in the question.
//It actually is returning the square of the actual area.
double a = calculatePerimeter()/2;
double Area = a * (a-side1)*(a-side2)*(a-side3);
return Area;
}
public String toString()
{
//A call to superclass toString is made using super.toString()
return "Value of Side 1: " + side1 + " Value of side 2: " + side2 + " Value of side 3: " + side3 + super.toString() + " Perimeter of triangle: " + calculatePerimeter();
}
}
GeometricObject.java
public class GeometricObject {
private String Color;
private boolean isFilled;
public String getColor()
{
return Color;
}
public void setColor(String Color)
{
this.Color = Color;
}
public void setFilled(boolean fill)
{
//method name has to be setFilled setFilled
isFilled = fill;
}
public boolean isFilled()
{
return isFilled;
}
@Override
public String toString() {
return " The color of the triangle is:" + Color + " The triangle is filled: " + isFilled;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.