import java.text.DecimalFormat;   // Class representing a student public class Student {   private String first;  // first name private String last;   // last name private double gpa;    // grade point average   // Student class constructor public Student(String first, String last, double gpa) { this.first = first; // first name this.last = last;   // last name this.gpa = gpa;     // grade point average }   public double getGPA() { return gpa; }   public String getLast() { return last; }   // Returns a String representation of the Student object, with the GPA formatted to one decimal public String toString() { DecimalFormat fmt = new DecimalFormat("#.0"); return first + " " + last + " " + "(GPA: " + fmt.format(gpa) + ")"; } } public class LabProgram {     public static void main(String args[]) {        Scanner scnr = new Scanner(System.in);         Course course = new Course();       int beforeCount;              // Example students for testing       course.addStudent(new Student("Henry", "Nguyen", 3.5));          course.addStudent(new Student("Brenda", "Stern", 2.0));        course.addStudent(new Student("Lynda", "Robison", 3.2));        course.addStudent(new Student("Sonya", "King", 3.9));                 Student student = course.findStudentHighestGpa();         System.out.println("Top student: " + student);     }     }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
import java.text.DecimalFormat;
 
// Class representing a student
public class Student {
 
private String first;  // first name
private String last;   // last name
private double gpa;    // grade point average
 
// Student class constructor
public Student(String first, String last, double gpa) {
this.first = first; // first name
this.last = last;   // last name
this.gpa = gpa;     // grade point average
}
 
public double getGPA() {
return gpa;
}
 
public String getLast() {
return last;
}
 
// Returns a String representation of the Student object, with the GPA formatted to one decimal
public String toString() {
DecimalFormat fmt = new DecimalFormat("#.0");
return first + " " + last + " " + "(GPA: " + fmt.format(gpa) + ")";
}
}
public class LabProgram {
    public static void main(String args[]) {
       Scanner scnr = new Scanner(System.in);
        Course course = new Course();
      int beforeCount;
      
      // Example students for testing
      course.addStudent(new Student("Henry", "Nguyen", 3.5));   
      course.addStudent(new Student("Brenda", "Stern", 2.0)); 
      course.addStudent(new Student("Lynda", "Robison", 3.2)); 
      course.addStudent(new Student("Sonya", "King", 3.9)); 
      
        Student student = course.findStudentHighestGpa();
        System.out.println("Top student: " + student);
    }    
}
Complete the Course class by implementing the `findStudentHighestGpa()` method, which returns the Student object with the highest GPA in the course. Assume that no two students have the same highest GPA.

Given classes:
- **Class LabProgram** contains the main method for testing the program.
- **Class Course** represents a course, which contains an ArrayList of Student objects as a course roster. *(Type your code in here.)*
- **Class Student** represents a classroom student, which has three fields: first name, last name, and GPA. *(Hint: `getGPA()` returns a student’s GPA.)*

**Note:** For testing purposes, different student values will be used.

**Example:** For the following students:

```
Henry Nguyen 3.5
Brenda Stern 2.0
Lynda Robison 3.2
Sonya King 3.9
```

The output is:

```
Top student: Sonya King (GPA: 3.9)
```
Transcribed Image Text:Complete the Course class by implementing the `findStudentHighestGpa()` method, which returns the Student object with the highest GPA in the course. Assume that no two students have the same highest GPA. Given classes: - **Class LabProgram** contains the main method for testing the program. - **Class Course** represents a course, which contains an ArrayList of Student objects as a course roster. *(Type your code in here.)* - **Class Student** represents a classroom student, which has three fields: first name, last name, and GPA. *(Hint: `getGPA()` returns a student’s GPA.)* **Note:** For testing purposes, different student values will be used. **Example:** For the following students: ``` Henry Nguyen 3.5 Brenda Stern 2.0 Lynda Robison 3.2 Sonya King 3.9 ``` The output is: ``` Top student: Sonya King (GPA: 3.9) ```
**Title: Understanding Java Classes and ArrayLists**

**Current File: Course.java**

