Written Exercise 1


  1. Type in the following Java program exactly as written(Do not cut and paste) and compile it. Type this in exactly as written, even if you see the error. public class DidNotDeclare{ public static void main(String [] args) { age = 50; System.out.println("I am " + age + " years old"); } }
    • - What happened ? (Read the error messages )
    • - How would you fix this ?

  2. Type in the following Java program exactly as written(Do not cut and paste) and compile it. public class HelloWorld { public static void main(String [] args) { System.out.print("Hello") System.out.print("World") System.out.println() } }
    • - What happened ? (Read the error messages )
    • - How would you fix this ?

  3. What are the following symbols or words used for in Java
    • *

    • =

    • class

    • main

  4. The following program attempts to get two integers from the user and displays to the console their sum. Type in the following program exactly as written and compile it. (Do not cut and paste). import javax.swing.JOptionPane; public class Add{ public static void main(String [] args ) { int number1,number2,result; number1 = Integer.parseInt(JOptionPane.showInputDialog("Enter Integer 1")); number2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Integer 2")); number1 + number2; System.out.println(number1 + " + " + number2 + " = " + result); System.exit(0); } }
    • - What happened ? (Read the error messages )
    • - How would you fix this ?

  5. The following program should display 33 to the screen.Type in the following program exactly as written. Compile it and Run it. Hint see section 3.2 public class ShouldDisplay33{ public static void main(String [] args) { System.out.println( 4 + 7 * 3 ); } }
    • - What was the output ?
    • - How would you fix this ?