You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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.


JExercise lar deg sjekke din egen kode vha. forhåndslagde JUnit-tester og JExercise-panelet

Bruk av JExercise:

  1. Sørg for at jexercise-standalone.jar er lagt til i ditt prosjekts Build Path. Dette må gjøres hver gang du oppretter et nytt prosjekt, og det er derfor lurt å gjenbruke samme prosjekt til alle oppgaver.
  2. JExercise-tillegget må være installert. Installer tillegget fra følgende oppdateringsadresse: http://folk.ntnu.no/hal/dev/updatesite.
  3. Åpne JExercise-panelet via Window -> Show View -> Other, og navigere deg fram til JExercise i vinduet som kommer opp, velge det og klikke OK.
  4. Klikke og dra oppgavens testklasse, <oppgavenavn>Test.java fra pakkeoversikten og slippe den i JExercise panelet. 
  5. Testene kan så kjøres ved å dobbeltklikke på testen som ønskes kjørt.

Ved trøbbel, se først om du finner løsningen i Løsninger på trøbbel med JExercise.

Unknown macro: {html}

Twitre gjerne om oppgaven når du er ferdig: <a href="https://twitter.com/share" class="twitter-share-button" data-hashtags="jexercise">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

 

 

 

  • No labels