Results 1 to 7 of 7
Thread: Subclass understanding?
- 04-13-2011, 04:30 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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"); } } }
- 04-13-2011, 04:36 AM #2
Your problem is in the Worker/SalariedWorker/HourlyWorker class but you thought it would be a good idea to post your main method instead.
- 04-13-2011, 05:12 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-13-2011, 06:08 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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.
- 04-13-2011, 06:08 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
hmm just the objects of Worker...maybe it doesnt change the code, i'm not sure
- 04-13-2011, 06:14 AM #6
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.
- 04-13-2011, 08:57 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
subclass won't compile - why?
By jDennis79 in forum New To JavaReplies: 15Last Post: 08-02-2010, 09:05 PM -
Subclass help
By amystauff in forum New To JavaReplies: 1Last Post: 05-30-2010, 04:36 AM -
Object is a Subclass of
By AndrewM16921 in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:42 AM -
Subclass name to be a parameter?
By Peetahzee in forum New To JavaReplies: 6Last Post: 12-12-2009, 03:51 PM -
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks