Advanced Programming Rabie A. Ramadan [email protected] vpro/ Lecture 4.
-
Upload
merilyn-gregory -
Category
Documents
-
view
219 -
download
1
Transcript of Advanced Programming Rabie A. Ramadan [email protected] vpro/ Lecture 4.
Advanced Programming
Rabie A. [email protected]
http://www.rabieramadan.org/classes/2011/Advpro/
Lecture 4
Q4
6
Wwhat is wrong with the following code: public interface Comparable
{
int compareTo(Object other);
}
int compareTo(Object otherObject)
{
Employee other = (Employee) otherObject;
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
}
Q5
7
if “Comparable” is an interface, what is the meaning of the following line of code :
if (anObject instanceof Comparable) { . . . }
Q6
8
What is wrong with the following code: public interface Moveable
{
void move(double x, double y);
}
A. public interface Powered extends Moveable
{
double milesPerGallon();
}
B.public interface Powered extends Moveable
{
double milesPerGallon();
double SPEED_LIMIT = 95; // a public static final constant
}
Q9
11
Describe what actually happen in:
Employee original = new Employee("John Public", 50000);
Employee copy = original;
copy.raiseSalary(10);
Q11
13
clone () method initially is “protected” method and returns an object , can I implement it as follows? If No , how can I implement it? class Employee implements Cloneable
{
public Employee clone() throws CloneNotSupportedException
{
return (Employee) super.clone();
}
. . .
}
Q12
14
clone () method is used to copy the whole class methods and fields. Why do I need to clone the hireDay field in the following code separately ? class Employee implements Cloneable
{
. . .
public Employee clone() throws CloneNotSupportedException
{
// call Object.clone()
Employee cloned = (Employee) super.clone();
cloned.hireDay = (Date) hireDay.clone()
return cloned;
}
}
Q13
15
Which of the following implementation to the clone () method is better than the other :
1. public Employee clone() throws CloneNotSupportedException
2. public Employee clone()
{
try
{
return super.clone();
}
catch (CloneNotSupportedException e) { return null; }
}
Q15
17
Is there anything wrong with this code : if nothing wrong , describe how the inner class accesses the fields of the outer class during the run time.
public class TalkingClock
{
public TalkingClock(int interval, boolean beep) { . . . }
public void start() { . . . }
private int interval;
private boolean beep;
private class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep) Toolkit.getDefaultToolkit().beep(); }}}
Q16
18
Which of the following statements will be passed by the compiler without any error message considering the previous implementation:
1. if (outer.beep) Toolkit.getDefaultToolkit().beep();
2.
3. if (beep) Toolkit.getDefaultToolkit().beep();
4.
Q18
20
What is the output of the following commands :
java ReflectionTest TalkingClock\$TimePrinter
or
javap -private TalkingClock\$TimePrinter
Q19
22
If the following is the output of the previous instruction , what is the meaning of
final TalkingClock this$0 ;
Q21
24
what is missing in the following code to start a timer that is listening on TimePrinter:
public void start()
{
class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
}
Q23
26
Why the following code does not compile: public void start(int interval, boolean beep)
{
class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
t.start();
}
Q24
27
The following is the output of javap -private TalkingClock\$TimePrinter:
What was the original class structure ?
Q27
32
What is the difference between the following two statements:
public static final double SPEED_LIMIT = 55;
public final double SPEED_LIMIT = 55;
Q29
34
It is obvious that the constructors of Anonymous inner classes must be declared at the beginning of the class ? (True/False)