<u>PART A</u>
import java.io.*;
import java.util.Scanner;
public class StudentsStanding {
private static final double CUT_OFF = 2.0;
public static void main(String[] args)
{
try {
PrintWriter goodFile = new PrintWriter(new FileWriter("goodStanding.txt"));
PrintWriter probationFile = new PrintWriter(new FileWriter("probation.txt"));
char choice;
String firstName,lastName;
Scanner input = new Scanner(System.in);
int id;
double gradePoint;
do {
System.out.print("Enter student Id: ");
id = input.nextInt();
input.nextLine();
System.out.print("Enter First name: ");
firstName = input.nextLine();
System.out.print("Enter last name: ");
lastName = input.nextLine();
System.out.print("Enter grade point: ");
gradePoint = input.nextDouble();
if(gradePoint<CUT_OFF)
probationFile.println(id+","+firstName+","+lastName+","+gradePoint);
else
goodFile.println(id+","+firstName+","+lastName+","+gradePoint);
System.out.print("Do you want to continue..(y/n): ");
choice = input.next().toLowerCase().charAt(0);
}while (choice!='n');
//close the streams
goodFile.close();
probationFile.close();
}
catch (IOException ex)
{
System.err.println("IO Exception occurs...");
}
}
}
<u>PART 2</u>
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentsStanding2 {
private static final double CUT_OFF = 2.0;
public static void main(String[] args)
{
String header = "ID\t"+"First Name"+"\t"+"Last Name"+"\t"+"Grade Point"+"\t"+"Exceeds/Fall\n";
try {
Scanner probationFile= new Scanner(new File("probation.txt"));
Scanner goodFile = new Scanner(new File("goodStanding.txt"));
System.out.println("Cut Off is: "+CUT_OFF);
System.out.println(header);
double point ,exceed;
while (probationFile.hasNextLine())
{
String[] tokens = probationFile.nextLine().split(",");
point = Double.parseDouble(tokens[3]);
exceed = CUT_OFF-point;
System.out.println(String.format("%4s %7s %10s %10s %10s",tokens[0],tokens[1],tokens[2],point,String.format("%.2f",exceed)));
}
//Close the stream
probationFile.close();
while (goodFile.hasNextLine())
{
String[] tokens = goodFile.nextLine().split(",");
point = Double.parseDouble(tokens[3]);
exceed = point - CUT_OFF;
System.out.println(String.format("%4s %7s %10s %10s %10s",tokens[0],tokens[1],tokens[2],point,String.format("%.2f",exceed)));
}
goodFile.close();
}
catch (FileNotFoundException ex)
{
System.err.println("File not found!!!");
}
}
}