Page 226 - Introduction to Programming with Java: A Problem Solving Approach
P. 226

                192
Chapter 5 Using Pre-Built Methods
 7.
Sample session:
Enter search text: Right
Number of occurrences of "Right": 3
[after §5.6] In the following program skeleton, replace <Insert code here.> with your own code. Hint: Use the variables that are already declared for you (songs, songNum, songIndex, eolIndex, and song). The resulting program should prompt the user for a song number and then extract the song number plus the rest of that string’s line from a given list of songs. Study the sample session. You may assume that the user enters a valid song number (no need for input validation).
8.
[after §5.7] Given the below program skeleton. Replace the four <add code here> items so that the program produces the below output. Try to mimic the output’s format precisely, but it’s OK if your column widths vary slightly from the shown column widths.
import java.util.Scanner;
public class ExtractLine
{
public static void main(String[] args)
{
}
System.out.print("Enter song number: ");
songNum = stdIn.nextLine();
<Insert code here.> System.out.println(song);
// end main
Scanner stdIn = new Scanner(System.in);
String songs =
"1. Bow Wow - Fresh Azimiz\n" +
"2. Weezer - Beverly Hills\n" +
"3. Dave Matthews Band - Crash Into Me\n" +
"4. Sheryl Crow - Leaving Las Vegas\n";
String songNum; // song number that is searched for
int songIndex;
int eolIndex;
String song;
Apago PDF Enhancer
// position of end of line character
// the specified line
} // end class ExtractLine
Sample session:
// position of where song number is found
 Enter song number: 3
3. Dave Matthews Band - Crash Into Me
public class CarInventoryReport
{
public static void main(String[] args)
{
final String HEADING_FMT_STR = <addcodehere>; final String DATA_FMT_STR = <addcodehere>; String item1 = "Mazda RX-8";

























































   224   225   226   227   228