Results 1 to 14 of 14
Thread: Brushing up on old skills
- 07-02-2010, 07:18 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Brushing up on old skills
Hi guys
Firstly wanna let everyone know i'm a new member of this forum.
I have been given an assignment which consists of partially completed code, which i need to complete and implement a GUI for the application.
Its been a while since i have done any programming in Java and to be quite honest i'm finding it hard to take it back on board.
I'm not so good at explaning my problems so please bare with me.
What i have been assigned to do is complete the code i have been given with comments. I have done pretty well so far i believe and nearly all the classes compile. Unfortunatly i cannot get hold of my lecturer as he is on leave and i am having a problem with his comments. His comments specify how the partially completed classes should be implemented, but unfortunatly i cant understand some of them.
I dont know how i should go about giving an explanation of the application so i will upload all my classes and hopefully that will help.
Course class
Java Code:package UniversityModel; public class Student extends Person { // Attributes private String studentId; /** * Constructor * @param name name of the student * @param address address of the student * @param gender gender of the student * @param age age of the student * @param id student id of the student */ public Student(String name, String address, String gender, int age, String studentId) { super(name, address, gender, age); this.studentId = studentId; } /** * @return the ID of this student */ public String getStudentId(){ return studentId; } /** * @return a string representation for this student * name, address, sex, age and type */ public String toString(){ String rval = "Name : "+this.getName()+"\n"+ "Address : "+this.getAddress()+"\n"+ "Gender : "+this.getGender()+"\n"+ "Age : "+this.getAge()+"\n"+ "Type : "+this.studentId+"\n"; return rval; } public void setStudentId(String studentId){ //COMPLETE THIS YOURSELF } /** * @return a booleen depending on whether this student * name matches the argument */ public boolean matchName(String name){ //COMPLETE THIS YOURSELF } }
Head of school class
Lecturer classJava Code:package UniversityModel; public class HeadOfSchool extends Lecturer { // Attributes private Lecturer[] lecturers; private int numLecturers; public HeadOfSchool(String name, String address, String gender, int age, int salary, Lecturer[] lecturers, int numLecturers) { super(name, address, gender, age, salary); this.lecturers = lecturers; this.numLecturers = numLecturers; } /** * Constructor * @param name name of the Head of School * @param address address of the Head of School * @param gender gender of the Head of School * @param age age of the Head of School * @param salary salary of the Head of School */ public void addLecturer(Lecturer lecturer){ if (numLecturers < lecturers.length) { lecturers[numLecturers++] = lecturer; } } /** * Get an array containing the lecturers supervised by this Head of School. * The size of the array should be the same as the number of lecturers supervised * (and may be zero). */ public Lecturer[] getLecturers(){ Lecturer[] result = new Lecturer[numLecturers]; for (int i = 0; i < numLecturers; i++){ result[i]=lecturers[i]; } return result; } /** * @return a string representation for this HOS * name, address, gender, age and salary plus * details of all the lecturers he/she supervises */ public String toString(){ String s = super.toString() + "\nLECTURERS SUPERVISED\n"; for (int i = 0; i < numLecturers; i++) s += lecturers[i].toString() + "\n"; return s; } }
Java Code:package UniversityModel; public class Lecturer extends Person{ // Attributes private int salary; /** * Constructor * @param thename name of the lecturer * @param theaddress address of the lecturer * @param gender gender of the lecturer * @param theage age of the lecturer * @param thesalary salary of the lecturer */ public Lecturer(String name, String address, String gender, int age, int salary) { super(name, address, gender, age); this.salary = salary; } /** * @return the salary of this lecturer */ public int getSalary() { return salary; } /** * @return a string representation for this lecturer * name, address, gender, age and salary */ public String toString(){ String rval="Name : "+this.getName()+"\n"+ "Address : "+this.getAddress()+"\n"+ "Gender : "+this.getGender()+"\n"+ "Age : "+this.getAge()+"\n"+ "Salary : "+this.getSalary()+"\n"; return rval; } }
Perwon class
Java Code:package UniversityModel; public abstract class Person{ // Attributes private String name; private String address; private String gender; private int age; /** * Constructor * * @param name name of the person * @param address address of the person * @param gender gender of the person * @param age age of the person */ public Person(String name, String address, String gender, int age){ this.name = name; this.address = address; this.gender = gender; this.age = age; } public String getAddress() { return address; } public int getAge() { return age; } public String getGender() { return gender; } public String getName() { return name; } @Override public abstract String toString(); public void setAddress(String address){ this.address=address; } public void setAge(int age){ this.age=age; } public void setGender(String gender){ this.gender=gender; } public void setName(String name){ this.name=name; } }
School class
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 */ public String toString(){ String s = "School name : " + this.schoolName+"\n"+ "Head of school : " + this.head.getName().toString()+"\n"+ "Courses"+"\n"; for (int i = 0; i < numCourses; i++){ s += courses[i].toString() + "\n"; 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)); //line below reads school name line = in.readLine(); this.head.setName(line); // line below reads head of school line = in.readLine(); fields = line.split(","); HeadOfSchool h = new HeadOfSchool(fields[0], fields[1], fields[2], Integer.parseInt(fields[3]), Integer.parseInt(fields[4])); //line below reads in num of lecturers line=in.readLine(); h. //line below reads 1st lecturer line = in.readLine(); fields = line.split(","); Lecturer l = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 2nd lecturer line = in.readLine(); fields = line.split(","); Lecturer m = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 3rd lecturer line = in.readLine(); fields = line.split(","); Lecturer n = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 4th lecturer line = in.readLine(); fields = line.split(","); Lecturer o = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 5th lecturer line = in.readLine(); fields = line.split(","); Lecturer p = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 6th lecturer line = in.readLine(); fields = line.split(","); Lecturer q = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 7th lecturer line = in.readLine(); fields = line.split(","); Lecturer r = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 8th lecturer line = in.readLine(); fields = line.split(","); Lecturer s = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); // COMPLETE THE READING IN FROM FILE // ADD YOUR CODE HERE in.close(); } catch (IOException e) { // ADD YOUR OWN CODE HERE } } }
Student class
There are two problems i am having:Java Code:package UniversityModel; public class Student extends Person { // Attributes private String studentId; /** * Constructor * @param name name of the student * @param address address of the student * @param gender gender of the student * @param age age of the student * @param id student id of the student */ public Student(String name, String address, String gender, int age, String studentId) { super(name, address, gender, age); this.studentId = studentId; } /** * @return the ID of this student */ public String getStudentId(){ return studentId; } /** * @return a string representation for this student * name, address, sex, age and type */ public String toString(){ String rval = "Name : "+this.getName()+"\n"+ "Address : "+this.getAddress()+"\n"+ "Gender : "+this.getGender()+"\n"+ "Age : "+this.getAge()+"\n"+ "Type : "+this.studentId+"\n"; return rval; } public void setStudentId(String studentId){ //COMPLETE THIS YOURSELF } /** * @return a booleen depending on whether this student * name matches the argument */ public boolean matchName(String name){ //COMPLETE THIS YOURSELF } }
1. If you look at the method matchName in classes course and student.. i have coded the method in the course class as best as i understood. But to be honest i don't know what these methods should do?
2. if you look at the school class i have began to read in data from a file..
Here is the format of the file that i am reading in:
I seem to be gettin on fine up untill the point i need to save the number of lecturers.. i cant seem to find out what code i need in order to save this value correctly.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
Many thanks
Hope you can helpLast edited by Fubarable; 07-02-2010 at 07:30 PM. Reason: mod edit: code tags added
- 07-02-2010, 07:19 PM #2
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Sorry about the failed code brackets guys, i'm new... how can this be done correctly?
-
Code tags added. Please see the link in my signature regarding how to use them.
Luck!
- 07-02-2010, 08:05 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Thanks mate
- 07-02-2010, 08:23 PM #5
I would assume that the method would compare the passed name to the name value in the object. Since they are Strings read the API doc for String class to see which method will test for equality./**
* @return a booleen depending on whether this student
* name matches the argument
*/
public boolean matchName(String name){
//COMPLETE THIS YOURSELF
}
What "value" and what does "correctly" mean?what code i need in order to save this value correctly
There is really too much code to sort thru. Can you narrow it down a bit?
- 07-02-2010, 08:47 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Hi Norm, thanks for your reply.
I also thought that was the meaning of that method, hope it is!
How does this look:
If you notice in the school class from above, this segment of code:Java Code:public boolean matchName(String name){ if (name.equals(this.getName())) { return true; } else { return false; }//COMPLETE THIS YOURSELF }
I have commeneted the line which i am having bother with.Java Code:/** * 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)); //line below reads school name line = in.readLine(); this.head.setName(line); // line below reads head of school line = in.readLine(); fields = line.split(","); HeadOfSchool h = new HeadOfSchool(fields[0], fields[1], fields[2], Integer.parseInt(fields[3]), Integer.parseInt(fields[4])); //line below reads in num of lecturers line=in.readLine(); //This is the part where i do not know how to read in number of lecturers //line below reads 1st lecturer line = in.readLine(); fields = line.split(","); Lecturer l = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 2nd lecturer line = in.readLine(); fields = line.split(","); Lecturer m = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 3rd lecturer line = in.readLine(); fields = line.split(","); Lecturer n = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 4th lecturer line = in.readLine(); fields = line.split(","); Lecturer o = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 5th lecturer line = in.readLine(); fields = line.split(","); Lecturer p = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 6th lecturer line = in.readLine(); fields = line.split(","); Lecturer q = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 7th lecturer line = in.readLine(); fields = line.split(","); Lecturer r = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); //line below reads 8th lecturer line = in.readLine(); fields = line.split(","); Lecturer s = new Lecturer(fields[0],fields[1],fields[2], Integer.parseInt(fields[3]),Integer.parseInt(fields[4])); // COMPLETE THE READING IN FROM FILE // ADD YOUR CODE HERE in.close(); } catch (IOException e) { // ADD YOUR OWN CODE HERE } }
The value that i need to save is integer - 8.
its been a while since i have done java, and i cant seem to figure out what needs to be done to save this integer value thats being read in from file, should it be a set method? an assignment? along with instance of an object? man am lost!
- 07-02-2010, 10:13 PM #7
The matches could be
I don't like arg names that match member names. Hence the XJava Code:public boolean matchName(String nameX){ return nameX.equals(name); }
The variable line now contains a String representing the number of lecturers. Say "7".
Look at the Integer class for a method to convert that String to an intLast edited by Norm; 07-02-2010 at 10:16 PM.
- 07-02-2010, 10:20 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Thanks Norm, not sure if i have picked up on what u mean correctly. Does look to the same effect as your meaning?
Java Code:public boolean matchName(String nameX){ if (nameX.equals(this.getName())) { return true; } else { return false; } }
- 07-02-2010, 10:22 PM #9
Your code is a bit redundant. You can return directly the results of the equals() method.
- 07-02-2010, 10:29 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Thanks for that advice, so the equals() method will return a boolean depending on the comparison? What can i use to check the attributes of these methods?
Any idea how to save the numberOfLecturers after reading it in from file?
- 07-02-2010, 10:33 PM #11
Get a copy of the API doc from the Sun site. There is NO way to write a program without access to the API doc.use to check the attributes of these methods
just like anything else the program saves: assign it to a class member variable.how to save the numberOfLecturers after reading it in from file
- 07-02-2010, 10:41 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Thanks again Norm
The class member variable which should store the numberOfLecture value is not located in the class that i am reading in from file.
Can you give me an example of how to assign to a variable from a different class?
- 07-02-2010, 10:48 PM #13
That interesting. Often you read in the data and then build the class object with the data you've read in.The class member variable which should store the numberOfLecture value is not located in the class that i am reading in from file
The other way is to add a setTheNbr(nbr) method to the target class and call it with the value.
- 07-02-2010, 10:55 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Yeah its held me back for a while... as this is partially completed code i believed that i would need to keep all the code to my lecturers commeneted preference. I even thought that it may be accmplished by the means of calling a set method but there are no such methods for the variable i wish to modify. So do you think that making method to ammend this value?
Similar Threads
-
Design Skills?
By preethidv in forum New To JavaReplies: 1Last Post: 08-23-2009, 03:10 PM -
Take Java skills assessments- assess your skills and provide insight on new tests
By michelle in forum Jobs OfferedReplies: 8Last Post: 08-11-2009, 03:57 PM -
Software developer with Java skills
By psoft_davao in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 02:54 PM -
How can I use my skills to the fullest as a JEE programmer?
By Bhavis in forum Suggestions & FeedbackReplies: 0Last Post: 03-30-2009, 11:49 AM -
Test Advisory Panel-Telecommute- Test your Java skills + share insights on Java tests
By michelle in forum Jobs OfferedReplies: 0Last Post: 04-05-2008, 12:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks