Worksheet: J2
Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.
- Github Classroom Link: https://classroom.github.com/a/IquMq6LX
Questions
-
Define polymorphism in the context of Java and provide one example where it is valuable?
-
Suppose you had a class
Pet
with the following data elements:public class Pet{ private String name; private String species; private String breed; //... }
Explain why such a design may not exhibit good OOP with respect to inheritance and polymorphism. Come up with a better object relationship instead.
-
Consider the following program from the class notes
public class Ex3 { public static void main(String[] args) { Random rand = new Random(System.currentTimeMillis()); Point v = new Point(3, 4); LabPoint w = new LabPoint(5, 2, "A"); String x = "I'm a string"; Scanner y = new Scanner(System.in); Object u; int i = rand.nextInt(4); if( i == 0 ) u = v; else if( i == 1 ) u = w; else if( i == 2 ) u = x; else u = y; System.out.println(u); //<-- } }
Explain how polymorphism makes this program possible.
-
What is the output of this program? You should be able to do this without running the program!
class A { public String toString(){ return "A"; } } class B extends A{ public String toString() { return "B"; } } class C extends A { public String toString() { return super.toString(); } } class D extends C { public String toString() { return super.toString(); } } public class tmp{ public static void main(final String args[]) { D d = new D(); System.out.println(d.toString()); } }
-
What is the output of this program? You should be able to do this without running the program!
class A { public String toString(){ return "A"; } public String fancyToString() { return "~~A~~"; } } class B extends A{ public String toString() { A letterA = this; return letterA.fancyToString(); } public String fancyToString(){ return "~~B~~"; } } public class LetterPrinter{ public static void main(final String args[]) { B letterB = new B(); System.out.print(letterB.toString()+ " "); A letterA = letterB; System.out.println(letterA.toString()); } }
-
What is the output of this program? You should be able to do this without running the program!
class A { public String toString(){ return "A"; } public String fancyToString() { return "~~A~~"; } } class B extends A{ public String fancyToString(){ return "~~B~~"; } } public class LetterPrinter{ public static void main(final String args[]) { B letterB = new B(); System.out.print(letterB.toString()+ " "); A letterA = letterB; System.out.println(letterA.toString()); } }
-
Consider the first 2 class declarations. What is the output of the program below? You should be able to do this without running the program!
abstract class Letter { protected boolean uppercase; abstract String get_name(); abstract int get_alphabet_position(); }
class A extends Letter{ public String toString() { return "A"; } protected int get_alphabet_position() { return 1; } private String get_name() { return "A"; } }
public class Main{ public static void main(final String args[]) { A a = new A(); System.out.println("A: " + a.get_alphabet_position()); } }
-
If we change the implementation of
A
to the following, how does that change the output?class A extends Letter{ public String toString() { return "A"; } public int get_alphabet_position() { return 1; } protected String get_name() { return "A"; } }
-
What is the output of this program? You should do this without running the program.
class A{ public String toString(){ return "A"; } } class B extends A{ public String toString(){ return "B"; } } public class PolymorphicOverload{ public void foo(B letterB1, B letterB2) { // 2 System.out.println("foo2: " + letterB1 + " " + letterB2); } public void foo(A letterA1, A letterA2){ //1 System.out.println("foo1: " + letterA1 + " " + letterA2); } public static void main(String args[]){ PolymorphicOverload f = new PolymorphicOverload(); B letterB = new B(); A letterA = (A) new B(); f.foo(letterB, letterA); } }
-
Assume that
class A
is implemented in such a way so that the program will compile and run. What is the output? You should do this problem without running the code.public class tmp{ public static void foo(A a){ System.out.println("foo1: "+a.get_name()); } public static void foo(Letter a) { System.out.println("foo2: " + a.get_name()); } public static void main(final String args[]) { Letter a =(Letter) new A(); foo(a); } }
-
What is the output of this code and provide a one-to-two sentence explanation of the output? You should do this problem first without running the code, and then run it to check your output. If you get it wrong at first, explain why your initial assumptions were wrong and what the right output is.
public class Dog{ private String name; public class Breed{ private String bname; public Breed(String b){this.bname=b;}; public String toString(){return name+":"+bname;} } public Dog(String n){this.name=n;}
public static void main(String args[]){ Dog d1 = new Dog("Jack"); Dog d2 = new Dog("Champ"); Dog.Breed b1 = d1.new Breed("pitbull"); Dog.Breed b2 = d1.new Breed("blacklab"); System.out.println(b1 + " " + b2); }
-
What is the differences between a normal java Class and an abstract java class?
-
Create an abstract class for a
Car
– what features should be defined in the implemented class vs. what can be defined in the abstract class? Provide justifications for your design.