Calling getter from imported class - java

I am having trouble calling a getter from an imported class.
I have created a working class (Students) and an action class (ProgressReport). The main class reads a text file and writes the data to an array. The data is then manipulated in the working class. Last, ProgressReport.generateReport will create a report giving the students name, their grade average and the letter grade associate with that average.
I am having trouble using the Students getters from the Progress Report class. Eclipse is saying the method is undefined. I am not exactly sure what I have done wrong or how to go about fixing it. Any help would be very much appreciated.NOTE: I added some println to make sure parts of the code were being executed. Thank you all in advance.
Code to follow:
Progress Report
package Lab1A;
import java.util.*;
import Lab1A.Students;
import java.io.*;
public class ProgressReport {
public Students section[][];
public static void main(String[] args) throws IOException
{
Students tmpStudent;
ProgressReport progressReport = new ProgressReport();
progressReport.readInputFile();
progressReport.generateReport();
System.out.println("\nSEARCH TEST");
tmpStudent = null;
tmpStudent = progressReport.sequentialSearch(0, "Cooper");
if (tmpStudent != null)
System.out.println("Found " + tmpStudent.getName() +
"\tAverage = " + tmpStudent.getAverage() +
"\tGrade = " + tmpStudent.getGrade());
else System.out.println("Fail to find the student");
tmpStudent = null;
tmpStudent = progressReport.sequentialSearch(0, "Bronson");
if (tmpStudent != null)
System.out.println("Found " + tmpStudent.getName() +
"\tAverage = " + tmpStudent.getAverage() +
"\tGrade = " + tmpStudent.getGrade());
else System.out.println("Fail to find the student");
tmpStudent = null;
tmpStudent = progressReport.sequentialSearch(1, "Diana");
if (tmpStudent != null)
System.out.println("Found " + tmpStudent.getName() +
"\tAverage = " + tmpStudent.getAverage() +
"\tGrade = " + tmpStudent.getGrade());
else System.out.println("Fail to find the student");
}
public ProgressReport()
{
section = new Students [2][];
}
public void readInputFile() throws FileNotFoundException
{
System.out.println("in readInputFile method");
//Open file
File input = new File("Lab1A.in");
Scanner inputFile = new Scanner(input);
System.out.println("file is open");
//Read file data
int currentStudent = 0;
while (inputFile.hasNext())
{
System.out.println("In while loop");
//Get Student count
int rows = inputFile.nextInt();
//Read student data
section[currentStudent] = new Students[rows];
System.out.println("array initiated");
for(int i = 0; i < rows; i++)
{
System.out.println("In for loop");
//read in a students info
String sName = inputFile.next();
//Read in grades
int grade1 = inputFile.nextInt();
int grade2 = inputFile.nextInt();
int grade3 = inputFile.nextInt();
int grade4 = inputFile.nextInt();
int grade5 = inputFile.nextInt();
//Send to Students Array
section[currentStudent][i] = new Students(sName, grade1, grade2, grade3, grade4, grade5);
}
//Next Student Line
currentStudent++;
}
inputFile.close();
}
public void generateReport()
{
System.out.println("Progress Report");
double average = Students.class.getAverage();
//String section = "Section\n";
}
public Students sequentialSearch(int section, String searchName)
{
return null;
}
}
Students Class
package Lab1A;
import java.lang.reflect.Array;
public class Students {
private String name;
private char grade;
private double average;
private int scores[];
public Students(String sName, int grade1, int grade2, int grade3, int grade4, int grade5)
{
//CONSTRUCTOR load data from ProgressReport
name = sName;
int newScores[] = {grade1, grade2, grade3, grade4, grade5};
scores = newScores;
}
//Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGrade() {
if(average >= 90 && average <= 100)
{
grade = 'A';
}
if(average >= 80 && average < 90)
{
grade = 'B';
}
if(average >= 70 && average < 80)
{
grade = 'C';
}
if(average >= 60 && average < 70)
{
grade = 'D';
}
if(average >= 0 && average < 60)
{
grade = 'F';
}
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
public int[] getScores() {
return scores;
}
public void setScores(int[] scores) {
this.scores = scores;
}
//Calculate average score
public void calculateAverage()
{
int total = 0;
for (int i = 0; i < scores.length; i++)
{
total += scores[i];
}
average = total*1.0/scores.length;
}
//calculate letter grade based on average score (calulateAverage.average)
public void calculateGrade()
{
if(average >= 90 && average <= 100)
{
grade = 'A';
}
if(average >= 80 && average < 90)
{
grade = 'B';
}
if(average >= 70 && average < 80)
{
grade = 'C';
}
if(average >= 60 && average < 70)
{
grade = 'D';
}
if(average >= 0 && average < 60)
{
grade = 'F';
}
}
}

