/******************************** FILE HEADER ********************************** Main Class File: Main.java File: Calculator.java Author: Michael Schultz CS Login: mschultz Collaborators: Completion Date: 9/26/02 Course: CS 302 TA: Mr. Schultz Compiler: Code Warrior IDE 5.0 Platform: Windows 2000 *******************************************************************************/ /** * Bugs: none known * * @author Michael Schultz * @version 1.0 * @see also Main.java */ /** * The Calculator class is designed primarily to illustrate how the * Math class works. In fact, the only real difference is that the * Math clas is part of a package, java.lang, which is automatically * imported (since we always at least need to extend Object) */ public final class Calculator extends Object { /** * The default constructor, inherited from Object, has been * overridden here with the private visibility modifier. This * means that no outside classes can declare new Calculator(). * Hence, the Calculator class is non-instantiable. */ private Calculator() {} /** * addition for integers (non-decimals) * * @param param1 the first addend * @param param2 the second addend * * @return the sum */ public static int add(int param1, int param2) { return param1 + param2; } /** * addition for real numbers (decimals) * * @param param1 the first addend * @param param2 the second addend * * @return the sum */ public static double add(double param1, double param2) { return param1 + param2; } /** * subtraction for integers (non-decimals) * * @param param1 the first subtraction parameter * @param param2 the second subtraction parameter * * @return the difference */ public static int subtract(int param1, int param2) { return param1-param2; } /** * subtraction for real numbers (decimals) * * @param param1 the first subtraction parameter * @param param2 the second subtraction parameter * * @return the difference */ public static double subtract(double param1, double param2) { return param1 - param2; } /** * multiplication for integers (non-decimals) * * @param param1 the first factor * @param param2 the second factor * * @return the product */ public static int multiply(int param1, int param2) { return param1 * param2; } /** * multiplication for real numbers (decimals) * * @param param1 the first factor * @param param2 the second factor * * @return the product */ public static double multiply(double param1, double param2) { return param1 * param2; } /** * division for integers (non-decimals) * * @param param1 the numerator * @param param2 the denominator * * @return the quotient without remainder */ public static int divide(int param1, int param2) { return param1 / param2; } /** * division for real numbers (decimals) * * @param param1 the numerator * @param param2 the denominator * * @return the quotient */ public static double divide(double param1, double param2) { return param1 / param2; } /** * average taking in integers (non-decimals) * * @param param1 the first parameter * @param param2 the second parameter * * @return the exact (decimal) average */ public static double average(int param1, int param2) { return ((double)param1 + (double)param2) / 2; } /** * average taking in real numbers (decimals) * * @param param1 the first parameter * @param param2 the second parameter * * @return the exact (decimal) average */ public static double average(double param1, double param2) { return (param1 + param2) / 2; } /** * the squarring function for integers (non-decimals) * * @param param the number to be squarred * * @return the square of the number */ public static int square(int param) { return (int)Math.pow(param,2); } /** * the squarring function for real numbers (decimals) * * @param param the number to be squarred * * @return the square of the number */ public static double square(double param) { return Math.pow(param,2); } /** * the square root function taking in integers (non-decimals) * * @param param the number to be rooted * * @return the exact (decimal) square root of the number */ public static double squareRoot(int param) { return Math.sqrt(param); } /** * the square root function taking in reeal numbers (decimals) * * @param param the number to be rooted * * @return the square root of the number */ public static double squareRoot(double param) { return Math.sqrt(param); } /** * recursive factorial. factorial is defined as the product of * all positive numbers from 1 to the argument, inclusively. * Factorial of n is sometimes written n!. 0! = 1 by definition. * n! is undefined for n < 0 and for non-integers. * * @param param the parameter * * @return the product of all integers from 1 to the parameter * inclusively */ public static int factorial(int param) { if (param < 0) { System.out.println("Invalid parameter value to factorial\n"); return param; } else if (param == 0) { return 1; } else { return param * factorial(param - 1); } } /** * the absolute value function for integers (non-decimals) * * @param param the parameter * * @return the euclidean distance from the paramter to zero */ public static int absoluteValue(int param) { return Math.abs(param); } /** * the absolute value function for real numbers (decimals) * * @param param the parameter * * @return the euclidean distance from the paramter to zero */ public static double absoluteValue(double param) { return Math.abs(param); } /** * a boolean test of integers (non-decimals) * * @param param the number to test for eveness * * @return true when the number is a an exact multiple of 2 * and false otherwise */ public static boolean even(int param) { return (param % 2 == 0); } /** * a boolean test of integers (non-decimals) * * @param param the number to test for oddness * * @return false when the number is a an exact multiple of 2 * and true otherwise */ public static boolean odd(int param) { return !even(param); } }