Create a student class with data members: name, roll number, city, total marks, Include get and display function and 3 queries functions. Implement the class by storing records of at least 3 students in a link list and solve the following queries:
- Display details of students belonging to a particular city.
- Count the number of students whose name starts with a particular letter.
- Display students’ details scoring the highest marks.
General code works but functions create exceptions with iterator such as no such element found or index out of bounds.
Code So Far:
import java.util.*;
class Student{
String name, rollnumber, city;
double totalmarks;
Scanner sc = new Scanner(System.in);
LinkedList <Student> link = new LinkedList<Student>();
void get(){
System.out.println("Enter the Name of Student");
name = sc.next();
System.out.println("Enter the Roll Number of Student");
rollnumber = sc.next();
System.out.println("Enter the City of Student");
city = sc.next();
System.out.println("Enter the Total Number of Student");
totalmarks = sc.nextDouble();
}
void display(){
System.out.println("Name: "+name+"\t"+"Roll Number: "+rollnumber+"\t"+"City: "+city+"\t"+"Total Marks: "+totalmarks);
}
} class Query extends Student{
Student s = new Student();
Student m = new Student();
String city_search, city_temp;
double marks;
int count = 0;
char str1, str2;
void city_query(){
System.out.println("Enter the city");
city_search = sc.next();
Iterator <Student> it = link.iterator();
while (it.hasNext()){
s = it.next();
if(s.city == city_search){
s.display();
}
}
}
void first_alphabet(){
System.out.println("Enter the letter");
str1 = sc.next().charAt(0);
Iterator <Student> it = link.iterator();
while (it.hasNext()){
s = it.next();
str2 = s.name.charAt(0);
if(str1 == str2){
s.display();
}
}
}
void highest_marks(){
Iterator <Student> it = link.iterator();
s = it.next();
m = s;
marks = s.totalmarks;
while (it.hasNext()){
s = it.next();
if (marks < s.totalmarks){
marks = s.totalmarks;
m = s;
}
}
m.display();
}
} public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
Student st[] = new Student[10];
Student s = new Student();
Student s2 = new Student();
Query q = new Query();
int i = 0,n;
System.out.println("Enter the number of records to enter");
n = sc.nextInt();
for (int j = 0; j<n; j++){
st[j] = new Student();
st[j].get();
s.link.add(st[j]);
}
Iterator <Student> it = s.link.iterator();
while (it.hasNext()){
s2 = it.next();
s2.display();
}
q.city_query();
q.first_alphabet();
q.highest_marks();
}
}