0% found this document useful (0 votes)
2 views

Array

The document contains a Java program that defines a Student class with attributes such as id, name, part, and cgpa, along with methods for setting and retrieving these values. The ArrayStudentApp class allows users to input data for five students, displays their information, and calculates the best student based on cgpa, the number of students in part 4, and those with a cgpa of 3.0 and above. The program utilizes JOptionPane for input and prints results to the console.

Uploaded by

Siti Nur Ariffa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array

The document contains a Java program that defines a Student class with attributes such as id, name, part, and cgpa, along with methods for setting and retrieving these values. The ArrayStudentApp class allows users to input data for five students, displays their information, and calculates the best student based on cgpa, the number of students in part 4, and those with a cgpa of 3.0 and above. The program utilizes JOptionPane for input and prints results to the console.

Uploaded by

Siti Nur Ariffa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXAMPLE (ARRAY)

import javax.swing.JOptionPane;

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 ArrayStudentApp
{
public static void main(String [] args)
{
Student [] StdArray = new Student[5];

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); to input 5 students into the array
int iPart = Integer.parseInt(sPart);
double dCgpa = Double.parseDouble(sCgpa);
StdArray[i] = new Student(iIdStd, nameStd, iPart, dCgpa);
}

for (int i=0; i<5; i++)


{
to display all the students in the array
System.out.println(StdArray[i].toString());
}

int part4 = 0, scorer = 0, ind = 0;


double cg = 0.0;
for (int j=0; j<5; j++)
{
if (StdArray[j].getCgpa() > cg) to find the best student
{ cg = StdArray[j].getCgpa();
ind = j;
}

if ( StdArray[j].getPart() == 4) to count the number of students in part4


part4++;

if (StdArray[j].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 :");


System.out.println(StdArray[ind].toString());
System.out.println("There are " + part4 + " part 4 students");
System.out.println("There are " + scorer + " students whose cgpa 3.0 and
above");

} // main
} // ArrayStudentApp

You might also like