LAB 4 - Introduction to Java (part 2)

Lab Exercises

Topics

 

Exercises

Casting

Try typing the following into the interaction pane:

 

> System.out.println(1 / 2);

> System.out.println(1.0 / 2);

> System.out.println(1 / 2.0);

> System.out.println(1.0 / 2.0);

> System.out.println((double) 1 / 2);

> System.out.println(1 /(double)  2);

> System.out.println((double) (1 / 2));

 

Relational Operators

Try typing the following into the interaction pane:

 

> System.out.println(4 > 6);

> System.out.println(6 <= 4);

> System.out.println(‘a’ < ‘e’);

> System.out.println(‘s’ > ‘v’);

> System.out.println(4 == 4);

> System.out.println(6 != 6);

> System.out.println(43 >= 43 );

> System.out.println(41 <= 46);

> System.out.println(true == false);

 

Strings

Try typing the following into the interaction pane:

 

> System.out.println(“Wayne”);

> System.out.print(“Wayne”); System.out.print(“Wayne”);

> System.out.println(“Wayne” + ‘C’ + “Summers”);                                    [how would you fix this so there are spaces between the names?]

 

> System.out.println(“23 + 7”);

> System.out.println(23 + 7);

> System.out.println(“The answer is “ + “23 + 7”);

> System.out.println(“The answer is “ + 23 + 7);

> System.out.println(“The answer is “ + (23 + 7));

 

> System.out.println(“Wayne says, \”Hi\”.”);

> System.out.println(“Wayne says, \t \”Hi\”.”);

> System.out.println(“Wayne says, \n \”Hi\”.”);

 

First Program

a)       Type or copy the following program into the definitions pane.

b)      Compile the program and when asked, name the program Hello.java. (This is the traditional first program a computer scientist writes in a new language.)

c)      Run the program.

 // ********************************************

// Hello.java

//

// Print a Hello, World message.

// ********************************************

 

public class Hello

{

      // -----------------------------------

      // main method -- prints the greeting

      // -----------------------------------

      public static void main (String[] args)

      {

            System.out.println ("Hello, World!");

      }

}

 

 

Assignment

 

Submit the file Hello.java and the answers to 2.10 through the DropBox in WebCT.