In the StudentArrayDemo.java, student array accept student object as parameter:
ID: 3808936 • Letter: I
Question
In the StudentArrayDemo.java, student array accept student object as parameter:
find the first student object information
according to s1 object, to find his/her name, id, gpa
if there is given student's name, we need search this name and find this student is in the student array or not
find the highest GPA from student array:
import java.util.*;
public class StudentArrayDemo {
public static void main(String[] args) {
// 1. instantiate several student object with their known name, id and gpa
Student s1 = new Student("John", 11111, 3.68);
Student s2 = new Student("Allen", 22222, 3.57);
Student s3 = new Student("Mary", 33333, 4.0);
Student s4 = new Student("Alex", 44444, 3.57);
// 2. create a student array with initial student object information
Student studentArray [] = {s1,s2,s3,s4};
// 3. output the student array information
for ( int index = 0; index < studentArray.length; index ++ )
{
System.out.println( studentArray [index] );
}
// 4. search student information in the array, and check this fruit is in the list or not
// Q1. find the first student object information
// Q2. according to s1 object, to find his/her name, id, gpa
// Q3. if there is given student's name,
// we need search this name and find this student
// is in the student array or not
Scanner scan = new Scanner(System.in);
System.out.println(" Which student you want to search from student array? ");
String typeName = scan.next();
// Q4. find the highest GPA from student array
}
}
Thank you for your help.
Explanation / Answer
Hi, Please find my code.
import java.util.*;
public class StudentArrayDemo {
public static void main(String[] args) {
// 1. instantiate several student object with their known name, id and gpa
Student s1 = new Student("John", 11111, 3.68);
Student s2 = new Student("Allen", 22222, 3.57);
Student s3 = new Student("Mary", 33333, 4.0);
Student s4 = new Student("Alex", 44444, 3.57);
// 2. create a student array with initial student object information
Student studentArray [] = {s1,s2,s3,s4};
// 3. output the student array information
for ( int index = 0; index < studentArray.length; index ++ )
{
System.out.println( studentArray [index] );
}
// 4. search student information in the array, and check this fruit is in the list or not
// Q1. find the first student object information
System.out.println(studentArray[0]);
// Q2. according to s1 object, to find his/her name, id, gpa
System.out.println("Name: "+s1.getName());
System.out.println("ID: "+s1.getId());
System.out.println("GPA: "+s1.getGpa());
// Q3. if there is given student's name,
// we need search this name and find this student
// is in the student array or not
Scanner scan = new Scanner(System.in);
System.out.println(" Which student you want to search from student array? ");
String typeName = scan.next();
int index = -1;
for(int i=0; i<studentArray.length; i++)
if(studentArray[i].getName().equalsIgnoreCase(typeName)){
index = i;
break;
}
if(index!=-1)
System.out.println(typeName+" is present at index "+index);
else
System.out.println(typeName+" is not available in array");
// Q4. find the highest GPA from student array
index = 0;
for(int i=1; i<studentArray.length; i++)
if(studentArray[i].getGpa() > studentArray[index].getGpa())
index = i;
System.out.println("Student with higest GPA: "+studentArray[index]);
}
}
class Student{
private String name;
private int id;
private double gpa;
public Student(String name, int id, double gpa) {
super();
this.name = name;
this.id = id;
this.gpa = gpa;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getGpa() {
return gpa;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return name+", "+id+", "+gpa;
}
}
/*
Sample run:
John, 11111, 3.68
Allen, 22222, 3.57
Mary, 33333, 4.0
Alex, 44444, 3.57
John, 11111, 3.68
Name: John
ID: 11111
GPA: 3.68
Which student you want to search from student array?
alex
alex is present at index 3
Student with higest GPA: Mary, 33333, 4.0
*/