Design a class named Triangle that extends GeometricObject (code given below). The Triangle class contains:
• Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
• A no-arg constructor that creates a default triangle.
• A constructor that creates a triangle with the specified side1, side2, and side3.
• The accessor methods for all three data fields.
• A method named getArea() that returns the area of this triangle.
• A method named getPerimeter() that returns the perimeter of this triangle.
• A method named toString() that returns a string description for the triangle.
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(String Color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** 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,
its get method is named 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 + "\ncolor: " + color +
" and filled: " + filled;
}
}
break
public class Triangle extends GeometricObject {
double side1 = 1.0;
double side2 = 1.0;
double side3 = 1.0;
public Triangle() {
side1=0.0;
side2=0.0;
side3=0.0;
}
public Triangle(double a, double b, double c) {
side1 = a;
side2 = b;
side3 = c;
}
public void show() {
System.out.println(side1+","+side2+","+side3+",");
}
public void calcArea(){
double s = 0.0, Num = 0.0;
s = 0.5*(side1+side2+side3);
Num = s*((s-side1)*(s-side2)*(s-side3));
}
public double getArea(){
return( 1/2*(side1*side2*side3));
}
public double getPerimeter(){
return (side1 + side2 + side3);
}
@Override
public String toString(){
return "Triangle Information:+ “\nside1 = " + side1 + "\nside2 = " + side2 +
"\nside3 = " + side3 + "\ncolor = " + getColor() + "\nfilled =" + isFilled();
}
}
break
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("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println(triangle);
}
}
I have the code running but for some reason it is always returning Area as “0.0” instead of the actual area of the triangle. What am I missing?
You are calculating the area and “getting” it in two different functions. I think you should just calculate and return the area in the getArea() method (actually, your calcArea method does nothing – it calculates the area (wrongly, I might add), and does nothing with it.)
The formula for area is the square root of what you calculated in the variable “Num” in calcArea(). Instead of having the separate functions getArea() and calcArea(), just put that code in getArea() and get rid of the calcArea() method.
The reason you get 0 as the area from your getArea() method is that you start the formula with “1/2”, which evaluates to 0 under integer division (remember integer division discards the remainder). To fix this, you should make it “1.0/2.0”, or alternatively just divide by “2.0”. (Of course, remember that the formula you use in getArea() is not even correct!)