cs120: inheritance - flyn computing

17
CS120: Inheritance W. Michael Petullo University of Wisconsin–La Crosse As of March 1, 2021

Upload: others

Post on 07-Dec-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS120: Inheritance - Flyn Computing

CS120: Inheritance

W. Michael Petullo

University of Wisconsin–La Crosse

As of March 1, 2021

Page 2: CS120: Inheritance - Flyn Computing

Inheritance: Book Example

These slides, along with the slides from the “writing classes” lecture, serve as

companions to Chapters 12–14 of the course book. The book provides an example

program that models a card game called Crazy Eights. The course website provides a

link to a simplified version of this game on its schedule page.

Page 3: CS120: Inheritance - Flyn Computing

Interfaces: Macdonald UML

Animal

+speak()

Pig

-weight : int

+getWeight(): int+eat(weight:int)+speak()

Cow

-weight : int

+getWeight(): int+eat(weight:int)+speak()

Chicken

-weight : int

+getWeight(): int+eat(weight:int)+speak()

The classes Pig, Cow, and Chicken implement the interface Animal. This means thatyou can assign an object of type Pig, Cow, or Chicken to a variable of type Animal.This will invoke Pig’s speak method despite p being an Animal:

Animal p = new Pig ( ) ;p . speak ( ) ;

Page 4: CS120: Inheritance - Flyn Computing

Inheritance: Macdonald2 UML

Animal

-weight : int

+getWeight(): int+eat(weight:int)+speak()

Pig

+speak()

Cow

+speak()

Chicken

+speak()

The classes Pig, Cow, and Chicken specialize Animal’s speak method, but they shareAnimal’s implementation of getWeight and eat. This will invoke Pig’s speak, ratherthan Animal’s speak:

Animal p = new Pig ( ) ;p . speak ( ) ;

Page 5: CS120: Inheritance - Flyn Computing

Inheritance: Specialization

c l a s s Animal {. . .

}

c l a s s Pig extends Animal {. . .

}

c l a s s Song {. . .

}

Your Madonald2.java should contain at leastfive classes (three illustrated here), and youmust define each class independently. Theclasses should not nest.

Page 6: CS120: Inheritance - Flyn Computing

Inheritance: Back to Card Game

A

A

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

1 0

10

J

J

Q

Q

K

K

A

A

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

1 0

10

J

J

Q

Q

K

K

A

A

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

1 0

10

J

J

Q

Q

K

K

A

A

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

1 0

10

J

J

Q

Q

K

K

J O K E R

JOKER

J O K E R

JOKER

©

Page 7: CS120: Inheritance - Flyn Computing

Inheritance: Deck 1

c l a s s Deck {p r i v a t e Card [ ] c a r d s ;

pub l i c Deck ( ) {t h i s . c a r d s = new Card [ 5 2 ] ;i n t i n d e x = 0 ;f o r ( i n t s u i t = 0 ; s u i t < 4 ; s u i t++) {

f o r ( i n t rank = 1 ; rank <= 13 ; rank++) {deck [ i nd e x++] = new Card ( s u i t , rank ) ;

}}

}}

1 Cannot easily shrink (draw) or grow (discard) an array and thus the Deck

2 Here we have a deck of cards; how can we model players’ hands?

Page 8: CS120: Inheritance - Flyn Computing

Inheritance: Deck 2: Variable-length

c l a s s Deck {p r i v a t e Ar r a yL i s t<Card> c a r d s ;

pub l i c Deck ( ) {t h i s . c a r d s = new Ar r a yL i s t<Card>() ;f o r ( i n t s u i t = 0 ; s u i t < 4 ; s u i t++) {

f o r ( i n t rank = 1 ; rank <= 13 ; rank++) {c a r d s . add (new Card ( s u i t , rank ) ) ;

}}

}}

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Page 9: CS120: Inheritance - Flyn Computing

Inheritance: Deck 2: Variable-length

c l a s s Deck {p r i v a t e Ar r a yL i s t<Card> c a r d s ;

pub l i c Deck ( ) {t h i s . c a r d s = new Ar r a yL i s t<Card>() ;f o r ( i n t s u i t = 0 ; s u i t < 4 ; s u i t++) {

f o r ( i n t rank = 1 ; rank <= 13 ; rank++) {c a r d s . add (new Card ( s u i t , rank ) ) ;

}}

}}

ArrayList allowsus to shrink andgrow the Deck

New syntax: theArrayList class isparameterized; thisis an ArrayList

of Card s

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Page 10: CS120: Inheritance - Flyn Computing

Inheritance: Deck 2: Variable-length

