Results 1 to 5 of 5
- 03-30-2009, 02:12 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Polymorphism Array Validation Help
Hi all,
I am sort of stuck with a problem i face within a program that i am creating for a uni assignment see attached:
Outline of program is that i have a Person class, Student, Lecturer, Module and Clerical classes. I am using a polymorphic array and for the students, lecturers and clerical staff and input is just provided from the console.
Problem i face is when i am assigning modules to both the students and lecturers i first search the person array to see if the student, lecturer exists and then assign a module but if i perform the search and a clerical person exits i want to prevent the user from assigning modules to a clerical person.
I dont really have much of a clue on how to implement this and any help would be great!
Cheers
-
In my opinion, your whole design is wrong starting with your Person class. It starts like so:
1) Everything that is public here should be private.Java Code:public class Person { public String name; public String date; public int salary; Module mod1=null,mod2=null,mod3=null; public Person() { Scanner scan = new Scanner(System.in); System.out.println("Please enter name: "); name = scan.nextLine(); }
2) Your Person constructor and in fact the entire Person class should not directly interact with the user; that's what other classes are for. It should just accept data that defines a person via parameters and getter methods. For instance, if these were my Person class, I'd get rid of the Scanner object and any direct user interaction and make my constructor like so:
Java Code:public Person(String name, String date, int salary) { this.name = name; this.date = date; this.salary = salary; }
-
By the way, one way to solve your main problem would be to use the instanceOf operation to check if a Person is a type that can accept a module, but I get suspicious of my program design any time I think of adding this operation into my program. It suggests to me that the array is holding objects that are just too disparate.
-
I guess that this wasn't that important to you. Must we assume that any other post of yours isn't important to you or us either?
- 03-30-2009, 10:30 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
question about polymorphism
By becky in forum New To JavaReplies: 4Last Post: 02-11-2009, 10:59 PM -
inheritance and polymorphism
By tester in forum EclipseReplies: 1Last Post: 12-21-2008, 04:58 AM -
what is polymorphism
By Nari in forum New To JavaReplies: 5Last Post: 04-04-2008, 03:14 AM -
Relation between Polymorphism and Inheritance
By janakiram.attuluri in forum Advanced JavaReplies: 1Last Post: 12-26-2007, 11:32 PM -
what's polymorphism?
By christina in forum New To JavaReplies: 2Last Post: 08-05-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks