Math

In this program you'll perform several calculations, including with mixed data types.

  1. Go into the CS labs (see this map for details) and log onto a Windows machine.
  2. Start up a new project, called Math, with a main class called Main.java. Refer to Lesson 2 if you are unsure how to do this. You do not need to include javabook2.jar in your class files. But be sure to delete TrivialApplication.java and set Main.java as the main file.
  3. In the blank file Main.java, write the following code:
  4. public class Main {
    
    	public static void main(String[] args) {
    		int a = 0;
    		int b = 1;
    		int c = 2;
    		
    		System.out.println("a = " + a);
    		System.out.println("b = " + b);
    		System.out.println("c = " + c);		
    
    		int r = b / c;
    		System.out.println(b + " / " + c + " = " + r);
    
    		double d = (double)b / (double)c;
    		System.out.println((double)b + " / " + (double)c + " = " + d);
    
    		++a;
    
    		System.out.println("a = " + a);
    		
    		System.out.println("a = " + ++a);
    
    		boolean aGreaterThanB = a > b;
    		System.out.println("a is greater than b is " + aGreaterThanB);
    		
    		boolean aEqualsB = a == b;
    		System.out.println("a equals b is " + aEqualsB);
    
    		System.out.println("a equals c is " + (a == c));
    
    		double x = Math.sqrt(c);
    		System.out.println("x = " + x);		
    		
    		x = Math.pow(c,b);
    		System.out.println("x = " + x);
    
    		x = Math.PI;
    		System.out.println("x = " + x);
    
    		System.out.println("e is approximately " + Math.E);
    
    		System.out.println("the remainder of 376 / 84 = " + (376 % 84));
    
    		System.out.println("Here is a random number between 0.0 and 1.0: " + Math.random());
    		System.out.println("Here is a random number between 0.0 and 1.0: " + Math.random());
    		System.out.println("Here is a random number between 0.0 and 1.0: " + Math.random());
    		
    	}
    
     }
    
  5. Compile and run your program.
  6. Press enter, as the console window says, and the black console window will disappear.