Need help with last part of this java. I can get everything except recognizing t
ID: 3700606 • Letter: N
Question
Need help with last part of this java. I can get everything except recognizing the name at the end. I posted the requirements and both java classes.
import java.util. Scanner;
import java.io.*;
import java.util.ArrayList;
public class StudentsQuiz2 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<StudentsQuiz> stuList = new ArrayList<StudentsQuiz>();
System.out.println("STUDENTS ROOSTER AND QUIZ AVERAGES");//Print title of rooster and quizzes
System.out.println("");//Print blank space
generateStuList(stuList);//Generate the array list
StudentsQuiz stQuiz = stuList.get(0); // Storing the returned object by stuList.get(0) in stQuiz object of type StudentsQuiz as initially nIndex was zero when this line was getting printed repeatedly
System.out.println("Students in Class: " + stQuiz.getNumberStudents());// Printing count of students just once
System.out.println("");//Print blank space
for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
printStudentsQuiz(stuList.get(nIndex));//Initilize the for loop for list
}//End of for loop
Scanner input = new Scanner(System.in);//Reads info from user
System.out.print("Please enter the last name of a specific student ");
String sSpecificStudent = input.next().toUpperCase();
boolean bPrint = false;
for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
if (stuList.get(nIndex).getLastName()==sSpecificStudent){
printStudentsQuiz(stuList.get(nIndex));
bPrint= true;
}
}
if (bPrint ==false) {
System.out.println("No student with name " + sSpecificStudent + "found");
}
}//End main method
public static void printStudentsQuiz(StudentsQuiz myStudentsQuiz){
System.out.println("Student Name: "+ myStudentsQuiz.getFirstName() + " " + myStudentsQuiz.sLastName.toUpperCase());//Print out first and last name of students from list
System.out.println("Quiz Average: "+ myStudentsQuiz.calculateQuizAverage());//Print out quiz averages for each student
System.out.println("");//Print blank space
}//End printStudentsQuiz method
public static void generateStuList(ArrayList<StudentsQuiz > list) throws FileNotFoundException {
//Read input from file
String sFileName = "Students.txt";
String sInputLine = "";
File fileToOpen = new File(sFileName); //Creating wrapper class for the file name and its directory path
Scanner inputFile = new Scanner(fileToOpen);//Creating scanner object for reading inputs
String[] saTokens = null;
//Initialize the while loop
while (inputFile.hasNext()){
sInputLine = inputFile.nextLine();
saTokens = sInputLine.split("-");
StudentsQuiz stu = new StudentsQuiz();
stu.setFirstName(saTokens[0]);
stu.setLastName(saTokens[1]);
stu.setQuiz1(Integer.parseInt(saTokens[2]));
stu.setQuiz2(Integer.parseInt(saTokens[3]));
stu.setQuiz3(Integer.parseInt(saTokens[4]));
list.add(stu);
}//End while loop and read all lines from the file
}//End generateStuList method
}//End of StudentsQuiz2 class
public class StudentsQuiz {
//This class stores information about the student and quiz grades
//Declare class attributes
private String sFirstName = ""; //First name of student
String sLastName = ""; //Last name of student
private int nQuiz1 = 0; //Grade for Quiz 1
private int nQuiz2 = 0; //Grade for Quiz 2
private int nQuiz3 = 0; //Grade for Quiz 3
private static int nNumberStudents = 0; //Number of students
//Declare constants
public static final int QUIZZES = 3; //Number of quiz grades
//Default constructor for Student
public StudentsQuiz() {
//Update number of students created
nNumberStudents++;
} //end default constructor
//Overloaded constructor for Student
public StudentsQuiz(String sFName, String sLName, int nQ1, int nQ2, int nQ3) {
//Assign parameter values to class attributes
sFirstName = sFName;
sLastName = sLName;
nQuiz1 = nQ1;
nQuiz2 = nQ2;
nQuiz3 = nQ3;
//Update number of students created
nNumberStudents++;
} //end overloaded constructor
//Accessor for First Name
public String getFirstName() {
return sFirstName;
} //end accessor for First Name
//Mutator for First Name
public void setFirstName(String sFstName) {
sFirstName = sFstName;
} //end mutator for First Name
//Accessor for Last Name
public String getLastName() {
return sLastName;
} //end accessor for Last Name
//Mutator for Last Name
public void setLastName(String sLstName) {
sLastName = sLstName;
} //end mutator for Last Name
//Accessor for Quiz 1 grade
public int getQuiz1() {
return nQuiz1;
} //end accessor for Quiz 1
//Mutator for Quiz 1 grade
public void setQuiz1(int nQz1) {
nQuiz1 = nQz1;
} //end mutator for Quiz 1
//Accessor for Quiz 2 grade
public int getQuiz2() {
return nQuiz2;
} //end accessor for Quiz 2
//Mutator for Quiz 2 grade
public void setQuiz2(int nQz2) {
nQuiz2 = nQz2;
} //end mutator for Quiz 2
//Accessor for Quiz 3 grade
public int getQuiz3() {
return nQuiz3;
} //end accessor for Quiz 3
//Mutator for Quiz 3 grade
public void setQuiz3(int nQz3) {
nQuiz3 = nQz3;
} //end mutator for Quiz 3
//Accessor for number of students
public static int getNumberStudents() {
return nNumberStudents;
} //end accessor for number of students
//Calculates the average of the three quiz grades for the student
public double calculateQuizAverage() {
double dAverage = 0.0; //Local variable to store quiz average
//Calculate quiz average
dAverage = (nQuiz1 + nQuiz2 + nQuiz3)/QUIZZES;
return dAverage;
} //end method calculateQuizAverage
} //end class StudentsQuiz
Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output.
Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by the user when the program runs (and therefore can change each time the program runs).
Scenario 1: User searches for a student who exists. STUDENT ROSTER AND QUIZ AVERAGES
Please enter the last name of a specific student: buchwald Student Name: Drew BUCHWALD
Quiz average: 73.0
1
Scenario 2: User searches for a student who does not exist. STUDENT ROSTER AND QUIZ AVERAGES
Students in class: 10
Student Name: Leann ROSENBERRY
Please enter the last name of a specific student: Smith No student with name Smith found.
Explanation / Answer
Note: As u didnt provided the Students.txt file i created a dummy file.U may replace with ur file.Thank you
_________________
Students.txt
Leann-ROSENBERRY-78-78-89
Gia-WALCK-65-78-88
Archie-SELLS-89-84-72
Giuseppe-DIMARTINO-84-82-86
Lizabeth-GOAD-90-94-81
Paul-ELLINGTON-84-72-60
Eusebia-TONGUE-73-76-89
Drew-BUCHWALD-89-87-63
Lyndsey-PACK-67-78-82
Arica-BARRETT-84-62-89
_______________
StudentsQuiz.java
public class StudentsQuiz {
//This class stores information about the student and quiz grades
//Declare class attributes
private String sFirstName = ""; //First name of student
String sLastName = ""; //Last name of student
private int nQuiz1 = 0; //Grade for Quiz 1
private int nQuiz2 = 0; //Grade for Quiz 2
private int nQuiz3 = 0; //Grade for Quiz 3
private static int nNumberStudents = 0; //Number of students
//Declare constants
public static final int QUIZZES = 3; //Number of quiz grades
//Default constructor for Student
public StudentsQuiz() {
//Update number of students created
nNumberStudents++;
} //end default constructor
//Overloaded constructor for Student
public StudentsQuiz(String sFName, String sLName, int nQ1, int nQ2, int nQ3) {
//Assign parameter values to class attributes
sFirstName = sFName;
sLastName = sLName;
nQuiz1 = nQ1;
nQuiz2 = nQ2;
nQuiz3 = nQ3;
//Update number of students created
nNumberStudents++;
} //end overloaded constructor
//Accessor for First Name
public String getFirstName() {
return sFirstName;
} //end accessor for First Name
//Mutator for First Name
public void setFirstName(String sFstName) {
sFirstName = sFstName;
} //end mutator for First Name
//Accessor for Last Name
public String getLastName() {
return sLastName;
} //end accessor for Last Name
//Mutator for Last Name
public void setLastName(String sLstName) {
sLastName = sLstName;
} //end mutator for Last Name
//Accessor for Quiz 1 grade
public int getQuiz1() {
return nQuiz1;
} //end accessor for Quiz 1
//Mutator for Quiz 1 grade
public void setQuiz1(int nQz1) {
nQuiz1 = nQz1;
} //end mutator for Quiz 1
//Accessor for Quiz 2 grade
public int getQuiz2() {
return nQuiz2;
} //end accessor for Quiz 2
//Mutator for Quiz 2 grade
public void setQuiz2(int nQz2) {
nQuiz2 = nQz2;
} //end mutator for Quiz 2
//Accessor for Quiz 3 grade
public int getQuiz3() {
return nQuiz3;
} //end accessor for Quiz 3
//Mutator for Quiz 3 grade
public void setQuiz3(int nQz3) {
nQuiz3 = nQz3;
} //end mutator for Quiz 3
//Accessor for number of students
public static int getNumberStudents() {
return nNumberStudents;
} //end accessor for number of students
//Calculates the average of the three quiz grades for the student
public double calculateQuizAverage() {
double dAverage = 0.0; //Local variable to store quiz average
//Calculate quiz average
dAverage = (nQuiz1 + nQuiz2 + nQuiz3)/QUIZZES;
return dAverage;
} //end method calculateQuizAverage
} //end class StudentsQuiz
________________
QuizAverage
package org.students;
import java.util. Scanner;
import java.io.*;
import java.util.ArrayList;
public class QuizAverage {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<StudentsQuiz> stuList = new ArrayList<StudentsQuiz>();
System.out.println("STUDENTS ROOSTER AND QUIZ AVERAGES");//Print title of rooster and quizzes
System.out.println("");//Print blank space
generateStuList(stuList);//Generate the array list
StudentsQuiz stQuiz = stuList.get(0); // Storing the returned object by stuList.get(0) in stQuiz object of type StudentsQuiz as initially nIndex was zero when this line was getting printed repeatedly
System.out.println("Students in Class: " + stQuiz.getNumberStudents());// Printing count of students just once
System.out.println("");//Print blank space
for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
printStudentsQuiz(stuList.get(nIndex));//Initilize the for loop for list
}//End of for loop
Scanner input = new Scanner(System.in);//Reads info from user
System.out.print("Please enter the last name of a specific student :");
String sSpecificStudent = input.next().toUpperCase();
boolean bPrint = false;
for (int nIndex = 0; nIndex < stuList.size(); nIndex++) {
if (stuList.get(nIndex).getLastName().equalsIgnoreCase(sSpecificStudent))
{
printStudentsQuiz(stuList.get(nIndex));
bPrint= true;
}
}
if (bPrint ==false) {
System.out.println("No student with name " + sSpecificStudent + " found");
}
}//End main method
public static void printStudentsQuiz(StudentsQuiz myStudentsQuiz){
System.out.println("Student Name: "+ myStudentsQuiz.getFirstName() + " " + myStudentsQuiz.sLastName.toUpperCase());//Print out first and last name of students from list
System.out.println("Quiz Average: "+ myStudentsQuiz.calculateQuizAverage());//Print out quiz averages for each student
System.out.println("");//Print blank space
}//End printStudentsQuiz method
public static void generateStuList(ArrayList<StudentsQuiz > list) throws FileNotFoundException {
//Read input from file
String sFileName = "Students.txt";
String sInputLine = "";
File fileToOpen = new File(sFileName); //Creating wrapper class for the file name and its directory path
Scanner inputFile = new Scanner(fileToOpen);//Creating scanner object for reading inputs
String[] saTokens = null;
//Initialize the while loop
while (inputFile.hasNext()){
sInputLine = inputFile.nextLine();
saTokens = sInputLine.split("-");
StudentsQuiz stu = new StudentsQuiz();
stu.setFirstName(saTokens[0]);
stu.setLastName(saTokens[1]);
stu.setQuiz1(Integer.parseInt(saTokens[2]));
stu.setQuiz2(Integer.parseInt(saTokens[3]));
stu.setQuiz3(Integer.parseInt(saTokens[4]));
list.add(stu);
}//End while loop and read all lines from the file
}//End generateStuList method
}//End of StudentsQuiz2 class
________________
Output:
STUDENTS ROOSTER AND QUIZ AVERAGES
Students in Class: 10
Student Name: Leann ROSENBERRY
Quiz Average: 81.0
Student Name: Gia WALCK
Quiz Average: 77.0
Student Name: Archie SELLS
Quiz Average: 81.0
Student Name: Giuseppe DIMARTINO
Quiz Average: 84.0
Student Name: Lizabeth GOAD
Quiz Average: 88.0
Student Name: Paul ELLINGTON
Quiz Average: 72.0
Student Name: Eusebia TONGUE
Quiz Average: 79.0
Student Name: Drew BUCHWALD
Quiz Average: 79.0
Student Name: Lyndsey PACK
Quiz Average: 75.0
Student Name: Arica BARRETT
Quiz Average: 78.0
Please enter the last name of a specific student :buchwald
Student Name: Drew BUCHWALD
Quiz Average: 79.0
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.