Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Reading from and Writing to Text Files Write a program that will read in a file of student academic credit data and create a list

Reading 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 Warning1.java contains a skeleton of the program. Do the following:

Create a new NetBeans project named Lab1. Within the project create a new java package called lab1. Within package lab1, make a new Java class called Warning1. You can then copy the startup code below into that new Java class.

1.Create an array of String objects that will be able to accommodate the number of lines in the input file.

2.Set up a Scanner object scan from the input file and a PrintWriter outFile to the output file inside a try Block. Note that youll have to create the PrintWriter from a FileWriter, but you can still do it in a single statement.

3.Inside a while loop add code to read each line and store each line as a String object in an Array.

4.Next create a for loop to iterate through the Array. For each String in the Array create a StringTokenizer object and use this object to tokenize the three data items stored in each String. Store these items in the appropriate variables. Note that the tokens are Strings, therefore it will be necessary to appropriately parse these tokens to the appropriate data type. Then 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.

5.After the loop, close the PrintWriter.

6Think about the exceptions that could be thrown by this program:

-A FileNotFoundException if the input file does not exist

-A NumberFormatException if it cant 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.

6. 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.

// ************************************************************************

import java.util.*;

import java.io.*;

public class Warning1

{

// ------------------------------------------------------------------

// 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)

{

int creditHrs; // number of semester hours earned

double qualityPts; // number of quality points earned

double gpa; // grade point (quality point) average

String line, name = "", inputName = "students.dat";

String outputName = "warning.dat";

//Create an Array of Strings

//Declare a StringTokenizer

//StringTokenizer st;

7. Alternative Solutions:

A.StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String. Modify the above solution using the split method from the String class instead of the StringTokenizer object.

-Within package lab1, make a new Java class called Warning2. You can then copy your code from Warning1

-Comment out all lines making use of the StringTokenizer

-Review the split method and then add the necessary code

B.The Java ArrayList class uses a dynamic array for storing the elements. Modify the previous 2 solutions using an ArrayList instead of an array to store the lines from the input file. Note: continue to use an array to store the tokens from each line.

-Within package lab1, make 2 new Java classes called Warning3 and Warning4. You can then copy your code from Warning1 and Warning2

-Comment out any lines making use of the array for storing the lines.

-Review the ArrayList class and make the necessary adjustments to your code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Select one: a. 3.25m b. 0.88m C. 0.58m d. 1.36m

Answered: 1 week ago

Question

Identify how culture affects appropriate leadership behavior

Answered: 1 week ago