Your not specifying which Student you want the average of (Not sure why you named your class students with an s). It needs to look something like this:
section[1][1].getAverage()

Related

Write a program that reads student scores, gets the best score, and then assigns grades

Can someone please tell me why my code is not providing the correct output? Here aare the instructions "
I need to write a program that reads student scores, gets the best
score, and then assigns grades based on the following scheme:
1) Grade is A if score is >= best - 10
2) Grade is B if score is >= best - 20;
3) Grade is C if score is >= best - 30;
4) Grade is D if score is >= best - 40;
5) Grade is F otherwise.
The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores, and concludes by displaying the grades. My problem comes from pulling the grades from an array, this is what I have so far:
// Here is my code. Thank You
import java.util.Scanner; // imports the scanner function
public class NBpractice { //class is formed
public static void main(String []args) { // main method
// user input is asked for the number of students
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int studentNum = input.nextInt();
//user input is asked for students scores
Scanner input2 = new Scanner(System.in);
System.out.print("Enter " + studentNum + " scores: ");
int scores = input2.nextInt();
int best = 80;
char letterGrade;
int scoresArray[] = new int[studentNum]; // array is created and holds the # of place values as students
for (int i = 0; i < scoresArray.length; i++) { // for loop created
scoresArray[i] = input2.nextInt(); //array values are assigned to user's input
best = scoresArray[0];
if (best < scoresArray[i]) {
best = scoresArray[i];
}
//-----------------------------------------------------------------------------
if (scores >= (best - 10)) {
letterGrade = 'A';
}
else if (scores >= (best - 20)) {
letterGrade = 'B';
}
else if (scores
>= (best - 30)) {
letterGrade = 'C';
}
else if (scores >= (best - 30)) {
letterGrade = 'D';
}
else {
letterGrade = 'F';
}
System.out.println("Student " + i + " Score is " + scoresArray[i] + " and grade is: " + letterGrade );
}
//------------------------------------------------------------
}
}
Some pointers...
This: System.out.print("Enter " + studentNum + " scores: "); and int scores = input2.nextInt(); need to go in the for loop body.
Use the for loop to populate the array.
Once that the for loop is executed, find the best (highest) score in the array.
Use another for loop to sort out the grades.
As is, your program will only ask for the grades and pretty much assumes that the best grade is 80, which might not always be the case.
You'll need two separate for loops. One to read the grades, and get the best, and the second for loop to normalize the grades.
int[] scores = new int[amount];
int best = -1;
for(int i = 0; i < amount; i++)
{
scores[i] = in.nextInt();
if(scores[i] > best)
best = scores[i];
}
System.out.println(Arrays.toString(scores));
// Now that we have the best, we can normalize
// the rest of the scores based on the best
// and assign the corresponding letter grade.
String[] grades = new String[amount];
for(int i = 0; i < amount; i++)
{
int score = scores[i] * 100 / best;
if(score >= 90)
grades[i] = "A";
else if(score >= 80)
grades[i] = "B";
else if(score >= 70)
grades[i] = "C";
else if(score >= 60)
grades[i] = "D";
else
grades[i] = "F";
scores[i] = score;
}
System.out.println(Arrays.toString(scores));
System.out.println(Arrays.toString(grades));
Test input 80 60 75 83 67 outputs:
[80, 60, 75, 83, 67]
[96, 72, 90, 100, 80]
[A, C, A, A, B]
I would recommend using a Student class and not work with parallel lists or arrays. A Student class can for example look like this:
class Student {
int score;
String grade; // could also be an Enum
public int getScore() {
return this.score;
}
public void setScore(int score) {
this.score = score;
}
public String getGrade() {
return this.grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
#Override
public String toString() {
return "Student{" + "score=" + score + ", grade='" + grade + '\'' + '}';
}
}
You can then make instance of the Students and add them to an ArrayList in you public static void main.
I think you have to use two loops because you cannot know beforehand what the best grade will be. In your main you can make enter the students add them to a List and compare their grades;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int highScore = 0;
int numOfStudent = input.nextInt();
List<Student> studentList = new ArrayList<>();
for (int i = 1; i <= numOfStudent; i++) { // you might want to add Exception handling here, by surrounding it with a try / catch or do more checks than only i <= numOfStudent
System.out.printf("please fill in the score of student no %d \n", i);
int score = input.nextInt();
Student student = new Student();
student.setScore(score);
if (score > highScore) {
highScore = score;
}
studentList.add(student);
}
System.out.println("these are the scores and grades of the Students");
for (Student s : studentList) {
if (s.getScore() >= highScore - 10) {
s.setGrade("A");
}
else if (s.getScore() >= highScore - 20) {
s.setGrade("B");
}
else if (s.getScore() >= highScore - 30) {
s.setGrade("C");
}
else if (s.getScore() >= highScore - 40) {
s.setGrade("D");
}
else {
s.setGrade("F");
}
System.out.println(s);
}
}
package Chapter7;
import java.util.Scanner;
public class Exercise7_1 {
public static void main(String[] args) {
// Assign grades
Scanner input = new Scanner(System.in);
int numStudents;
int[] scores;
int best;
System.out.println("Enter the number of students: ");
numStudents = input.nextInt();
scores = new int[numStudents];
System.out.println("Enter " + numStudents + " scores: ");
for (int i = 0; i < numStudents; i++) {
scores[i] = input.nextInt();
}
displayGrades(findBestScore(scores), scores);
}
public static int findBestScore(int[] scores) {
int best = scores[0];
for (int i = 1; i < scores.length-1; i++) {
if (scores[i] > best)
best = scores[i];
}
return best;
}
public static void displayGrades(int best, int[] scores ) {
char grade = ' ';
for (int i = 0; i < scores.length; i++) {
if (scores[i] >= best-10)
grade = 'A';
else if (best - 10 > scores[i] && scores[i] >= best - 20)
grade = 'B';
else if (best - 20 > scores[i] && scores[i] >= best -30)
grade = 'C';
else if (best - 30 > scores[i] && scores[i] >= best -40)
grade = 'D';
else if (best - 40 > scores[i])
grade = 'F';
System.out.println("Student " + i + " score is " + scores[i] + " and grade is " + grade);
}
}
}

