0% found this document useful (0 votes)
68 views3 pages

Example of Array List Using Object Stud With Data Type Student

This document demonstrates using an ArrayList to store Student objects. It defines a Student class with ID, name, part, and GPA attributes. The main method creates an ArrayList to store 3 Student objects input from the user. It then displays the details of all students, finds the student with the highest GPA, and counts the number of students in part 4.

Uploaded by

Nurul Ainni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

Example of Array List Using Object Stud With Data Type Student

This document demonstrates using an ArrayList to store Student objects. It defines a Student class with ID, name, part, and GPA attributes. The main method creates an ArrayList to store 3 Student objects input from the user. It then displays the details of all students, finds the student with the highest GPA, and counts the number of students in part 4.

Uploaded by

Nurul Ainni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Example of Array List using object stud with data type Student

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("\nID:"+ID+"\nName:"+name+"\nPart:"+part+"\nCGPA:"+cgpa);}
}

1
import java.util.*;
public class StudAppArrList
{
public static void main(String args[])
{
ArrayList<Student> data=new ArrayList<Student>();
Scanner inputText=new Scanner(System.in);
Scanner input=new Scanner(System.in);

int id,part;
String name;
double cgpa;
//enter detail for 3 students
for(int i=0;i<3;i++)
{ System.out.println("Enter ID:");
id=input.nextInt();
System.out.println("Enter name:");
name=input.next();
System.out.println("Enter part:");
part=input.nextInt();
System.out.println("Enter CGPA:");
cgpa=input.nextDouble();
Student stud=new Student(id, name, part,cgpa);
data.add(stud);
}

//display all the students data


System.out.println("Details of all students:");
Student stud=null;
for (int i=0; i<data.size();i++)
{ stud = data.get(i);
System.out.println(stud.toString());
}

//to display detail of the best student


double highCGPA=0;
int index=0;
for(int i=0;i<data.size();i++)
{ stud=data.get(i);
if(stud.getCGPA()>highCGPA)
{ highCGPA=stud.getCGPA();
index=i;
}
}
stud=data.get(index);
System.out.println("Data for highest student:"+stud.toString());

2
//to count the number of students in part 4
int count=0;
for(int i=0;i<data.size();i++)
{ stud=data.get(i);
if(stud.getPart()==4)
count++;
}
System.out.println("There are "+count+" student in part 4");
}
}

You might also like