Results 1 to 6 of 6

Thread: Iterators

  1. #1
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default 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
    Attached Files Attached Files

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    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 :)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    StudentDemo.java:
    Java 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));
    	}
    }
    Student.java
    Java 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;
    	}
    }
    StudentRecords.java
    Java 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 ++;
    		}
    	}
    }
    StudentSpecification.java
    Java 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();
    }
    StudentRecordsSpecification.java
    Java 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();
    }
    RecordsIterator.java
    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;
    	}
    }
    StudentComparator.java (Not yet implemented)
    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);
    	}
    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Carefully reread my previous reply before you throw all that code towards us.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Archer is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. generics and iterators
    By TopNFalvors in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 05:18 PM
  2. Understanding Iterators
    By Domo230 in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 12:03 AM
  3. Iterators - can you assign new values to them?
    By DerekRaimann in forum New To Java
    Replies: 2
    Last Post: 12-09-2010, 07:11 PM
  4. Iterator over Iterators
    By chawlakunal in forum New To Java
    Replies: 2
    Last Post: 05-22-2010, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •