留学生95-712讲解、辅导Java编程设计

- 首页 >> Java编程


95-712 Practice problem 8

This problem brings together what you learned in inheritance,

polymorphism, access specifiers, and JavaFX

Problem statement: The GUI for this app is provided to you

(Fig.1). It currently supports three pets: Cat, Dog, and Bird.

User clicks on the any one of the three pet buttons, and the

app displays the sound made by that pet. It also displays a

count of number of times the user clicks on each pet. (Fig.2, 3, 4). You

need to create the Pet classes and write event-handlers to complete the app.

Figure 2: User clicked Cat once Figure 3: Then user clicked Dog once Figure 4: Then user clicked Dog again, and then Bird

Solution Design: The UML in Figure 5 shows classes, their methods and variables. The app is launched from PickAPet.java.

Its GUI has three buttons as shown in Fig.1. You need to do the following

1. Create an abstract Pet class with abstract talk() method, and a variable petCount to count all pets selected by user.

2. Create Cat, Dog, and Bird classes that extend Pet and implement talk() that returns a string “Meow…”, “Bark…”, and

“Tweet…” respectively. They also have their own count variables to count the number of times they are chosen.

3. Create three handlers as member classes in PickAPet.java to update countLabels and resultLabel as shown in Figure

2, 3, 4. Bind them to the three buttons in setupScreen() method - buttons[0], buttons[1], and buttons[2].

4. Finally, run the test-cases to check correct execution of your program.

Figure 5: Solution Design

Note: The pet objects are already created for you and stored in pets array in PickAPet.java. You should not be creating

any new pet objects.

main(args: String[]): void

resultLabel: Label

countLabels: Lable[]

start(primaryStage: Stage): void

setupScreen(): void

<<Interface>>

ActionEvent

EventHandler

handle(event:ActionEvent): void

Application

start(primaryStage: Stage): void

Figure 1: GUI components in opening screen



站长地图