c l a s s Deck {p r i v a t e Ar r a yL i s t<Card> c a r d s ;

pub l i c Deck ( ) {t h i s . c a r d s = new Ar r a yL i s t<Card>() ;f o r ( i n t s u i t = 0 ; s u i t < 4 ; s u i t++) {

f o r ( i n t rank = 1 ; rank <= 13 ; rank++) {c a r d s . add (new Card ( s u i t , rank ) ) ;

}}

}}

An ArrayList isempty by default(an array hasa fixed length)

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Page 11: CS120: Inheritance - Flyn Computing

Inheritance: Deck 2: Variable-length

c l a s s Deck {p r i v a t e Ar r a yL i s t<Card> c a r d s ;

pub l i c Deck ( ) {t h i s . c a r d s = new Ar r a yL i s t<Card>() ;f o r ( i n t s u i t = 0 ; s u i t < 4 ; s u i t++) {

f o r ( i n t rank = 1 ; rank <= 13 ; rank++) {c a r d s . add (new Card ( s u i t , rank ) ) ;

}}

}}

Can add anynumber of elements

to ArrayList,and can removeone with remove

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Page 12: CS120: Inheritance - Flyn Computing

Inheritance: Array vs. ArrayList

An array of Card elements

Card [ ] deck = new Card [ 5 2 ] ;

A r r a yL i s t<Card> deck = new Ar r a yL i s t<Card>() ;

An ArrayList of Card elements

It is easier to add or remove elements from an ArrayList. An array has a fixed length.

Page 13: CS120: Inheritance - Flyn Computing

Inheritance: Deck 3: Subclass

c l a s s Ca r dCo l l e c t i o n {p r i v a t e S t r i n g l a b e l ;p r i v a t e Ar r a yL i s t<Card> c a r d s ;

pub l i c Ca r dCo l l e c t i o n ( S t r i n g l a b e l ) {t h i s . l a b e l = l a b e l ;t h i s . c a r d s = new Ar r a yL i s t<Card>() ;

}}

c l a s s Deck extends Ca r dCo l l e c t i o n {pub l i c Deck ( S t r i n g l a b e l ) {

super ( l a b e l ) ;f o r ( i n t s u i t = 0 ; s u i t < 4 ; s u i t++) {

f o r ( i n t rank = 1 ; rank <= 13 ; rank++) {c a r d s . add (new Card ( s u i t , rank ) ) ;

}}

}}

CardCollection isthe superclass of Deck

A Deck is a (extends)CardCollection;

Deck inherits all codefrom CardCollection,and Deck can be usedanywhere that expectsCardCollection

The super keywordinvokes the super-class constructor.

Page 14: CS120: Inheritance - Flyn Computing

Inheritance: Hand

c l a s s Ca r dCo l l e c t i o n {. . .

}

c l a s s Hand extends Ca r dCo l l e c t i o n {pub l i c Hand ( S t r i n g l a b e l ) {

super ( l a b e l ) ;}

pub l i c vo id d i s p l a y ( ) [System . out . p r i n t l n ( g e tLab e l ( ) + ” : ” ) ;f o r ( i n t i = 0 ; i < s i z e ( ) ; i++) {

System . out . p r i n t l n ( getCard ( i ) ) ;}System . out . p r i n t l n ( ) ;

}}

◮ A Deck is a

CardCollection with a

constructor that creates a

standard 52-card deck

◮ A Hand is a

CardCollection that can

print its contents

Page 15: CS120: Inheritance - Flyn Computing

Inheritance: UML

CardCollection

+label : String+card : ArrayList<Card>

+CardCollection(label:String)+addCard(c:Card)+removeCard(i:index): Card+popCard(): Card+size(): int+empty(): boolean+deal(that:CardCollection,n:int)+getCard(i:int)+last()+shuffle()

Deck

+Deck(label:String)

Hand

+Hand(label:String)+display()

Page 16: CS120: Inheritance - Flyn Computing

Inheritance: Crazy Eights

1 Deal five or more cards to each player, and then deal one card face up to createthe “discard pile.” Place the remaining cards face down to create the “draw pile.”

2 Each player takes a turn placing a single card on the discard pile. The played cardmust match the rank or suit of the previously-played card, or it must be an eight.

3 A player that does not have a matching card or an eight must draw new cardsuntil he has a card he can play.

4 If the pile ever runs out, the discard pile is shuffled (except the top card) to createthe new draw pile.

5 As soon as a player has no cards, the game ends. All other players score penaltypoints for their remaining cards. Eights are worth 20 points, face cards are worthten points, and all others are worth their rank.

Page 17: CS120: Inheritance - Flyn Computing

Inheritance: Assignments

Graded Homework Aquinas: macdonald2 in Java

Ungraded Labs Aquinas: vehicle2 in Java

Reading Read Chapter 14

https://www.flyn.org/courses/cs120-2021-spring/schedule