Exercise 3: In this exercise, you will first create a Student class. A Student h
ID: 3726165 • Letter: E
Question
Exercise 3: In this exercise, you will first create a Student class. A Student has two attributes: name (String) and gradePointAverage (double). The class has a constructor that sets the name and grade point average of the Student. It has appropriate get and set methods. As well, there is a toString method that prints the student's name and their grade point average (highest is 4.0) You will then write a demo program. In the demo program, you will create an ArrayList of Students. You will populate the arraylist, This should be done in the main method. with user inputted data. You should store the students alphabetically by name (A to Z) in the The algorithm to sort is simple. As you read in each name for the Student (say namel), compare it with each name (say name2 of a Student) stored in the arraylist starting from the index 0. As soon (namel.compareTo(name2) >0), that is the right index to put namel. Be sure that you do not cross the arraylist boundary In addition, you will write a method in your demo class that will take in your araylist of students and return a new arraylist with the students sorted by their grade point average (highest to lowest). You can apply the same sort algorithm that you used for the names. See below for a sample output. Enter student name or quit: Bush, Suc Enter grade point ac: 3.4 Enter student name or quit: Ncddlc, Ned Enter grade point ac: 3.2 Enter student name or quit: Zhang, Bin Enter grade point ac: 3.7 Enter student name or quit. Adda Ida Enter grade point ac: 3.9 Enter student name or quit: quit Students: I Adda, Ida Grade Point Avc: 3.9, Bush, Sue Grade Point Avc: 3.4, Ncddls, Ned Grade Point Ave: 3.2, Zhang, Bin Grade Point Ave: 3.7] Students in order of Grade Point Average: I Adda, Ida Grade Point Ave: 3.9_Zhang, Bin Grade Point Ave: 3.7, Bush. Sue Grade Point Ave: 3.4, Ncddle, Ned Grade Point Ave: 3.2]Explanation / Answer
Student.java
public class Student {
//Declaring instance variables
private String name;
private double gradePointAverage;
//Parameterized constructor
public Student(String n , double a){
name = n;
gradePointAverage = a;
}
// getters and setters
public String getName(){
return name;
}
public double getGradePointAverage(){
return gradePointAverage;
}
public void setName(String n){
name = n;
}
public void setGradePointAverage(double a){
gradePointAverage = a;
}
}
____________________
StudentDemo.java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDemo{
public static ArrayList<Student> sort(ArrayList<Student> list){
//Creating an ArrayList Which holds Student Objects
ArrayList<Student> list1 = new ArrayList<Student>();
/* This nested for loop will sort the ArrayList which holds
* Students based on Student grade Point in Ascending order
*/
for (int i = 0; i<list.size(); i++){
int index = -1;
for (int j = 0; j< list1.size(); j++){
if (list.get(i).getGradePointAverage() > list1.get(j).getGradePointAverage()){
index = j;
list1.add(j,list.get(i));
break;
}
}
if (index == -1)
list1.add(list.get(i));
}
return list1;
}
public static void main(String[] args){
ArrayList<Student> list = new ArrayList<Student>();
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters "quit"
*/
while(true){
//Getting the name entered by the user
System.out.print("Enter student name or quit:");
String name = sc.nextLine();
//checking whether user enters "quit" or not
if (name.equals("quit"))
break;
//Getting the grade entered by the user
System.out.print("Enter grade point ave:");
double gpa = Double.parseDouble(sc.nextLine());
/* Creating the Student Object parameterized constructor
* by passing the name and student average passing as arguments
*/
Student st = new Student(name,gpa);
int index = -1;
//Sorting the array List based on user entered name
for (int i = 0; i< list.size(); i++){
if (name.compareTo(list.get(i).getName()) < 0){
index = i;
list.add(i,st);
break;
}
}
if (index == -1)
list.add(st);
}
//Displaying the Student objects Before Sorting based on Student average in decending order
System.out.println("Students:");
System.out.print("[ ");
for (int i = 0; i<list.size(); i++){
if (i < list.size() -1)
System.out.print(list.get(i).getName() + " Grade Point Avg:" + list.get(i).getGradePointAverage() + ",");
else
System.out.print(list.get(i).getName() + " Grade Point Avg:" + list.get(i).getGradePointAverage());
}
System.out.println(" ]");
//calling the sort method
list = sort(list);
System.out.println("Students in order of Grade Point Average:");
System.out.print("[ ");
//Displaying the Student objects after Sorting based on Student average in decending order
for (int i = 0; i<list.size(); i++){
if (i < list.size() -1)
System.out.print(list.get(i).getName() + " Grade Point Avg:" + list.get(i).getGradePointAverage() + ",");
else
System.out.print(list.get(i).getName() + " Grade Point Avg:" + list.get(i).getGradePointAverage());
}
System.out.println(" ]");
}
}
__________________
Output:
Enter student name or quit:Bush, Sue
Enter grade point ave:3.4
Enter student name or quit:Neddle, Ned
Enter grade point ave:3.2
Enter student name or quit:Zhang, Bin
Enter grade point ave:3.7
Enter student name or quit:Adda, Ida
Enter grade point ave:3.9
Enter student name or quit:quit
Students:
[ Adda, Ida Grade Point Avg:3.9,Bush, Sue Grade Point Avg:3.4,Neddle, Ned Grade Point Avg:3.2,Zhang, Bin Grade Point Avg:3.7 ]
Students in order of Grade Point Average:
[ Adda, Ida Grade Point Avg:3.9,Zhang, Bin Grade Point Avg:3.7,Bush, Sue Grade Point Avg:3.4,Neddle, Ned Grade Point Avg:3.2 ]
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.