Consider a Person class (which keeps track of a Person’s name) and a Student class (which keeps track of a Student’s name and student ID number). State the relationship between the two classes as either an is-a or a has-a relationship (similar to the last sentence of each paragraph in the above Model).

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter4: More Object Concepts
Section: Chapter Questions
Problem 15RQ
icon
Related questions
Question

4. Consider a Person class (which keeps track of a Person’s name) and a Student class (which keeps track of a Student’s name and student ID number). State the relationship between the two classes as either an is-a or a has-a relationship (similar to the last sentence of each paragraph in the above Model).

### "Is-a" versus "Has-a"

A large program will often be built out of multiple classes. Often, these classes will have a “has-a” relationship. For example, a game program might include three classes: PlayingCard, Suit, and Rank. A PlayingCard object **has a** Suit and **has a** Rank, which are handled in a Java program as instance variables.

#### Diagram Explanation

The diagram below illustrates the `PlayingCard` class and its components:

```
         ______________________
        |        PlayingCard     |   <- Class Name
        |______________________|
        | - cardRank: Rank        |   <- Data Fields
        | - cardSuit: Suit          |
        |______________________|
        | + setRank(Rank rank): void |   <- Methods
        | + setSuit(Suit suit): void     |
        | + getRank(): Rank            |
        | + getSuit(): Suit               |
        |______________________|
```

This diagram is divided into three sections:
1. **Class Name**: The name of the class (`PlayingCard`).
2. **Data Fields**: Instance variables (`cardRank` and `cardSuit`), which are of types `Rank` and `Suit`.
3. **Methods**: Functions that can be used to set and get the values of `cardRank` and `cardSuit` (`setRank`, `setSuit`, `getRank`, and `getSuit`).

Less often, you might have two classes which have an “is-a” relationship, where one class is a more general class, while the second class is a more specialized version of the original class. A blackjack program might have a `BlackJackCard` class which **is a** `PlayingCard`. Notice the relationship is not symmetrical: every `BlackJackCard` **is a** `PlayingCard`, but not every `PlayingCard** is a** `BlackJackCard`.
Transcribed Image Text:### "Is-a" versus "Has-a" A large program will often be built out of multiple classes. Often, these classes will have a “has-a” relationship. For example, a game program might include three classes: PlayingCard, Suit, and Rank. A PlayingCard object **has a** Suit and **has a** Rank, which are handled in a Java program as instance variables. #### Diagram Explanation The diagram below illustrates the `PlayingCard` class and its components: ``` ______________________ | PlayingCard | <- Class Name |______________________| | - cardRank: Rank | <- Data Fields | - cardSuit: Suit | |______________________| | + setRank(Rank rank): void | <- Methods | + setSuit(Suit suit): void | | + getRank(): Rank | | + getSuit(): Suit | |______________________| ``` This diagram is divided into three sections: 1. **Class Name**: The name of the class (`PlayingCard`). 2. **Data Fields**: Instance variables (`cardRank` and `cardSuit`), which are of types `Rank` and `Suit`. 3. **Methods**: Functions that can be used to set and get the values of `cardRank` and `cardSuit` (`setRank`, `setSuit`, `getRank`, and `getSuit`). Less often, you might have two classes which have an “is-a” relationship, where one class is a more general class, while the second class is a more specialized version of the original class. A blackjack program might have a `BlackJackCard` class which **is a** `PlayingCard`. Notice the relationship is not symmetrical: every `BlackJackCard` **is a** `PlayingCard`, but not every `PlayingCard** is a** `BlackJackCard`.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT