Results 1 to 7 of 7
  1. #1
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Subclass understanding?

    i get the setName has private access in my Worker class, worker also has two subclasses salariedWorker and HourlyWorker. The Worker class obtains the set/get methods. How do i assign name from a file to the Worker name?

    Java Code:
    public class Hw5
    {
      
      public static void main(String[] args) {
      
          Worker wk;
          try{
        Scanner sc = new Scanner(new FileReader("payroll.txt"));
          sc.useDelimiter(",");
          while(sc.hasNext())
          {
            String stuff = sc.next();
              if(stuff.equalsIgnoreCase("Hourly"))
              {
                String name = sc.next();
                wk.setName(name);
              }
              
                 
                 
          }
         }catch(IOException ioe){
           System.out.println("FileNotFoundException");
         }
       } 
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Your problem is in the Worker/SalariedWorker/HourlyWorker class but you thought it would be a good idea to post your main method instead.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Agree with junky. Why do you have the setName method as private? The idea of the setter is to let other classes manipulate information inside the class safely.

  4. #4
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default great...

    I have it set up that way because that was the way we were assigned to set it up by our Professor.

    the class diagram hierarchy looks like this:


    Worker{abstract}

    -name, boss : String
    - hoursWorked : int
    - hourlyRate : double

    // --- Constructors ---
    + Worker( name : String, hoursWorked : int, hourlyRate : double, boss : String )

    // --- Instance Methods : Setter / Mutator Methods ---
    - setName( name : String ) : void
    - setHoursWorked( hoursWorked : int ) : void
    - setHourlyRate( hourlyRate : double ) : void
    - setBoss( boss : String ) : void

    // --- Instance Methods : Getter / Accessor Methods ---
    + getName( ) : String
    + getHoursWorked( ) : int
    + getHourlyRate( ) : double
    + getBoss( ) : String

    // --- Instance Methods : Other
    + getWages( ) : abstract double
    + toString( ) : String


    (+ is public, - is private, you get the point)
    thats just for Worker then it branches out to HourlyWorker or SalariedWorker both having seperate getWages() methods and a tostring method.

    In the Hw5 class we can write it any way we want, it has to:
    1) read the data from file and 2) display the results
    When reading from
    file, use the data to construct the appropriate object (HourlyWorker, SalariedWorker,
    TemporaryWorker), and store the objects into an array of Workers.

    i just noticed i haven't made the array...which completely changes this code...any ideas? i'm at a blank.

  5. #5
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    hmm just the objects of Worker...maybe it doesnt change the code, i'm not sure

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If your instructions explicitly say that the setter methods must be private then your only option is to use contructors. The constructors of the subclasses make a call to the super constructor.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    + Worker( name : String, hoursWorked : int, hourlyRate : double, boss : String )

    You have a constructor that takes all the data needed.
    Use that.

Similar Threads

  1. subclass won't compile - why?
    By jDennis79 in forum New To Java
    Replies: 15
    Last Post: 08-02-2010, 09:05 PM
  2. Subclass help
    By amystauff in forum New To Java
    Replies: 1
    Last Post: 05-30-2010, 04:36 AM
  3. Object is a Subclass of
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:42 AM
  4. Subclass name to be a parameter?
    By Peetahzee in forum New To Java
    Replies: 6
    Last Post: 12-12-2009, 03:51 PM
  5. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM

Posting Permissions

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