Java | how can I use multiple methods to print out grade scores?

import java.util.Scanner;
public class BA4 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Hello Drews, how many total grades do you want to process?");
int numberOfGrades = keyboard.nextInt();
int[] storeGrades = new int[numberOfGrades];
for (int i = 0; i < numberOfGrades; i++) {
System.out.println("Please enter grade " + (i + 1) + ": ");
storeGrades[i] = keyboard.nextInt();
}
System.out.println("Total score is: " + (getTotalScore(storeGrades)));
System.out.println("Lowest score is: " + (getLowestScore(storeGrades)));
System.out.println("Highest score is: " + (getHighestScore(storeGrades)));
System.out.println("Average score is: " + (averageScore(String.format("%.2f", storeGrades))));
}
public static int getTotalScore(int[] storeGrades) {
int sum = 0;
for (int i = 0; i < storeGrades.length; i++) {
sum += storeGrades[i];
}
return sum;
}
public static int getLowestScore(int[] storeGrades) {
int getLowestScore = 0;
for (int i = 0; i > storeGrades.length; i++) {
getLowestScore = storeGrades[i];
}
return getLowestScore;
}
public static int getHighestScore(int[] storeGrades) {
int getHighestScore = 0;
for (int i = 0; i < storeGrades.length; i++) {
getHighestScore = storeGrades[i];
}
return getHighestScore;
}
public static double averageScore(double[] storeGrades) {
double averageScore = 0;
for (int i = 0; i < storeGrades.length; i++) {
averageScore = (double) storeGrades[i];
}
return averageScore;
}
public static int printGrade(int[] storeGrades) {
int printGrade;
if (printGrade > 89) {
String gradeSoFar = "A";
System.out.println("Your grade so far is an " + gradeSoFar);
}
else if ((printGrade > 79) && (printGrade < 90)) {
String gradeSoFar = "B";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 69) && (printGrade < 80)) {
String gradeSoFar = "C";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 59) && (printGrade < 70)) {
String gradeSoFar = "D";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 0) && (printGrade < 60)) {
String gradeSoFar = "F";
System.out.println("Your grade so far is an " + gradeSoFar);
}
return printGrade;
}
}
I am trying to figure out where I am going wrong. I have a couple of errors which leads me to believe I really just don't understand methods as well as I thought I did.
The goal is to create 5 methods displaying to the user the total, lowest, highest and average scores, and then to print the letter grade. Thank you for your assistance to this noobie java coder! :)
You are passing in a String when you should be passing in a Double[] into averageScore function in this line:
System.out.println("Average score is: " + (averageScore(String.format("%.2f", storeGrades))));
and you did not initialize the printGrade variable inside the printGrade function, you need to give it an initial value if you are going to use it in a comparison.
That's all the errors that

