Simple newby program, multiple problems - help please!

P

Phrank

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();

}
}
 
B

Bryce (Work)

======================
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);
}
 
R

Roedy Green

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

Roedy Green

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
 
P

Phrank

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
 
M

mromarkhan

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
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top