LAB 5 - Introduction to Java (part 3)

Lab Exercises

Topics

Variables

 

Exercises

2.4.2 Using Variables in Calculations

Try typing the following into the interaction pane:

 

> int numPeople = 2;

> System.out.println(numPeople);

 

> double bill = 32.45;

> System.out.println(bill);

 

> double tip = bill * 0.2;

> System.out.println(tip);

 

> double total = bill + tip;

> System.out.println(total);

 

> double totalPerPerson = total / numPeople;

> System.out.println(“each person owes: “ + totalPerPerson);

 

2.4.4 Object Variables

Try typing the following into the interaction pane:

 

> String test;

> System.out.println(test);

 

> test = “Hi”;

> System.out.println(test);

 

> test = new String (“Bye”);

> System.out.println(test);

 

2.4.5 Reusing Variables

Try typing the following into the interaction pane:

 

> String myName = “Wayne”;

> System.out.print(myName);                

 

> myName = “Sue”;

> System.out.print(myName);

 

> String yourName = “Lee”;

> System.out.print(yourName);

 

> String yourName = “Barb”;

> System.out.print(yourName);

 

2.4.6 Multiple References to an Object

Try typing the following into the interaction pane:

 

> String name1 = “CS Dept.”;

> System.out.print(name1);                   

 

> String name2 = name1;

> System.out.print(name2);                   

 

 

Assignment

 

Submit the answers to 2.12, 2.13, 2.17 through the DropBox in WebCT.