Question

Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file:

Count: 5
Sally 78.0 84.0 79.0 86.0
Rachel 68.0 76.0 87.0 76.0
Melba 87.0 78.0 98.0 88.0
Grace 76.0 67.0 89.0 0.0
Lisa 68.0 76.0 65.0 87.0

The first line of the file will indicate the number of students in the class. Each sub sequence line will list the student's first name, followed by their four exam scores (all separated by spaces).

The overall average for each student will be calculated by averaging the three highest test scores for each student. The teacher uses the following grading scale to assign a letter grade to a student:

  • 90 to 100 is an A
  • 80 to 89 is a B
  • 70 to 79 is a C
  • 60 to 69 is a D
  • 59 and lower is E

    Write a class called GradeBook that uses the following instance variables (attributes):

  • A public constant, NUM_TESTS that contains the number of test scores.
  • A private int, numStudents that stores the number of students in the grade book.
  • An array of Strings to hold names of students. It will be sized based on the number of students in the file
  • An array of characters to hold the final letter grade of each student. It will be the same size as array above.
  • A two-dimensional array of type double consisting of test scores for each student. It is of size number of students by NUM_TESTS (there are 4 tests). The number of students in the file might vary. You should check the first line in the file to get the count of the number of students so that you can instantiate Arrays of the appropriate size.Method Description This constructor method will: Open the file whos name is passed in as a String . Instantiate the names, l
  • Note: You may add more methods to the class if it you wish.

    After implementing the GradeBook class, use the given Assgnment8.java. which has the main method to test your class. The program asks the user for the file name.

  • Assgnment8.java

import java.util.Scanner;
import java.io.*;

public class Assignment8
{
static Scanner in = new Scanner(System.in);

public static void main (String[] args) throws IOException
{
String command = " ";
System.out.print("Please enter the filename: ");
String fileName = in.nextLine();

GradeBook grades = new GradeBook(fileName);

while (command.charAt(0) != 'q') {   
System.out.println();
System.out.print("Please enter a command or type \"?\" to see the menu: ");
command = in.nextLine();

switch (command.charAt(0)) {
case 'a': // display all
System.out.println(grades.toString());
break;
case 'b' : // search
System.out.print("Type student name: ");
String name = in.nextLine();
int index = grades.getIndex(name);
char grade = grades.getLetterGrade(index);
if (index != -1)
System.out.println(name + " " + grade);
else
System.out.println("Student doesn't exist!");
break;

case '?' : // help menu
System.out.println("a: Display ");
System.out.println("b: Search ");
System.out.println("?: Help menu ");
System.out.println("q: Stop the program ");
break;

case 'q': // stop the program
break;

default:
System.out.println("Illegal cammand!!!");
} // end of switch
} // end of while
}// end main
} // end Assignment8

Helpful hints for doing this assignment:

• Work on it in steps – write one method, test it with a test driver and make sure it works before going on to the next method
• Always make sure your code compiles before you add another method

Sample Output: with user input in red

Please enter the filename: roster.txt

Please enter a command or type "?" to see the menu: a
Sally         83.0  B
Rachel        79.7  C
Melba         91.0  A
Grace         77.3  C
Lisa          77.0  C

Please enter a command or type "?" to see the menu: b
Type student name: Melba
Melba A

Please enter a command or type "?" to see the menu: b
Type student name: Lisa
Lisa C

Please enter a command or type "?" to see the menu: b
Type student name: Rick
Student doesn't exist!

Please enter a command or type "?" to see the menu: q

 

Note: Do not use any Java language or library features that we have not already covered in class. For example, do not use ArrayLists.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.util.Scanner;

import java.io.*;

public class GradeBook{

public static final int NUM_TESTS = 4;

private int numStudents;

private String[] names;

private char[] grades;

private double scores[][];

public GradeBook(String fileName) throws IOException{

Scanner fin = new Scanner(new File(fileName));

int n=0;

fin.next(); //skipping count string

numStudents = fin.nextInt();

names = new String[numStudents];

grades = new char[numStudents];

scores = new double[numStudents][NUM_TESTS];

while(fin.hasNext()){

names[n] = fin.next();

for(int i=0; i<NUM_TESTS; i++)

scores[n][i] = fin.nextDouble();

n++;

}

assignGrades();

fin.close();

}

private double getStudentAverage(int index){

double sum = 0.0;

for(int i=0; i<NUM_TESTS;i++){

sum += scores[index][i];

}

return sum/NUM_TESTS;

}

private void assignGrades(){

for(int i=0; i<numStudents; i++){

double avg = getStudentAverage(i);

if(avg>=90 && avg<=100)

grades[i] = 'A';

else if(avg>=80 && avg<90)

grades[i] = 'B';

else if(avg>=70 && avg<80)

grades[i] = 'C';

else if(avg>=60 && avg<70)

grades[i] = 'D';

else

grades[i] = 'E';

}

}

public String getStudentName(int index){

return names[index];

}

public char getLetterGrade(int index){

return grades[index];

}

public int getIndex(String studentName){

for(int i=0; i<numStudents; i++){

if(names[i].equalsIgnoreCase(studentName))

return i;

}

return -1;

}

public String toString(){

String str = "";

for(int i=0; i<numStudents; i++){

str += names[i]+"\t"+getStudentAverage(i)+"\t"+grades[i]+"\n";

}

return str;

}

}

import java.util.Scanner;

import java.io.*;

public class Assignment8 {

static Scanner in = new Scanner(System.in);

public static void main(String[] args) throws IOException {

String command = " ";

System.out.print("Please enter the filename: ");

String fileName = in.nextLine();

GradeBook grades = new GradeBook(fileName);

while (command.charAt(0) != 'q') {

System.out.println();

System.out.print("Please enter a command or type \"?\" to see the menu: ");

command = in.nextLine();

switch (command.charAt(0)) {

case 'a': // display all

System.out.println(grades.toString());

break;

case 'b': // search

System.out.print("Type student name: ");

String name = in.nextLine();

int index = grades.getIndex(name);

char grade = grades.getLetterGrade(index);

if (index != -1)

System.out.println(name + " " + grade);

else

System.out.println("Student doesn't exist!");

break;

case '?': // help menu

System.out.println("a: Display ");

System.out.println("b: Search ");

System.out.println("?: Help menu ");

System.out.println("q: Stop the program ");

break;

case 'q': // stop the program

break;

default:

System.out.println("Illegal cammand!!!");

} // end of switch

} // end of while

}// end main

} // end Assignment8

Add a comment
Know the answer?
Add Answer to:
Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process th...

    You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process the tokens and determine if there are errors in the program. This program have 3 files Lexer.java, Token.java, Parser.java Part 1 Lexer: The class Lexer should have the following methods: public constructor which takes a file name as a parameter○private getInput which reads the data from the...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT