I am just trying to figure out basic classes/objects. I am trying to write a program where it will return the area of a triangle. I realize this can be done easier another way but I am just trying to figure out how these things work....
import java.util.Scanner; class Triangle { double height; double base; double getArea() { return 1/2 * height * base; } } public class study { public static void main(String[]args) { Triangle Triangle = new Triangle(); Scanner in = new Scanner(System.in); System.out.println("Height?"); Triangle.height = in.nextInt(); System.out.println("Base?"); Triangle.base = in.nextInt(); Triangle.getArea(); System.out.println("The area of a triangle with height " + Triangle.height + " and base " + Triangle.base + " is " + Triangle.getArea()); } }
It keeps printing the null value 0.0 for area of a triangle.