// CSE 142, Spring 2010, Marty Stepp // This program demonstrates calling some methods from the Math class. // Math methods use return values rather than directly printing their results. // This program also demonstrates writing our own methods that return values, // to perform computations such as line slope and object displacement. public class MathFun { public static void main(String[] args) { double x = Math.pow(3, 4); System.out.println("The answer is " + x); // 3^4 + 9^2 + 14 * 2 double y = x + Math.pow(9, 2) + 14 * 2; System.out.println("The second answer is " + y); Math.round(8.6); // prints nothing // test the slope method by calling it, printing result double s = slope(1, 3, 5, 11); System.out.println(s); // test the displacement method double d = displacement(3.0, 4.0, 5.0); System.out.println("The displacement is " + d); double d2 = displacement(7.2, 1.0, 30.0); System.out.println("The second displacement is " + d2); } // Returns the slope of the line between the given points. // Assumes that x1 does not equal x2. public static double slope(int x1, int y1, int x2, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; } // Computes and returns the change in position (displacement) of a moving // body with the given initial velocity v0 in m/s, the given // acceleration a in m/s^2, over the given interval of time t in s. public static double displacement(double v0, double a, double t) { // v0t + (1/2)at^2 double answer = (v0 * t) + 0.5 * a * Math.pow(t, 2); return answer; } }