Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Announcements
• HW14 – due Thursday 12/8
• Last day of class is Thursday 12/8
• Final is on Wednesday 12/14 from 9am to 12pm
• Questions ?
2
Final review
• Instance variables which are mark private in a
base class can be directly accessed by their
name in a derived class?
False: variable which are private can only be
accessed using a “getter”
1. True.
2. True.
3. False.
4. True.
5. True.
8
Final review
• Which statements set variable even to true if and only if the integer n
(assume >= 0) is even?
1. boolean even = (n/2.0 == (double)(n/2));
2. boolean even = (n % 2 == 0);
3. boolean even = (n div 2 == 0);
4. boolean even = (n % 2 == n/2);
i=1
i++ returns 1 but the value of i remains unchanged.
10
Final review
• What is the output of this program?
String greet = "Hi";
String name = "Smedley";
String nickName = name.substring(0,4);
if (nickName == name.substring(0,4));
System.out.println("has real nickname");
else if (greet + name == greet + nickName)
System.out.println("no real nickname");
else
System.out.println("hmmm...changed names?");
1,3 because the method m() 1,1 because the method m(i)
is called and the global is called and the local variable
variable i is modified. i is modified.
12
Final review
• What is the output of this program?
String S = ""; String T = "";
int i = 4;
for (i = 1; i <= 3; i++);
S = S + "!";
for (i = 1; i < 4; i++)
T = T + "*";
System.out.print(S);
System.out.println(T);
!***
First for loop has a ; at the end: empty loop.
13
Final review
• What are p.value and q.value after line 1 ? Line 2 ?
public class IntObj { public static void main(String[] args) {
private int value; IntObj p = new IntObj();
public void setValue (int newVal){ IntObj q;
value = newVal; q = new IntObj();
} q.setValue(20);
public int getValue(){ p.setValue(q.getValue()); //line 1
return value; p.setValue(15);
} q = p; //line 2
} p = null;
System.out.print(p.getValue());//line 3
}
}
17
Final review
• What happens?
class A { public class MainClass {
int i = 10; public static void main(String[] args) {
} A a = new B();
System.out.println(a.i);
class B extends A { }
int i = 20; }
}
10
18
Final review
• Is this correct ?
class X {
//Class X Members
}
class Y {
//Class Y Members
}
class Z extends X, Y{
//Class Z Members
}
123
20
Final review
• Consider a class Person as described below.
– Write the following methods:
• Equals (true if 2 person have same name and same age)
• LTA (Person p) (true if this person age < p.age)
• LEA (Person p) (true if this person age <= p.age)
• LTN (Person p) (true if this person name < p.name)
• LEN (Person p) (true if this person name <= p.name)
class Person {
private String name;
private int age;