Reputation: 1
this is my mock test from my professor and I having trouble writing it in Java.
This is the question:
An ADT to manage a collection of students in a course is required. You can assume that there are no more than 100 students in any course. A student's record consists of ID (String), name (String), and GPA (double). There is no duplication in student IDs, but it is possible to have two or more students with the same name and/or GPA.
Create a new type StudentCollection (it is equivalent to a class in Java). Except for the constructor, your StudentCollection type must support the following 3 public operations (feel free to add additional private operations as needed - but their scope have to be private)
void addStudent(Student std): add a new student std to your collection. If there is a student having the same ID as std in your collection already, do nothing.
Student searchByName(String name): search the student collection and return any student whose name contains name completely (case sensitive). Examples: "ABC" contains "ABC" completely; "ABC" contains "A" completely; "ABC" contains "C" completely, "ABC DEF" contains "C D" completely; "ABC" does NOT contain "CB" completely; "ABC" does NOT contain "abc" completely. If there is more than one matching student, your method can return any student. If there is no matching student, return null. int rankStudent(String sID): return the rank of a student whose ID is sID with regard to this collection. The ranking is done using students' GPAs. A student with the highest GPA has a rank of 1. In this example, let assume there are 4 GPA values [9.0, 8.5, 7.0, 8.5]. A student whose GPA = 9.0 has a rank of 1, a student whose GPA = 8.5 has a rank of 2 (there are 2 students who have the same rank of 2), and a student whose GPA = 7.0 has a rank of 4. If there is no student found with the provided sID, return -1.
Create a StudentCollection object and use it in the main method (client code). Your client code must call all the above 3 public methods one or more times.
You are NOT allowed to use the Java Collection Framework classes for this problem. Your code for this problem must be stored in a single file StudentCollection.java.
The ADT I'm choosing here is Set. Since the instruction doesn't allow me to use the Java Collection Framework, I have to manually implement all of the functions.
But here is the problem:
for the first function, the question ask me to write void addStudent(Student std)
which when implementing a Set ADT, I cannot pass in a user defined data type Student
into the function, I have done some research and we have to pass in a Set parameter instead of a user defined data type. Here is the code for class Student:
static class Student {
private String ID;
private String name;
private double GPA;
Student(String ID, String name, double GPA) {
this.ID = ID;
this.name = name;
this.GPA = GPA;
}
}
let's say that we put in the Student
class, then there have to be some getters and setters
inside of the Student
class. But the question limit the amount of public function to implement and all functions beside the three specify function above have to be private
. How can a getter and setter be private
? Is it possible?
The overall question is: How to add a user-defined data type into a set? I'm sorry if there is any explanation of mine is not clear. Please reply to this question if you have any further question.
Here is the code that I have been working on:
import java.util.HashSet;
import java.util.Set;
public class StudentCollection {
static Set<Student> manage = new HashSet<>();
static class Student {
private String ID;
private String name;
private double GPA;
Student(String ID, String name, double GPA) {
this.ID = ID;
this.name = name;
this.GPA = GPA;
}
}
public static void addStudent(Student std) {
manage.add(std);
}
// public static Student searchByName(String name) {
//
// }
//
// public static int rankStudent(String sID) {
//
// }
public static void main(String[] args) {
Student std = new Student("s387", "nam", 3.7);
addStudent(std);
}
}
Upvotes: 0
Views: 1235
Reputation: 51551
The Student
class has to be public, with public getters. Otherwise, you couldn't create a Student
instance to add a student.
I went ahead and coded the addStudent
method. I'm leaving the rest of the code for you to finish.
You'll have to go over your class notes to verify, but this is how I would start coding the StudentCollection
class. There are no static fields or methods, other than the main
method.
public class StudentCollection {
public static void main(String[] args) {
StudentCollection sc = new StudentCollection();
sc.addStudent(sc.new Student("10001", "George", 9.0));
}
private int studentLength;
private Student[] students;
public StudentCollection() {
this.studentLength = 0;
this.students = new Student[100];
}
public void addStudent(Student student) {
for (int index = 0; index < studentLength; index++) {
if (student.getSID().equals(students[index].getSID())) {
return;
}
}
students[studentLength++] = student;
}
public Student searchByName(String name) {
}
public int rankStudent(String sID) {
}
public class Student {
private final double gpa;
private final String sID, name;
public Student(String sID, String name, double gpa) {
this.sID = sID;
this.name = name;
this.gpa = gpa;
}
public double getGpa() {
return gpa;
}
public String getSID() {
return sID;
}
public String getName() {
return name;
}
}
}
Upvotes: 1