This Java code defines a Triangles class with private variables for base, height, and calculated area. It includes getter and setter methods for base and height, and a calculateArea method that sets the area variable. The main method creates two Triangle objects, calls calculateArea on each, and prints their areas.
This Java code defines a Triangles class with private variables for base, height, and calculated area. It includes getter and setter methods for base and height, and a calculateArea method that sets the area variable. The main method creates two Triangle objects, calls calculateArea on each, and prints their areas.
This Java code defines a Triangles class with private variables for base, height, and calculated area. It includes getter and setter methods for base and height, and a calculateArea method that sets the area variable. The main method creates two Triangle objects, calls calculateArea on each, and prints their areas.
public Triangles(double ba,double he){ base = ba; height = he; } public void setBase(double b){ base = b; } public double getBase(){ return base; } public void setHeight(double h){ height = h; } public double getHeight(){ return height; } public void calculateArea(){ area = 0.5 * base * height; } public double getArea(){ return area; } }
2. public class TestTriangles{
public static void main(String[]args){ Triangles triangel = new Triangles(6.5, 2.0); triangel.calculateArea(); System.out.println("The area of triangel is"+"\t"+triangel.getArea()); Triangles triangel2 = new Triangles(5.0, 4.0); triangel2.calculateArea(); System.out.println("The area of triangel is"+"\t"+triangel2.getArea()); } }