Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Java Help - Inheritance and OOP

  1. #1
    EdOBannon is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default 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

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

    Default Re: Java Help - Inheritance and OOP

    You will need to tell us what is or isn't working, what errors you're getting.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  4. #4
    EdOBannon is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default 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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Help - Inheritance and OOP

    I enter the information but cannot get back to the main page
    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.

    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.
    Fubarable likes this.

Similar Threads

  1. Java Help - Inheritance
    By EdOBannon in forum New To Java
    Replies: 5
    Last Post: 11-06-2011, 02:18 AM
  2. Help - How the inheritance of java works?
    By freetochoose in forum Advanced Java
    Replies: 4
    Last Post: 05-15-2011, 01:13 PM
  3. Inheritance in Java
    By socboy6579 in forum New To Java
    Replies: 2
    Last Post: 02-08-2011, 04:40 PM
  4. Help with java gui and inheritance
    By sssss in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2011, 04:38 AM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 02:25 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
  •