java - Typecasting methods with "instanceof" -
so i'm running problems setting methods, i've been staring @ computer screen long. i'm having difficulty 4 of 5 methods. last 2 methods need call compscistudent class, hence use of 'instanceof' because not students in array compscistudents. 2 methods need add cs language student in student array, , other 1 check see how many compscistudents in array know specific language. other 2 methods deal whole array. first 1 add test score specific student in position, each student in array have specific name when called in array. last method getting average average test score. method call average test score both students , compscistudents in array , average each separately take average of averages. take @ class structure:
public class school { private student[] thestudents; public school() { this.thestudents = new student[] { null };// needs start out empty } /* * next 2 methods allow user add or drop student * student array school ??also enroll student, should able * assign class student, i.e. calculas, history, etc?? */ public void enrollstudent(student newstudent) { student totalstudents[] = new student[thestudents.length + 1]; (int = 0; < thestudents.length; i++) { totalstudents[i] = thestudents[i]; } totalstudents[thestudents.length] = newstudent; thestudents = totalstudents; } public void dropstudent(student dropstudent) { student totalstudents[] = new student[thestudents.length - 1]; (int = 0; > thestudents.length; i--) { totalstudents[i] = thestudents[i]; } totalstudents[thestudents.length] = dropstudent; thestudents = totalstudents; } // add test score student public double addtestscore(string newstudent, double testscore) { testscores[posi] = testscore; } /* * count number of students in given class, not school */ public int countclasssize(string course) { // need access how course names stored school // count size. string coursename; if (this.coursename.equals(course)) { (int = 0; < thestudents.length; i++) { int count = count + 1; } } } /* * average average score of student array * student array made of regular students , * compscistudents. average should take average of * both average score students , average score of * compscistudents , return average average. */ public double averageaveragescore(string course) { double averagecoursescore; if(this.thestudents.equals(course)/2){ return averagecoursescore; } } /* * add programming language cs students @ school need * use instanceof proper type casting */ public string addprogramminglanguage(string studentname, string programlanguage) { (int = 0; ; i++); if (this.thestudents[i] instanceof compscistudent); compscistudent tempstudent = (compscistudent)this.thestudents[i]; tempstudent.knowslanguage(knowthislanguage); } /* * count number of students in school know * programming language, again need typecast */ public int numberthatknowlanguage(string programlanguage) { for(); if(this.thestudents[i] instanceof compscistudent); compscistudent tempstudent = (compscistudent)this.thestudents[i]; tempstudent.learnedlanguage(knowthislanguage); }
the main problem facing design of application. i'll suggest few design improvements should in solving problems.
- rather declaring
thestudents[]
array, make listlist<student> students
. substantially reduce effort putting in while enrolling student , dropping student. - adding subjects student using explicit method call should not ideal case. subjects should associated particular student. in order adhere design principles, student object should designed in such way that, "student has-a name, has-a id, has subjects". take care of adding subjects while creating student object. eliminates need of adding method.
- categorizing student computer science student, electronics student etc. should handled in separate class let's call department. sake of simplicity, consider 2 departments. 1 computer science , electronics. "each department has students". design principles(has-a relation), should creating
list<student> students
(like mentioned in point no. 1.) in department class , school class havelist<department> departments
. it's 1 many relation. - don't forget override
hashcode()
,equals(object obj)
methods they'll necessary while comparing 2 or morestudent
objects.
so now, can "school
has departments. each department
has students. each student
has subjects".
and classes should this:
public class school { private list<department> departments; /* * methods , constructors goes here. */ } public class department { private list<student> students; /* * methods , constructors goes here. */ } public class student { private string name; private string id; private string coursestream; private list<string> subjects; /* * methods , constructors goes here. */ }
note: i'm not saying perfect design. quite subtle scenario have explained. again, code design might vary person person.
Comments
Post a Comment