Finding the highest and the lowest value

I have this assignment I gotta do. It looks good, except for the getHighest output and the getLowest result. Here's my code:
KNW_CourseGradesV2 Class:
public class KNW_CourseGradesV2 implements KNW_Analyzable
{
//Declare array variable
private KNW_GradedActivity[] grades = new KNW_GradedActivity[4];
String results;
/**
* Constructor
* */
public KNW_CourseGradesV2()
{
}
public void setLab(KNW_GradedActivity lab)
{
grades[0] = lab;
}
public void setPassFailExam(KNW_GradedActivity pfe)
{
grades[1] = pfe;
}
public void setEssay(KNW_GradedActivity essay)
{
grades[2] = essay;
}
public void setFinalExam(KNW_FinalExam fe)
{
grades[3] = fe;
}
public void setTotalGrade(KNW_GradedActivity tg)
{
grades[4] = tg;
}
public String toString()
{
//Lay out all of the scores
return "Lab Score: " + grades[0].getScore() +
"\tGrade: " + grades[0].getGrade() +
"\nPass/Fail Exam: " + grades[1].getScore() +
"\tGrade: " + grades[1].getGrade() +
"\nEssay Score: " + grades[2].getScore() +
"\tGrade: " + grades[2].getGrade() +
"\nFinal Exam Score: " + grades[3].getScore() +
"\tGrade: " + grades[3].getGrade();
}
//Find the average of the grades
public double getAverage()
{
double sum = 0.0;
double average = 0.0;
double total = 0.0;
//For loop to calc. the average
for(int i =0; i<grades.length; i++)
{
total =+ grades[i].getScore();
average = sum/grades.length;
}
return average;
}
public KNW_GradedActivity getHighest()
{
double highest = grades[0].getScore();
for(int i = 0; i < grades.length; i++)
{
if(grades[i].getScore() > highest)
{
highest = grades[i].getScore();
}
}
//Created KNW_GradedActivity object to bring the highest number
//into the setScore method so it can be a double
KNW_GradedActivity gaH = new KNW_GradedActivity();
gaH.setScore(highest);
return gaH;
}
public KNW_GradedActivity getLowest()
{
double lowest = grades[0].getScore();
for(int i = 0; i < grades.length; i++)
{
if(grades[i].getScore() > lowest)
{
lowest = grades[i].getScore();
}
}
//Samething here, just doing it for the lowest number
KNW_GradedActivity gaL = new KNW_GradedActivity();
gaL.setScore(lowest);
return gaL;
}
}
KNW_Analyzable Class:
public interface KNW_Analyzable
{
double getAverage();
KNW_GradedActivity getHighest();
KNW_GradedActivity getLowest();
}
KNW_GradedActivity Class:
public class KNW_GradedActivity
{
private double score; // Numeric score
/**
The setScore method sets the score field.
#param s The value to store in score.
*/
public void setScore(double s)
{
score = s;
}
/**
The getScore method returns the score.
#return The value stored in the score field.
*/
public double getScore()
{
return score;
}
/**
The getGrade method returns a letter grade
determined from the score field.
#return The letter grade.
*/
public char getGrade()
{
char letterGrade;
if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
And here is the output I get. I will just show the problem I am facing, won't show all of the output.
Output:
Average: 0.0
Highest: KNW_GradedActivity#565d3fa7
Lowest: KNW_GradedActivity#655c4395>
I have been trying for about a week to figure this out and find what is happening, and I can't seem to find it.
Notice that in the return of your methods, you are actually returning gaH and gaL, both of which are KNW_GradedActivity Objects. If you want to see the score, you should probably return gaH.getScore() and gaL.getScore().
Why not to use Stream and DoubleSummaryStatistics to get the stats? Let the api do all the calculations:
List<KNW_GradedActivity> grades = Arrays.asList(new KNW_GradedActivity(), new KNW_GradedActivity(), new KNW_GradedActivity());
grades.get(0).setScore(91);
grades.get(1).setScore(81);
grades.get(2).setScore(71);
DoubleSummaryStatistics statistics = grades.stream().
collect(Collectors.summarizingDouble(KNW_GradedActivity::getScore));
System.out.println(statistics);
// DoubleSummaryStatistics{count=3, sum=243.000000, min=71.000000, average=81.000000, max=91.000000}
Another alternative is to override toString method in your KNW_GradedActivity class:
#Override
public String toString() {
return "{" +
"score=" + score +
", grade='" + getGrade() + '\'' +
'}';
}
This way you dont have to change your actual print statement and will have a nice output combining your score and grade. It will look like:
{score=90.0, grade='A'}

Getter and setter methods and reading a txt file

Esentially, I'm reading a text file in my driver class, but I'm calling a methods from another class with instructions to read from the text file from another class, but it just isn't working. This is what I have so far in my driver class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CourseGrade {
static int numberOfStudents;
private int numberOfAssignAndLabs;
private int numberOfTestsAndQuiz;
private static int patZyanteAttend = 3;
static int patZyanteAttendScore = 0;
public void setNumberOfAssignAndLabs() {
numberOfAssignAndLabs = 21;
}
public int getNumberOfAssignAndLabs() {
return numberOfAssignAndLabs;
}
public void setNumberOfTestsAndQuiz() {
numberOfTestsAndQuiz = 4;
}
public int getNumberOfTestsAndQuiz() {
return numberOfTestsAndQuiz;
}
public static void main(String[] args) {
Student myStudent = new Student();
Scanner scanner = new Scanner(System.in);
try {
scanner = new Scanner(new File("grades.txt"));
} catch (FileNotFoundException e) {
System.out
.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
System.exit(0);
}
System.out.println("Name" + "\t" + "\t" + "Assignment Score" + "\t"
+ "Test Score");
numberOfStudents = scanner.nextInt();
for (int i = 0; i < numberOfStudents; i++) {
myStudent.setFirstName();
System.out.print(myStudent.getFirstName() + " ");
myStudent.setLastName();
System.out.print(myStudent.getLastName());
myStudent.setHomeworkScore();
System.out.print(myStudent.getHomeworkScore() + " ");
myStudent.setTestScore();
System.out.print(myStudent.getTestScore());
myStudent.computeGrade();
System.out.println(myStudent.getGrade());
for (int l = 0; l < patZyanteAttend; l++) {
int temp;
temp = scanner.nextInt();
patZyanteAttendScore += temp;
}
}
}
}
and this is the class I'm calling:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Student {
private String firstName;
private String lastName;
private String letterGrade;
private int homeworkScore;
private int testScore;
CourseGrade myCourseGrade = new CourseGrade();
Scanner scanner = new Scanner(System.in);
public Student() {
homeworkScore = 0;
testScore = 0;
}
public void setFirstName() {
firstName = scanner.next();
}
public String getFirstName() {
return firstName;
}
public void setLastName() {
lastName = scanner.next();
}
public String getLastName() {
return lastName;
}
public void setHomeworkScore() {
int temp;
for (int j = 0; j < myCourseGrade.getNumberOfAssignAndLabs(); j++) {
temp = scanner.nextInt();
homeworkScore += temp;
}
}
public int getHomeworkScore() {
return homeworkScore;
}
public void setTestScore() {
int temp;
for (int k = 0; k < myCourseGrade.getNumberOfTestsAndQuiz(); k++) {
temp = scanner.nextInt();
testScore += temp;
}
}
public int getTestScore() {
return testScore;
}
public void computeGrade() {
if (homeworkScore <= 599 || testScore <= 149 || homeworkScore <= 719
&& testScore <= 179 || homeworkScore <= 779 && testScore <= 164
|| homeworkScore <= 659 && testScore <= 209) {
letterGrade = "P";
}
if (homeworkScore >= 1140 && testScore >= 180 || homeworkScore >= 1080
&& testScore >= 195 || homeworkScore >= 960 && testScore >= 210
|| homeworkScore >= 900 && testScore >= 225
|| homeworkScore >= 840 && testScore >= 240
|| homeworkScore >= 780 && testScore >= 255
|| homeworkScore >= 720 && testScore >= 285) {
letterGrade = "G";
} else {
letterGrade = "A";
}
}
public String getGrade() {
return letterGrade;
}
}
For whatever reason, the program terminates when I get inside the for loop
Your setters should take a parameter, so it knows what value to set
for example, in Student:
public void setFirstName(String fName) {
firstName = fName;
}
sets the field firstname in the Student class, from the value of the fName method parameter.
When you call the method from main, include the forename from the file you are reading:
myStudent.setFirstName(scanner.next());
You don't need a scanner in your Student class.
Change all your setters to take a parameter, and use the Scanner in main to supply the values.

