0% found this document useful (0 votes)
43 views2 pages

Example Arraylist Adt

This document contains code that defines a Student class with attributes like ID, name, part, and CGPA. It also defines an ArrayListApp class that takes student details as input, stores Student objects in an ArrayList, and performs operations like finding the best student and counting students by part and CGPA. The main method gets input for 5 students, adds them to an ArrayList, displays all students, finds the student with the highest CGPA, counts students in part 4, and counts students with CGPA above 3.0 who are in part 4.

Uploaded by

Amirah Rasyiqah
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)
43 views2 pages

Example Arraylist Adt

This document contains code that defines a Student class with attributes like ID, name, part, and CGPA. It also defines an ArrayListApp class that takes student details as input, stores Student objects in an ArrayList, and performs operations like finding the best student and counting students by part and CGPA. The main method gets input for 5 students, adds them to an ArrayList, displays all students, finds the student with the highest CGPA, counts students in part 4, and counts students with CGPA above 3.0 who are in part 4.

Uploaded by

Amirah Rasyiqah
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 ABSTRACT DATA TYPE (ADT) CSC248

1) CLASS DEFINITION

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);
}
}
EXAMPLE: ARRAYLIST ABSTRACT DATA TYPE (ADT) CSC248

2) CLASS APPLICATION

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

public class ArrayListApp


{
public static void main(String [] args)
{
ArrayList theList = new ArrayList();

// a) Get input 5 students details and insert into arraylist named theList
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");
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);
}

// b) Display all students in theList


Student S = null;
for (int i=0; i<theList.size(); i++)
{
S = (Student) theList.get(i);
System.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);

// c) Find the best student


if (S.getCgpa() > cg)
{
cg = S.getCgpa();
ind = j;
}

// d) Count the number of students in part4


if ( S.getPart() == 4)
part4++;

// e) Count the number of students who score cgpa 3.00 and above
// in part4
if (S.getCgpa() > 3.00)
scorer++;
}

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