Results 1 to 6 of 6
Thread: Iterators
- 04-09-2011, 10:49 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Iterators
Sorry to come groveling for help again, but here goes...
I'm running into trouble again...
I've attached the files as a zip because there's a few of them.
Here's what it returns:
Java Code:javac *.java StudentComparator.java:9: int cannot be dereferenced return yearStu1.compareToIgnoreCase(yearStu2); ^ StudentRecords.java:3: StudentRecords is not abstract and does not override abstract method iterator() in java.lang.Iterable public class StudentRecords implements StudentRecordsSpecification ^ StudentRecords.java:72: getYearOfCommencement(int) in StudentRecords cannot implement getYearOfCommencement(int) in StudentRecordsSpecification; attempting to assign weaker access privileges; was public int getYearOfCommencement(int studentNumber) ^ StudentRecords.java:56: getName(int) in StudentRecords cannot implement getName(int) in StudentRecordsSpecification; attempting to assign weaker access privileges; was public String getName(int studentNumber) ^ StudentRecords.java:30: removeStudent(int) in StudentRecords cannot implement removeStudent(int) in StudentRecordsSpecification; attempting to assign weaker access privileges; was public boolean removeStudent(int studentNumber) ^ StudentRecords.java:9: addStudent(Student) in StudentRecords cannot implement addStudent(Student) in StudentRecordsSpecification; attempting to assign weaker access privileges; was public void addStudent(Student student) //throws Duplicate Number ^ StudentRecords.java:36: cannot find symbol symbol : method hasNext() location: class java.util.LinkedList<Student> while (students.hasNext()) ^ 7 errors
- 04-09-2011, 10:59 AM #2
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
First thing i learned over here is to NEVER attach a ziped file and your whole code, since no one will answer because they have no time for running over the whole thing. So determine where your problem is and send it in the SSCCE form :)
- 04-09-2011, 12:02 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Judging from the error diagnostic messages alone:
1) yearStu1 is an int and ints don't have methods;
2) all methods that implement interface methods have to be public;
3) you class doesn't implement the hasNext() method.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-09-2011, 12:04 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
StudentDemo.java:
Student.javaJava Code: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)); } }
StudentRecords.javaJava Code: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.javaJava Code:import java.util.*; public class StudentRecords implements StudentRecordsSpecification { private LinkedList<Student> students = new LinkedList<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 ++; } } }
StudentRecordsSpecification.javaJava Code:// 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(); }
RecordsIterator.javaJava Code:// 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 yet implemented)Java Code:import java.util.*; public class RecordsIterator<E> implements Iterator<E> { private LinkedList<E> list; private int previous; boolean canRemove; //constructor //param is the list type to iterate over public RecordsIterator(LinkedList<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; } }
Java Code: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); } }
- 04-09-2011, 12:11 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Carefully reread my previous reply before you throw all that code towards us.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-09-2011, 12:13 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
generics and iterators
By TopNFalvors in forum New To JavaReplies: 3Last Post: 03-29-2011, 05:18 PM -
Understanding Iterators
By Domo230 in forum New To JavaReplies: 2Last Post: 02-12-2011, 12:03 AM -
Iterators - can you assign new values to them?
By DerekRaimann in forum New To JavaReplies: 2Last Post: 12-09-2010, 07:11 PM -
Iterator over Iterators
By chawlakunal in forum New To JavaReplies: 2Last Post: 05-22-2010, 09:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks