Question

Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

Why is my program returning 0 for the area of a triangle?

public class GeometricObjects {

   private String color = " white ";
    private boolean filled;
    private java.util.Date dateCreated;

    public GeometricObjects() {
        dateCreated = new java.util.Date();
    }

    public GeometricObjects(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 + "\n color: " + color + " and filled ";               
   }
}

Below is the class containing the formula for the area of a triangle:

import java.lang.Math;

public class Triangle extends GeometricObjects {
  
   private double side1, side2, 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 getSide1(double side1) {
        this.side1 = side1;
    }

    public void getSide2(double side2) {
        this.side2 = side2;
    }

    public void getSide3(double side3) {
        this.side3 = side3;
    }
  
    public double getPerimeter() {
        return side1 + side2 + side3;
    }

    public double getArea() {
        double p = (side1 + side2 + side3) / 2;
        double i = (p * ((p - side1) * (p - side2)
               * (p - side3)));
        double area = Math.sqrt(i);
        return area;
    }

    public String toString() {
    return "Triangle: Side 1 = " + side1 + " Side 2 = " + side2
           + " Side 3 = " + side3;
    }
}   

And finally, below is my code for the test class:

import java.util.Scanner;

public class TestGeometricObjects {
  
   private double side1, side2, side3, length, width = 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 triangleColor = input.next();

        System.out.println("Is the triangle filled? Reply with"
            + " 'True' or 'False' ");
        boolean triangleFilled = input.nextBoolean();
      
       Triangle triangle = new Triangle(side1, side2, side3);
       triangle.setFilled(triangleFilled);
       triangle.setColor(triangleColor);
          
       System.out.println("The triangle's 3 sides are " + side1 + ", " + side2 +
               ", " + side3);
       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());
     
       System.out.println("Enter the length and width of a rectangle: ");
       double length = input.nextDouble();
       double width = input.nextDouble();
     
       System.out.println("Enter the color of the rectangle: ");
        String rectangleColor = input.next();
      
       System.out.println("Is the rectangle filled? Reply with"
                + " 'True' or 'False' ");
       boolean rectangleFilled = input.nextBoolean();
     
       Rectangle rectangle = new Rectangle(length, width);
       rectangle.setFilled(rectangleFilled);
       rectangle.setColor(rectangleColor);
     
       System.out.println("The rectangle's length is " + length +
               " and the rectangle's width is " + width);
       System.out.println("The rectangle's area is " + rectangle.getRectArea());
       System.out.println("The rectangle's perimeter is " + rectangle.getRectPerimeter());
       System.out.println("The rectangle's color is " + rectangle.getColor());
       System.out.println("Is the rectangle filled? " + rectangle.isFilled());
      
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Its working fine your have to enter Sides of triangle./
//its returning correct area.
//Add rectangle class also .


public class GeometricObjects {
   private String color = " white ";
    private boolean filled;
    private java.util.Date dateCreated;
    public GeometricObjects() {
        dateCreated = new java.util.Date();
    }
    public GeometricObjects(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 + "\n color: " + color + " and filled ";             
   }
}
import java.lang.Math;
public class Triangle extends GeometricObjects {
   private double side1, side2, 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 getSide1(double side1) {
        this.side1 = side1;
    }
    public void getSide2(double side2) {
        this.side2 = side2;
    }
    public void getSide3(double side3) {
        this.side3 = side3;
    }
    public double getPerimeter() {
        return side1 + side2 + side3;
    }
    public double getArea() {
        double p = (side1 + side2 + side3) / 2;
        double i = (p * ((p - side1) * (p - side2)* (p - side3)));
        double area = Math.sqrt(i);
        return area;
    }
    public String toString() {
    return "Triangle: Side 1 = " + side1 + " Side 2 = " + side2
           + " Side 3 = " + side3;
    }
}
import java.util.Scanner;
public class TestGeometricObjects {
   private double side1, side2, side3, length, width = 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 triangleColor = input.next();
        System.out.println("Is the triangle filled? Reply with"
            + " 'True' or 'False' ");
        boolean triangleFilled = input.nextBoolean();
       Triangle triangle = new Triangle(side1, side2, side3);
       triangle.setFilled(triangleFilled);
       triangle.setColor(triangleColor);
       System.out.println("The triangle's 3 sides are " + side1 + ", " + side2 +
               ", " + side3);
       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());
       System.out.println("Enter the length and width of a rectangle: ");
       double length = input.nextDouble();
       double width = input.nextDouble();
       System.out.println("Enter the color of the rectangle: ");
        String rectangleColor = input.next();
       System.out.println("Is the rectangle filled? Reply with"
                + " 'True' or 'False' ");
       boolean rectangleFilled = input.nextBoolean();
       Rectangle rectangle = new Rectangle(length, width);
       rectangle.setFilled(rectangleFilled);
       rectangle.setColor(rectangleColor);
       System.out.println("The rectangle's length is " + length +
               " and the rectangle's width is " + width);
       System.out.println("The rectangle's area is " + rectangle.getRectArea());
       System.out.println("The rectangle's perimeter is " + rectangle.getRectPerimeter());
       System.out.println("The rectangle's color is " + rectangle.getColor());
       System.out.println("Is the rectangle filled? " + rectangle.isFilled());
   }
}

Add a comment
Know the answer?
Add Answer to:
Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public...

    The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject() { dateCreated = new java.util.Date(); } protected 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...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class 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(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • Can the folllowing be done in Java, can code for all classes and code for the...

    Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The 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 with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a 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...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT