Write a program that will read in a file of student academic credit data Solution

eading from and Writing to Text Files
Write a program that will read in a file of student academic credit data and create a list of students on academic
warning. The list of students on warning will be written to a file. Each line of the input file will contain the
student name (a single String with no spaces), the number of semester hours earned (an integer), the total
quality points earned (a double). The following shows part of a typical data file:
Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
The program should compute the GPA (grade point or quality point average) for each student (the total quality
points divided by the number of semester hours) then write the student information to the output file if that
student should be put on academic warning. A student will be on warning if he/she has a GPA less than 1.5 for
students with fewer than 30 semester hours credit, 1.75 for students with fewer than 60 semester hours credit,
and 2.0 for all other students. The file Warning.java contains a skeleton of the program. Do the following:
1. Set up a Scanner object scan from the input file and a PrintWriteroutFile to the output file inside the try
clause (see the comments in the program). Note that you’ll have to create the PrintWriter from a FileWriter,
but you can still do it in a single statement.
2. Inside the while loop add code to read and parse the input—get the name, the number of credit hours, and
the number of quality points. Compute the GPA, determine if the student is on academic warning, and if so
write the name, credit hours, and GPA (separated by spaces) to the output file.
3. After the loop close the PrintWriter.
4. Think about the exceptions that could be thrown by this program:
• A FileNotFoundException if the input file does not exist
• A NumberFormatException if it can’t parse an int or double when it tries to - this indicates an error in
the input file format
• An IOException if something else goes wrong with the input or output stream
Add a catch for each of these situations, and in each case give as specific a message as you can. The program
will terminate if any of these exceptions is thrown, but at least you can supply the user with useful information.
5. Test the program. Test data is in the file students.dat. Be sure to test each of the exceptions as well.
// ************************************************************************
// Warning.java
//
// Reads student data from a text file and writes data to another text file.
// ************************************************************************
importjava.util.Scanner;
import java.io.*;
public class Warning

// -------------------------------------------------------------------
// Reads student data (name, semester hours, quality points) from a
// text file, computes the GPA, then writes data to another file
// if the student is placed on academic warning.
// -------------------------------------------------------------------
public static void main (String[] args)
{
intcreditHrs; // number of semester hours earned
doublequalityPts; // number of quality points earned
doublegpa; // grade point (quality point) average
String line, name, inputName = "students.dat";
String outputName = "warning.dat";
214 Chapter 11: Exceptions
try

// Get the credit hours and quality points and
// determine if the student is on warning. If so,
// write the student data to the output file.

Placing Exception Handlers
􀀀 Modify the program to add a try statement that encompasses the entire while loop. The try and opening {
should go before the while, and the catch after the loop body. Catch a NumberFormatException and have
an empty body for the catch.
􀀀 Compile and run the program and enter a line with mixed integers and other values. You should find that it
stops summing at the first non-integer, so the line above will produce a sum of 0, and the line “1 fish 2 fish”
will produce a sum of 1. This is because the entire loop is inside the try, so when an exception is thrown the
loop is terminated. To make it continue, move the try and catch inside the loop. Now when an exception is
thrown, the next statement is the next iteration of the loop, so the entire line is processed. The dogs-and-cats
input should now give a sum of 3, as should the fish input.
// ****************************************************************
// ParseInts.java
//
// Reads a line of text and prints the integers in the line.
//
// ****************************************************************
importjava.util.Scanner;
public class ParseInts

intval, sum=0;
Scanner scan = new Scanner(System.in);
String line;
System.out.println("Enter a line of text");
Scanner scanLine = new Scanner(scan.nextLine());
while (scanLine.hasNext())

Palindromes
Write a program that prompts for and reads in a string, then prints a message saying whether it is a palindrome.
Your main method should read the string and call a recursive (static) method palindrome that takes a string and
returns true if the string is a palindrome, false otherwise. Recall that for a string s in Java,
s.length returns the number of charaters in s s.charAt returns the ith character of s , 0-based
s.substring returns the substring that starts with the ith character of s and ends with the j-1st character of
Recursive Binary Search
The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The
algorithm (in pseudocode) is as follows:
highIndex - the maximum index of the part of the list being searched
lowIndex - the minimum index of the part of the list being searched
target -- the item being searched for
//look in the middle
middleIndex = (highIndex + lowIndex) / 2
if the list element at the middleIndex is the target
return the middleIndex
else
if the list element in the middle is greater than the target
search the first half of the list
else
search the second half of the list
Notice the recursive nature of the algorithm. It is easily implemented recursively. Note that three parameters are
needed—the target and the indices of the first and last elements in the part of the list to be searched. To “search
the first half of the list” the algorithm must be called with the high and low index parameters representing the
first half of the list. Similarly, to search the second half the algorithm must be called with the high and low
index parameters representing the second half of the list. The file IntegerListB.java contains a class representing
a list of integers (the same class that has been used in a few other labs); the file IntegerListBTest.java contains a
simple menu-driven test program that lets the user create, sort, and print a list and search for an item in the list
using a linear search or a binary search. Your job is to complete the binary search algorithm (method
binarySearchR). The basic algorithm is given above but it leaves out one thing: what happens if the target is not
in the list? What condition will let the program know that the target has not been found? If the low and high
indices are changed each time so that the middle item is NOT examined again (see the diagram of indices
below) then the list is guaranteed to shrink each time and the indices “cross”—that is, the high index becomes
less than the low index. That is the condition that indicates the target was not found.
lo middle high
lo middle-1 middle+1 high
Powered by