RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw10
Playlists
for-each; input

Part (a)

Due Apr.04 (Fri), 20pts: only hardcopy is needed.

  1. (3pts) What is the syntax of a for-each loop?
                     (                  :                  ) {
                       
      }
    
  2. (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:

    i namesSoFar
    0                
    1                
    2                
    3                

  3. (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;
        }
      }
    
  4. (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;
        }
      }
    
  5. (1pt) In a loop, should the accumulator variable be declared and initialized...
    1. before the loop,
    2. or
    3. in the loop-body?
  6. (1pt) In a loop that finds the age of the oldest Dog in a list-of-Dogs,
  7. (1pt) The statement “return x = 5;” is…
    1. A well-written return statement
    2. A well-written assignment statement
    3. Extremely confused about whether it is returning a result or assigning to a variable.
  8. (1pt) The constructor for java.util.Scanner takes how many arguments?                 
  9. (2pts) If s is a java.util.Scanner reading from System.in,
    1. What is the value of s.nextInt() + s.nextInt(), if the user types “12 34”?
    2. What is the value of s.next() + s.next(), if the user types “12 34”?
    3. What is the value of s.hasNextInt() if the user types “12 34 hi”?
    4. 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.)
  10. (1pt) Java's name for the keyboard is                  Java's name for the console (terminal window) is                 
  11. (1pt)
    1. true or false?: In Java, import statement does nothing more than save you some typing.
    2. 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:

(*) 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?

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.

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Apr.06 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme