// 1. calulate the side of pyramid // Math formula: s = sqrt( h^2 + (b *.5)^2 ) // // Java: s = Math.sqrt( h*h + (b*.5)*(b*.5) ); // s = Math.sqrt( Math.pow(h, 2.) + Math.pow(b*.5, 2.) ); // 2. Calcualte surface and volume // Msth: sa = 2bs + b^2 Java: sa = 2. * b * s + b * b; // vol = 1/3hb^2 Java: vol = 1./3. * h * b * b; import java.util.Scanner; import java.text.DecimalFormat; class CalPyramid { public static void main(String[] args) { Scanner input = new Scanner(System.in); DecimalFormat f2 = new DecimalFormat("0.00"); double base, height, side, sa, vol; System.out.print("Input base: "); base = input.nextDouble(); System.out.print("Input height: "); height = input.nextDouble(); side = Math.sqrt( Math.pow(height, 2.) + Math.pow(base*.5, 2.) ); sa = 2. * base * side + base * base; vol = 1./3. * height * base * base; System.out.println("side = " + f2.format(side) ); System.out.println("s.a = " + f2.format(sa) ); System.out.println("vol = " + f2.format(vol) ); } }