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

    Default Java Help - Inheritance

    Need help with my code I keep receiving errors for some reason and cannot figure it out.

    import java.util.Scanner; // needed to import Scanner class
    import java.util.ArrayList;
    import java.util.*;

    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(); // create employee object and initialize it
    cpb09eWageWorker_A5 wage = new cpb09eWageWorker_A5();
    System.out.println("Enter the Objects Name ");
    String Name = input.next(); // input name from user
    wage.SetName(Name); // set the name in employee class


    System.out.println("Enter the Wage ");
    int Wage = input.nextInt(); // input wage from user
    wage.SetWage(Wage); // set wage of employee

    System.out.println("Enter the Hours ");
    int hours = input.nextInt(); // input hours from the user
    wage.SetHours(hours); //set the hours

    wage.SetSalary(hours * Wage); // calculate and set the salary

    emplist.add(wage); // set the object in array list


    }

    else if(input.equals("2")){
    //create Executive object
    }

    else if(input.equals("3")){
    //exit the program
    }
    }
    }

  2. #2
    SRaith is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Java Help - Inheritance

    Java Code:
    String input = scan.nextLine();
    Java Code:
    String Name = input.next(); // input name from user
    Java Code:
    int Wage = input.nextInt(); // input wage from user
    Java Code:
    int hours = input.nextInt(); // input hours from the user
    input isn't a scanner object, so you can't call the methods "next()" and "nextInt()", input is just your String :).

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

    Default Re: Java Help - Inheritance

    So how would I fix it? Sorry I am fairly new with Java. I want to 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

  4. #4
    SRaith is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Java Help - Inheritance

    Java Code:
    import java.util.Scanner; // needed to import Scanner class
    import java.util.ArrayList;
    import java.util.*;
    
    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(); // create employee object and initialize it
    cpb09eWageWorker_A5 wage = new cpb09eWageWorker_A5();
    System.out.println("Enter the Objects Name ");
    String Name = input.next(); // input name from user
    wage.SetName(Name); // set the name in employee class
    
    
    System.out.println("Enter the Wage ");
    int Wage = input.nextInt(); // input wage from user
    wage.SetWage(Wage); // set wage of employee
    
    System.out.println("Enter the Hours ");
    int hours = input.nextInt(); // input hours from the user
    wage.SetHours(hours); //set the hours
    
    wage.SetSalary(hours * Wage); // calculate and set the salary
    
    emplist.add(wage); // set the object in array list
    
    
    }
    
    else if(input.equals("2")){
    //create Executive object
    }
    
    else if(input.equals("3")){
    //exit the program
    }
    }
    }
    Just change "input" in line 29, 34 and 38 to "scan".
    I think this should work :P.

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

    Default Re: Java Help - Inheritance

    Thanks it worked!

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

    Default Re: Java Help - Inheritance

    For the following code after I get the wage and salary I try and calculate and set the salary. But when I run the program I cannot get it to take the wage and hours that the user enters and calculate it. How can I do that?

    if(input.equals("1")){
    //create WageWorker object

    cpb09eEmployee_A5 obj = new cpb09eEmployee_A5(); // create employee object and initialize it

    cpb09eWageWorker_A5 wage = new cpb09eWageWorker_A5();

    System.out.println("Enter the Objects Name ");
    String Name = scan.next(); // input name from user
    wage.SetName(Name); // set the name in employee class


    System.out.println("Enter the Wage ");
    int Wage = scan.nextInt(); // input wage from user
    wage.SetWage(Wage); // set wage of employee

    System.out.println("Enter the Hours ");
    int hours = scan.nextInt(); // input hours from the user
    wage.SetHours(hours); //set the hours

    wage.SetSalary(hours * Wage); // calculate and set the salary

    emplist.add(wage); // set the object in array list


    }

Similar Threads

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