Title: CIS 120 Lab 3
1CIS 120 Lab 3
2- public class Furniture
- private Color c
- public Furniture(Color color)
- c color
-
- public Furniture() c Color.Red()
-
- public class Chair
- private int nLegs
- public Chair(Color c, int legs)
- super(c)
- nLegs legs
-
- public Chair(int legs)nLegs legs
3- gt Item i new Item(null)
- gt i
- null
- gt i null
- false
4Why Extend?
5Midterm 1 Wednesday, Sep 30th (in Class)
6Warm-up Code Comprehension
7public class Arrays int a, b
Arrays(int a, int b) this.a a
this.b b public int arrayDiff()
...
8public int arrayDiff() int diff new
inta.length int i for (i 0 i lt
a.length i) diffi ai - bi
return diff
gt Arrays both, more gt x 1,2,3,4 gt y
5,5,5,5 gt both new Arrays(x, y) gt
both.arrayDiff()
gt z both.arrayDiff() gt more new Arrays(x,
z) gt more.arrayDiff()
9public int arrayDiff() int diff new
inta.length int i for (i 0 i lt
a.length i) diffi ai - bi
return diff
Find an IndexOutOfBoundsException.
What other exception is possible?
10public int arrayDiff() int diff new
inta.length int i for (i 0 i lt
a.length i) diffi ai - bi
return diff
- Fix the method to avoid exceptions.
11Stack and Heap
12- public class Arm
- public int length
- public Arm (int length)
- this.length length
-
13- public class Torso
- public Arm l, r
- public Torso (Arm l, Arm r)
- this.l l
- this.r r
-
14gt Arm a new Arm(1) gt Torso t new Torso(a, a)
Draw the stack and the heap.
15- gt Arm a, b
- gt Torso t1, t2
- gt a new Arm(3)
- gt b new Arm(3)
- gt t1 new Torso(a, b)
- gt t2 new Torso(t1.l, t1.r)
- gt t1.r.length 6
Draw the stack and the heap.
16- gt Arm a new Arm(3)
- gt Arm b new Arm(4)
- gt Torso t new Torso(a, b)
- gt a new Arm(3)
- gt b new Arm(a.length)
- gt t new Torso(a, b))
Draw the stack and the heap.
17Inheritance
18public class A int x() return 0 int
y() return 1 public class B extends A
int y() return 2
gt A a new B() gt a.x() gt a.y()
19public class A int x() return 0 int
y() return 1 public class C extends A
int x() return super.x() - 1 int y()
return x() 5
gt A a new C() gt a.x() gt a.y()
20public class C extends A int x() return
super.x() - 1 int y() return x() 5
public class D extends C int x()
return super.x() - 1
gt A a new D() gt a.x() gt a.y()
21Inheritance and Heap Drawings
22public class Counter int count
Counter(int count) this.count count
Counter increment() count
return this String toString()
return at count
23public class Strange extends Counter
Strange(int count) super(count)
Counter increment() return new
Strange(count 1)
24Counter increment() // Counter count
return this Counter increment() // Strange
return new Strange(count 1)
gt Counter a new Counter(0) gt Counter b a gt
a.increment() gt a gt b
25Counter increment() // Counter count
return this Counter increment() // Strange
return new Strange(count 1)
gt Counter a new Counter(0) gt Counter b a gt
a.increment().increment().increment() gt a gt b
26Counter increment() // Counter count
return this Counter increment() // Strange
return new Strange(count 1)
gt Counter a new Strange(0) gt Counter b a gt
a.increment().increment().increment() gt a gt b
27That's all, folks.