a. Create an application that allows you to enter student data that consists of
ID: 3864172 • Letter: A
Question
a. Create an application that allows you to enter student data that consists of an ID number, first name, last name, and credit hours earned. Depending on the student’s number of credit hours completed, output each record to either a lowerclassman file (under 60 hours completed) or an upperclassman file (60 hours or more completed). Save the program as LowerAndUpper.java.
Expand the LowerAndUpper application so that it displays each record one by one from the lowerclassman file and then from the upperclassman file. Display a heading to introduce each list. For each record, display the ID number, first name, last name, credits earned, and number of additional credits needed to graduate. Assume that 120 credits are required for graduation. Save the program as LowerAndUpperDisplay.java.
Explanation / Answer
Here is the code for LowerAndUpper.java:
import java.io.*;
import java.util.*;
class LowerAndUpper
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(System.in);
PrintWriter pw1 = (new PrintWriter(new File("lowerclassman.txt")));
PrintWriter pw2 = (new PrintWriter(new File("upperclassman.txt")));
while(true)
{
System.out.print("Enter the ID number ("0 to quit"): ");
int ID = sc.nextInt();
if(ID == 0)
break;
System.out.print("Enter the first name: ");
String fName = sc.next();
System.out.print("Enter the last name: ");
String lName = sc.next();
System.out.print("Enter the number of credit hours earned: ");
int credits = sc.nextInt();
if(credits < 60)
pw1.write(ID + " " + fName + " " + lName + " " + credits + " ");
else
pw2.write(ID + " " + fName + " " + lName + " " + credits + " ");
}
pw1.close();
pw2.close();
return;
}
}
And the code for LowerAndUpperDisplay.java:
import java.io.*;
import java.util.*;
class LowerAndUpperDisplay
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(new File("lowerclassman.txt"));
System.out.println("Lower class men:");
while(sc.hasNext())
{
int ID = sc.nextInt();
String fName = sc.next();
String lName = sc.next();
int numOfCredits = sc.nextInt();
System.out.println(ID + " " + fName + " " + lName + " " + numOfCredits + (120 - numOfCredits));
}
sc.close();
sc = new Scanner(new File("upperclassman.txt"));
System.out.println("Upper class men:");
while(sc.hasNext())
{
int ID = sc.nextInt();
String fName = sc.next();
String lName = sc.next();
int numOfCredits = sc.nextInt();
System.out.println(ID + " " + fName + " " + lName + " " + numOfCredits + (120 - numOfCredits));
}
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.