Chapter 9 Practice Set

Section 1 Vocabulary Field Record Token Delimiter Problems 1.

The file grades.txt has the following format: Score1 Score2 . ScoreN

Write a program to open the file grades.txt. Be sure to handle a FileNotFoundException. Write code to read in all the scores and print the average to the screen. 2. The file small.txt has words separated by white space characters. Write a program that reads the file, changes all the letters to capital letters, and prints them to the file caps.txt. Each word should be on a new line. Section 2 Problems 3. Write a Part class with the following instance variables: String name int numOnHand int numOnOrder double cost

Write a constructor for the class that takes a Scanner object that is open to a file containing Part records. The constructor should read one record and set the instance variables. The file has the data for each Part object in the order: name numOnHand numOnOrder cost The file might look like this: hammer 5 2 3.45 Phillips screwdriver 14 4 4.20 wrench 7 2 5.00 Write a method writeFile that takes a PrintWriter that is open to a file. This method writes a Part’s data to the file in the exact same format as the input file. Finally, write a main that attaches a Scanner to the file named parts.txt for reading and create a PrintWriter attached to the file parts2.txt for writing. It should create 3 Part objects, instantiating them by calling the constructor you wrote, then write the 3 parts to the part2.txt using the writeFile method. Section 3 Vocabulary extension Section 4 Problems 4. Write a while loop that takes an integer input from the user, then prompts for additional integers and prints all integers that are greater than or equal to the original input until the user enters 20, which is not printed. 5. Write a loop that takes words as input from the user and concatenates them until the user types in the word “end” (which is not concatenated). The code then outputs the concatenated String.

Solutions 1. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Solutions { public static void main(String[] args) { Scanner in = null; try { File grades = new File("grades.txt"); in = new Scanner(grades); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); System.out.println("in " + System.getProperty("user.dir")); System.exit(1); } int sum = 0; int numScores = 0; while (in.hasNextInt()) { sum += in.nextInt(); numScores++; } System.out.println("Average: " + sum / numScores); } } 2. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter;

import java.util.Scanner; public class Solutions { public static void main(String[] args) { Scanner in = null; PrintWriter out = null; try { File small = new File("small.txt"); in = new Scanner(small); out = new PrintWriter("caps.txt"); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); System.out.println("in " + System.getProperty("user.dir")); System.exit(1); } String word; while (in.hasNext()) { word = in.next(); word = word.toUpperCase(); out.println(word); } out.close(); } } 3. import java.io.PrintWriter; import java.util.Scanner; public class Part { private String name; private int numOnOrder; private int numOnHand;

private double cost; public Part(Scanner fin) { this.name = fin.nextLine(); this.numOnHand = fin.nextInt(); this.numOnOrder = fin.nextInt(); this.cost = fin.nextDouble(); fin.nextLine(); } public void writeFile(PrintWriter fout) { fout.println(this.name); fout.println(this.numOnHand + " " + this.numOnOrder + " " + this.cost); } } import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class Solutions { public static void main(String[] args) { Scanner in = null; PrintWriter out = null; try { File parts = new File("parts.txt"); in = new Scanner(parts); out = new PrintWriter("part2.txt"); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); System.out.println("in " + System.getProperty("user.dir"));

System.exit(1); } Part part1 = new Part(in); Part part2 = new Part(in); Part part3 = new Part(in); part1.writeFile(out); part2.writeFile(out); part3.writeFile(out); out.close(); } } 4. import java.util.Scanner; public class ScannerPractice { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter the cutoff value: "); int cutOff = in.nextInt(); System.out.print("Please enter a value (Enter 20 to quit): "); int num = in.nextInt(); while (num != 20) { if (num >= cutOff) { System.out.println(num); } System.out.print("Please enter a value (20 to quit): "); num = in.nextInt(); } System.out.println("Thank you :)"); } } 5. import java.util.Scanner;

public class ScannerPractice { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a word. Enter \"end\" to quit."); String word = in.next(); String result = ""; while (!word.equals("end")) { result += word; System.out.print("Please enter a word ( \"end\" to quit): "); word = in.next(); } System.out.println(result); } }