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
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());
}
}
Comments
Leave a comment