Designing Classes

Help Questions

AP Computer Science A › Designing Classes

Questions 1 - 10
1

Design a shell for a class named Dog that has 3 attributes:

  • Does the dog have a tail?
  • Is the dog purebred?
  • A list of strings of definitive features for the dog

class Dog {

boolean tail;

boolean purebred;

List features;

}

class Dog {

boolean tail;

boolean purebred;

}

class Dog {

boolean purebred;

List features;

}

class Dog {

}

Explanation

In the prompt, any yes or no questions should be addressed as booleans (i.e. tail, purebred). Any lists should have the type and name of the list. This was just a shell, so there should be no declarations of variables.

2

Design a shell for a class named Dog that has 3 attributes:

  • Does the dog have a tail?
  • Is the dog purebred?
  • A list of strings of definitive features for the dog

class Dog {

boolean tail;

boolean purebred;

List features;

}

class Dog {

boolean tail;

boolean purebred;

}

class Dog {

boolean purebred;

List features;

}

class Dog {

}

Explanation

In the prompt, any yes or no questions should be addressed as booleans (i.e. tail, purebred). Any lists should have the type and name of the list. This was just a shell, so there should be no declarations of variables.

3

Design a shell for a class named Dog that has 3 attributes:

  • Does the dog have a tail?
  • Is the dog purebred?
  • A list of strings of definitive features for the dog

class Dog {

boolean tail;

boolean purebred;

List features;

}

class Dog {

boolean tail;

boolean purebred;

}

class Dog {

boolean purebred;

List features;

}

class Dog {

}

Explanation

In the prompt, any yes or no questions should be addressed as booleans (i.e. tail, purebred). Any lists should have the type and name of the list. This was just a shell, so there should be no declarations of variables.

4

Design a class, SuperHero, that will be used for an existing application. The class should extend the parent class Man. The class should define clothes, skin color, hair color, eye color, good or evil, whether or not powers are had, and a list of personality traits. The class Man supports an implementation of the method doGood. Here is the method stub for doGood:

public String doGood(Boolean good);

Your class will need to support an implementation of doGood.

Choose the best answer.

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

}

}

class SuperHero {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

Explanation

This is the correct answer because the class extends the super class Man. It also lists all of the attributes defined in the prompt. In addition to listing all of the attributes, the class also defines an implementation of the doGood method which the prompt states is defined by the parent. One of the answer choices simply defined the stub for the method. The prompt clearly states to support an implementation of the method.

5

Design a class, SuperHero, that will be used for an existing application. The class should extend the parent class Man. The class should define clothes, skin color, hair color, eye color, good or evil, whether or not powers are had, and a list of personality traits. The class Man supports an implementation of the method doGood. Here is the method stub for doGood:

public String doGood(Boolean good);

Your class will need to support an implementation of doGood.

Choose the best answer.

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

}

}

class SuperHero {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

Explanation

This is the correct answer because the class extends the super class Man. It also lists all of the attributes defined in the prompt. In addition to listing all of the attributes, the class also defines an implementation of the doGood method which the prompt states is defined by the parent. One of the answer choices simply defined the stub for the method. The prompt clearly states to support an implementation of the method.

6

Design a class, SuperHero, that will be used for an existing application. The class should extend the parent class Man. The class should define clothes, skin color, hair color, eye color, good or evil, whether or not powers are had, and a list of personality traits. The class Man supports an implementation of the method doGood. Here is the method stub for doGood:

public String doGood(Boolean good);

Your class will need to support an implementation of doGood.

Choose the best answer.

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

class SuperHero extends Man {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

}

}

class SuperHero {

List clothes;

String skinColor;

String hairColor;

String eyeColor;

Boolean good;

Boolean powers;

List personalityTraits;

public String doGood(Boolean good) {

String result = "No, do bad!";

if (good == true) {

result = "Yes, do good!";

}

return result;

}

}

Explanation

This is the correct answer because the class extends the super class Man. It also lists all of the attributes defined in the prompt. In addition to listing all of the attributes, the class also defines an implementation of the doGood method which the prompt states is defined by the parent. One of the answer choices simply defined the stub for the method. The prompt clearly states to support an implementation of the method.

7

The letters above the code samples are for the explanation.

Design a class in the programming language Swift (iOS) to best solve this problem:

I have a dog company. My company takes in dogs for boarding on the weekdays, weekends, and during holidays. I want my website to display when we're open, how many spots we have left, and the price for boarding.

(A)

class DogBoarding {

internal var open: Bool

internal var spotsLeft: Int

internal var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

internal func getPrice() -> Double {

return self.price

}

internal func getSpotsLeft() -> Int {

return self.spotsLeft

}

internal func isOpen() -> Bool {

return self.open

}

}

(B)

class DogBoarding {

private var open: Bool

private var spotsLeft: Int

private var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

``

private func getPrice() -> Double {

return self.price

}

``

private func getSpotsLeft() -> Int {

return self.spotsLeft

}

``

private func isOpen() -> Bool {

return self.open

}

}

(C)

