CECS 277 – Lab 11 solution

$30.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

Ball Selector – Singleton Pattern

Create a program that allows the user to select a Ball object by entering a color. Create
the Ball class as a singleton to ensure that there is only one instance of each of the
different colors (ie. there is only one ‘Red’ ball that the user can play with), by using a
HashMap with the color as the key, and the corresponding Ball object as the value.

The
getInstance method should check if that color ball is already in the map, if it is, then
return it, otherwise construct it and return it. Once the user chooses a ball, let them
choose to bounce or roll the ball.

The Ball object keeps track of the number of times the
user has bounced and/or rolled that particular color ball.
Example Output:
Choose a ball color (q to quit):
Red
1. Roll ball
2. Bounce ball
1
You roll the Red ball.
Red Ball:
——–
Rolls: 1
Bounces: 0
Choose a ball color (q to quit):
Yellow
1. Roll ball
2. Bounce ball
2
You bounce the Yellow ball.
Yellow Ball:
——–
Rolls: 0
Bounces: 1
Choose a ball color (q to quit):
Red
1. Roll ball
2. Bounce ball
1
You roll the Red ball.
Red Ball:
——–
Rolls: 2
Bounces: 0
Choose a ball color (q to quit):
Red
1. Roll ball
2. Bounce ball
2
You bounce the Red ball.
Red Ball:
——–
Rolls: 2
Bounces: 1
Choose a ball color (q to quit):
Yellow
1. Roll ball
2. Bounce ball
2
You bounce the Yellow ball.
Yellow Ball:
——–
Rolls: 0
Bounces: 2
Choose a ball color (q to quit):
q