Question: Important note: the file containing the class names and grades must be passed as a command line argument to the program. Starting with a file
Important note: the file containing the class names and grades must be passed as a command line argument to the program.
Starting with a file that contains courses names and grades earned for each course:
The text file "coursesTaken.txt" would include:
Computer Science I A
Physics 101 B
Mathematics 50 A
when running the program through the command line with "java gpa coursesTaken.txt" it should display the content of the text file along with the gpa.
The second sample run includes an optional command line argument -o that requires a second file name. This second file, entered after the -o, must be created by your program and include the courses taken, grade, and GPA, similar to the output from the first sample run, but to a written to a file. The GPA and number of courses attempted must also be written to the screen as shown above when the -o option is used.
Undefined command line options must be flagged as an error
Here is my code so far:
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
public class gpa1
{
public static void main(String[] args) throws IOException
{
if (args.length !=1)
{
System.out.println("Usage: java gpa filename");
System.exit(-1);
}
File fr = new File(args[0]);
if (!fr.exists())
{
System.out.println("Error: file " + args[0] + " not found. ");
System.exit(0);
}
Scanner infile = new Scanner(fr);
double score = 0;
int count = 0;
while (infile.hasNextLine())
{
String line = infile.nextLine();
System.out.println(line);
switch(line.charAt(line.length()-1)){
case 'A' : score = score + 4;
break;
case 'B' : score = score + 3;
break;
case 'C' : score = score + 2;
break;
case 'D' : score = score + 1;
break;
case 'F' : score = score + 0;
break;
}
count++;
}
infile.close();
score = score/count;
System.out.printf(" GPA : %.2f ",score);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
