Docsity

Implementing New Types: Song and JukeboxAccount using Test-Driven Development, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
C Sc 227 Project #4: Four New Types
Due Wednesday, 22-Feb by 9:00 pm via Web Based Turnin
This project is to be done individually. All worked turned in must be yours. You may get
help from Rick or Section leaders. You may begin this in your scheduled lab solo on 14- or
15-Feb. You need not finish it before you leave, but completing Song and JukeboxAccount
will be a good start at this Project 4, which asks you to implement four new types.
Goals
•Experience Test-Driven Development (TDD) as a way to develop software
•Experience writing new types: class heading, instance variables, constructors,
methods
Four Classes
•Two classes that are part of the cashless Jukebox: Song and JukeboxAccount
Built as part of a long tutorial (about 10 pages) on using TDD in Eclipse
•class PiggyBank
•class WeeklyEmployee
JUnit
JUnit is a regression testing framework written by Erich Gamma and Kent Beck. With a class that
extends TestCase, many methods to help with testing are available. In this project, you will only need
these assertions:
1. assertEquals passes if the expected value equals the actual value (ints, Strings, chars)
2. assertTrue passes if its boolean argument is true
3. assertFalse passes if its boolean argument is false
The following method headings help describe how programmers can call assertions. They will be
used in a test method to see if the assertions pass (green bar) or fail (red bar).
public static void assertEquals(String expected, String actual)
An assertion that passes if expected and actual are equal (case sensitive)
public static void assertEquals(int expected, int actual)
An assertion that passes if expected and actual are the same integer value.
public static void assertEquals(double expected, double actual, double tolerance)
An assertion that passes if expected and actual differ by no more than tolerance.
public static void assertTrue(boolean condition)
An assertion that passes if condition is true.
public static void assertFalse(boolean condition)
An assertion that passes if condition is false.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Implementing New Types: Song and JukeboxAccount using Test-Driven Development and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

C Sc 227 Project #4: Four New Types

Due Wednesday, 22-Feb by 9:00 pm via Web Based Turnin

This project is to be done individually. All worked turned in must be yours. You may get

help from Rick or Section leaders. You may begin this in your scheduled lab solo on 14- or

15-Feb. You need not finish it before you leave, but completing Song and JukeboxAccount

will be a good start at this Project 4, which asks you to implement four new types.

Goals

  • Experience Test-Driven Development (TDD) as a way to develop software
  • Experience writing new types: class heading, instance variables, constructors,

methods

Four Classes

  • Two classes that are part of the cashless Jukebox: Song and JukeboxAccount Built as part of a long tutorial (about 10 pages) on using TDD in Eclipse
  • class PiggyBank
  • class WeeklyEmployee JUnit

JUnit is a regression testing framework written by Erich Gamma and Kent Beck. With a class that

extends TestCase, many methods to help with testing are available. In this project, you will only need these assertions:

1. assertEquals passes if the expected value equals the actual value (ints, Strings, chars)

2. assertTrue passes if its boolean argument is true

3. assertFalse passes if its boolean argument is false

The following method headings help describe how programmers can call assertions. They will be

used in a test method to see if the assertions pass (green bar) or fail (red bar).

public static void assertEquals (String expected, String actual)

An assertion that passes if expected and actual are equal (case sensitive)

public static void assertEquals (int expected, int actual)

An assertion that passes if expected and actual are the same integer value.

public static void assertEquals (double expected, double actual, double tolerance)

An assertion that passes if expected and actual differ by no more than tolerance.

public static void assertTrue (boolean condition)

An assertion that passes if condition is true.

public static void assertFalse (boolean condition)

An assertion that passes if condition is false.

1. Implement Song using Test Driven Development

In class, we find the objects, and role play based on this problem specification:

The student affairs office has decided to put some newfound activity fee funds

toward a music jukebox in the student center. The jukebox will allow a student to

play individual songs. No money will be required. Instead, a student will swipe a

magnetic ID card through a card reader. Students will each be allowed to play up to

1,500 minutes of “free” songs in their academic careers at the school. A student may

only play a song from the available collection if she has enough time remaining.

First consider the two classes that you are about to write are part of a bigger system described below. Here is an overview of a Cashless Jukebox System: The following design shows that JukeboxAccount has a dependency on Song. The arrow with dotted line states "JukeboxAccount needs Song". So you will be asked to test and build the Song class first. Note: the + means public—the return types and parameters are not sow here. Each Song object knows its title and its length in playtime (seconds). JukeboxAccount object will know the name of its human equivalent and how much time the user has remaining to play songs. Anyone can ask the JukeboxAccount if it can play a Song. This canSelect message to a JukeboxAccout will return true if the JukeboxAccount has enough time to play the given Song.

  • Click Next
  • When prompted to add JUnit.jar, click Yes
  • Enter the Name as SongTest
  • Enter the name SongTest. and click Finish. Eclipse should look something like this:
  • Now add a public void method named testGetters()
  • Write code that will eventually construct many Song objects. We need to give it the title of the Song and the playtime in seconds. Your programming environment should look something like this: There is no Song class. With TDD, you write what you want before you write what you need. So add a new class named Song with a constructor.
  • Select File > New > Class
  • Enter the class name Song
  • Click Finish
  • To the file created with some code, add a constructor with two parameters as shown next

Modify the Song class so it has a getTitle method that returns an incorrect value (but has no

errors). Your programming environment should now look like this:

  • Click the SongTest.java tab and Select Run > Run as > JUnit test

You should see the red bar since the assertion fails.

You should see that the expected value My Favorite did not equal the actual return value that is

the bogus "need to remember the title". If not, hover your mouse over the Failure Trace

window where the assertion failed. Better yet, with strings that you probably can not see, right

click on the ComparisonFailure and select Compare Result

  • First make sure Song.java is the active editor (click the correct tab above the edit window)
  • Add an instance variable named myTitle, initialize it in the constructor, and return the value of that instance variable from a getTitle method. The Song class should look like this:
  • Construct another Song object inside the testGetters method to confirm that at least two different Song objects store their own title, one for each object.
  • Save both files and run SongTest as a JUnit test
  • Correct any and all errors until you get the green bar. Song should be complete. This Song class will be needed in JukeboxAccount, which is up next 2. Class JukeboxAccount

Using test-driven development, write a unit test and class JukeboxAccount with the methods

shown below. Implement one method at a time as much as possible.

A JukeboxAccount maintains the amount of time remaining for any Jukebox user. Programs send

canSelect messages to find out if a user can play a specific track (the method returns true or

false). You can also update the time remaining and the Songs played today with a debit message.

// Construct a new JukeboxAccount object, using the initial // state supplied by the arguments. Parameters: // ID: The identification string associate with this user // secondsOfPlayTime: How many seconds this user has left to play Songs public JukeboxAccount(String ID, int secondsOfPlayTime) // Provide access to this JukeboxAccount's ID string so // you can find this JukeBoxAccount in a collection public String getID() // Reduce this JukeboxAccount's time credit by the playtime of theSong public void debit(Song theSong)

// Provide access to how much playtime this user has remaining. // Returns the number of seconds of time credit for this user public int getSecondsRemaining() // Let the sender know if the current user can play a specific Song. // Note: The user can have the Song played if this JukeboxAccount has // as much time remaining as the time it takes to play theSong // Parameter: theSong is the Song (with playtime) to be played. // Return true if the JukeboxAccount has enough time credit. public boolean canSelect(Song theSong) 2b Optional. Download the Unit test JukeBoxAccountGradingTest.java to make sure all of our assertions will pass.

The following two classes have nothing to do with the Jukebox System

3. Implement class PiggyBank using TDD Implement class PiggyBank, an object to which pennies, nickels and dimes can be added. Each PiggyBank object keeps track of how much money is in the PiggyBank. A PiggyBank can be raided anytime to reduce the total amount of money to 0.00. Make sure you include a unit test named PiggyBankTest that tests all methods. // Construct a piggy bank with a name but no money public PiggyBank ( String piggyBankName) // Return the name of this PiggyBank public String getName() // Return the amount of money in this PiggyBank as a dollars and cents // amount. After construction or a jiggle, getTotal() must return 0. public double getTotal() // Simulate a complete raid on this piggy bank. After a jiggle // message, this PiggyBank object has no money remaining. public void jiggle() // Add the given number of pennies to this PiggyBank public void addPennies(int penniesToAdd) // Add the given number of mickles this PiggyBank public void addNickels(int nickelsToAdd); // Add the given number of dimes to this PiggyBank public void addDimes(int dimesToAdd) // Add the given number of quarters to this PiggyBank public void addQuarters(int quartersToAdd)

Run a GUI that will use your WeeklyEmployee class Once you have finished the WeeklyEmployee class, see your solution in action with a graphical user interface. First copy and paste the code from these two files into the correctly named files in your project:

  • http://www.cs.arizona.edu/classes/cs127a/spring06/programs/WeeklyEmployeeCollection.java
  • http://www.cs.arizona.edu/classes/cs127a/spring06/programs/WeeklyEmployeeGUI.java In Eclipse, select WeeklyEmployeeGUI.java and run it as an application. You should see this GUI shown below). Enter the number of hours worked to see a "paycheck" for this one employee.

What we'll look for in grading

Part of your grade is based on the completeness and quality of your tests. You must test every

method. Each test must have enough assertions to verify everything the method should do. For

example, make sure no WeeklyEmployee object is charged more than the maximum amount.

Grading Criteria

___/ 10 You turned in an Eclipse Project and has all of the required files:

SongTest, and Song, JukeboxAccountTest and JukeboxAccount, PiggyBank and

PiggyBankTest, WeeklyEmployee and WeeklyEmployeeTest and WeeklyEmployeeGUI.

___/ 20 You wrote a reasonable number of assertions. You tested every method in each of the

our classes and all of your assertions pass

___/ 50 All of our assertions pass (method behavior matches the specification in comments)

This program will be added and run after handin.

___/ 20 You followed these style and design guidelines

  • Used intention-revealing names (meaningful identifiers).
  • Used public and private correctly (public methods & classes, private instance variables).
  • Used consistent spacing/indentation (format all code in Eclipse with Source > Format ).
  • Have one blank line between methods.
  • Each class that you write has your name and a description of the class.
  • Used local variables when appropriate and instance variables when appropriate (if a variable is only used in one method, declare it local to that method, not as an instance variable).
  • Unanticipated violation of style or design

Turn in your project through Web-Based Turnin: Go to the turnin page for instructions