Source of SimpleMath.java


  1: import java.util.Scanner;

  3: /**
  4:  * An application to demonstrate variables and simple math
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  * @version 1.1 2011-05-13
  8:  */
  9: public class SimpleMath {
 10:     // this variable is called a CONSTANT:
 11:     public static final double MIDTERM_WEIGHT = 0.20;
 12:     // it is declared outside the main method (any method, actually)
 13:     // its declaration starts with "public static final"
 14:     // its name is in ALL_CAPITALS (with _ between the words)
 15:     // its value cannot be changed

 17:     public static void main(String[] args) {
 18:         // declare some variables
 19:         int     midtermScore    = 78;
 20:         double  midtermWeighted = midtermScore * MIDTERM_WEIGHT;
 21:         char    letterGrade     = 'C';
 22:         boolean failedMidterm   = false;
 23:         Scanner kbd = new Scanner(System.in);

 25:         // describe self
 26:         System.out.println();
 27:         System.out.println("Using Variables");
 28:         System.out.println("---------------");
 29:         System.out.println();

 31:         // print variables & values
 32:         System.out.println("midtermScore is " + midtermScore);
 33:         System.out.println("midtermWeighted is " + midtermWeighted);
 34:         System.out.println("letterGrade is " + letterGrade);
 35:         System.out.println("failedMidterm is " + failedMidterm);
 36:         System.out.println();
 37:         System.out.print("Please press Enter key...");
 38:         kbd.nextLine();
 39:         System.out.println();

 41:         // Some simple math
 42:         System.out.println("7 + 3 is " + (7 + 3));
 43:         System.out.println("7.0 + 3.0 is " + (7.0 + 3.0));
 44:         System.out.println("7 - 3 is " + (7 - 3));
 45:         System.out.println("7.0 - 3.0 is " + (7.0 - 3.0));
 46:         System.out.println("7 * 3 is " + (7 * 3));
 47:         System.out.println("7.0 * 3.0 is " + (7.0 * 3.0));
 48:         System.out.println("7 / 3 is " + (7 / 3));
 49:         System.out.println("7.0 / 3.0 is " + (7.0 / 3.0));
 50:         System.out.println("7 % 3 is " + (7 % 3));
 51:         System.out.println("7.0 % 3.0 is " + (7.0 % 3.0));
 52:         System.out.println();
 53:         System.out.print("Please press Enter key...");
 54:         kbd.nextLine();
 55:         System.out.println();

 57:         // math using variables
 58:         int m, n;
 59:         System.out.println("                       m   n");
 60:         m = 3 + 5;
 61:         System.out.println("m = 3 + 5;             " + m + "   ?");
 62:         n = 4 * m;
 63:         System.out.println("n = 4 * m;             " + m + "   " + n);
 64:         m = n % 5;
 65:         System.out.println("m = n % 5;             " + m + "   " + n);
 66:         n = n / 5;
 67:         System.out.println("n = n / 5;             " + m + "   " + n);
 68:         System.out.print("Please press Enter key...");
 69:         kbd.nextLine();
 70:         System.out.println();
 71:         
 72:         // be careful of order-of-operations!
 73:         n = m + 8 / m + 3;
 74:         System.out.println("n = m + 8 / m + 3;     " + m + "   " + n);
 75:         n = (m + 8) / (m + 3);
 76:         System.out.println("n = (m + 8) / (m + 3); " + m + "   " + n);
 77:         System.out.println();
 78:         System.out.print("Please press Enter key...");
 79:         kbd.nextLine();
 80:         System.out.println();

 82:         // with parentheses
 83:         System.out.println("3*6+4/2 = " + (3*6+4/2));
 84:         System.out.println("3*(6+4)/2 = " + (3*(6+4)/2));
 85:         System.out.println("3*(6+4/2) = " + (3*(6+4/2)));
 86:         System.out.println("2+2+2*3   = " + (2+2+2*3));
 87:         System.out.println();
 88:         System.out.print("Please press Enter key...");
 89:         kbd.nextLine();
 90:         System.out.println();

 92:         // mixed mode expressions
 93:         System.out.println("3.6 + 4 / 2 = " + (3.6 + 4 / 2));
 94:         System.out.println("3.6 + 5 / 2 = " + (3.6 + 5 / 2));
 95:         System.out.println("(3.6 + 5) / 2 = " + ((3.6 + 5) / 2));
 96:         System.out.println();
 97:         System.out.print("Please press Enter key...");
 98:         kbd.nextLine();
 99:         System.out.println();

101:         // type casting
102:         System.out.println("45 / 4 = " + (45 / 4));
103:         System.out.println("(double)45 / (double)4 = "
104:                 + ((double)45 / (double)4));
105:         System.out.println("(int)19.95 = " + ((int)19.95));
106:         System.out.println();
107:         System.out.print("Please press Enter key...");
108:         kbd.nextLine();
109:         System.out.println();
110:     }

112: }