class DogBoarding {

internal var open: Bool

internal var spotsLeft: Int

internal var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

``

internal func getPrice() {

return self.price

}

``

internal func getSpotsLeft() {

return self.spotsLeft

}

``

internal func isOpen() {

return self.open

}

}

(D)

class DogBoarding {

init () {

}

``

internal func getPrice() -> Double {

return 0.0

}

``

internal func getSpotsLeft() -> Int {

return 0

}

``

internal func isOpen() -> Bool {

return false

}

}

Explanation

Coding sample (A) is the correct answer. This is due to the completeness of the class. The class has getters for open, spotsLeft, and price which is what the user in the prompt asks for to be able to display them on a website.

Coding sample (B) is incorrect because everything is set to private. If the getter functions are private, then the user cannot use them for the website.

Coding sample (C) is incorrect because there are no return types on the functions. In Swift, the functions must have return types.

Coding sample (D) is incorrect because it is just the shell of a class and does not actual perform any functions.

8

The letters above the code samples are for the explanation.

Design a class in the programming language Swift (iOS) to best solve this problem:

I have a dog company. My company takes in dogs for boarding on the weekdays, weekends, and during holidays. I want my website to display when we're open, how many spots we have left, and the price for boarding.

(A)

class DogBoarding {

internal var open: Bool

internal var spotsLeft: Int

internal var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

internal func getPrice() -> Double {

return self.price

}

internal func getSpotsLeft() -> Int {

return self.spotsLeft

}

internal func isOpen() -> Bool {

return self.open

}

}

(B)

class DogBoarding {

private var open: Bool

private var spotsLeft: Int

private var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

``

private func getPrice() -> Double {

return self.price

}

``

private func getSpotsLeft() -> Int {

return self.spotsLeft

}

``

private func isOpen() -> Bool {

return self.open

}

}

(C)

class DogBoarding {

internal var open: Bool

internal var spotsLeft: Int

internal var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

``

internal func getPrice() {

return self.price

}

``

internal func getSpotsLeft() {

return self.spotsLeft

}

``

internal func isOpen() {

return self.open

}

}

(D)

class DogBoarding {

init () {

}

``

internal func getPrice() -> Double {

return 0.0

}

``

internal func getSpotsLeft() -> Int {

return 0

}

``

internal func isOpen() -> Bool {

return false

}

}

Explanation

Coding sample (A) is the correct answer. This is due to the completeness of the class. The class has getters for open, spotsLeft, and price which is what the user in the prompt asks for to be able to display them on a website.

Coding sample (B) is incorrect because everything is set to private. If the getter functions are private, then the user cannot use them for the website.

Coding sample (C) is incorrect because there are no return types on the functions. In Swift, the functions must have return types.

Coding sample (D) is incorrect because it is just the shell of a class and does not actual perform any functions.

9

The letters above the code samples are for the explanation.

Design a class in the programming language Swift (iOS) to best solve this problem:

I have a dog company. My company takes in dogs for boarding on the weekdays, weekends, and during holidays. I want my website to display when we're open, how many spots we have left, and the price for boarding.

(A)

class DogBoarding {

internal var open: Bool

internal var spotsLeft: Int

internal var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

internal func getPrice() -> Double {

return self.price

}

internal func getSpotsLeft() -> Int {

return self.spotsLeft

}

internal func isOpen() -> Bool {

return self.open

}

}

(B)

class DogBoarding {

private var open: Bool

private var spotsLeft: Int

private var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

``

private func getPrice() -> Double {

return self.price

}

``

private func getSpotsLeft() -> Int {

return self.spotsLeft

}

``

private func isOpen() -> Bool {

return self.open

}

}

(C)

class DogBoarding {

internal var open: Bool

internal var spotsLeft: Int

internal var price: Double

init (open: Bool, spotsLeft: Int, price: Double) {

self.open = open

self.spotsLeft = spotsLeft

self.price = price

}

``

internal func getPrice() {

return self.price

}

``

internal func getSpotsLeft() {

return self.spotsLeft

}

``

internal func isOpen() {

return self.open

}

}

(D)

class DogBoarding {

init () {

}

``

internal func getPrice() -> Double {

return 0.0

}

``

internal func getSpotsLeft() -> Int {

return 0

}

``

internal func isOpen() -> Bool {

return false

}

}

Explanation

Coding sample (A) is the correct answer. This is due to the completeness of the class. The class has getters for open, spotsLeft, and price which is what the user in the prompt asks for to be able to display them on a website.

Coding sample (B) is incorrect because everything is set to private. If the getter functions are private, then the user cannot use them for the website.

Coding sample (C) is incorrect because there are no return types on the functions. In Swift, the functions must have return types.

Coding sample (D) is incorrect because it is just the shell of a class and does not actual perform any functions.

10

Which of the following lines represents a data member?

1. class animal
2. {
3. public:
4. animal();
5. void fetch();
6. private:
7. char bone;
8. }

Line 7

Line 6

Line 3

Line 5

Line 1

Explanation

Data members are variables created in a class, and are typically found under the private: label, as data members are typically kept local to that class. It can be distinguished from member functions because of its lack of parenthesis().

Page 1 of 2