Results 1 to 5 of 5
  1. #1
    kazit is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default 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
    Attached Files Attached Files

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    In my opinion, your whole design is wrong starting with your Person class. It starts like so:
    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();
      }
    1) Everything that is public here should be private.
    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;
      }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  5. #5
    kazit is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    I dont understand why you think that this is not important to me????
    I have done a bit of reading there about instanceOf but it seems that an overridden method would be a better solution, is this right??

    If so could anybody give me some pointers on how to implement this in my program?

Similar Threads

  1. question about polymorphism
    By becky in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 11:59 PM
  2. inheritance and polymorphism
    By tester in forum Eclipse
    Replies: 1
    Last Post: 12-21-2008, 05:58 AM
  3. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 04:14 AM
  4. Relation between Polymorphism and Inheritance
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-27-2007, 12:32 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 11:29 PM

Posting Permissions

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