Answer to Question #224061 in Java | JSP | JSF for Fatima

Question #224061

Write a java class to represents triangle. The triangle has two private data member which are base and height. Public methods are: setBase, setHeight, getBase, getHeight, getArea and getCirconference. Then create an object of type triangle in your main and call its methods


1
Expert's answer
2021-08-06T16:49:55-0400
import java.util.Scanner;

public class Triangle {
    private double base;
    private double height;

    public double getBase() {
        return base;
    }

    public void setBase(double base) {
        this.base = base;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getArea()
    {
        return base*height/2;
    }

    public double getCirconference()
    {
        return base+height;
    }

    public static void main(String[] args)
    {
        System.out.print("Enter base: ");
        double base = new Scanner(System.in).nextDouble();

        System.out.print("Enter height: ");
        double height = new Scanner(System.in).nextDouble();

        Triangle triangle = new Triangle();
        triangle.setBase(base);
        triangle.setHeight(height);

        System.out.print("\nBase: "+triangle.getBase());
        System.out.print("\n"+"Height: "+ triangle.getHeight());
        System.out.print("\n"+ "Area: "+triangle.getArea());
        System.out.print("\n"+ "Circonference: "+triangle.getCirconference());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS