The document provides an example of using an ArrayList to manage Student objects in Java. It includes a Student class with attributes like id, name, part, and cgpa, along with methods for setting and getting these attributes. The main application collects data for five students, displays their information, identifies the best student based on CGPA, and counts students in part 4 and those with a CGPA of 3.0 or above.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views2 pages
Array List
The document provides an example of using an ArrayList to manage Student objects in Java. It includes a Student class with attributes like id, name, part, and cgpa, along with methods for setting and getting these attributes. The main application collects data for five students, displays their information, identifies the best student based on CGPA, and counts students in part 4 and those with a CGPA of 3.0 or above.
public void setStudent(int i, String n, int p, double c)
{ id = i; name = n; part = p; cgpa = c; }
public int getId()
{ return id; }
public String getName()
{ return name; }
public int getPart()
{ return part; }
public double getCgpa()
{ return cgpa; }
public String toString()
{ return("Id = " + id + " Name = " + name + " Part = " + part + " Cgpa = " + cgpa); } }
public class ArrayListApp
{ public static void main(String [] args) { ArrayList theList = new ArrayList();
// to input 5 students into the list
for (int i=0; i<5; i++) { String sIdStd = JOptionPane.showInputDialog("Enter student id"); String nameStd = JOptionPane.showInputDialog("Enter name"); String sPart = JOptionPane.showInputDialog("Enter part"); String sCgpa = JOptionPane.showInputDialog("Enter cgpa"); to input 5 students into the array int iIdStd = Integer.parseInt(sIdStd); int iPart = Integer.parseInt(sPart); double dCgpa = Double.parseDouble(sCgpa); Student stud = new Student(iIdStd, nameStd, iPart, dCgpa); theList.add(stud); }
Student S = null; to display all the students in the arrayList
for (int i=0; i<theList.size(); i++) { S = (Student) theList.get(i); Sytem.out.println(S.toString()); }
int part4 = 0, scorer = 0, ind = 0;
double cg = 0.0;
for (int j=0; j<theList.size(); j++)
{ S = (Student) theList.get(j); if (S.getCgpa() > cg) to find the best student { cg = S.getCgpa(); ind = j; } to count the number of students in part4 if ( S.getPart() == 4) part4++;
if (S.getCgpa() > 3.00)
scorer++; to count the number of students who score cgpa 3.00 } and above in part4
System.out.println("The best student is :");
S = (Student) theList.get(ind); System.out.println(S.toString()); System.out.println("There are " + part4 + " part 4 students"); System.out.println("There are " + scorer + " students whose cgpa 3.0 and above");