Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Iterators and lists and oh god my brain hurts!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Iterators and lists and oh god my brain hurts!

    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);
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterators and lists and oh god my brain hurts!

    but it's just not working for me
    We will probably require more information than that, especially given the amount of code you posted (helps to break the problem down and define the cause of something, typically as an SSCCE). What about it isn't working? Are there exceptions? Be as specific as possible, and again you might want to trim the code down to demonstrate the problem more clearly.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterators and lists and oh god my brain hurts!

    Quote Originally Posted by copeg View Post
    We will probably require more information than that, especially given the amount of code you posted (helps to break the problem down and define the cause of something, typically as an SSCCE). What about it isn't working? Are there exceptions? Be as specific as possible, and again you might want to trim the code down to demonstrate the problem more clearly.
    Sorry.... I'm getting a heap of compiler time errors:
    RecordsIterator.java:5: cannot find symbol
    symbol  : class GeneralList
    location: class RecordsIterator<E>
    	private GeneralList<E> list;
    	        ^
    RecordsIterator.java:11: cannot find symbol
    symbol  : class GeneralList
    location: class RecordsIterator<E>
    	public RecordsIterator(GeneralList<E> aList)
    	                       ^
    StudentRecords.java:5: cannot find symbol
    symbol  : class GeneralList
    location: class StudentRecords
    	private GeneralList<Student> students = new GeneralList<Student>();
    	        ^
    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:5: cannot find symbol
    symbol  : class GeneralList
    location: class StudentRecords
    	private GeneralList<Student> students = new GeneralList<Student>();
    	                                            ^
    10 errors

    I thought I could use General List? The examples I've seen have?

Similar Threads

  1. [SOLVED] Linked Lists
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 08:39 AM
  2. Array Lists help!!
    By lilika in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 4th, 2011, 09:21 AM
  3. iterators & linked lists in java question
    By somewhat_confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2010, 10:59 AM
  4. [SOLVED] My head Hurts... such a simple problem but I'm stuck!
    By Kassino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2010, 08:38 AM
  5. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM