Java. Use ArrayLists. Write a program that allows the user to enter and then sea
ID: 3705343 • Letter: J
Question
Java. Use ArrayLists.
Write a program that allows the user to enter and then search a list of student names and grades. Your program will execute in two phases. In the first phase, the user will enter student name and grade pairs. In the second phase, the user will search this data for matching parts of names, or matching grades. Each name and grade pair must be stored in an object of type Student. You will need to write the student class. (Remember that each class is stored in its own file.) The Student class should have private instance variables to store a single name (a String) and a single grade (a char). In addition, it should have methods with the following signarures. public Student (String aName, char aGrade] public String getName () public char getGrade () Your class should have no other public mcthods, and it mast hanve proper Jazadoc documeniation. In the first phase, your program will prompt the user for the number of name and grade pairs. Note that, unlike in project 3, you should not prompt the user for the number of students. Create a new Student object for each pair, and then store this object in an ArrayList. In the second phase, the user will enter a series of queries. There are two types of queries: a name search indicated by the String "name" followed by another String representing part of a name, and a grade search, indicated by the String "grade" followed by another String representing the grade. . For each query, your program should print out all information for any student that matches that particular queryExplanation / Answer
StudentTest.java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Student> list = new ArrayList<Student>();
System.out.println("Enter the name and grade pair(-1 to quit): ");
String name = scan.next();
while(!name.equals("-1")) {
char grade = scan.next().charAt(0);
Student s = new Student(name, grade);
list.add(s);
System.out.println("Enter the name and grade pair(-1 to quit): ");
name = scan.next();
}
System.out.println("Enter the name to be searched: ");
name = scan.next();
for(Student s: list) {
if(s.getName().equals(name)) {
System.out.println(s);
}
}
System.out.println("Enter the grade to be searched: ");
char grade = scan.next().charAt(0);
for(Student s: list) {
if(s.getGrade()==grade) {
System.out.println(s);
}
}
}
}
Student.java
public class Student {
private String name;
private char grade;
public Student(String name, char grade) {
this.name = name;
this.grade=grade;
}
public String getName() {
return name;
}
public char getGrade() {
return grade;
}
public String toString() {
return "Student [grade=" + grade + ", name=" + name + "]";
}
}
Output:
Enter the name and grade pair(-1 to quit):
Suresh A
Enter the name and grade pair(-1 to quit):
Sekhar B
Enter the name and grade pair(-1 to quit):
Anshu A
Enter the name and grade pair(-1 to quit):
Revathi C
Enter the name and grade pair(-1 to quit):
-1
Enter the name to be searched:
Anshu
Student [grade=A, name=Anshu]
Enter the grade to be searched:
A
Student [grade=A, name=Suresh]
Student [grade=A, name=Anshu]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.