In this example, we explore basic Java programming concepts, specifically focusing on classes and the use of ArrayLists to manage collections of objects.

```java
import java.util.ArrayList;

public class Course {
    private ArrayList<Student> roster; // Collection of Student objects
    
    public Course() {
        roster = new ArrayList<Student>();
    }
    
    public Student findStudentHighestGpa() {
        /* Type your code here */
    }
    
    public void addStudent(Student s) {
        roster.add(s);
    }
}
```

### Explanation:
1. **Import Statement**
   - `import java.util.ArrayList;`: This line imports the `ArrayList` class from the `java.util` package, allowing the program to use dynamic arrays.

2. **Class Declaration**
   - `public class Course {`: Declares a public class named `Course`.

3. **Attributes**
   - `private ArrayList<Student> roster;`: Declares a private attribute `roster`, which is an `ArrayList` to store a collection of `Student` objects.

4. **Constructor**
   - `public Course() {`: Defines the constructor for the `Course` class.
   - `roster = new ArrayList<Student>();`: Initializes `roster` as a new `ArrayList` of `Student` objects.

5. **Methods**
   - `public Student findStudentHighestGpa() {`: A public method skeleton intended to find the student with the highest GPA. The implementation is left for future coding.
   - `public void addStudent(Student s) {`: A public method to add a `Student` object to the `roster`.
   - `roster.add(s);`: Adds the student `s` to the `roster` ArrayList.

This example illustrates how to manage a collection of student objects using an `ArrayList` in a Java class. The method `findStudentHighestGpa` suggests that the class could include functionality to compare and find specific student data based on GPA.
Transcribed Image Text:**Title: Understanding Java Classes and ArrayLists** **Current File: Course.java** In this example, we explore basic Java programming concepts, specifically focusing on classes and the use of ArrayLists to manage collections of objects. ```java import java.util.ArrayList; public class Course { private ArrayList<Student> roster; // Collection of Student objects public Course() { roster = new ArrayList<Student>(); } public Student findStudentHighestGpa() { /* Type your code here */ } public void addStudent(Student s) { roster.add(s); } } ``` ### Explanation: 1. **Import Statement** - `import java.util.ArrayList;`: This line imports the `ArrayList` class from the `java.util` package, allowing the program to use dynamic arrays. 2. **Class Declaration** - `public class Course {`: Declares a public class named `Course`. 3. **Attributes** - `private ArrayList<Student> roster;`: Declares a private attribute `roster`, which is an `ArrayList` to store a collection of `Student` objects. 4. **Constructor** - `public Course() {`: Defines the constructor for the `Course` class. - `roster = new ArrayList<Student>();`: Initializes `roster` as a new `ArrayList` of `Student` objects. 5. **Methods** - `public Student findStudentHighestGpa() {`: A public method skeleton intended to find the student with the highest GPA. The implementation is left for future coding. - `public void addStudent(Student s) {`: A public method to add a `Student` object to the `roster`. - `roster.add(s);`: Adds the student `s` to the `roster` ArrayList. This example illustrates how to manage a collection of student objects using an `ArrayList` in a Java class. The method `findStudentHighestGpa` suggests that the class could include functionality to compare and find specific student data based on GPA.
Expert Solution
Step 1: Providing the algorithm

1. Create a class named Student with private fields: first (String), last (String), and gpa (double).

2. Implement a constructor in the Student class to initialize first, last, and gpa.
3. Implement getGPA() method in the Student class to retrieve the GPA of the student.
4. Override toString() method in the Student class to return a formatted string representation of the student.

5. Create a class named Course with an ArrayList of Student objects named roster.
6. Implement a method named findStudentHighestGpa() in the Course class.
7. Initialize a Student object variable (highestGpaStudent) to null to keep track of the student with the highest GPA.
8. Iterate through the roster list:
   a. For each student, compare their GPA with the GPA of highestGpaStudent.
   b. If the student's GPA is higher, update highestGpaStudent with the current student.
9. After iterating through the roster, highestGpaStudent will contain the student with the highest GPA.
10. Return highestGpaStudent from the findStudentHighestGpa() method.

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education