Skip to content

Instantly share code, notes, and snippets.

@talothman
Created September 21, 2015 23:25
Show Gist options
  • Save talothman/b586ba300095378f2ae6 to your computer and use it in GitHub Desktop.
Save talothman/b586ba300095378f2ae6 to your computer and use it in GitHub Desktop.

CSCI 1302 Fall 2015 (Alothman) - Homework 2

Hard copy is due at the beginning of class on Monday 2015-09-28

If a question asks you to create a class, do not include any constructors unless explicitly asked to. If a question gives you a code snippet and asks you to determine its output or identify if lines of the code snippet compile or not, then please rewrite the code snippet and mark your answer(s) to the right of code in the form of comments.

Also, students are encourage to read the following article before attempting the homework: https://gist.github.com/mepcotterell/34905c95cd260007c293.

I would suggest students read the section on inheritance first and then go through the sections on interfaces and generics.

Question 1

Consider the following classes:

package cs1302.hw3.foo;
public class Parent {
    private int a = 2;
    protected int b = 7;
    int c = 9;
    public int d = 4;
} // Person
package cs1302.hw3.bar;
import cs1302.hw3.foo.Parent;
public class Child extends Parent {
    private int e = 3;
    public void baz() {
        System.out.println(a); // Question 1.3
        System.out.println(b); // Question 1.4
        System.out.println(c); // Question 1.5
        System.out.println(d); // Question 1.6
        System.out.println(e); // Question 1.7
    } // baz
} // Child
  1. Assuming the code above compiles, what does a Parent object look like in heap memory?

  2. Assuming the code above compiles, what does a Child object look like in heap memory?

  3. Would the line marked as Question 1.3 compile? Why or why not?

  4. Would the line marked as Question 1.4 compile? Why or why not?

  5. Would the line marked as Question 1.5 compile? Why or why not?

  6. Would the line marked as Question 1.6 compile? Why or why not?

  7. Would the line marked as Question 1.7 compile? Why or why not?

Question 2

Consider the following classes:

package cs1302.hw3.foo;
public class Parent {
    private void a() { System.out.println("2"); }
    protected void b() { System.out.println("7"); }
    void c() { System.out.println("9"); }
    public void d() { System.out.println("4"); }
} // Person
package cs1302.hw3.bar;
import cs1302.hw3.foo.Parent;
public class Child extends Parent {
    private void e() { System.out.println("3"); }
    public void baz() {
        a(); // Question 2.1
        b(); // Question 2.2
        c(); // Question 2.3
        d(); // Question 2.4
        e(); // Question 2.5
    } // baz
} // Child
  1. Would the line marked as Question 2.1 compile? Why or why not?

  2. Would the line marked as Question 2.2 compile? Why or why not?

  3. Would the line marked as Question 2.3 compile? Why or why not?

  4. Would the line marked as Question 2.4 compile? Why or why not?

  5. Would the line marked as Question 2.5 compile? Why or why not?

Question 3

  1. Create a simple Parent and Child class such that Child extends the Parent class. Include two methods in the Parent called foo() and bar() (both void) that each print out the name of method. In the Child class, override the foo() method to print out "Child-foo()".

  2. Write a small code snippet that demonstrates that the overriding in question 3.1 is taking place and include its output.

Question 4

Consider the following classes:

public class Parent {
    public void foo() { }
} // Parent
public class Child extends Parent {
    public void bar() { }
} // Child
public class Grandchild extends Child { }
    public void baz() { }
} // GrandChild
  1. Suppose we have Parent p = new Parent();. What methods can we invoke using p?

  2. Suppose we have Child c = new Child();. What methods can we invoke using c?

  3. Suppose we have GrandChild g = new GrandChild();. What methods can we invoke using g?

  4. Suppose we have Parent p = new Child();. What methods can we invoke using p?

  5. Suppose we have Parent p = new GrandChild();. What methods can we invoke using p?

  6. The assignments in questions 4.4 and 4.5 are correct (i.e., they would compile). Why is that?

Question 5

Consider the following classes:

public class Parent {
    public void foo() { System.out.println("Parent-foo()"); }
} // Parent
public class Child extends Parent {
    public void foo() { System.out.println("Child-foo()"); }
    public void bar() { System.out.println("Child-bar()"); }
} // Child
public class Grandchild extends Child { }
    public void foo() { System.out.println("GrandChild-foo()"); }
    public void baz() { System.out.println("GrandChild-baz()"); }
} // GrandChild
  1. For each line in the following code snippet, indicate whether or not it will compile? If a line produces any output when executed, indicate what the output would be.

    Parent p = new Parent();
    p.foo();
  2. For each line in the following code snippet, indicate whether or not it will compile? If a line produces any output when executed, indicate what the output would be.

    Parent p = new Child();
    p.foo();
    p.bar();
  3. For each line in the following code snippet, indicate whether or not it will compile? If a line produces any output when executed, indicate what the output would be.

    Parent p = new GrandChild();
    p.foo();
    p.bar();
    p.baz();

Question 6

