Part 1: Create an application that allows you to enter student data that consists of an ID number, first name, last name, and grade point average. Depending on whether the student's grade point average is at least 2.0, output each record either to a file of students in good standing or those on academic probation. Save the program as StudentStanding.java.

Part 2: Create an application that displays each record in the two files created in the StudentStanding application. Display a heading to introduce the list produced from each file. For each record, display the ID number, first name, last name, grade point average, and the amount by which the grade point average exceeds or falls short of the 2.0 cutoff. Save the program as StudentStanding2.java.

Respuesta :

PART A

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...");

       }

   }

}

PART 2

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!!!");

       }

   }

}