Docsity

Lab Exercise: Song Class Implementation with TDD and JukeboxAccount Creation, Lab Reports of Computer Science

Toggle sidebar
bg1
C Sc 127A
Lab 6: 26- or 27-March: Test Driven Development, and
Homework 3: Submit Song and JukeboxAccount to WebCat
Lab 7 Due Date The end of your scheduled lab
Lab 7 Grading For 100%, don't be late to lab, stay to end of lab finishing what you can –or–
complete the Homework by submitting Song and JukeboxAccount to WebCat for a score of 10/10
before you leave lab early (few will be able to finish both Song and JukeboxAccount in 1:50)
Homework 3 Due Date Tuesday 3-March by 10:00 pm
Homework 3 Grading Submit to WebCat Song and Jukebox for a score of 0 through 10pts
Read this Lab exercise and when asked to, begin a new Eclipse project to implement the
Song class using Test Driven Development. Then if time permits in lab, work on
JukeboxAccount
Test Driven Development
In this lab you will be asked to implement one new type in the context of building a larger software
system. This narrative describes what the software system should eventually be able to do (we
considered this system in lecture on 21-Feb)
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 2,000 minutes of “free”
jukebox music in their academic careers at the school. A student may select a song from
the available collection. The user must be able to look up songs by artist and see how
long any song takes to play. No user may play more than two songs on any given date.
No song may be played more than five times on the same date, even if six different users
try to play the same song.
One way to begin building this system is to first find the objects. Nouns are potential objects. We
are looking for "things" that may model the desired system. This is not the time to worry about too
many details, or even if you may be right or wrong. A first pass at finding the objects will represent
a list that may end up as part of the system. Others will be eliminated. And still others may be
found later.
A List of Candidate Objects
Jukebox
Student
Song
Card reader
ID card
Collection
Date
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lab Exercise: Song Class Implementation with TDD and JukeboxAccount Creation and more Lab Reports Computer Science in PDF only on Docsity!

C Sc 127A

Lab 6: 26- or 27-March: Test Driven Development, and

Homework 3: Submit Song and JukeboxAccount to WebCat

Lab 7 Due Date The end of your scheduled lab

Lab 7 Grading For 100%, don't be late to lab, stay to end of lab finishing what you can – or–

complete the Homework by submitting Song and JukeboxAccount to WebCat for a score of 10/

before you leave lab early (few will be able to finish both Song and JukeboxAccount in 1:50)

Homework 3 Due Date Tuesday 3-March by 10:00 pm

Homework 3 Grading Submit to WebCat Song and Jukebox for a score of 0 through 10pts

Read this Lab exercise and when asked to, begin a new Eclipse project to implement the

Song class using Test Driven Development. Then if time permits in lab, work on

JukeboxAccount

Test Driven Development

In this lab you will be asked to implement one new type in the context of building a larger software

system. This narrative describes what the software system should eventually be able to do (we

considered this system in lecture on 21-Feb)

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 2,000 minutes of “free” jukebox music in their academic careers at the school. A student may select a song from the available collection. The user must be able to look up songs by artist and see how long any song takes to play. No user may play more than two songs on any given date. No song may be played more than five times on the same date, even if six different users try to play the same song.

One way to begin building this system is to first find the objects. Nouns are potential objects. We

are looking for "things" that may model the desired system. This is not the time to worry about too

many details, or even if you may be right or wrong. A first pass at finding the objects will represent

a list that may end up as part of the system. Others will be eliminated. And still others may be

found later.

A List of Candidate Objects

Jukebox Student Song Card reader

ID card Collection Date

(^2) Lab 7

While trying to discover useful objects, it helps to eliminate redundant entries. There is no useful

purpose for writing down student more than once, for example. Also, you should include only

those objects that have meaning within the realm of the system. Activity fee funds provide the

money to pay for the Jukebox. However, fees and money need not be developed. We do not need to

build the student center or keep track of the student's academic careers. The seven candidate

objects listed above will do for now.

It is also important to name things as accurately as possible. Although the name Student

appears many times, Student sounds more like an object that might represent the academic career.

JukeboxAccount better represents this new type. SongCollection is more meaningful than

collection. It is also good to recognize those things that are outside of the system that needs to be

developed. The software may play a particular song over the stereo system, but we do not need to

model, nor build, the stereo system—this is outside the system. It also seems like we could use a

way to store all JukeboxAccount objects, so it is reasonable to list another candidate object named

JukeboxAccountCollection.

The following picture represents another view of these candidate objects and their main

responsibilities. It also recognizes other parts, such as a physical stereo system, a card reader, and a

user, all part of the final system to be controlled by the software we develop.

Candidate Objects that Model a Cashless Jukebox (the big picture)

It helps to consider the main responsibility for each object. The responsibilities convey the purpose