Consider the following class:

public abstract class Parent {
    int a = 2;
    public void foo() { System.out.println("foo()");
    public abstract void bar();
} // Parent
  1. Create a class called Child such that Child extends the Parent class. Implement the bar() so that it prints out the method name.

  2. Assuming the Parent class provided above as well as your Child class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Parent a = new Parent();
    a.foo();
  3. Assuming the Parent class provided above as well as your Child class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Child b = new Child();
    b.foo();
    b.bar();
  4. Assuming the Parent class provided above as well as your Child class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Parent c = new Child();
    c.foo();
    c.bar();

Question 7

Consider the following classes and interfaces:

public interface Openable {
    public void open();
} //  Openable
public interface Closeable {
    public void close();
} // Closeable
public abstract class Product {
    private String name;
    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
} // Product
  1. Create a class called Book such that Book extends the Product class and implements both the Openable and Closeable interfaces. If you need to implement a method, just have that method print out its name.

  2. Assuming the classes and interfaces provided above as well as your Book class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Book b = new Book();
    b.setName("book");
    System.out.println(b.getName());
    b.open();
    b.close();
  3. Assuming the classes and interfaces provided above as well as your Book class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Product b = new Book();
    b.setName("book");
    System.out.println(b.getName());
    b.open();
    b.close();
  4. Assuming the classes and interfaces provided above as well as your Book class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Openable b = new Book();
    b.setName("book");
    System.out.println(b.getName());
    b.open();
    b.close();
  5. Assuming the classes and interfaces provided above as well as your Book class, go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Closeable b = new Book();
    b.setName("book");
    System.out.println(b.getName());
    b.open();
    b.close();

Question 8

Consider the following generic interface:

public interface Comparable<T> {
    public int compareTo(T o);
} // Comparable
  1. Suppose we have public class Employee implements Comparable<Employee>. What would the signature of the compareTo method look like in the Employee class?

  2. Suppose we have public class Employee implements Comparable<Integer>. What would the signature of the compareTo method look like in the Employee class?

  3. Suppose we have public class Employee implements Comparable<Employee>. Go through each line in the following code snippet and indicate whether or not it will compile? If a line does not compile, then explain why not. If a line produces any output when executed, indicate what the output would be.

    Employee a = new Employee();
    Comparable<Employee> b = new Employee();
    Comparable c = new Employee();

Question 9

Consider the following generic class:

public class Box<T> {
    private T item;
    public void set(T item) { this.item = item; }
    public T get() { return this.item; } 
} // Box
  1. Write a snippet of code that creates a new Box<Integer> object and assigns it to a reference variable called box. You should be sure to declare the variable.

  2. Suppose you have a Box<Integer>. What would you expect the signatures of the set and get methods to be? Why?

  3. Suppose you have a Box. What would you expect the signature of the set and get methods to be? Why?

  4. Suppose you have a reference to a Box<Number> object via reference variable called box (of the same type). Will the following code snippet compile? Why or why not?

    box.set(new Integer(7));
  5. Suppose you have a reference to a Box<Number> object via reference variable called box (of the same type). Will the following code snippet compile? Why or why not?

    box.set(new Double(7.0));
  6. Suppose you have the following method:

    public void foo(Box<Number> b) { ... }

    Will the following code snippet compile? Why or why not?

    foo(new Box<Integer>());

Question 10

  1. What does it mean for a generic type parameter, say T, to be unbounded? What assumptions can you make when using references of type T in your code?

  2. What does it mean for a generic type parameter, say T, to be bounded? What assumptions can you make when using references of type T in your code?

  3. Before the existence of official generics in Java, how did programmers write and use generic code. Here, the term generic refers to the ability for a class or method to store or operate with different classes/types.

  4. What is type erasure and why is it performed?

Question 11

Consider the following class:

public class Outer {
    private String s;
    static class Inner {
        void foo() {
           s = "Set from Inner";
        } // foo
    } // Inner
} // Outer
  1. Why does the code presented above not compile?

  2. What change should be made to make the code compile?

Question 12

Use the Java API documentation for the Box class (in the javax.swingpackage) to help you answer the following questions.

  1. What static nested class does Box define?

  2. What inner class does Box define?

  3. What is the superclass of Box's inner class?

  4. Which of Box's nested classes can you use from any class?

  5. How do you create an instance of Box's Filler class?

Question 13

Consider the following interface and classes (assume the Event class exists):

public interface EventHandler<T extends Event> {
    void handle(T event);
} // EventHandler
public class ActionEvent extends Event { ... }
public class MyClass {
    public void foo() {
        String s = "initial value";
        EventHandler<ActionEvent> e = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                s = "set from anonymous class";
            } // handle
        };
        s = "hello";
    } // foo
} // MyClass
  1. Why does the code presented above for the MyClass class not compile?

  2. What change should be made to make the code compile? Hint: declaring s as final is not the correct answer.

  3. Rewrite the assignment to the reference variable e to use a lambda expression instead of an anonymous class. Consider your answer to 3.2 when doing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment