16. (15 marks) Write the Uti1Student class that includes the followings. A stati
ID: 3709153 • Letter: 1
Question
16. (15 marks) Write the Uti1Student class that includes the followings. A static method readstudent that reads age, gpa, and name from the user, and creates a student object with the three values, and returns the object .A static method findWorstGpa method that accepts an array of Student objects, finds the worst gpa from the array, and returns the worst gpa. A static method searchStudentWithName method that accepts an array of Student object and a name, and returns the index of a Student object in the array, which has the same nameExplanation / Answer
import java.util.Scanner;
// This class is used to create objects throughout the program
class Student {
int age;
float gpa;
String name;
}
public class UtilStudent {
// This method returns an object after getting data from user input
static Student readStudent() {
Scanner in = new Scanner(System.in);
Student std = new Student();
System.out.print("Enter age of student : ");
std.age = in.nextInt();
System.out.print("Enter gpa of student : ");
std.gpa = in.nextFloat();
in.nextLine();
System.out.print("Enter age of student : ");
std.name = in.nextLine();
return std;
}
// This method loops through all objects in the array and returns
// the worst gpa found
static float findWorstGpa(Student[] stuArray) {
float worstGpa = stuArray[0].gpa;
for(int i = 1; i < stuArray.length; i++) {
if(worstGpa < stuArray[i].gpa)
worstGpa = stuArray[i].gpa;
}
return worstGpa;
}
// This method loops through all the object of type Student and looks
// for name field with the same value as the second string name argument
// If it doesn't find a match it returns -1
static int searchStudentWithName(Student[] stuArray, String name) {
int index = -1;
for(int i = 0; i < stuArray.length; i++) {
if (stuArray[i].name.equals(name))
index = i;
}
return index;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.