// Tyler Rigsby, CSE 142 // Some examples of using Math and returns public class ReturnExamples { public static void main(String[] args) { int base = 2; int exponent = -3; double result = Math.pow(base, exponent); double roundedResult = round2(result); System.out.println(base + " to the " + exponent + " is " + roundedResult); double hyp1 = hypotenuse(3, 4); double hyp2 = hypotenuse(2.6, 6.0); System.out.println("the hypotenese are: " + hyp1 + ", " + round2(hyp2)); System.out.println("the displacement is: " + displacement(3.0, 7.0, 2.0)); System.out.println(repeatTwice("nananana") + " batman!"); } // Rounds the specified value to two decimal places and returns the result public static double round2(double val) { // .125 // 12.5 // 13 // .13 return Math.round(val * 100) / 100.0; } // Returns the length of the hypotenuse of a triangele // given the lengths of shorter sides a and b public static double hypotenuse(double a, double b) { return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); } // Returns the displacement of an object given acceleration a, // initial velocity v0, and time t public static double displacement(double a, double v0, double t) { return 0.5 * a * t * t + v0 * t; } // Returns the given String repeated twice (that is, concatenated with itself) public static String repeatTwice(String text) { return text + text; } }