Write a program that will read student’s ID, first name, last name, and 5 scores
ID: 3798778 • Letter: W
Question
Write a program that will read student’s ID, first name, last name, and 5 scores from an input file “hw2data.txt”. The 5 scores are midterm exam score, final exam score, homework score, lab score, and quiz score. The program calculates a student’s weighted total based on the formula: Weighted total = 25%(midterm score) + 25%(final exam score) + 30%(homework score) + 10%(lab score) + 10%(quiz score). And then assigns a letter grade to student using the grade scale: 90 <= weighted total <= 100 A 80 <= weighted total < 90 B 70 <= weighted total < 80 C 60 <= weighted total < 70 D 0 <= weighted total < 60 F The program should output each student’s ID, full name, 5 scores, weighted total, and letter grade in a neat format. Your program should define a class Student and implement it as required. The class Student should have the following private member variables. Member Variable Description ID An int variable that holds a student’s ID. firstName A string variable that holds a student’s first name. lastName A string variable that holds a student’s last name. scores An int array that holds a student’s 5 scores in the order of midterm exam score, final exam score, homework score, lab score, and quiz score. The class Student should also have the following public member functions. Member Function Description Default constructor Set ID to 0, firstName and lastName to empty string, and all elements of scores to 0. Overload constructor Accepts a student’s ID, first name, last name, and an int array (storing 5 scores) as arguments. Calls other member functions to copy these values into the appropriate member variables. setID Accepts an int argument and copies it into the ID member variable. setFName Accepts a string argument and copies it into the firstName member variable. setLName Accepts a string argument and copies it into the lastName member variable. setScores Accepts an int array argument and copies it into the scores member variable. getID Returns the value in ID. getFName Returns the value in firstName. getLName Returns the value in lastName. getWeightedTotal Calculates and returns the weighted total as a floating-point value. The weight for the midterm score, final score, homework score, lab score, and quiz score is 25%, 25%, 30%, 10%, and 10%, respectively. getGrade Finds and returns the letter grade based on the student’s weighted total. printStudent Call other member functions and output a student’s ID, first name followed by a space, followed by last name, and followed by 5 scores, weighted total, and letter grade. Align each column in a neat format as shown in the sample output. Suppose that the input data file contains the records of 25 students. Use an array of Student that holds 25 objects. In the main function, the program reads from the data file and calls member functions of class Student to set member variables and output the results. The hw2data.txt file can be downloaded separately. Assume all the data are valid. Submission: You should submit a ZIP file that contains three files: the class header file Student.h, the class implementation file Student.cpp and the test program hw2main.cpp. Be sure to include the integrity statement “I certify that this submission is my own original work” with your name and RAM ID in each source file. Do NOT use any other names for the above three files.
Explanation / Answer
Student.java
public class Student {
private int id;
private String firstName;
private String lastName;
private int[] scores = new int[5];
// default constructor
public Student() {
}
// student constructor to set member values
public Student(int id, String firstName, String lastName, int[] scores) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.scores = scores;
}
// set id
public void setId(int id) {
this.id = id;
}
// set first name
public void setFName(String firstName) {
this.firstName = firstName;
}
// set last name
public void setLName(String lastName) {
this.lastName = lastName;
}
// set scores
public void setScores(int[] scores) {
this.scores = scores;
}
public int getId() {
return id;
}
public String getFName() {
return firstName;
}
public String getLName() {
return lastName;
}
// function returns weighted average
public double getWeightedTotal() {
double total = scores[0] * 0.25 + scores[1] * 0.25 + scores[2] * 0.30
+ scores[3] * 0.10 + scores[4] * 0.10;
return Math.floor(total * 100)/100;
}
// returns the grade as per scores
public char getGrade() {
double weightedTotal = getWeightedTotal();
if (weightedTotal <= 100 && weightedTotal >= 90) {
return 'A';
}
if (weightedTotal < 90 && weightedTotal >= 80) {
return 'B';
}
if (weightedTotal < 80 && weightedTotal >= 70) {
return 'C';
}
if (weightedTotal < 70 && weightedTotal >= 60) {
return 'D';
}
return 'F';
}
// print student Information
public void printStudent() {
System.out.println(id + " " + firstName + " " + lastName + " Scores:["
+ scores[0] + " " + scores[1] + " " + scores[2] + " " + scores[3] + " " + scores[4]
+ "] Weighted Total: " + getWeightedTotal() + " Grade: " + getGrade());
}
}
StudentDemo.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class StudentDemo {
public static void main(String s[]) throws IOException {
// creating file reader
BufferedReader reader = new BufferedReader(new FileReader("hw2data.txt"));
String line = reader.readLine();
// read while there are entries in the file
while(line != null) {
// tokenize the read line by spaces
String tokens[] = line.split(" ");
// read information
int id = Integer.parseInt(tokens[0]);
String fName = tokens[1];
String lName = tokens[2];
int[] scores = new int[5];
// fill the scores
scores[0] = Integer.parseInt(tokens[3]);
scores[1] = Integer.parseInt(tokens[4]);
scores[2] = Integer.parseInt(tokens[5]);
scores[3] = Integer.parseInt(tokens[6]);
scores[4] = Integer.parseInt(tokens[7]);
// create the student object based on the above read values
Student sd = new Student(id, fName, lName, scores);
sd.printStudent();
// read next line
line = reader.readLine();
}
// close the file reader
reader.close();
}
}
Input file(hw2data.txt)
1 First1 Last1 12 23 67 32 22
2 First2 Last2 46 76 74 46 74
3 First3 Last3 76 76 76 34 34
4 First4 Last4 34 46 34 34 34
5 First5 Last5 94 72 96 91 92
6 First6 Last6 46 34 22 34 92
7 First7 Last7 56 34 76 34 92
8 First8 Last8 76 76 34 34 22
9 First9 Last9 76 46 76 22 74
10 First10 Last10 76 22 56 76 34
11 First11 Last11 56 76 46 76 46
12 First12 Last12 76 46 76 34 34
13 First13 Last13 76 22 22 92 56
14 First14 Last14 56 92 74 76 56
15 First15 Last15 76 92 56 74 56
16 First16 Last16 76 22 74 46 34
17 First17 Last17 56 76 22 56 92
18 First18 Last18 76 76 56 22 34
19 First19 Last19 22 76 76 74 74
20 First20 Last20 76 22 92 74 22
21 First21 Last21 56 92 22 22 74
22 First22 Last22 56 92 92 74 76
23 First23 Last23 56 22 56 22 76
24 First24 Last24 92 22 76 76 76
25 First25 Last25 92 74 74 22 74
Sample Output:
1 First1 Last1 Scores:[12 23 67 32 22] Weighted Total: 34.25 Grade: F
2 First2 Last2 Scores:[46 76 74 46 74] Weighted Total: 64.7 Grade: D
3 First3 Last3 Scores:[76 76 76 34 34] Weighted Total: 67.6 Grade: D
4 First4 Last4 Scores:[34 46 34 34 34] Weighted Total: 37.0 Grade: F
5 First5 Last5 Scores:[94 72 96 91 92] Weighted Total: 88.6 Grade: B
6 First6 Last6 Scores:[46 34 22 34 92] Weighted Total: 39.2 Grade: F
7 First7 Last7 Scores:[56 34 76 34 92] Weighted Total: 57.9 Grade: F
8 First8 Last8 Scores:[76 76 34 34 22] Weighted Total: 53.8 Grade: F
9 First9 Last9 Scores:[76 46 76 22 74] Weighted Total: 62.9 Grade: D
10 First10 Last10 Scores:[76 22 56 76 34] Weighted Total: 52.3 Grade: F
11 First11 Last11 Scores:[56 76 46 76 46] Weighted Total: 59.0 Grade: F
12 First12 Last12 Scores:[76 46 76 34 34] Weighted Total: 60.09 Grade: D
13 First13 Last13 Scores:[76 22 22 92 56] Weighted Total: 45.9 Grade: F
14 First14 Last14 Scores:[56 92 74 76 56] Weighted Total: 72.39 Grade: C
15 First15 Last15 Scores:[76 92 56 74 56] Weighted Total: 71.8 Grade: C
16 First16 Last16 Scores:[76 22 74 46 34] Weighted Total: 54.7 Grade: F
17 First17 Last17 Scores:[56 76 22 56 92] Weighted Total: 54.4 Grade: F
18 First18 Last18 Scores:[76 76 56 22 34] Weighted Total: 60.4 Grade: D
19 First19 Last19 Scores:[22 76 76 74 74] Weighted Total: 62.09 Grade: D
20 First20 Last20 Scores:[76 22 92 74 22] Weighted Total: 61.7 Grade: D
21 First21 Last21 Scores:[56 92 22 22 74] Weighted Total: 53.2 Grade: F
22 First22 Last22 Scores:[56 92 92 74 76] Weighted Total: 79.59 Grade: C
23 First23 Last23 Scores:[56 22 56 22 76] Weighted Total: 46.1 Grade: F
24 First24 Last24 Scores:[92 22 76 76 76] Weighted Total: 66.5 Grade: D
25 First25 Last25 Scores:[92 74 74 22 74] Weighted Total: 73.3 Grade: C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.