0% 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.

Uploaded by

Siti Nur Ariffa
Copyright
© © All Rights Reserved
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% 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.

Uploaded by

Siti Nur Ariffa
Copyright
© © All Rights Reserved
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
You are on page 1/ 2

EXAMPLE (ARRAYLIST)

Example of ArrayList with Object Student.

import javax.swing.JOptionPane;
import java.util.ArrayList;

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)


{
this.id = id;
this.name = name;
this.part = part;
this.cgpa = 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 = 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");

} // main
} // ArrayListApp

You might also like