I'm new to Java and am following a series of lectures on Stanford's YouTube. I know that the code works, but I'd like to know if it can be improved upon.
/*
* File: PythagoreanTheorem.java
* Name: Will
* Section Leader: N/A, I'm freeloading the class off Youtube.
* -----------------------------
* This file is the starter file for the PythagoreanTheorem problem.
*/
import acm.program.*;
public class PythagoreanTheorem extends ConsoleProgram {
public PythagoreanTheorem() {}
public void run() {
PythagoreanTheorem myCalculator = new PythagoreanTheorem();
double a = readDouble("a= ");
double b = readDouble("b= ");
if(myCalculator.formula(a,b) ==
Math.floor(myCalculator.formula(a,b))) {
println("C= "+(int)myCalculator.formula(a,b));}
else {
println("C= "+myCalculator.formula(a,b));}
}
private double formula(double x, double y) {
double c = Math.sqrt(x*x+y*y);
return c;
}
}
formula
there is nothing much to say. But we do not have much to review. \$\endgroup\$