These are the Guidelines: In your main method 1. Create two arrays, one for the
ID: 3699650 • Letter: T
Question
These are the Guidelines:
In your main method
1. Create two arrays, one for the names and one for the grades.
2. Loop through the text files to load the arrays
3. Call the methods below to display the appropriate information
4. Promt the user to enter a specific name, if the name does not exist, indicate so, otherwise return individual letter grade.
Write individual methods for the following:
• Display how many people earned an A
• Display how many people earned a B
• Who has the highest grade
• Who has the lowest grade
• What was the average grade
• A method for the individual grade
This is what I've done so far:
/**
*/
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;
public class Grades {
public static void main(String[] args) throws IOException {
File namesFile = Paths.get(".", "resources", "Names.txt").normalize().toFile();
File gradesFile = Paths.get(".", "resources", "Grades.txt").normalize().toFile();
// File file = new File(fileName); // create a File object
if (namesFile.exists()) // check that the file exists
{ // before trying to create a
// Scanner to read the file
// Create a Scanner from the file.
// This statement can cause a FileNotFoundException.
Scanner namesInputFile = new Scanner(namesFile);
Scanner gradesInputFile = new Scanner(gradesFile);
// For each line in the file, read in the line and display it with the line number
int lineNum = 0;
// Use the results of calling the hasNext method to
// determine if you are at the end of the file before
// reading the next line of the file.
String[] names = new String[20];
int[] grades = new int[20];
// char array for letter grades...
while (namesInputFile.hasNext()) {
names[lineNum] = namesInputFile.nextLine();
grades[lineNum] = gradesInputFile.nextInt();
// Output the line read to the screen for the user
System.out.println(lineNum + ": " + names[lineNum]);
// read the next line
lineNum++;
}
// When we're done reading the file,
// close the Scanner object attached to the file
namesInputFile.close();
gradesInputFile.close();
System.out.print ("The number of A's is : " + findNumberOfAs(grades));
// Loop to prompt for individual grades...
}
}
public static int findNumberOfAs(int[] g){
int total = 0;
for (int i = 0; i < g.length; i++){
if (findLetterGrade(g[i]) == 'A'){
total++;
}
}
return total;
}
public static char findLetterGrade(int score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70){
return 'C';
} else if (score >= 60){
return 'D';
} else {
return 'F';
}
}
}
These are the grades:
99
78
52
82
70
77
72
83
67
61
87
93
56
74
54
94
96
79
92
80
These are the names:
Tammy Henning
Rosendo Palafox
Cleveland Chuang
Nolan Levering
Luciana Yeh
Ada Kelsey
Emelia Collyer
Helen Mccullen
Nancie Regina
Efren Garza
Aleen Zeller
Sang Harwell
Kati Blanck
Ima Lenart
Renna Clothier
Glen Mortellaro
Nanci Mcquillan
Hilario Mcnutt
Renaldo Deramus
Leeanna Sciortino
Explanation / Answer
grades.txt
99
78
52
82
70
77
72
83
67
61
87
93
56
74
54
94
96
79
92
80
____________
names.txt
Tammy Henning
Rosendo Palafox
Cleveland Chuang
Nolan Levering
Luciana Yeh
Ada Kelsey
Emelia Collyer
Helen Mccullen
Nancie Regina
Efren Garza
Aleen Zeller
Sang Harwell
Kati Blanck
Ima Lenart
Renna Clothier
Glen Mortellaro
Nanci Mcquillan
Hilario Mcnutt
Renaldo Deramus
Leeanna Sciortino
______________
Grades.java
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;
public class Grades {
public static void main(String[] args) throws IOException {
String str;
// File file = new File(fileName); // create a File object
File namesFile = new File("names.txt");
File gradesFile = new File("grades.txt");
if (namesFile.exists()) // check that the file exists
{ // before trying to create a
// Scanner to read the file
// Create a Scanner from the file.
// This statement can cause a FileNotFoundException.
Scanner namesInputFile = new Scanner(namesFile);
Scanner gradesInputFile = new Scanner(gradesFile);
// For each line in the file, read in the line and display it with the line number
int lineNum = 0;
// Use the results of calling the hasNext method to
// determine if you are at the end of the file before
// reading the next line of the file.
String[] names = new String[20];
int[] grades = new int[20];
// char array for letter grades...
while (namesInputFile.hasNext()) {
names[lineNum] = namesInputFile.nextLine();
grades[lineNum] = gradesInputFile.nextInt();
str = names[lineNum];
// Output the line read to the screen for the user
System.out.println((lineNum + 1) + ": " + str);
// read the next line
lineNum++;
}
// When we're done reading the file,
// close the Scanner object attached to the file
namesInputFile.close();
gradesInputFile.close();
System.out.println("The number of A's is : " + findNumberOfAs(grades));
System.out.println("The number of B's is : " + findNumberOfBs(grades));
System.out.println("Person who got the highest grade :" + names[displayHighGradePerson(grades)]);
System.out.println("Person who got the lowest grade :" + names[displayLowGradePerson(grades)]);
System.out.printf("Average Garde : %.2f ", avgGrade(grades));
displayPersonGrades(names, grades);
// Loop to prompt for individual grades...
}
}
private static void displayPersonGrades(String[] names, int[] grades) {
System.out.println(" Name Grade");
System.out.println("----- ----");
for (int i = 0; i < names.length; i++) {
System.out.printf("%-20s %c ", names[i], findLetterGrade(grades[i]));
}
}
private static double avgGrade(int[] grades) {
double sum = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
}
return sum / grades.length;
}
//This method will return the index of the person who got the lowest score
private static int displayLowGradePerson(int[] grades) {
int min, index = 0;
min = grades[0];
for (int i = 0; i < grades.length; i++) {
if (min > grades[i]) {
min = grades[i];
index = i;
}
}
return index;
}
//This method will return the index of the person who got the highest score
private static int displayHighGradePerson(int[] grades) {
int max, index = 0;
max = grades[0];
for (int i = 0; i < grades.length; i++) {
if (max < grades[i]) {
max = grades[i];
index = i;
}
}
return index;
}
//This method will return the no of person whose grade is B
private static int findNumberOfBs(int[] g) {
int total = 0;
for (int i = 0; i < g.length; i++) {
if (findLetterGrade(g[i]) == 'B') {
total++;
}
}
return total;
}
//This method will return the no of person whose grade is A
public static int findNumberOfAs(int[] g) {
int total = 0;
for (int i = 0; i < g.length; i++) {
if (findLetterGrade(g[i]) == 'A') {
total++;
}
}
return total;
}
public static char findLetterGrade(int score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70) {
return 'C';
} else if (score >= 60) {
return 'D';
} else {
return 'F';
}
}
}
__________________
1: Tammy Henning
2: Rosendo Palafox
3: Cleveland Chuang
4: Nolan Levering
5: Luciana Yeh
6: Ada Kelsey
7: Emelia Collyer
8: Helen Mccullen
9: Nancie Regina
10: Efren Garza
11: Aleen Zeller
12: Sang Harwell
13: Kati Blanck
14: Ima Lenart
15: Renna Clothier
16: Glen Mortellaro
17: Nanci Mcquillan
18: Hilario Mcnutt
19: Renaldo Deramus
20: Leeanna Sciortino
The number of A's is : 5
The number of B's is : 4
Person who got the highest grade :Tammy Henning
Person who got the lowest grade :Cleveland Chuang
Average Garde : 77.30
Name Grade
----- ----
Tammy Henning A
Rosendo Palafox C
Cleveland Chuang F
Nolan Levering B
Luciana Yeh C
Ada Kelsey C
Emelia Collyer C
Helen Mccullen B
Nancie Regina D
Efren Garza D
Aleen Zeller B
Sang Harwell A
Kati Blanck F
Ima Lenart C
Renna Clothier F
Glen Mortellaro A
Nanci Mcquillan A
Hilario Mcnutt C
Renaldo Deramus A
Leeanna Sciortino B
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.