Java OOP

 

SHORT ANSWER QUESTIONS

INTERFACES & POLYMORPHISM

 

SOLUTIONS


1-implements

 

2-class, interface

 

3-method

4-true

5-all

 

6-a

 

7-b

 

8-b

 

9-d

 

10-no

 

11-@Override 

 

12-true

 

13-Both method headers are missing their return-type.


14-The class Coconut needs to implement both methodA and methodB.

 

15-Because the method() in Coconut does not have an int parameter, it is not a match to the methodA in the interface.  So we are not implementing the methods from the interface.

 

16-The @Override used over methodC() means that methodC() would have to be in the interface.  As it is not there, Java gives an error.  We could simply remove @Override over the methodC header.

 

17-compareTo(Object o)

18-We could call compareTo using two strings like this:
           String s1 = "bacon";

           String s2 = "apple";

           System.out.println(s1.compareTo(s2));
And then we could try different string values until we understand what is happening.


Also, instead, we could create an array of Strings and sorted it to see the order in which they end up.  Like this:

           String[] arr = {"zebra", "cat", "lion", "tiger", "dog", "fish"};

           Arrays.sort(arr);

           System.out.println(Arrays.toString(arr));

 

19-Here are the outputted values:

a) 0    (zero, because both strings are the same)

b) 32   (positive, because l comes after L in the ascii chart – 32 characters later)

c)-10   (negative, because l comes before v alphabetically – 10 letters before)

d) 10   (positive, because v comes after l alphabetically – 10 letters later)

e) 2    (positive, because c comes alphabetically after a – 2 letters after)

f)-6    (negative, after the tied l letters, the o comes before u – 6 letters before

 

20-The order:


[2rtle, Tiger, cat, dog, doggie, lion, zebra]

 

21-Strings are sorted alphabetically, or alphanumerically.  However, all capital letters appear before all lower case letters in the sorting alphabet.  And numbers appear before all letters.  (Symbols also have an order but we won’t worry about this now.)