Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Excerpt

This exercise concerns a VotingMachine class that manages votes for two parties, democrats and republicans, and is based on Exercise 8.10 in Cay Horstmann's Big Java, Late Objects.

The VotingMachine class remembers and counts votes in a simple election with only parties, democrats and republicans.

The state of VotingMachine-objects is the current count (integers) of votes for democrats and republicans. In the initial state both counters should be zero.

The VotingMachine class has two method for reading the current state, getDemocratTally() and getRepublicanTally(), and three methods for altering the state, clear(), voteDemocrat() and voteRepublican(), with the following behavior:

  • getDemocratTally - returns the current number (an int) of democrat votes
  • getRepublicanTally - returns the current number (an int) of republican votes

  • voteDemocrat() - registers a(nother) democrat vote
  • voteRepublican() - registers a(nother) republican vote
  • clear - clears all the votes and returns to the initial state

Part 1 - object state diagram

Draw an object state diagram for a hypothetical usage of a VotingMachine object. Choose a suitable sequence of calls to VotingMachine's methods, so all the behavior is illustrated.

Part 2 - Java code

Write Java code for the VotingMachine class with behavior as specified above.

Write a suitable toString() metode and a main method, so you can see for yourself that the behavior is consistent with the object state diagram (use the same sequence of calls).

JExercise test code for this exercise can be found here: stateandbehavior/VotingMachineTest.java. Original-koden (jextest) finner du her: stateandbehavior/VotingMachine.jextest.

Include Page
Bunntekst for JExercise-oppgaver
Bunntekst for JExercise-oppgaver