Simple newby program, multiple problems - help please!

Discussion in 'Java' started by Phrank, Apr 16, 2004.

  1. Phrank

    Phrank Guest

    Hello, this should be a simple program, but I seem to be stumbling at
    about every turn. I've dealing with an abstract class called Employee
    and a subclass named CommissionWorker. Here are my three main
    problems listed in order first, and below that will be the code:

    1) Unable to create a second constructor in the subclass
    CommissionWorker - I keep getting an error message stating
    "Employee(java.lang.String,java.lang.String) in Employee cannot be
    applied to () {"

    2) I've got the getFirstName and getLastName methods in my abstract
    class, but I can't figure out how to implement those in the subclass.

    3) The program is compiling and outputting to the screen, but the
    values for salary, commission and earnings are all zeros.

    What I need to be able to do is create multiple objects with plugging
    in different names, salaries and quantities and have it output the
    person's name, salary, quatity of items sold, total commission earned
    and total earnings (salary + commission). I know that what I'm doing
    wrong starts with the constructor, but I don't know how to fix this.
    I'd appreciate any help. Here's my code (abstract class is on top):

    ======================
    abstract class Employee
    {
    private String firstName;
    private String lastName;

    abstract void getFirstName(String fName); // abstract method
    abstract void getLastName(String lName); // abstract method

    Employee(String FName, String LName)
    {
    System.out.println("The new employee is " + FName + " " + LName);
    }
    abstract void earnings(); // abstract method
    }

    ====================

    import java.text.*;

    public class CommissionWorker extends Employee
    {

    float salary = 0.0f;
    int qty = 0;
    int commiss = 0;
    float earn = 0.0f;

    DecimalFormat df = new DecimalFormat(".00");


    CommissionWorker() // constructor
    {
    super("Tom", "Bell");
    }


    // CommissionWorker(String fName, String lName, float salary)
    // {
    // System.out.println("First Name: " + fName);
    // System.out.println("Last Name: " + lName);
    // System.out.println("Salary: " + this.setSalary);
    // }

    void getFirstName(String fName)
    {
    System.out.println("First Name: " + fName);
    }

    void getLastName(String lName)
    {
    System.out.println("Last Name: " + lName);
    }

    float setSalary(float salary)
    {
    System.out.println("Salary is: $" + this.salary);
    return salary;
    }

    int setQuantity(int qty)
    {
    return this.qty;
    }

    void setCommission()
    {
    commiss = this.qty * 10;
    System.out.println("Commission for " + this.qty +
    " items is: $" + commiss);
    }
    void earnings()
    {
    earn = this.salary + this.commiss;
    System.out.println("Total earnings: $" +
    df.format(this.earn));
    }

    public static void main(String[] args)
    {
    CommissionWorker cw1 = new CommissionWorker();
    cw1.setSalary(100000);
    cw1.setQuantity(100);
    cw1.setCommission();
    cw1.earnings();

    // CommissionWorker cw2 = new CommissionWorker();

    }
    }
     
    Phrank, Apr 16, 2004
    #1
    1. Advertising

  2. Phrank

    Bryce (Work) Guest

    On Fri, 16 Apr 2004 14:40:21 -0400, Phrank <> wrote:

    >======================
    >abstract class Employee
    >{
    > private String firstName;
    > private String lastName;
    >
    > abstract void getFirstName(String fName); // abstract method
    > abstract void getLastName(String lName); // abstract method
    >
    > Employee(String FName, String LName)
    > {
    >System.out.println("The new employee is " + FName + " " + LName);
    > }
    > abstract void earnings(); // abstract method
    >}
    >
    >====================
    >
    >import java.text.*;
    >
    >public class CommissionWorker extends Employee
    >{
    >
    > float salary = 0.0f;
    > int qty = 0;
    > int commiss = 0;
    > float earn = 0.0f;
    >
    > DecimalFormat df = new DecimalFormat(".00");
    >
    >
    > CommissionWorker() // constructor
    > {
    > super("Tom", "Bell");
    > }
    >
    >
    >// CommissionWorker(String fName, String lName, float salary)
    >// {
    >// System.out.println("First Name: " + fName);
    >// System.out.println("Last Name: " + lName);
    >// System.out.println("Salary: " + this.setSalary);
    >// }


    This requires a default constructor for your base class. Your abstract
    Employee class has no default constructor. Try:

    CommissionWorker(String fName, String IName, float salary) {
    super(fName, iName);
    }

    --
    now with more cowbell
     
    Bryce (Work), Apr 16, 2004
    #2
    1. Advertising

  3. Phrank

    Roedy Green Guest

    On Fri, 16 Apr 2004 14:40:21 -0400, Phrank <> wrote or quoted :

    >Hello, this should be a simple program, but I seem to be stumbling at
    >about every turn.


    Try comp.lang.java.help for newbie problems. The people there will be
    more patient.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 17, 2004
    #3
  4. Phrank

    Roedy Green Guest

    On Fri, 16 Apr 2004 14:40:21 -0400, Phrank <> wrote or quoted :

    > Employee(String FName, String LName)
    > {
    >System.out.println("The new employee is " + FName + " " + LName);
    > }

    That is a very strange constructor. Normally it would do something
    like this:

    Employee(String givenName, String surname )
    {
    this.givenName = givenName;
    this.surname = surname;
    }

    Note that variables must begin with a lower case letter if you don't
    want to drive people crazy trying to make sense of your code. See
    http://mindprod.com/jgloss/codingconventions.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 17, 2004
    #4
  5. Phrank

    Roedy Green Guest

    On Fri, 16 Apr 2004 14:40:21 -0400, Phrank <> wrote or quoted :

    >public class CommissionWorker extends Employee


    you can solve your problem two ways:

    1. put both these classes into the same package, e.g.
    com.phrank.dilbert so that they can see each other. This is the grown
    up solution. EVERY class should be in a package to give it a globally
    unique name.

    See http://mindprod.com/jgloss/package.html

    2. declare the Employee class public.

    See http://mindprod.com/jgloss/scope.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 17, 2004
    #5
  6. Phrank

    Phrank Guest

    On Fri, 16 Apr 2004 23:51:56 GMT, Roedy Green
    <> wrote:

    >On Fri, 16 Apr 2004 14:40:21 -0400, Phrank <> wrote or quoted :
    >
    >>public class CommissionWorker extends Employee

    >
    >you can solve your problem two ways:
    >
    > 1. put both these classes into the same package, e.g.
    >com.phrank.dilbert so that they can see each other. This is the grown
    >up solution. EVERY class should be in a package to give it a globally
    >unique name.
    >
    >See http://mindprod.com/jgloss/package.html
    >
    >2. declare the Employee class public.
    >
    >See http://mindprod.com/jgloss/scope.html


    Thanks Bryce and Roedy! I appreciate the help. I've changed my
    constructor around, and it works a lot better. As far as packaging
    the classes, I'm going to be sending this to a friend, and I'm not
    sure of his directories and paths. Maybe I don't need to know that, I
    obviously don't have much experience packaging classes. I read the
    site you suggested above on packaging, and I'm still a bit foggy on
    how to go about doing it. I'll keep working at it and I'll slowly but
    surely get there. Until then, I'll check out the other java.help
    group (along with this one ... this one has about 40,000 entries vs
    13,500 for the .help) for examples and guidance. Thank you both so
    much for your help!

    Frank
     
    Phrank, Apr 17, 2004
    #6
  7. Phrank

    Roedy Green Guest

    On Sat, 17 Apr 2004 00:42:36 -0400, Phrank <> wrote or quoted :

    >Maybe I don't need to know that, I
    >obviously don't have much experience packaging classes.


    see http://mindprod.com/jgloss/package.html
    If you are the author, YOU make up the package names. They are the
    same on every user of your code.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 17, 2004
    #7
  8. Phrank

    Guest

    import java.text.*;

    public class CommissionWorker extends Employee
    {

    float salary = 0.0f;
    int qty = 0;
    int commiss = 0;
    float earn = 0.0f;

    DecimalFormat df = new DecimalFormat(".00");


    CommissionWorker() // constructor
    {
    super("Tom", "Bell");
    }


    CommissionWorker(String fName, String lName, float salary)
    {
    super(fName, lName);
    this.setSalary(salary);
    }



    public void setSalary(float salary)
    {
    this.salary = salary;
    System.out.println("Salary is: $" + this.salary);

    }

    void setQuantity(int qty)
    {
    this.qty = qty;
    }

    int getCommission()
    {
    commiss = this.qty * 10;
    System.out.println("Commission for " + this.qty +
    " items is: $" + commiss);
    return commiss;
    }
    float getEarnings()
    {
    earn = this.salary + this.commiss;
    System.out.println("Total earnings: $" +
    df.format(this.earn));
    return earn;
    }

    public static void main(String[] args)
    {
    CommissionWorker cw1 = new CommissionWorker();
    cw1.setSalary(100000);
    cw1.setQuantity(100);
    cw1.getCommission();
    cw1.getEarnings();

    CommissionWorker cw2 = new CommissionWorker("omar","khan",1000f);

    }
    }

    abstract class Employee
    {
    protected String firstName;
    protected String lastName;

    String getFirstName()
    {
    System.out.println("First Name: " + firstName);
    return firstName;
    }

    String getLastName()
    {
    System.out.println("Last Name: " + lastName);
    return lastName;
    }

    Employee(String FName, String LName)
    {
    this.firstName=FName;
    this.lastName=LName;
    System.out.println("The new employee is " + FName + " " + LName);
    } abstract float getEarnings(); // abstract method
    }
     
    , Apr 18, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Guy
    Replies:
    3
    Views:
    635
    Paul Guy
    May 27, 2004
  2. KK
    Replies:
    2
    Views:
    728
    Big Brian
    Oct 14, 2003
  3. Replies:
    9
    Views:
    442
    Old Wolf
    Oct 3, 2005
  4. Replies:
    14
    Views:
    558
    Karl Heinz Buchegger
    Nov 2, 2005
  5. Stefano
    Replies:
    4
    Views:
    355
    Stefano
    Oct 26, 2007
Loading...

Share This Page