Write a Java program that can maintain grades for a simple course. Functional re
ID: 3912495 • Letter: W
Question
Write a Java program that can maintain grades for a simple course.
Functional requirements
Your program will be command driven. It will repeatedly print a prompt, read a command, the execute a command. Grade information is stored in a text file name grades.txt, located in the same directory as the executable. The course is assumed to have exams and assignments, each worth 50% of the grade. Within a category, all entries are weighted equally. All updates should be saved to the file after each command.
q
Quit the program.
addS
Add a new student. If a student of that name already exists an error message is displayed and no changes are made.
addA
Add a new assignment. Assignments are not named, they are indexed starting at 1.
addE
Add a new exam. Exams are not named, they are indexed starting at 1.
grade
For each assignment, display the student's grade and prompt for a new grade. If no grade has been entered a 0 is displayed for the current grade. If the user enters no data the current grade is kept. If an invalid grade is given (outside of 0-100, oir non-numeric), the program reprompts.
Then repeat the process for each exam.
gradeA
For each student prompt for that student's grade on the given assignment.
gradeE
For each student prompt for that student's grade on the given exam.
display
Print a table of all entered grades, with the overall grade displayed in the right-most column.
Grade information is stored in a text file name grades.txt.
Explanation / Answer
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.PrintWriter;
public class SimpleCourse{
public static void save(ArrayList<String> st,ArrayList<ArrayList<Integer>> assig,ArrayList<ArrayList<Integer>> ex){
try{
PrintWriter sc = new PrintWriter(new File("grades.txt"));
for(int i = 0;i<st.size();i++){
sc.println(st.get(i));
for(int j = 0;j<assig.get(i).size();j++){
sc.print(assig.get(i).get(j)+" ");
}
sc.print(" ");
for(int j = 0;j<ex.get(i).size();j++){
sc.print(ex.get(i).get(j)+" ");
}
}
sc.close();
}catch(Exception e){
e.printStackTrace();
System.exit(0);
}
}
public static void display(ArrayList<String> st,ArrayList<ArrayList<Integer>> assig,ArrayList<ArrayList<Integer>> ex){
ArrayList<Integer> fullGradeA = new ArrayList<>();
ArrayList<Integer> fullGradeE = new ArrayList<>();
for(int i = 0;i<st.size();i++){
fullGradeA.add(new Integer(0));
fullGradeE.add(new Integer(0));
System.out.println("=================== "+st.get(i)+" ========================");
for(int j = 0;j<assig.get(i).size();j++){
System.out.print(assig.get(i).get(j)+" ");
fullGradeA.set(i,new Integer((int)fullGradeA.get(i) + (int)assig.get(i).get(j)));
}
for(int j = 0;j<ex.get(i).size();j++){
System.out.print(ex.get(i).get(j)+" ");
fullGradeE.set(i,new Integer((int)fullGradeE.get(i) + (int)ex.get(i).get(j)));
}
System.out.print((((int)fullGradeA.get(i)+(int)fullGradeE.get(i))/(assig.get(i).size()+ex.get(i).size()))+" ");
}
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
ArrayList<String> students = new ArrayList<>();
ArrayList<ArrayList<Integer>> assignments = new ArrayList<>();
ArrayList<ArrayList<Integer>> exams = new ArrayList<>();
String command = "";
//main loop
while(true){
System.out.print(" CMD >> ");
command = key.next();
switch(command){
case "q":save(students,assignments,exams);System.exit(0);
case "addS":
{
String name;
System.out.print(" Enter the student name : ");
name = key.next();
if(!students.contains(name)){
students.add(name);
ArrayList<Integer> assig = new ArrayList<>();
ArrayList<Integer> ex = new ArrayList<>();
if(assignments.size() > 0){
int n = assignments.get(0).size();
for(int i = 0;i<n;i++){
assig.add(new Integer(0));
}
}
if(exams.size() > 0){
int n = exams.get(0).size();
for(int i = 0;i<n;i++){
ex.add(new Integer(0));
}
}
assignments.add(assig);
exams.add(ex);
}else{
System.out.println("Student of name "+name+" already exist !!! ");
}
break;
}
case "addA":
{
for(int i = 0;i<students.size();i++){
assignments.get(i).add(new Integer(0));
}
break;
}
case "addE":
{
for(int i = 0;i<students.size();i++){
exams.get(i).add(new Integer(0));
}
break;
}
case "grade":
{
if(students.size() <= 0){
break;
}
int data;
for(int i = 0;i<assignments.get(0).size();i++){
System.out.println("Assignment "+(i+1)+" : ");
for(int j = 0;j<students.size();j++){
while(true){
try{
System.out.println(students.get(j)+" : "+assignments.get(j).get(i));
data = Integer.parseInt(key.next());
if(data > 1 && data <= 100){
break;
}
}catch(Exception e){
continue;
}
}
assignments.get(j).set(i,new Integer(data));
}
}
for(int i = 0;i<exams.get(0).size();i++){
System.out.println("Exams "+(i+1)+" : ");
for(int j = 0;j<students.size();j++){
while(true){
try{
System.out.println(students.get(j)+" : "+exams.get(j).get(i));
data = Integer.parseInt(key.next());
if(data > 1 && data <= 100){
break;
}
}catch(Exception e){
continue;
}
}
exams.get(j).set(i,new Integer(data));
}
}
break;
}
case "gradeA":
{
if(students.size() <= 0){
break;
}
int data;
for(int i = 0;i<assignments.get(0).size();i++){
System.out.println("Assignment "+(i+1)+" : ");
for(int j = 0;j<students.size();j++){
while(true){
try{
System.out.println(students.get(j)+" : "+assignments.get(j).get(i));
data = Integer.parseInt(key.next());
if(data > 1 && data <= 100){
break;
}
}catch(Exception e){
continue;
}
}
assignments.get(j).set(i,new Integer(data));
}
}
break;
}
case "gradeE":
{
if(students.size() <= 0){
break;
}
int data;
for(int i = 0;i<exams.get(0).size();i++){
System.out.println("Exams "+(i+1)+" : ");
for(int j = 0;j<students.size();j++){
while(true){
try{
System.out.println(students.get(j)+" : "+exams.get(j).get(i));
data = Integer.parseInt(key.next());
if(data > 1 && data <= 100){
break;
}
}catch(Exception e){
continue;
}
}
exams.get(j).set(i,new Integer(data));
}
}
break;
}
case "display":
display(students,assignments,exams);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.