You are given the following definitions of five classes:
class A { public A() { print("A()"); } public int m1 (int n) { print("A.m1()"); return n+2; } public int m2 (int n) { print("A.m2("+n+")"); return n*2; } public int m3 (int n) { print("A.m3("+n+")"); return n/2; } public int m4 (int n) { print("A.m4("+n+")"); return m1(n) + m2(n); } void print (String msg) { System.out.println(msg); } } |
class B extends A { protected int j; public B () { print("B()"); j=10; } public B (int n) { print("B("+n+")"); j=n; } public int m1 (int n) { print("B.m1("+n+")"); return super.m3(n); } public int m3 (int n) { print("B.m3("+n+")"); return n - 2; } public int m5 (int n) { print("B.m5("+n+")"); return m3(n) + super.m3(n); } } |
class C extends B { protected int k; public C () { this(4,5); // must be first line in method print("C()"); } public C (int k) { print("C("+k+")"); this.k = k; } public C (int j, int k) { super(j); // must be first line in method print("C("+j+","+k+")"); this.k = k; } public int m2 (int n) { print("C.m2("+n+")"); return n*3; } } |
class D extends B { public D (int k) { super(k); // must be first line in method print("D("+k+")"); } public int m2 (int n) { print("D.m2("+n+")"); return n + j; } public int m3 (int n) { print("D.m3("+n+")"); return n + 4; } public int m6 (int n) { print("D.m6("+n+")"); return m3(n) + super.m3(n); } } |
class E extends D { public E () { super(8); // why is this line required? print("E()"); } } |
Draw an inheritance diagram for the five classes and write out what would be printed if we invoked the following series of statements:
spacer("Creating instanceA"); A instanceA = new A(); spacer("Invoking instanceA.m4(10)"); System.out.println("instanceA.m4(10)="+instanceA.m4(10)); spacer("Creating instanceB1"); B instanceB1 = new B(); spacer("Creating instanceB2"); B instanceB2 = new B(8); spacer("Invoking instanceB2.m4(10)"); System.out.println("instanceB2.m4(10)="+instanceB2.m4(10)); spacer("Invoking instanceB2.m5(10)"); System.out.println("instanceB2.m5(10)="+instanceB2.m5(10)); spacer("Creating instanceC1"); C instanceC1 = new C(); spacer("Creating instanceC2"); C instanceC2 = new C(5); spacer("Creating instanceC3"); C instanceC3 = new C(8,4); spacer("Invoking instanceC3.m4(10)"); System.out.println("instanceC3.m4(10)="+instanceC3.m4(10)); spacer("Creating instanceD"); D instanceD = new D(8); spacer("Invoking instanceD.m4(10)"); System.out.println("instanceD.m4(10)="+instanceD.m4(10)); spacer("Invoking instanceD.m5(10)"); System.out.println("instanceD.m5(10)="+instanceD.m5(10)); spacer("Invoking instanceD.m6(10)"); System.out.println("instanceD.m6(10)="+instanceD.m6(10)); spacer("Creating instanceE"); E instanceE = new E(); spacer("Invoking instanceE.m4(10)"); System.out.println("instanceE.m4(10)="+instanceE.m4(10)); spacer("Invoking instanceE.m5(10)"); System.out.println("instanceE.m5(10)="+instanceE.m5(10)); spacer("Invoking instanceE.m6(10)"); System.out.println("instanceE.m6(10)="+instanceE.m6(10));where
spacer
is defined as the following:
public static void spacer(String msg) { System.out.println("-----"); System.out.println(msg); }Finally, answer the question posed next to the constructor method of E.
public Panel makeLabelPanel (String text) { Panel p = new Panel(); p.add(new Label(text)); return p; } public Panel makeButtonPanel (String text) { Panel p = new Panel(); p.add(new Button(text)); return p; } public Panel makeTextFieldPanel (String text, int cols) { Panel p = new Panel(); p.add(new TextField(text, cols)); return p; } public Panel makeSpacerPanel () { Panel p = new Panel(); p.setBackground(Color.black); return p; }Study the following applet layouts. Think about how they would be displayed. Draw them out for yourself (but nothing to turn in) and check yourself against the answers. Bring any questions you have to lab.
Labels in Different Layouts | ||
---|---|---|
public void init () { // Label Layout 1 this.add(new Label("one")); this.add(new Label("twotwo")); this.add(new Label("Three's Company")); this.add(new Label("four more!")); this.add(new Label("five")); } | public void init () { // Label Layout 2 this.setLayout(new GridLayout(5,1)); this.add(new Label("one")); this.add(new Label("twotwo", Label.CENTER)); this.add(new Label("Three's Company", Label.LEFT)); this.add(new Label("four more!", Label.RIGHT)); this.add(makeLabelPanel("five")); } | |
public void init () { // Label Layout 3 this.setLayout(new GridLayout(1,3)); this.add(new Label("one")); this.add(makeSpacerPanel()); this.add(new Label("twotwo")); } | public void init () { // Label Layout 4 this.setLayout(new GridLayout(5,1)); this.add(new Label("one")); this.add(new Label("twotwo")); this.add(new Label("Three's Company")); this.add(new Label("four more!")); this.add(new Label("five")); } | |
public void init () { // Label Layout 5 this.setLayout(new GridLayout(1,3)); this.add(makeLabelPanel("one")); this.add(makeSpacerPanel()); this.add(makeLabelPanel("twotwo")); } | public void init () { // Label Layout 6 this.setLayout(new GridLayout(5,1)); this.add(makeLabelPanel("one")); this.add(makeSpacerPanel()); this.add(makeLabelPanel("twotwo")); this.add(makeLabelPanel("Three's Company")); } | |
public void init () { // Label Layout 7 this.setLayout(new BorderLayout()); this.add("North", new Label("North")); this.add("South", new Label("South")); this.add("East", new Label("East")); this.add("West", new Label("West")); this.add("Center", new Label("Center")); } | public void init () { // Label Layout 8 this.setLayout(new BorderLayout()); this.add("North", makeLabelPanel("North")); this.add("South", makeLabelPanel("South")); this.add("East", makeLabelPanel("East")); this.add("West", makeLabelPanel("West")); this.add("Center", makeLabelPanel("Center")); } |
Buttons in Different Layouts | ||
---|---|---|
public void init () { // Button Layout 1 this.add(new Button("one")); this.add(new Button("twotwo")); this.add(new Button("Three's Company")); this.add(new Button("four more!")); this.add(new Button("five")); } | ||
public void init () { // Button Layout 2 this.setLayout(new GridLayout(1,3)); this.add(new Button("one")); this.add(makeSpacerPanel()); this.add(makeButtonPanel("twotwo")); } | public void init () { // Button Layout 3 this.setLayout(new GridLayout(5,1)); this.add(new Button("one")); this.add(new Button("twotwo")); this.add(makeSpacerPanel()); this.add(makeButtonPanel("Three's Company")); this.add(makeButtonPanel("four more!")); } | |
public void init () { // Button Layout 4 this.setLayout(new BorderLayout()); this.add("North", new Button("North")); this.add("South", new Button("South")); this.add("East", new Button("East")); this.add("West", new Button("West")); this.add("Center", new Button("Center")); } | public void init () { // Button Layout 5 this.setLayout(new BorderLayout()); this.add("North", makeButtonPanel("North")); this.add("South", makeButtonPanel("South")); this.add("East", makeButtonPanel("East")); this.add("West", makeButtonPanel("West")); this.add("Center", makeButtonPanel("Center")); } |
TextFields in Different Layouts | ||
---|---|---|
public void init () { // TextField Layout 1 this.add(new TextField("one",6)); this.add(new TextField("twotwo",4)); this.add(new TextField("Three's Company")); this.add(new TextField("four more!",8)); this.add(new TextField("five",20)); } | ||
public void init () { // TextField Layout 2 this.setLayout(new GridLayout(1,3)); this.add(new TextField("one",6)); this.add(new TextField("twotwo",4)); this.add(new TextField("Three's Company")); } | public void init () { // TextField Layout 3 this.setLayout(new GridLayout(5,1)); this.add(new TextField("one",6)); this.add(new TextField("twotwo",4)); this.add(new TextField("Three's Company")); this.add(makeTextFieldPanel("four more!",8)); this.add(makeTextFieldPanel("five",20)); } | |
public void init () { // TextField Layout 4 this.setLayout(new BorderLayout()); this.add("North", new TextField("North",5)); this.add("South", new TextField("South",10)); this.add("East", new TextField("East",4)); this.add("West", new TextField("West",8)); this.add("Center", new TextField("Center",10)); } | public void init () { // TextField Layout 5 this.setLayout(new BorderLayout()); this.add("North", makeTextFieldPanel("North",5)); this.add("South", makeTextFieldPanel("South",10)); this.add("East", makeTextFieldPanel("East",4)); this.add("West", makeTextFieldPanel("West",8)); this.add("Center", makeTextFieldPanel("Center",10)); } |