Sei sulla pagina 1di 21

1

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”

• If a class constructor wants to call another


constructor in the same class it uses a call to super( )
False: super () is used to call a constructor from
the upper class.
3
Final review
• If a constructor in a derived class includes a call to
a constructor in the base class then the call must be
the 1st action in the derived class constructor?
True.

• A class can inherit from multiple classes in Java?


False: a class can inherit only from one class.
But…a class can inherit from multiple interfaces.
4
Final review
• In an inheritance hierarchy, the most specific
class is at the top and the most generic is at the
bottom?
False. The opposite.

• Private methods of a base class are not accessible in


derived classes?
True: private means really private.
5
Final review
• If a class has a static variable x then modifying x
for only one object of the class is enough for all
objects to see the new value?
True. That is what static is for.
Remember static int studentCnt.

• Default value of a static integer variable in a class is


null?
False: default value of an integer is 0. Static has
nothing to do with default value.
6
Final review
• To prevent a method from being overridden we can
declare the method as:
static ? const ? final ? abstract ? Not possible ?
final. Final methods in the base class can’t be
overridden in the derived classes.

• All classes should override the methods toString()


and equals() from the Object class?
True: if we don’t we get them from the Object
class which is not what we want.
7
Final review
• Class constructors:
1. constructor has no return type?
2. Its name must be the same as the class in which it is defined?
3. There must be exactly one constructor defined for a class?
4. Constructors are almost always declared as public
5. They can appear anywhere in the class where it is legal to
declare a method?

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);

1. True but really not a good way to test.


2. Yes – classical way to test.
3. div ??? What’s that ???
4. No.
9
Final review
• What is the output of this program?
public class testincr{
public static void main(String args[]){
int i = 0;
i = i++ + i;
System.out.println("i = " +i);
}
}

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?");

Does not compile.


There is a ; at the end of the if statement which
ends the statement. The else is invalid !
11
Final review
• What is the output of this program?
public class Test { public class Test {
static int i = 1; static int i = 1;
public static void main(String[] args) { public static void main(String[] args) {
System.out.print(i + ","); System.out.print(i + ",");
m(); m(i);
System.out.println(i); System.out.println(i);
}//main }//main
public static void m(){ public static void m(){
i+=2; i+=2;
} }
public static void m(int i){ public static void m(int i){
i+=2; i+=2;
} }
}//Test }//Test

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
}

After line 1: p.value = 20 q.value = 20


After line 2: p.value = 15 q.value = 15
14
Final review
• What is the output of this program?
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
}

Null pointer exception (p is set to null prior to print).


15
Final review
• Write the content of the array after each call:
public static void mystery(int[] a) { public static void main(String[] args) {
int[] b = new int[2]; int[] a1 = {7, 5};
for (int i=0; i<a.length; i++) { mystery(a1);
if (a[i] >= 0) {
int x = a[i] % 2; int[] a2 = {3, 11, 4, 9, -3};
b[x]++; mystery(a2);
a[i] = b[x];
} int[] a3 = {0, 0, 0, 1, 1, 1};
} mystery(a3);
} }

Using ArraysTostring (arr)


[1, 2]
[1, 2, 1, 3, -3]
[1, 2, 3, 1, 2, 3]
16
Final review
• A file content has lines, each one contents an integer
between 1 and 1000. All numbers are different except
one (which is missing).
• Write a java program to find out which number is
missing.
public static void main(String[] args) throws FileNotFoundException {
final int THEORICALSUM = 1000 * 1001 / 2;
int actualSum = 0;
Scanner reader = new Scanner(new File("ints.txt"));
while (reader.hasNext()){
actualSum = actualSum + reader.nextInt();
}
System.out.println ("Number missing:" + (THEORICALSUM - actualSum));

}
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
}

class Z can’t extend 2 classes.


19
Final review
• What is printed?
class A{ public class MainClass {
A(){ public static void main(String[] args){
System.out.print(1);
C c = new C();
}
}
}
class B extends A{ }
B(){
System.out.print(2);
}
}
class C extends B{
C(){
System.out.print(3);
}
}

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;

public Person(String s, int a){


this.name = s;
this.age = a;
}
public String toString(){
return (this.name + " " + this.age;
}
}//Person
21
Final review
Consider a class Population that would be an array of person.
1. Is Population a super class of Person ?
2. The whole array is not going to be “full” day one, how would
you keep track of how many “person” are in the array ?
3. Write a constructor for Population . Make the array of size 10.
4. Write a method add (Person p) that would add a person to the
existing “population”. If the array is full, throw an exception.
5. Write a method remove (Person p) that would remove a
person from the existing “population”. If the array is empty,
throw an exception.
6. Write a method SortA that would sort the array by age.

Potrebbero piacerti anche