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.
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;
} // Personpackage 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-
Assuming the code above compiles, what does a
Parentobject look like in heap memory? -
Assuming the code above compiles, what does a
Childobject look like in heap memory? -
Would the line marked as
Question 1.3compile? Why or why not? -
Would the line marked as
Question 1.4compile? Why or why not? -
Would the line marked as
Question 1.5compile? Why or why not? -
Would the line marked as
Question 1.6compile? Why or why not? -
Would the line marked as
Question 1.7compile? Why or why not?
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"); }
} // Personpackage 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-
Would the line marked as
Question 2.1compile? Why or why not? -
Would the line marked as
Question 2.2compile? Why or why not? -
Would the line marked as
Question 2.3compile? Why or why not? -
Would the line marked as
Question 2.4compile? Why or why not? -
Would the line marked as
Question 2.5compile? Why or why not?
-
Create a simple
ParentandChildclass such thatChildextends theParentclass. Include two methods in theParentcalledfoo()andbar()(both void) that each print out the name of method. In theChildclass, override thefoo()method to print out "Child-foo()". -
Write a small code snippet that demonstrates that the overriding in question 3.1 is taking place and include its output.
Consider the following classes:
public class Parent {
public void foo() { }
} // Parentpublic class Child extends Parent {
public void bar() { }
} // Childpublic class Grandchild extends Child { }
public void baz() { }
} // GrandChild-
Suppose we have
Parent p = new Parent();. What methods can we invoke usingp? -
Suppose we have
Child c = new Child();. What methods can we invoke usingc? -
Suppose we have
GrandChild g = new GrandChild();. What methods can we invoke usingg? -
Suppose we have
Parent p = new Child();. What methods can we invoke usingp? -
Suppose we have
Parent p = new GrandChild();. What methods can we invoke usingp? -
The assignments in questions 4.4 and 4.5 are correct (i.e., they would compile). Why is that?
Consider the following classes:
public class Parent {
public void foo() { System.out.println("Parent-foo()"); }
} // Parentpublic class Child extends Parent {
public void foo() { System.out.println("Child-foo()"); }
public void bar() { System.out.println("Child-bar()"); }
} // Childpublic class Grandchild extends Child { }
public void foo() { System.out.println("GrandChild-foo()"); }
public void baz() { System.out.println("GrandChild-baz()"); }
} // GrandChild-
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();
-
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();
-
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();
Consider the following class:
public abstract class Parent {
int a = 2;
public void foo() { System.out.println("foo()");
public abstract void bar();
} // Parent-
Create a class called
Childsuch thatChildextends theParentclass. Implement thebar()so that it prints out the method name. -
Assuming the
Parentclass provided above as well as yourChildclass, 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();
-
Assuming the
Parentclass provided above as well as yourChildclass, 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();
-
Assuming the
Parentclass provided above as well as yourChildclass, 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();
Consider the following classes and interfaces:
public interface Openable {
public void open();
} // Openablepublic interface Closeable {
public void close();
} // Closeablepublic abstract class Product {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
} // Product-
Create a class called
Booksuch thatBookextends theProductclass and implements both theOpenableandCloseableinterfaces. If you need to implement a method, just have that method print out its name. -
Assuming the classes and interfaces provided above as well as your
Bookclass, 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();
-
Assuming the classes and interfaces provided above as well as your
Bookclass, 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();
-
Assuming the classes and interfaces provided above as well as your
Bookclass, 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();
-
Assuming the classes and interfaces provided above as well as your
Bookclass, 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();
Consider the following generic interface:
public interface Comparable<T> {
public int compareTo(T o);
} // Comparable-
Suppose we have
public class Employee implements Comparable<Employee>. What would the signature of thecompareTomethod look like in theEmployeeclass? -
Suppose we have
public class Employee implements Comparable<Integer>. What would the signature of thecompareTomethod look like in theEmployeeclass? -
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();
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-
Write a snippet of code that creates a new
Box<Integer>object and assigns it to a reference variable calledbox. You should be sure to declare the variable. -
Suppose you have a
Box<Integer>. What would you expect the signatures of thesetandgetmethods to be? Why? -
Suppose you have a
Box. What would you expect the signature of thesetandgetmethods to be? Why? -
Suppose you have a reference to a
Box<Number>object via reference variable calledbox(of the same type). Will the following code snippet compile? Why or why not?box.set(new Integer(7));
-
Suppose you have a reference to a
Box<Number>object via reference variable calledbox(of the same type). Will the following code snippet compile? Why or why not?box.set(new Double(7.0));
-
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>());
-
What does it mean for a generic type parameter, say
T, to be unbounded? What assumptions can you make when using references of typeTin your code? -
What does it mean for a generic type parameter, say
T, to be bounded? What assumptions can you make when using references of typeTin your code? -
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.
-
What is type erasure and why is it performed?
Consider the following class:
public class Outer {
private String s;
static class Inner {
void foo() {
s = "Set from Inner";
} // foo
} // Inner
} // Outer-
Why does the code presented above not compile?
-
What change should be made to make the code compile?
Use the Java API documentation for the
Box class
(in the javax.swingpackage) to help you answer the following questions.
-
What static nested class does
Boxdefine? -
What inner class does
Boxdefine? -
What is the superclass of
Box's inner class? -
Which of
Box's nested classes can you use from any class? -
How do you create an instance of
Box'sFillerclass?
Consider the following interface and classes (assume the Event class exists):
public interface EventHandler<T extends Event> {
void handle(T event);
} // EventHandlerpublic 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-
Why does the code presented above for the
MyClassclass not compile? -
What change should be made to make the code compile? Hint: declaring
sasfinalis not the correct answer. -
Rewrite the assignment to the reference variable
eto use a lambda expression instead of an anonymous class. Consider your answer to 3.2 when doing this.