of an object and its role in the system. The proper assignment of responsibilities is an important

skill developed over time. It can be fuzzy at first. People on the development team may disagree. It

is not exact science; it is more about common sense and what makes sense to most people. While

working on this system, consider that each object is responsible for two things:

  1. What should an object be able to do?
  2. What should an object remember?

These responsibilities can be implemented as the methods and the instance variables of a class.

JukeboxAccountCollection

Keep track of all accounts

SongCollection

Maintain list of all songs

Song

Keep song info

JukeboxAccount

Keep one's history

Jukebox

Coordinates Activities

CardReader

Get student ID

(^4) Lab 7

@Test public void testGetters() {

Self-Check

7-2 Add the test method above to SongTest.

The test name testGetters indicates that the method will ensure we can get the artist and the

title from two different songs. I recommend you think of two different objects right away rather

than just one. I'm thinking of "Help" by the Beatles and "Good is Good" by Sheryl Crow. The test

method begins with what we want to happen. The code has many compile-time errors that will be

addressed later. @Test public void testGetters() { // We have not even constructed aSong or anotherSong yet. And there is no // getArtist or getTitle method yet, not even a Song class! // But here is what I think I need at this point: assertEquals("Beatles", aSong.getArtist()); assertEquals("Help", aSong.getTitle()); assertEquals("Sheryl Crow", anotherSong.getArtist ()); assertEquals("Good is Good", anotherSong.getTitle()); }

Self-Check

7-3 Add the four assertions to the test method above. There will be many compile-time errors.

Consider what is needed to construct Song objects. We know the name of the constructor should be

Song(), but we have to decide which (if any) values should be sent to the constructor as

arguments. The test method shows that we decided to have a title and an artist. Each Song object

will need its own artist and title, so we'll pass that data as arguments to the constructor.

@Test public void testGetters() { Song aSong = new Song("Beatles", "Help"); Song anotherSong = new Song("Sheryl Crow", "Good is Good");

assertEquals("Help", aSong.getTitle()); assertEquals("Beatles", aSong.getArtist()); assertEquals("Good is Good", anotherSong.getTitle()); assertEquals("Sheryl Crow", anotherSong.getArtist ()); }

Self-Check

7-4 Add the two object constructions to the test method above in SongTest. There are even more

compile-time errors.

At this point, I am just writing what I think I need and not worrying about the compile-time errors

because we'll fix those compile time errors next.

Test Driven Development 5

2. Make the test fail

To make the test fail, the code must compile. The following minimal Song class removes the

compile-time errors as long as a newly designed constructor is included. The method names,

parameters and return type must match.

public class Song {

public Song(String songArtist, String songTitle) { }

public String getTitle() { return null; }

public String getArtist() { return null; }

}

Self-Check

7 - 5 Create a new class named Song and add the constructor and the two methods as shown above.

You can return any string for getTitle and getArtist or the default value null as shown

above.

Now running the unit test will show that the getTitle method does not return the correct value

(no surprise). We expected the string value "Help", however the actual value intentionally returned

from the method is null.

With compile-time errors fixed and the test failing, I now want to make the tests pass.

3. Makes the test pass

We need a way for every Song object to remember its own artist and title. Different instances of

the same class remember their own state through instance variables. Specifically, adding a title

instance variable to the Song class allows many Song objects to have their own titles. The

constructor must also assign the argument to the correct instance variable. Additionally,

getTitle()must return that remembered value. These changes are shown below in the updated

Song class that will make both assertions pass (thus, you should see the green bar in JUnit).

Red Bar

Test Driven Development 7

Add this to SongTest

@Test public void testCanPlayTodayAtFirst() { // Should be able to play this Song once Song aSong = new Song("Beatles", "Help"); assertTrue(aSong.canPlayToday()); }

Your programming environment should have several errors since canPlayToday doesn't yet exist.

2. Make the test fail

The following additions to Song will make everything compile. Because canPlayToday returns

false, the new test will fail.

public class Song {

// ...

public boolean canPlayToday() { return false; }

}

3. Make the test pass

By keeping track of how many songs can be played, canPlayToday can compare that to 5 to see

if the song could be played again. With less than 5 plays, the method should return true. We also

need to maintain how frequently each Song has been played. The instance variable

songsPlayedToday gets initialized to 0 in the Song's constructor.

public class Song {

// ...

private int songsPlayedToday;

public Song(String songArtist, String songTitle) {

// ...

songsPlayedToday = 0; }

// ...

public boolean canPlayToday() { return songsPlayedToday < 5; } }

Now the new test will pass. I am obviously not done. What happens if a song has been played 5

times on the date it gets asked to canPlayToday? I write a test to describe what should happen.

(^8) Lab 7

1. Write the Test

Because some method is needed to let the Song know that it has been played, this test shows a

need for a new method named recordOnePlay.

@Test public void testCanPlayTodayWithUpTo5Plays() { Song aSong = new Song("Beatles", "Help");

aSong.recordOnePlay(); assertTrue(aSong.canPlayToday()); aSong.recordOnePlay(); assertTrue(aSong.canPlayToday()); aSong.recordOnePlay(); assertTrue(aSong.canPlayToday()); aSong.recordOnePlay(); assertTrue(aSong.canPlayToday());

aSong.recordOnePlay(); // This song has been played 5 times already assertFalse(aSong.canPlayToday()); }

The assertions above suggest what should happen during the messages: canPlayToday returns

true until there have been five recordOnePlay messages. Some other object will likely use these

two new methods to either help play the Song or to notify the user that the Song has been played

its maximum number of times.

2. Make the test fail

First get the code to compile by adding method recordOnePlay to Song. Since recordOnePlay is

a void method, it need not return a bogus value to make the test fail.

public void recordOnePlay() { }

3. Make the test pass

Without any thought about the day upon which the Song was played, we can get canPlayToday

working (test passes) by incrementing the instance variable that keeps track of how often the song

has been played inside this new recordOnePlay method.

public void recordOnePlay() { songsPlayedToday++; }

However, this will do precisely what the name implies. Keep track of how often this song has been

played at any time since the Song was constructed. This may be good for a top ten list to remember

every play , but as the name suggests, songsPlayedToday does not yet do what its name reveal it

should do. songsPlayedToday must maintain a number that starts at 0 at midnight and gets up to

5 maximum during the same calendar date. Just adding 1 to songsPlayedToday every time won't

do this. We need to consider the calendar date upon which the canPlayToday() message is sent

and also the days upon which the song has been played in the past.

(^10) Lab 7

Now the recordOnePlay method can keep songsPlayedToday as the correct value.

// Maintain how often this song has been played on any date. // Precondition: This song can be played today. public void recordOnePlay() { DayCounter today = new DayCounter(); if (sameDay(today, mostRecentDatePlayed)) songsPlayedToday++; else { songsPlayedToday = 1; mostRecentDatePlayed = today; } }

} // End class Song

If you have retyped everything accurately, you should actually see a compile time error at the

sameDay message. This code represents a wish for a method named sameDay that returns true if

the two DayCounter objects represent the same day on the calendar. So to get Song completed,

you need to add a sameDay method to class Song.

1. Write the Test

Add the following test method to SongTest that compares several days to see if they are and are not

the same day on the calendar.

// Another test method for a new method: sameDay(DayCounter, DayCounter)

@Test public void testSameDay() { DayCounter d1 = new DayCounter(200 9 , 1, 20); DayCounter d2 = new DayCounter(200 9 , 1, 20); DayCounter d3 = new DayCounter(20 10 , 1, 20); DayCounter d4 = new DayCounter(200 9 , 2, 20); DayCounter d5 = new DayCounter(200 9 , 1, 19);

Song aSong = new Song("Beatles", "Help");

assertTrue(aSong.sameDay(d1, d2)); assertFalse(aSong.sameDay(d1, d3)); assertFalse(aSong.sameDay(d1, d4)); assertFalse(aSong.sameDay(d1, d5)); }

2. Make the test fail

First get the test method code to compile by adding method sameDay to Song. Since sameDay is a

boolean method, it needs some bogus value to make the test fail, which could be true or false. Run

the SongTest as a JUnit test to see the red bar because it currently always returns true (when false is

expected 4 times in the test method).

Test Driven Development 11

public boolean sameDay(DayCounter d1, DayCounter d2) { return true; }

3. Make the test pass

Two DayCounters are the same day when their getDayInYear, getMonth, and getYear are the

same. Replace the method body with this rather long Boolean expression:

// Return true if d1 and d2 are on the same Year, Month, and Date public boolean sameDay(DayCounter d1, DayCounter d2) {

return d1.getYear() == d2.getYear() && d1.getMonth() == d2.getMonth() && d1. getDayInMonth () == d2. getDayInMonth ();

}

} // end class Song

Run the unit test again, modifying any code until it all compiles and you get the green bar.

Add Two Song methods and a new Type JukeboxAccount

For Homework 3 that must be submitted to WebCat by Monday 2-March @ 2:45, do the following

two things.

  1. Modify Song add one more instance variable and a getter and setter to the Song class
  2. Add JukeboxAccount To the same project, add one more type to the Jukebox System

You may begin this in lab, just remember that before you leave lab, you should email your Song

and SongTest classes to yourself if you plan to finish the WebCat submission homework away

from the lab.

Ask your section leader for a hard copy of

Homework 3, which exists as a separate lab handout

and is also is linked from the homework page

http://www.cs.arizona.edu/classes/cs127a/spring09/homework.htm