CIS 120 Lab 3 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CIS 120 Lab 3

Description:

public Torso (Arm l, Arm r) { this.l = l; this.r = r; Arm a = new Arm(1) ... t = new Torso(a, b)); Draw the stack and the heap. Inheritance. public class A ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 28
Provided by: seasU
Category:
Tags: cis | lab | torso

less

Transcript and Presenter's Notes

Title: CIS 120 Lab 3


1
CIS 120 Lab 3
  • Before we get started...

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

4
Why Extend?
5
Midterm 1 Wednesday, Sep 30th (in Class)
6
Warm-up Code Comprehension
7
public class Arrays int a, b
Arrays(int a, int b) this.a a
this.b b public int arrayDiff()
...
8
public 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()
9
public 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?
10
public 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.

11
Stack 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

14
gt 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.
17
Inheritance
18
public 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()
19
public 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()
20
public 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()
21
Inheritance and Heap Drawings
22
public class Counter int count
Counter(int count) this.count count
Counter increment() count
return this String toString()
return at count
23
public class Strange extends Counter
Strange(int count) super(count)
Counter increment() return new
Strange(count 1)
24
Counter 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
25
Counter 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
26
Counter 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
27
That's all, folks.
Write a Comment
User Comments (0)
About PowerShow.com