Results 1 to 6 of 6

Thread: Specialisation

  1. #1
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default Specialisation

    Hi Guys,

    I am doing an assignment which entails completing partially completed code i have been given and implementing a GUI front end.

    I have 6 classes:
    School
    Person - abstract class which superclasses- Student, Lecturer, HeadOfSchool
    HeadOfSchool - extends Lecturer
    Student - extends Person
    Lecturer - extends Person
    Course

    The application simulates a part of the University, Therefore i am required to read in data from a file.
    Here is the data format:
    Java Code:
    Name of school
    Details of the Head of School
    Number of Lecturers
    List of Lecturers
    Name of course1, number of students
    List of Students
    Name of course2, number of students
    List of Students
    Name of course2, number of students
    List of Students
    Name of course3, number of students
    List of Students
    
    School of Computing
    Prof Shirley Campbell,Aberdeen,female,45,45000
    8
    Dr John Smith,Aberdeen,male,40,28000
    Dr Joan Simpson,Aberdeen,female,30,27500
    Mr Tony Orlando,Banchory,male,40,27500
    Dr Craig Stuart,Aberdeen,male,38,18500
    Dr Moira Cables,Aberdeen,female,35,22500
    Dr William Green,Aberdeen,male,38,35500
    Mr Henry Brown,Aberdeen,male,37,27265
    Ms Jill Blossom,Aberdeen,female,48,30000
    Course UG1,4
    Alan Smith,Aberdeen,male,35,A
    Mary Grant,Banchory,female,25,B
    Joe Bloggs,Aberdeen,male,42,A
    Jack Hawkins,Stonehaven,male,30,C
    Course UG2,5
    Susan Clark,Aberdeen,female,18,C
    Carol Roberts,Murcar,female,32,B
    Roy Rogers,Ellon,male,37,B
    Melanie Sykes,Murcar,female,22,A
    Colin Firth,Aberdeen,male,27,A
    Course UG3,3
    Cindy Lauper,Aberdeen,female,27,A
    Jim Bowen,Banchory,male,47,C
    Andy Townsend,Arbroath,male,35,B
    I have managed to read in the school name,details of Head of School and number of lecturers. The problem i am having is reading in the lecturers. Within the HeadOfSchool class there is an attribute which is an array of lecturer objects. But the constructor has no array of lecturer objects param. Advice please? Sorry if i havent been very informative, having trouble understanding whats going on in my head ha

  2. #2
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    What i am trying to say is... i can read in all the lecturers from the file and assign them to an array of Lecturer type. But what i dont know is, where i assign the object array to?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Within the HeadOfSchool class there is an attribute which is an array of lecturer objects.
    That makes no sense, a HeadOfSchool hasn't got any lecturers, but a school has. If there is no constructor with the right parameters you have to implement a setLecturers(Lecturer[] arr) method.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    Quote Originally Posted by Sage25 View Post
    What i am trying to say is... i can read in all the lecturers from the file and assign them to an array of Lecturer type. But what i dont know is, where i assign the object array to?
    Don't lecturers 'belong' to a school?

    kind regards,

    Jos

    edit: darn, the forum is slow today, otherwise I would've been way faster than Phil ... ;-)
    Last edited by JosAH; 07-06-2010 at 04:52 PM.

  5. #5
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    Heres the School class, i coudnt see anywhere that i can assign the lecturers to.
    I might no be pickin up on something due to my "analysis paralysis"
    Java Code:
    package UniversityModel;
    
    import java.io.*;
    
    
    
    public class School {
    	// Attributes
    	private String schoolName;
    	private HeadOfSchool head;
    	private Course[] courses;
    	private int numCourses;
    
    	/**
            * Constructor
            *
            * @param name name of the school
            * @param head the school HOS object
    	*/
    	public School(String name, HeadOfSchool head){
    		this.schoolName = name;
    		this.courses = new Course[10];
    		this.numCourses = 0;
    		this.head = head;
    	}
    
    	/**
            * @return the name of this school
            */
    	public String getName(){
    		return schoolName;
    	}
    
    
            /**
            * sets the name of this school
            */
            public void setSchoolName(String schoolName) {
            this.schoolName = schoolName;
            }
    
    
    	/**
            * @return the head of this school
            */
    	public HeadOfSchool getHead() {
                    return head;
            }
    
    	public void addCourse(Course course){
    		if (numCourses < courses.length)
    			courses[numCourses++] = course;
    	}
    
    	/**
            * @return an array containing the courses offered by this school.
            */
    	public Course[] getCourses(){
    		Course[] result = new Course[numCourses];
    
                    for (int i = 0; i < numCourses; i++)
    		result[i] = courses[i];
                    //COMPLETE THIS YOURSELF
    		//HINT: you should use a "for" loop
    		return result;
    	}
    
    	public Course getCourse(String name){
    		for (int i = 0; i < numCourses; i++){
    			if (courses[i].matchName(name))
    				return courses[i];
    		}
    		return null;
    	}
    	/**
            * @param courseID @return true if there exists a course in this school
    	* whose name contains the specified identifier string, e.g."PG1"
    	*/
    	public boolean courseExists(String courseId){
    		for (int i = 0; i < numCourses; i++){
    			if (courses[i].matchName(courseId))
    				return true;
    		}
    		return false;
    	}
    
    	/**
            * @param courseID @return an array of Student objects for the course that
    	* matches the specified ID
    	*/
    	public Student[] getStudents(String courseId){
    		for (int i = 0; i < numCourses; i++){
    			if (courses[i].matchName(courseId)){
    				return courses[i].getStudents();
    			}
    		}
    		return null;
    	}
    
    	/**
            * @param studentName
    	* @return the student with the specified name,
    	* if the student does not exist, return null.
    	*/
    	public Student getStudent(String name){
    		for (int i = 0; i < numCourses; i++){
    			Student[] students = courses[i].getStudents();
    
    			//check each student in the student array to find a name
    			//that matches the parameter
    			for (int y = 0; y < students.length; y++){
    				if (students[y].getName().matches(name))
                                        return students[y];
                                            //COMPLETE THIS YOURSELF)
    						//COMPLETE THIS YOURSELF
    
    			}
    		}
    		return null;
    	}
    
    	/**
            * @return a string representation for this school school name, HOS name and
    	* a list of course names offered by this school
    	*/
        @Override
    	public String toString(){
    		String s = "School name : " + this.getName()+"\n"+
                               "Head of school : "+this.head.getName();
    
                    
     
               return s;
            }
            /**
            * Method to read in data from a file in the appropriate format
            */
            public void load(File file){
                String line;
                String[] fields;
                try {
                    BufferedReader in = new BufferedReader(new FileReader(file));
                   
                    
                    //Below reads in school name
                    line = in.readLine();
                    
                    this.setSchoolName(line);
                    //Below reads details of head of school
                    line = in.readLine();
                    fields = line.split(",");
                    // line below reads number of lecturers
                    int numL = Integer.parseInt(in.readLine());
                   // lines below reads the lecturers
                      String [] LecturerField;//array to store data after read
                      Lecturer sLecturer[] = new Lecturer[numL];// temp array of lecturers
    
                    for (int i = 0; i < numL; i++) {
                        line = in.readLine();
                        if (line == null)
                            break;
                        LecturerField = line.split(",");
                        sLecturer[i] = new Lecturer(LecturerField[0], LecturerField[1], LecturerField[2],
                                Integer.parseInt(LecturerField[3]), Integer.parseInt(LecturerField[4]));
    
                }
    
    
                    HeadOfSchool h  = new HeadOfSchool(fields[0], fields[1], fields[2],
                    Integer.parseInt(fields[3]), Integer.parseInt(fields[4]));
                    
                    this.head=h;
                    
    
                    // all above is complete do not touch
    
    
                  
                    // COMPLETE THE READING IN FROM FILE
                    // ADD YOUR CODE HERE
                    
                    
                    in.close();
                } catch (IOException e) {
                        // ADD YOUR OWN CODE HERE
                }
        }
    
            
            
    }

  6. #6
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    Yes, Lecturers do belong to a School but then again, a HeadOfSchool is in charge of them lecturers?

Posting Permissions

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