Question

In Java, Please do not forget the ToString method in the Student class!

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 students 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 with user inputted data. You should store the students alphabetically by name (A to Z) in the arraylist The algorithm to sort is simple. As you read in each name for the Student (say name1), compare it with each name (say name2 of a Student) stored in the arraylist starting from the index 0. As soon (name1.compareTo(name2) >0) that is the right index to put name1. 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 arraylist 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 vou used for the names. See below for a sample output. Enter student name or quit: Bush, Sue Enter grade point aye; 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 Ave: 3.9, Bush, Sue Grade Point Ave: 3.4, Neddle. Ned Grade Point Ave: 3.2, Zhang, Bin Grade Point Ave: 3.7] Students in order of Grade Point Average [ Adda, Ida Grade Point Ave: 3.9Zhang, Bin Grade Point Ave: 3.7, Bush, Sue Grade Point Ave: 3.4, Neddle. Ned Grade Point Ave: 3.2]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi

The following code includes 2 classes

1. Student.java with required member variables and methods
2. StudentDemo.java is the driver program to create the Student objects, add them to sorted list and also sorts them by Grade Point Average

I have included comments inline with the code for your reference. Hope that helps.

Let me know if you need any help.


//File: Student.java


public class Student {
//Member variables
private String name;
private double gradePointAverage;
//Constructors
public Student(String newName, double newGradePointAverage) {
name = newName;
gradePointAverage = newGradePointAverage;
}
//Getters and Setters for the member variables
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public void setGradePointAverage(double newGradePointAverage) {
gradePointAverage = newGradePointAverage;
}
public double getGradePointAverage() {
return gradePointAverage;
}
//Override the String representation of this class
public String toString() {
return name + " Grade Point Ave:" + gradePointAverage;
}
}


D Student-Java 2 public class Student 4 5 6 //Member variables private String name; private double gradePointAverage 8 //Cons

//File: StudentDemo.java

import java.util.Scanner;
public class StudentDemo {
//Member variable - ArrayList of Students
private static ArrayList<Student> studentList = new ArrayList<>();
//Method to add Student into the list in the alphabetical order of their names
private static void addStudent(Student newStudent) {
boolean addedToList = false;
for(int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if(student.getName().compareTo(newStudent.getName()) > 0) {
studentList.add(i, newStudent);
addedToList = true;
break;
}
}
//Case: when the list is empty
if(!addedToList) {
studentList.add(newStudent);
}
}
//Method - returns an Arraylist of Students sorted by the Grade Point Average
private static ArrayList<Student> sortByGradePointAverage(ArrayList<Student> studentList) {
ArrayList<Student> sortedStudentList = new ArrayList<>();
for(int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
boolean addedToSortedList = false;
for(int j = 0; j < sortedStudentList.size(); j++) {
Student sortedStudent = sortedStudentList.get(j);
if(student.getGradePointAverage() >= sortedStudent.getGradePointAverage()) {
sortedStudentList.add(j, student);
addedToSortedList = true;
break;
}
}
if(!addedToSortedList) {
sortedStudentList.add(student);
}
}
return sortedStudentList;
}
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
//Read the input from user
while(true) {
System.out.print("Enter student name or quit: ");
String name = inputScanner.nextLine();
if(name.equalsIgnoreCase("quit")) {
break;
}
System.out.print("Enter grade point ave: ");
String gradePointAverageStr = inputScanner.nextLine();
double gradePointAverage = 0.0d;
//Precaution to ensure Grade Point Average is valid input
try {
gradePointAverage = Double.parseDouble(gradePointAverageStr);
if(gradePointAverage > 4.0d) {
gradePointAverage = 4.0d;
}
} catch(NumberFormatException numFormatEx) {
//In case of invalid GradePointAverage, consider it as zero
}
//Create Student object and add to the list
Student newStudent = new Student(name, gradePointAverage);
addStudent(newStudent);
}
inputScanner.close();
//Display the results
System.out.println("\n\nStudents");
System.out.println(studentList);
System.out.println("Students in order of Grade Point Average:");
System.out.println(sortByGradePointAverage(studentList));
}
}



DStudentDemo.java Ex java.util.Scanner; 1 import 2 3 public class StudentDemo 4 /Member variable ArrayList of Students privat

59 60 public static void main (String args) 61 62 63 64 65 Scanner inputScanner new Scanner (System.in) //Read the input from

//Output
bin java StudentDemo Enter student name or quit: Bush, Sue Enter grade point ave: 3.4 Enter student name or quit: Neddle, Ned

Add a comment
Know the answer?
Add Answer to:
In Java, Please do not forget the ToString method in the Student class! In this exercise,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

  • Complete the following Java class. In this class, inside the main() method, user first enters the...

    Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise,...

    By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods get Name, addQuiz(score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took. (b) Modify the Student class to compute grade point averages. Methods are needed to add a grade and get the current GPA. Specify grades as...

  • please use java do what u can 1. Modify the Student class presented to you as...

    please use java do what u can 1. Modify the Student class presented to you as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test score is assumed to be initially zero. Provide a method called set Test Score that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called get...

  • JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out...

    JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out the name of the student with the lowestgpa. In the attached zip file, you will find two files HighestGPA.java and students.dat. Students.dat file contains names of students and their gpa. Check if the code runs correctly in BlueJ or any other IDE by running the code. Modify the data to include few more students and their gpa. import java.util.*; // for the Scanner class...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Please help, provide source code in c++(photos attached) develop a class called Student with the following protected...

    Please help, provide source code in c++(photos attached) develop a class called Student with the following protected data: name (string), id (string), units (int), gpa (double). Public functions include: default constructor, copy constructor, overloaded = operator, destructor, read() to read data into it, print() to print its data, get_name() which returns name and get_gpa() which returns the gpa. Derive a class called StuWorker (for student worker) from Student with the following added data: hours (int for hours assigned per week),...

  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT