Source of DoMath.java


  1: import java.util.Scanner;

  3: /**
  4:  * A program that uses various Math methods.
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  */
  8: public class DoMath {

 10:     public static void main(String[] args) {
 11:         // create variables
 12:         Scanner kbd = new Scanner(System.in);
 13:         int n, m, big;
 14:         int a, b, c;
 15:         double x, y, z;

 17:         // Introduce yourself
 18:         System.out.println("\n\n"
 19:                 + "I'm just doin' some math!\n\n");
 20:         pause();

 22:         // Math.pow
 23:         System.out.println("5 squared is " + Math.pow(5, 2));
 24:         System.out.println("2 to the 5th is " + Math.pow(2, 5));
 25:         pause();

 27:         // Math.max
 28:         System.out.print("Enter two integers: ");
 29:         n = kbd.nextInt();
 30:         m = kbd.nextInt();
 31:         kbd.nextLine();
 32:         big = Math.max(n, m);
 33:         System.out.println("The maximum of " + m + " and " + n + " is "
 34:                 + big + ".");
 35:         System.out.println("I wonder what the minimum is....");
 36:         pause();

 38:         // Math.sqrt
 39:         x = Math.sqrt(10);
 40:         System.out.println("The square root of 10 is " + x + ".");
 41:         pause();

 43:         // Math.hypot
 44:         System.out.print("Enter two numbers (with decimals): ");
 45:         y = kbd.nextDouble();
 46:         z = kbd.nextDouble();
 47:         kbd.nextLine();
 48:         System.out.println("The diagonal of a rectangle of width " + y 
 49:                 + " and length " + z + " is " + Math.hypot(y, z) + ".");
 50:         pause();


 53:         // solve a quadratic equation
 54:         System.out.print("Enter the a, b, and c co-ordinates "
 55:                 + "of a quadratic equation.\nFor example, "
 56:                 + "the 3, 5, and -1 in 3x^2 + 5x - 1: ");
 57:         a = kbd.nextInt();
 58:         b = kbd.nextInt();
 59:         c = kbd.nextInt();
 60:         kbd.nextLine();
 61:         System.out.println("The roots of the equation "
 62:                 + a + "x^2 + " + b + "x + " + c + " are:\n\t"
 63:                 + ((-b + Math.sqrt(b*b - 4*a*c)) / (2 * a)) + " and \n\t"
 64:                 + ((-b - Math.sqrt(b*b - 4*a*c)) / (2 * a)) + ".");
 65:         pause();

 67:         // Math.round
 68:         x = 3.6;
 69:         m = (int)Math.round(x);
 70:         System.out.println(x + " rounded off to the nearest int is " + m);
 71:         System.out.print("Enter two numbers (with decimals): ");
 72:         y = kbd.nextDouble();
 73:         z = kbd.nextDouble();
 74:         kbd.nextLine();
 75:         n = (int)Math.round(y);
 76:         m = (int)Math.round(z);
 77:         System.out.println(y + " rounded off to the nearest int is " + n);
 78:         System.out.println(z + " rounded off to the nearest int is " + m);
 79:         x = 3456789012.34567890;
 80:         m = (int)Math.round(x);
 81:         System.out.printf("%,.1f rounded off to the nearest int is %,d.\n",
 82:                 x, m);
 83:         System.out.println("\tor IS it????");
 84:         pause();
 85:     }

 87:     /**
 88:      * Prompt the user and wait for them to press the enter key.
 89:      */
 90:     public static void pause() {
 91:         Scanner kbd = new Scanner(System.in);

 93:         System.out.println();
 94:         System.out.print("Press Enter...");
 95:         kbd.nextLine();
 96:         System.out.println();
 97:     }

 99: }