home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw10
Playlists
for-each; input
Part (a)
Due Apr.04 (Fri), 20pts:
only hardcopy is needed.
- (3pts)
What is the syntax of a for-each loop?
-
(5pts)
Consider the following program fragment:
java.util.LinkedList<Dog> someDogs = new java.util.LinkedList<Dog>();
someDogs.add( new Dog( "rex", 5 ) );
someDogs.add( new Dog( "fifi", 7 ) );
someDogs.add( new Dog( "hobo", 15 ) );
String namesSoFar = "";
for ( Dog d : someDogs ) {
namesSoFar = d.getName() + ", " + namesSoFar;
}
|
(Note how the order
of the string-contatenation is a bit different than in
most loops we've seen in lecture.)
Complete the following table, showing
the value of namesSoFar
after i times through the loop:
- (2pts)
If employees is a list of PizzaServers,
what does the following loop compute?
.
Your answer should be a short English sentence (fragment).
int somethingSoFar = 0;
for ( PizzaServer p : employees ) {
if (p.getSalary() < PizzaServer.MINIMUM_WAGE) {
somethingSoFar = somethingSoFar + 1;
}
}
|
- (2pts)
What does the following loop compute?
.
Your answer should be a short English sentence (fragment).
PizzaServer ssf = employees.get(0);
for ( PizzaServer p : employees ) {
if (p.getSalary() > ssf.getSalary()) {
ssf = p;
}
}
|
- (1pt)
In a loop, should the accumulator variable be declared and initialized...
-
before the loop,
or
-
in the loop-body?
- (1pt)
In a loop that finds the age of the oldest Dog
in a list-of-Dogs,
-
the accumulator variable (or “so-far” variable)
is of type .
-
the index variable
is of type .
-
(1pt)
The statement “return x = 5;” is…
- A well-written return statement
- A well-written assignment statement
- Extremely confused about whether it is returning a result
or assigning to a variable.
- (1pt)
The constructor for java.util.Scanner
takes how many arguments?
- (2pts)
If s is a java.util.Scanner
reading from System.in,
-
What is the value of s.nextInt() + s.nextInt(),
if the user types “12 34”?
-
What is the value of s.next() + s.next(),
if the user types “12 34”?
- What is the value of s.hasNextInt()
if the user types “12 34 hi”?
-
What is the value of int n = s.nextInt(); n+n,
if the user types “12 34 hi”?
(In all cases, assume the user hits the return key at the end of each input.)
- (1pt)
Java's name for the keyboard is
Java's name for the console (terminal window) is
- (1pt)
-
true or false?: In Java, import statement
does nothing more than save you some typing.
-
true or false?: In Java, import statements
can occur inside a class declaration.
Part (b)
Due Apr.07 (Mon), 15pts:
test cases for class PlayList.
(Javadoc provided below.)
A playlist has a name (e.g. “beach music”
or “my library”),
and a list of Songs.
Make a class to represent this, with the following methods:
-
Add a song to a playlist.
/** Add a Song to this Playlist.
* @param aSong The Song to add to this Playlist.
*/
|
-
Find the total time of all songs in the playlist.
/** Return the total playing time of Songs in this Playlist.
* @return the total playing time of Songs in this Playlist.
*/
|
-
Write a method toString,
which returns a description including
the playlist's name, its total time,
and a description of each song (one per line):
No unit-tests required.
For example,
Ian's Driving Music (6:57):
Title Album Artist Time Play count Last Played Rating
----- ----- ------ ---- ---------- ----------- ------
Jus A Rascal Boy In Da Corner Dizzee Rascal 3:28 10 2007.Nov.10 5/5
Lost in Space Respond II Aimee Mann 3:29 12 2006.Dec.14 4/5
|
Your output doesn't have to look exactly like this, but it should be similar.
(Think about what output you want/expect from
a music-library-program.)
It's fine to just include a tab-character (“\t”)
between each field, so it's okay if the columns don't line up
when some fields are particularly short.
Hint: the method for converting an individual Song to a string
should be in class Song, not class PlayList!
/** Return a (long) string representation of this entire playlist,
* with each Song on its own line.
* @return a (long) string representation of this entire playlist.
*/
|
-
Select a song at random from the playlist. (*) [No unit-tests required.]
Check out the documentation for java.util.Random.
/** Return a randomly-chosen Song from this Playlist.
* @return a randomly-chosen Song from this Playlist.
*/
|
-
Find the least-played Song on the playlist. (*)
/** Return a least-played Song on this Playlist.
* @return a least-played Song on this Playlist.
*/
|
-
Find the least-recently-played Song on the playlist. (*)
/** Return the least-recently played Song on this Playlist.
* @return the least-recently played Song on this Playlist
* (or, in case of ties, a least-recently-played Song).
*/
|
- Given a Song, find how many covers are on this playlist.
/** The number of Songs on this Playlist which are a cover of a given Song.
* @param targetSong the Song to look for covers of.
* @return the number of Songs on this Playlist which are a cover of
* targetSong. (Note that if targetSong is itself on this Playlist,
* then ...YOU FILL IN YOUR DESIRED BEHAVIOR HERE )
*/
|
-
Given a Date,
return a new playlist which contains all songs
played since since that given day.
/** Return all Songs on this Playlist which have been played since a given Date.
* @param aDate The cut-off Date to find songs-more-recently-played-than.
* @return all Songs on this Playlist which have been played since aDate.
*/
|
(*) For problems marked with an asterisk,
you can presume that the playlist contains at least one song.
(Be sure to note this in your documentation!)
When providing test cases,
be sure to test your methods on an empty playlist (when appropriate),
as well as a playlist containing just one song,
as well as a longer playlist.
[Note:
You don't need the following technique for test cases;
it would be needed only for static methods which take lists as arguments.]
When recording test cases in BlueJ,
how do you create a LinkedList
which shows up on the object bench?
-
In the code pad, type
“new java.util.LinkedList<Song>()”,
and hit return.
(Note that you are only creating the list, not storing
it in a local variable.)
-
Then, drag the tiny red box to the object-bench;
BlueJ will then ask you what name you want to use to hold the list.
You can now use that list in test cases, right-click on it, etc..
Part (c)
Due Apr.09 (Wed) (25pts):
implementation (Java code)
for the above methods.
Extra Credit / Challenge
Due Apr.10 (Thu),
including documentation, test cases, and code.
Turn these in separately from hw10.
-
Count how many different album-names are in the playlist.
(Hint:
use an accumulator which is a list of album-names;
only add names to that accumulator if they aren't already included.)
/** Return how many different albums are on this Playlist.
* @return how many different albums are on this Playlist.
*/
|
-
Select a song at random, weighted by length. (*)
That is, if the playlist contains 3 songs
(one minute, eight minutes, and one minute respectively)
the middle song is chosen 80% of the time, while the other two songs
are each chosen 10% of the time.
/** Return a randomly-chosen Song on this Playlist, weighting songs by
* length (e.g. a four-minute Song is twice as likely as a two-minute Song.)
* @return a randomly chosen Song from this Playlist, weighted by time.
*/
|
Hint:
choose a random length-of-time (from 0 up to the length of the entire playlist).
Then look at each song in the list,
accumulating the time of all songs considered so far;
when that accumulator exceeds your random length-of-time,
you have your song.
-
In the toString,
get the columns to line up (perhaps truncating long fields).
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs