Question One:
Shouldn't the pointer in the above line from exercise 2 change toSystem.out.println(r.getChannel()); //should print 0
System.out.println(t.getChannel()); //should print 0
If not, please drop a hint or two, as for what is this exercise is aiming to accomplish.
Question Two:
Lets assume that the above pointer issue was just a typo. If so, which one of these two solutions would be a good code for exercise 2, overloaded methods or use of instanceof and casting to create a single method?
Code: Select all
static void reset (Object input){
if (input instanceof TV)
((TV)input).setChannel(0);
else
((Radio)input).setFrequency(0.0);
}
Code: Select all
static void reset (TV input){
input.setChannel(0);
}
static void reset (Radio input) {
input.setFrequency(0.0);
}
Code: Select all
class Radio{
private double frequency = 1.1; //insert appropriate getter and setter
public void setFrequency (double f){
this.frequency = f;
}
public double getFrequency(){
return frequency;
}
}
class TV{
private int channel = 5; //insert appropriate getter and setter
public void setChannel(int c){
this.channel = c;
}
public int getChannel(){
return channel;
}
}
Code: Select all
public static void main(String[] args){
/* This segment is for Exercise 1 and not relevant to this set of questions
Document d = new PdfDocument();
d.setType("pdf");
System.out.println(d.getType()); //should print "pdf"
*/
TV t = new TV();
Radio r = new Radio();
reset(t);
reset(r);
System.out.println(r.getFrequency()); //should print 0.0
System.out.println(t.getChannel()); //should print 0
}