// CSE 142, Autumn 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 tenToTheThird = Math.pow(10, 3); double a = Math.pow(10, 3) + 17.6 / Math.sqrt(121.0); double r = 7; double area = Math.PI * Math.pow(r, 2); System.out.println("area is " + area); System.out.println("10^3 = " + tenToTheThird); System.out.println("a = " + a); System.out.println("2 to the 5th is " + Math.pow(2, 5)); // test the slope method by calling it, printing result double s2 = slope(2, 3, 6, 11); System.out.println("The slope is " + s2); System.out.println(displacement(3.0, 4.0, 5.0)); } // 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 s = dy / dx; return s; } // 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) { double result = v0 * t + (double) 1 / 2 * a * Math.pow(t, 2); return result; } }