I have used two approaches to solve the Question Here, Both Works But what i need to know is Which will be the Best to do Should I create an Array of Object or Should I create data members as an array ?
// Write a program in java to input and display the details of n number of students having roll, name and cgpa as data members. Also display the name of the student having lowest cgpa.
import java.util.Scanner;
public class _3 {
int n;
int roll[];
String name[];
float cgpa[];
_3(int n, int roll[], String name[], float cgpa[]) {
this.roll = new int[n];
this.name = new String[n];
this.cgpa = new float[n];
this.n = n;
this.roll = roll;
this.name = name;
this.cgpa = cgpa;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the No. of Students to store data of: ");
int num = sc.nextInt();
int roll[] = new int[num];
String name[] = new String[num];
float cgpa[] = new float[num];
for (int i = 0; i < num; i++) {
System.out.println("Enter the Detail of Student : " + i);
sc.nextLine();
System.out.print("Name: ");
name[i] = sc.nextLine();
System.out.print("Roll: ");
roll[i] = sc.nextInt();
System.out.print("CGPA: ");
cgpa[i] = sc.nextFloat();
}
_3 obj = new _3(num, roll, name, cgpa);
System.out.println("
");
System.out.println("Student Details are: ");
for (int i = 0; i < num; i++) {
obj.display(i, name[i], roll[i], cgpa[i]);
}
sc.close();
int min = obj.lowest(cgpa);
System.out.println("
The Student with Lowest CGPA is: ");
System.out.println("Name: " + name[min] + "
Roll: " + roll[min] + "
CGPA: " + cgpa[min]);
}
void display(int i, String name, int roll, float cgpa) {
System.out.println("Detail of Student: " + i);
System.out.println("Name: " + name + "
Roll: " + roll + "
CGPA: " + cgpa);
}
int lowest(float arr[]) {
int min = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < arr[min]) {
min = i;
}
}
return min;
}
}
在此,我设立了数据成员,作为阿雷拉。
//// Write a program in java to input and display the details of n number of students having roll, name and cgpa as data members. Also display the name of the student having lowest cgpa.
import java.util.Scanner;
public class Student_3 {
int roll;
String name;
float cgpa;
Student_3(int roll, String name, float cgpa) {
this.roll = roll;
this.name = name;
this.cgpa = cgpa;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the No. Of Students to store the Data of: ");
int num = sc.nextInt();
Student_3 student[] = new Student_3[num];
int roll;
String name;
float cgpa;
for (int i = 0; i < num; i++) {
System.out.println("Enter the Details of Student No: " + (i + 1));
System.out.print("Name: ");
sc.nextLine();
name = sc.nextLine();
System.out.print("Roll No.: ");
roll = sc.nextInt();
System.out.print("CGPA: ");
cgpa = sc.nextFloat();
student[i] = new Student_3(roll, name, cgpa);
}
sc.close();
// Displaying the Detail of Students
System.out.println();
for (int i = 0; i < num; i++) {
student[i].display(i, student[i]);
}
// Lowest CGPA
student[0].lowest_cgpa(student);
}
void display(int i, Student_3 student) {
System.out.println("Details of Student No: " + (i + 1));
System.out.println("Name: " + name + "
Roll: " + student.roll + "
CGPA: " + student.cgpa);
}
void lowest_cgpa(Student_3 student[]) {
int min = 0;
for (int i = 0; i < student.length; i++) {
if (student[i].cgpa < student[min].cgpa) {
min = i;
}
}
System.out.println("The Student Details of lowest CGPA Holder is:");
display(min, student[min]);
}
}
Here, I have created Array of Object Both program runs fine but Which will be the Best Approach to do ???