java - Right Triangle calculations returning 0? -
i can't figure out doing wrong here. wrote similar code worked fine. 1 time got handle on how utilize scanner class. working on programme takes height , base of operations of right triangle , calculates perimeter, area, , hypotenuse. returns 0.0 each method , can't figure out if missing sort of syntax or if messing calculations. can point me in right direction of mistake?
public class righttriangle { private double height; private double base; private final double hypotenuse = math.sqrt(math.pow(height, 2) + math.pow(base, 2)); public righttriangle(double triheight, double tribase) { triheight = height; tribase = base; } public double getperimeter() { double perimeter = height + base of operations + hypotenuse; homecoming perimeter; } public double getarea() { double area = (height + base) / 2; homecoming area; } public double gethypotenuse() { homecoming hypotenuse; } }
here main method:
import java.util.scanner; public class calculatetriangle { public static void main(string[] args) { scanner triangleheight = new scanner(system.in); system.out.print("enter triangle height: "); double height = triangleheight.nextdouble(); scanner trianglebase = new scanner(system.in); system.out.print("enter triangle base: "); double base of operations = trianglebase.nextdouble(); righttriangle newtriangle = new righttriangle(height, base); newtriangle.getperimeter(); double perimeter = newtriangle.getperimeter(); newtriangle.getarea(); double area = newtriangle.getarea(); newtriangle.gethypotenuse(); double hypotenuse = newtriangle.gethypotenuse(); system.out.println(perimeter); system.out.println(area); system.out.println(hypotenuse); } }
your code:
public righttriangle(double triheight, double tribase) { triheight = height; tribase = base; }
should be:
public righttriangle(double triheight, double tribase) { height = triheight; base of operations = tribase; }
java
No comments:
Post a Comment