Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:41:58+00:00 2026-06-15T17:41:58+00:00

Design a class named Triangle that extends GeometricObject (code given below). The Triangle class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:41:59+00:00Added an answer on June 15, 2026 at 5:41 pm

    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!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have a file named instance.php that contains my class listing.page 2 is
In my project, say I am instantiating a class named SubClass , which extends
I have a class named Defense with custom init method as below: // initialize
I have a class named Jellyfish who use the singleton design pattern : jellyfish.h
I've got a design problem in a C++ project. I have a class, named
Here is the project that I have been given: Design and implement a set
I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This
A brief preface: I'm in a game design class where our project for the
hey everyone... so i'm working on a database design class for university. I've got
In my class design I ran into the following problem: class MyData { int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.