Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple newby program, multiple problems - help please!

Reply
Thread Tools

Simple newby program, multiple problems - help please!

 
 
Phrank
Guest
Posts: n/a
 
      04-16-2004
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();

}
}

 
Reply With Quote
 
 
 
 
Bryce (Work)
Guest
Posts: n/a
 
      04-16-2004
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-16-2004
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-16-2004
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-16-2004
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.
 
Reply With Quote
 
Phrank
Guest
Posts: n/a
 
      04-17-2004
On Fri, 16 Apr 2004 23:51:56 GMT, Roedy Green
<(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-17-2004
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.
 
Reply With Quote
 
mromarkhan@rogers.com
Guest
Posts: n/a
 
      04-17-2004
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
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newby - very simple program that don't work Stefano C++ 4 10-26-2007 09:45 AM
I think I'm gonna cry. (or newby problems with simple string function) robbie.carlton@gmail.com C Programming 9 10-03-2005 10:49 PM
Dropdown database bound - simple screen with only dropdownbox, bestway to solve it [newby] Pietje puk ASP .Net 0 11-16-2004 11:07 AM
newby: eliminating excess flipflops from simple state machine Paul Guy VHDL 3 05-27-2004 01:16 AM
Newby question - any airport security problems with digital? Anthony Buckland Digital Photography 5 12-05-2003 05:00 PM



Advertisments