Finding the two highest scores using file input

Since we haven't covered arrays and lists yet, he said to stick to loops and if statements.
I can't seem to figure out how to make it display the names of the two highest scores from the text file. Also I can't figure out how to make it display two scores that are identical. Here is what I've done so far:
package lab06;
import java.util.*;
import java.io.*;
public class Lab06 {
public static void main(String[] args) throws Exception {
Scanner lab06txt = new Scanner(new File("Lab06.txt"));
Scanner duplicateScanner = new Scanner(new File("Lab06.txt"));
int totalstudents = 0;
int grade = 0;
int grade2 = 0;
int record = 0;
int Highest = 0;
int Highest2 = 0;
int ACounter = 0;
int BCounter = 0;
int CCounter = 0;
int DCounter = 0;
int FCounter = 0;
double average = 0;
String lastName = "";
String lastNameHigh = "";
String lastNameHigh2 = "";
String firstName = "";
String firstNameHigh = "";
String firstNameHigh2 = "";
while (lab06txt.hasNext()){
record ++;
totalstudents++;
lastName = lab06txt.next();
firstName = lab06txt.next();
grade = lab06txt.nextInt();
{
average += grade;
if (grade >= Highest){
Highest = grade;
firstNameHigh = firstName;
lastNameHigh = lastName;
}
}
{
if ((grade >= 90) && (grade <= 100))
{
ACounter++;
}
if ((grade >= 80) && (grade <= 89))
{
BCounter++;
}
if ((grade >= 70) && (grade <= 79))
{
CCounter++;
}
if ((grade >= 60) && (grade <= 69))
{
DCounter++;
}
if ((grade < 60))
{
FCounter++;
}
if ((grade < 0) || (grade > 100))
{
System.out.print("Score is out of bounds in record " + record + ": " + lastName + " "+ firstName + " " + grade + ".\nProgram ending\n");
return;
}
}
}
while(lab06txt.hasNext())
{
lastName = lab06txt.next();
firstName = lab06txt.next();
grade2 = lab06txt.nextInt();
if(grade2 > Highest2 && grade2 < Highest){
Highest2 = grade2;
firstNameHigh2 = firstName;
lastNameHigh2 = lastName;
}
}
System.out.println("Total students in class: " +totalstudents);
System.out.println("Class average: " + average/totalstudents);
System.out.println("Grade Counters: ");
System.out.println("A's B's C's D's F's");
System.out.printf(ACounter + "%7d %7d %8d %7d\n", BCounter,CCounter,DCounter,FCounter);
System.out.println("");
System.out.println("Top Two Students: \n");
System.out.printf(lastNameHigh + " " + firstNameHigh + "%15d \n", Highest);
System.out.printf(lastNameHigh2 + " " + firstNameHigh2 + "%15d\n", Highest2);
}
}
The two parts of your code where you have:
firstName = firstNameHigh;
lastName = lastNameHigh;
should be:
firstNameHigh = firstName;
lastNameHigh = lastName;

Categories

Resources