EXAMPLE (ARRAYLIST)
Example of ArrayList with Object Student.
import [Link];
import [Link];
public class Student
{
private int id;
private String name;
private int part;
private double cgpa;
public Student()
{
id = -1;
name = "";
part = -1;
cgpa = -1.0;
}
public Student(int id, String name, int part, double cgpa)
{
[Link] = id;
[Link] = name;
[Link] = part;
[Link] = cgpa;
}
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 = [Link]("Enter student id");
String nameStd = [Link]("Enter name");
String sPart = [Link]("Enter part");
String sCgpa = [Link]("Enter cgpa");
to input 5 students into the array
int iIdStd = [Link](sIdStd);
int iPart = [Link](sPart);
double dCgpa = [Link](sCgpa);
Student stud = new Student(iIdStd, nameStd, iPart, dCgpa);
[Link](stud);
}
Student S = null; to display all the students in the arrayList
for (int i=0; i<[Link](); i++)
{
S = (Student) [Link](i);
[Link]([Link]());
}
int part4 = 0, scorer = 0, ind = 0;
double cg = 0.0;
for (int j=0; j<[Link](); j++)
{
S = (Student) [Link](j);
if ([Link]() > cg) to find the best student
{ cg = [Link]();
ind = j;
}
to count the number of students in part4
if ( [Link]() == 4)
part4++;
if ([Link]() > 3.00)
scorer++; to count the number of students who score cgpa 3.00
} and above in part4
[Link]("The best student is :");
S = (Student) [Link](ind);
[Link]([Link]());
[Link]("There are " + part4 + " part 4 students");
[Link]("There are " + scorer + " students whose cgpa 3.0 and above");
} // main
} // ArrayListApp