001 Tutorial 2
1
Topics
• Control Structures
• Objects and Classes
• Problem Set 2
2
Control Structures: Branch
(Review)
if (boolean){
statement;}
if (boolean) {
statement1;}
else{
statement2;}
3
Control Structures: Branch
(Review)
if (boolean1){
statement1;}
else if (boolean2){
statement2;}
else if (booleanN){
statementN;}
else {statement;}
4
Control Structures –
Branch Example
int x = Integer
.parseInt(JOptionPane
if(x<0)
else if(x>0)
else
5
Control Structures: Iteration (1)
while (boolean){
statement;
never executes.*/
6
Control Structures - Exercise 1
7
Control Structures: Iteration (2)
do{
statement;
} while (boolean);
execute once.*/
8
Control Structures - Exercise 2
9
Control Structures: Iteration (3)
statement; }
executes.*/
10
Control Structures - Exercise 3
11
Control Structures - Exercise 4
12
Classes and Objects
specifications)
13
Classes and Objects cont’d
– A skyscraper sways
14
Anatomy of Class
Data Members
Constructor
Methods
15
Example of Class
//data members
//constructor
ID = i;
name = s;}
//methods
16
Create and Use Instance (Object)
• Keyword new
Student(12,”Dave”);
• . operator
int id = dave.yourID();
System.out.println(dave.name+”\t”
+id);
17
Exercise 4
18
Problem Set 2
19
Problem Set 2 cont’d
• Interest Calculation
$1030
20
Problem Set 2 cont’d
• Math.random()
– Takes no arguments
– Returns a double between 0 and 1
Exercise:
• Initial investment = $2,500
22