You will need to complete the code in the three classes to finish the lab: The S
ID: 3602393 • Letter: Y
Question
You will need to complete the code in the three classes to finish the lab:
The Student class is complete -- look at it to see what it does, but don't change any of the code
Complete the methods in the Course class (following the comments there)
Complete the main method in the Lab8 class (following the comments there)
import java.util.*;
public class Lab8 {
public static void main(String[] args) {
//YOU DO THIS
//Create a Course object that can hold 10 students
//Loop to ask the user for information for 10 students
//for each one, get the name, major, and gpa
//add each student to the Course object
//Ask the user to enter a major
//Call a method in Course to print all students with that major
//Print all student's on the honor roll (by calling a method in Course
}
}
-------------------
public class Course {
private Student[] roster;
private int size;
public Course(int capacity) {
roster = new Student[capacity];
size = 0;
}
public void addStudent(String name, String major, double gpa) {
//YOU DO THIS
//Create a Student object representing the parameter information
//Add the Student object to position "size" in the roster array
//Update size
//If the array is full, do nothing (don't add the student)
//(Alternatively, you can resize the array if you want)
}
public void printStudentsWithMajor(String m) {
//YOU DO THIS
//Call the "print" method for all students on the roster
//whose major matches "m"
}
public void printHonorRoll() {
//YOU DO THIS
//Call the "print" method for all students on the roster
//whose gpa is at least 3.5.
}
}
----------
public class Student {
private String name;
private String major;
private double gpa;
public Student(String n, String m, double g) {
name = n;
major = m;
gpa = g;
}
public void print() {
System.out.println(name + " " + major + " " + gpa);
}
public String getName() {
return name;
}
public double getGPA() {
return gpa;
}
public String getMajor() {
return major;
}
}
Explanation / Answer
Course.java
public class Course {
private Student[] roster;
private int size;
public Course(int capacity) {
roster = new Student[capacity];
size = 0;
}
public void addStudent(String name, String major, double gpa) {
//YOU DO THIS
//Create a Student object representing the parameter information
//Add the Student object to position "size" in the roster array
//Update size
if(size == roster.length) {
System.out.println("Array is full");
} else {
Student s = new Student(name, major, gpa);
roster[size++]=s;
}
//If the array is full, do nothing (don't add the student)
//(Alternatively, you can resize the array if you want)
}
public void printStudentsWithMajor(String m) {
//YOU DO THIS
//Call the "print" method for all students on the roster
//whose major matches "m"
for(int i=0;i<size;i++) {
if(roster[i].getMajor().contains("m")) {
roster[i].print();
}
}
}
public void printHonorRoll() {
//YOU DO THIS
//Call the "print" method for all students on the roster
//whose gpa is at least 3.5.
for(int i=0;i<size;i++) {
if(roster[i].getGPA()>=3.5) {
roster[i].print();
}
}
}
}
Stundet.java
public class Student {
private String name;
private String major;
private double gpa;
public Student(String n, String m, double g) {
name = n;
major = m;
gpa = g;
}
public void print() {
System.out.println(name + " " + major + " " + gpa);
}
public String getName() {
return name;
}
public double getGPA() {
return gpa;
}
public String getMajor() {
return major;
}
}
Lab8.java
import java.util.*;
public class Lab8 {
public static void main(String[] args) {
//YOU DO THIS
//Create a Course object that can hold 10 students
Course c = new Course(10);
//Loop to ask the user for information for 10 students
//for each one, get the name, major, and gpa
//add each student to the Course object
Scanner scan = new Scanner(System.in);
for(int i=0;i<10;i++) {
System.out.println("Enter the student name: ");
String name = scan.next();
System.out.println("Enter the student major: ");
String major = scan.next();
System.out.println("Enter the student gpa: ");
double gpa = scan.nextDouble();
c.addStudent(name, major, gpa);
}
//Ask the user to enter a major
//Call a method in Course to print all students with that major
System.out.println("Enter the studnet major to search: ");
String m = scan.next();
c.printStudentsWithMajor(m);
//Print all student's on the honor roll (by calling a method in Course
c.printHonorRoll();
}
}
Output:
Enter the student name:
aaaa
Enter the student major:
fdsf
Enter the student gpa:
67
Enter the student name:
fdsfds
Enter the student major:
ytuty
Enter the student gpa:
78
Enter the student name:
vvcbc
Enter the student major:
werwe
Enter the student gpa:
45
Enter the student name:
fdgdf
Enter the student major:
yuyu
Enter the student gpa:
88
Enter the student name:
xczc
Enter the student major:
ewqe
Enter the student gpa:
22
Enter the student name:
hgj
Enter the student major:
retr
Enter the student gpa:
78
Enter the student name:
bvnvb
Enter the student major:
kjhkhj
Enter the student gpa:
99
Enter the student name:
ewqe
Enter the student major:
asda
Enter the student gpa:
44
Enter the student name:
fghf
Enter the student major:
uyiu
Enter the student gpa:
87
Enter the student name:
cxxvcv
Enter the student major:
ewer
Enter the student gpa:
67
Enter the studnet major to search:
hgjgh
aaaa fdsf 67.0
fdsfds ytuty 78.0
vvcbc werwe 45.0
fdgdf yuyu 88.0
xczc ewqe 22.0
hgj retr 78.0
bvnvb kjhkhj 99.0
ewqe asda 44.0
fghf uyiu 87.0
cxxvcv ewer 67.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.