Results 1 to 5 of 5
Thread: Java Help - Inheritance and OOP
- 11-06-2011, 03:22 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Java Help - Inheritance and OOP
Need help with an assignment. I cannot figure out what I am doing wrong. This is the description for the assignment:
For this assignment you will extend the Employee class to build WageWorker, and Executive classes. In the main method of the TestDrive class you will create an Employee ArrayList that will hold all of the Employees. In a continuous loop you will ask the user if they would like to enter a new WageWorker or Executive or exit the program (see Helpful Hints for a suggestion on how to do this). If the user selects to enter a WageWorker or Executive you will have to create that type of object. Then ask the user for the object’s name and salary. If the object is a WageWorker you will need to ask for Wage and Hours to calculate the salary, and if the object is an Executive you will have to ask for the executive’s Title. Then add the object to the ArrayList.
If the user chooses to exit the program then you should print out (to the screen) a summary of all the employees and their names, salaries, and titles if they are executives.
Java Code:import java.util.Scanner; // needed to import Scanner class import java.util.ArrayList; public class cpb09eTestDrive_A5 { // Create Scanner private static Scanner scan = new Scanner(System.in); //Program Execution public static void main(String[] args) { ArrayList<cpb09eEmployee_A5> emplist = new ArrayList<cpb09eEmployee_A5>(); //Prompt System.out.println("Please indicate the type of employee by typing its number: "); System.out.println("1 - Wage Worker"); System.out.println("2 - Executive"); System.out.println("3 - Exit"); //Read String input = scan.nextLine(); if(input.equals("1")){ //create WageWorker object cpb09eEmployee_A5 obj = new cpb09eEmployee_A5(); cpb09eWageWorker_A5 wage = new cpb09eWageWorker_A5(); System.out.println("Enter the Objects Name "); String Name = scan.next(); wage.SetName(Name); System.out.println("Enter the Wage "); int Wage = scan.nextInt(); wage.SetWage(Wage); System.out.println("Enter the Hours "); int hours = scan.nextInt(); wage.SetHours(hours); wage.SetSalary(hours * Wage); emplist.add(wage); } else if(input.equals("2")){ //create Executive object cpb09eEmployee_A5 obj = new cpb09eEmployee_A5(); cpb09eExecutive_A5 exe_obj = new cpb09eExecutive_A5(); System.out.println("Enter the Objects Name "); String Name = scan.next(); exe_obj.SetName(Name); System.out.println("Enter the Salary "); int Sal = scan.nextInt(); exe_obj.SetSalary(Sal); System.out.println("Enter the Title "); String title = scan.next(); exe_obj.SetTitle(title); emplist.add(exe_obj); } else if(input.equals("3")){ //exit the program int i=1; System.out.println("The employee list is: " + emplist.size()); for(cpb09eEmployee_A5 item : emplist) { if(item instanceof cpb09eWageWorker_A5) {cpb09eWageWorker_A5 wage= (cpb09eWageWorker_A5) item; System.out.println((i++) + " " + wage.GetName() + " " + wage.GetSalary()); } else { cpb09eExecutive_A5 exe_obj = (cpb09eExecutive_A5) item; System.out.println((i++) + " " + exe_obj.GetName() + " " + exe_obj.GetSalary() + " " + exe_obj.GetTitle() ); } } } } }Last edited by pbrockway2; 11-06-2011 at 03:36 AM. Reason: code tags added
-
Re: Java Help - Inheritance and OOP
You will need to tell us what is or isn't working, what errors you're getting.
- 11-06-2011, 03:40 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Java Help - Inheritance and OOP
When posting code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the code is readable. It is also a good idea to use spaces (<=4) and not tabs to indent code, and to do so consistently as web browsers tend to render tabs quite generously.
- 11-06-2011, 03:41 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 12
- Rep Power
- 0
Re: Java Help - Inheritance and OOP
After I run 1 (WageWorker) or 2 (Executive) I enter the information but cannot get back to the main page to exit the program to display the summary.
- 11-06-2011, 03:48 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Java Help - Inheritance and OOP
It's not that you cannot get back, but rather that you do not go back to the menu once an option has been chosen. Basically an option is chosen, one of the if blocks is followed, and the program (having nothing more to do) ends.I enter the information but cannot get back to the main page
Read the assignment instructions carefully.
"In a continuous loop you will ask the user if they would like to enter a new WageWorker or Executive or exit the program (see Helpful Hints for a suggestion on how to do this)."
Those Helpful Hints are your first port of call. If you have trouble deciphering what they're saying, post them and say what you are having trouble understanding.
Similar Threads
-
Java Help - Inheritance
By EdOBannon in forum New To JavaReplies: 5Last Post: 11-06-2011, 02:18 AM -
Help - How the inheritance of java works?
By freetochoose in forum Advanced JavaReplies: 4Last Post: 05-15-2011, 01:13 PM -
Inheritance in Java
By socboy6579 in forum New To JavaReplies: 2Last Post: 02-08-2011, 04:40 PM -
Help with java gui and inheritance
By sssss in forum Advanced JavaReplies: 8Last Post: 01-19-2011, 04:38 AM -
Multiple Inheritance in java
By paty in forum New To JavaReplies: 4Last Post: 08-02-2007, 02:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks