Question

Using Java Create the class RightTriangle. Its constructor will require the lengths only of its two...

Using Java

Create the class RightTriangle.

  • Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem:

a2 + b2 = c2.

  • This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle.
  • It will inherit equals from its parent class
  • You also should enhance the String returned from the toString method of its parent class to identify this as a right triangle
  • It will inherit getNumberOfSides, calculatePerimeter, and calculateArea from its parent.

You also should recalculate the hypotenuse every time one of the two legs changes. How would you do this?

In your main method,  create a right triangle with legs of length 3 and 4, use toString to print the lengths of all sides, the perimeter, and the area. Do the same using legs of length 5 and 12.

Triangle.java

public class Triangle extends Figure{

private double length1, length2, length3;
  
public Triangle(double len1, double len2, double len3)
{
this.length1 = len1;
this.length2 = len2;
this.length3 = len3;
}

public double getLength1() {
return length1;
}

public void setLength1(double length1) {
this.length1 = length1;
}

public double getLength2() {
return length2;
}

public void setLength2(double length2) {
this.length2 = length2;
}

public double getLength3() {
return length3;
}

public void setLength3(double length3) {
this.length3 = length3;
}
  
@Override
public int getNumberOfSides(){ return 3; }
  
@Override
public String toString()
{
return("Number of sides: " + getNumberOfSides() + ", Side1: " + String.format("%.2f", getLength1())
+ ", Side2: " + String.format("%.2f", getLength2())
+ ", Side3: " + String.format("%.2f", getLength3())
+ ", Area: " + String.format("%.2f", super.getArea())
+ ", Perimeter: " + String.format("%.2f", super.getPerimeter()));
}

@Override
public boolean equals(Object object) {
if(object instanceof Triangle)
{
return(getLength1() == ((Triangle) object).getLength1()
&& getLength2() == ((Triangle) object).getLength2()
&& getLength3() == ((Triangle) object).getLength3());
}
else
return false;
}

@Override
public void calculatePerimeter() {
setPerimeter(length1 + length2 + length3);
}

@Override
public void calculateArea() {
double p = super.getPerimeter() / 2;
double area = Math.sqrt(p * (p - length1) * (p - length2) * (p - length3));
super.setArea(area);
}
}

Homework Answers

Answer #1

Thanks for the question,

Here is the implemented class - RightTriangle that inherits from Triangle class.

I have provided detailed comments so that you can follow the code precisely. The right triangle contains also the main test method that demonstrates the below two points -

create a right triangle with legs of length 3 and 4, use toString to print the lengths of all sides, the perimeter, and the area. Do the same using legs of length 5 and 12.

Please note : The question didnt had the Figure parent class which the Triangle class extends. So incase there is any error please do comment the errror you are getting so that I can change the code accordngly. But should not be the case.

===========================================================================

public class RightTriangle extends Triangle {


    public RightTriangle(double sideOne, double sideTwo) {

        super(sideOne, sideTwo, 0);

        setLength3(getHypotenuse());
    }


    // calculate length3 which is the Hypotenuse
    private double getHypotenuse() {
        // length3^2 = length1^2 + length2^2
        return Math.sqrt(getLength1() * getLength1() + getLength2() * getLength2());
    }



    // You also should recalculate the hypotenuse every time one of the two legs changes.
    @Override
    public void setLength1(double length1) {
        super.setLength1(length1);
        // everytime length 1 or length 2 changes we call getHypotenuse() method
        // and set the length of hypotenuse to that
        setLength3(getHypotenuse());
    }


    @Override
    public void setLength2(double length2) {
        super.setLength2(length2);
        // everytime length 1 or length 2 changes we call getHypotenuse() method
        // and set the length of hypotenuse to that
        setLength3(getHypotenuse());
    }

    @Override
    public String toString() {
        return super.toString();
    }

   public static void main(String[] args) {

       
        RightTriangle rightTriangle = new RightTriangle(3,4);
        // length3 should be 5
        System.out.println(rightTriangle);
       
        // set length1 to 5
        rightTriangle.setLength1(5);
        // set length2 to 12
        rightTriangle.setLength2(12);
        // length3 should be 13
        System.out.println(rightTriangle);
    }


}

=========================================================

thanks a lot !

Know the answer?
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
Similar Questions
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
JAVA 4.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by...
JAVA 4.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Here's the code: import java.util.Scanner; class TriangleYN {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);               double a;        double b;        double c;               System.out.print("Enter three sizes, separated by spaces(decimals values are acceptable):");...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT