Well, I'm trying to teach myself java, but I'm having major issues. I'm apparently supposed to be able to implement a Student Records program using an iterator, and the comparable interface, but it's just not working for me...
Attached is what I've done. If anyone has any input, that would be lovely!
StudentDemo.java
public class StudentDemo { public static void main(String[] args) { StudentRecords records = new StudentRecords(); records.addStudent(new Student(123, "Fran", 2009)); records.addStudent(new Student(465, "Bernard", 2008)); records.addStudent(new Student(222, "Manny", 2008)); System.out.println(records.getName(222)); System.out.println(records.getYearOfCommencement(123)); // for (Integer i : s) System.out.println(s.getName(i)); } }
Student.java
public class Student implements StudentSpecification { String stuName = null; int stuYear = 0, stuNum = 0; public Student(int studentNumber, String name, int year) { stuName = name; stuYear = year; stuNum = studentNumber; } public void setName(String name) { stuName = name; } public void setYearOfCommencement(int year) { stuYear = year; } public void setStudentNumber(int num) { stuNum = num; } public String getName() { return stuName; } public int getYearOfCommencement() { return stuYear; } public int getStudentNumber() { return stuNum; } }
StudentSpecification.java
// Interface type for a student record with student number, name // and year of commencement interface StudentSpecification { // Constructor // public Student(int studentNumber, String name, int year); // Change the name of the student void setName(String name); // Change the year of commencement void setYearOfCommencement(int year); // Get the student number of a student int getStudentNumber(); // Get the name String getName(); // Get the year of commencement int getYearOfCommencement(); }
StudentRecords.java
import java.util.*; public class StudentRecords implements StudentRecordsSpecification { private GeneralList<Student> students = new GeneralList<Student>(); int i = 0; boolean stuExists = false; void addStudent(Student student) //throws Duplicate Number { RecordsIterator<Student> iteratorAdd = new RecordsIterator<Student>(students); i = 0; stuExists = false; while (i < students.size()) { if (student.getStudentNumber() == iteratorAdd.next().getStudentNumber()) { //Dont add System.out.println("Student with that number exists"); stuExists = true; } i ++; } if (stuExists == false) { students.add(student); } } boolean removeStudent(int studentNumber) { //loop through list, checking against student number stuExists = false; Student s; RecordsIterator<Student> iteratorRem = new RecordsIterator<Student>(students); while (students.hasNext()) { s = iteratorRem.next(); if (studentNumber == s.getStudentNumber()) { System.out.println("Student with that number exists, removing"); stuExists = true; } } if (stuExists == true) { iteratorRem.remove(); return true; } else { return false; } } String getName(int studentNumber) { RecordsIterator<Student> iteratorNum = new RecordsIterator<Student>(students); i = 0; Student s; while (i < students.size()) { s = iteratorNum.next(); if (studentNumber == s.getStudentNumber()) { return s.getName(); } i ++; } } int getYearOfCommencement(int studentNumber) { RecordsIterator<Student> iteratorYear = new RecordsIterator<Student>(students); i = 0; int year, num; Student s; while (i < students.size()) { s = iteratorYear.next(); year = s.getYearOfCommencement(); num = s.getStudentNumber(); if (num == studentNumber) { return year; } i ++; } } }
RecordsIterator.java
import java.util.*; public class RecordsIterator<E> implements Iterator<E> { private GeneralList<E> list; private int previous; boolean canRemove; //constructor //param is the list type to iterate over public RecordsIterator(GeneralList<E> aList) { list = aList; previous = -1; //iterator is before first element canRemove = false; //is empthy } public boolean hasNext() { if ((previous + 1) < list.size()) { return true; } else { return false; } } public E next() { if(!hasNext()) { throw new NoSuchElementException(); } previous ++; canRemove = true; return list.get(previous); } public void remove() { if (!canRemove) { throw new IllegalStateException(); } list.remove(previous); previous --; canRemove = false; } }
StudentRecordsSpecification.java
// Interface type for a collection of student records interface StudentRecordsSpecification extends Iterable<Integer> { // Constructor: create with zero records // public StudentRecords(); // Add a student to the records // Throw an exception if there is already a student with the // same student number void addStudent(Student student); //throws DuplicateStudentNumber; // Remove the student with this student number // Return 'false' if no such student boolean removeStudent(int studentNumber); // Get the name of the student with this student number // Return null if no such student String getName(int studentNumber); // Get the year of commencement of the student with this student number // Return zero if no such student int getYearOfCommencement(int studentNumber); // Return an iterator which iterates through the student numbers of // all of the students. // The order should be by increasing year of commencement and if several // students have the same year of commencement then in alphabetic // order of their names. // This method is declared in the interface type 'Iterable'. // Iterator<Integer> iterator(); }
StudentComparator.java //Not implemented just yet. Any hints welcome!
import java.util.*; public class StudentComparator<T extends Student> implements Comparator<T> { public int compare(Student stu1, Student stu2) { int yearStu1 = stu1.getYearOfCommencement(); int yearStu2 = stu2.getYearOfCommencement(); return yearStu1.compareToIgnoreCase(yearStu2); } }