It works but is it correct?
I'm very new here, and I may be over thinking things. But ya'll seem to have already heard every question :)
My assignment is "Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered application should terminate.
My Question is......does this mean I need at least 2 classes in one code project, (ie: Payroll.java) or do I need 2 seperate .java projects (Employee.java and Payroll.java)
The code I have is:
//Payroll Program Part 1
//Program calculates employee pay
import java.util.Scanner;// program uses class Scanner
publicclass Payroll
{
privatedouble rate;
privatedouble hours;
private String name;
//Constructor to store data
public Payroll(String Name,double PayRate,double Hours)
{
String name = Name;
double rate = PayRate;
double hours = Hours;
}
privatestaticvoid Quit()
{
System.out.println("Goodbye");
System.exit(0);
}
// main method begins execution of Java application
publicstaticvoid main(String args[])
{
// create Scanner to obtain input from command window
Scanner input =new Scanner(System.in);
String name ="";
do{
System.out.print("Enter Employee Name or stop to quit: ");
// prompt for name
name = input.next();// get name
if (name.equals("stop"))
{
System.out.println("stop entered, Thank you");
Quit();
}//end if
else
{
double PayRate;
double Hours;
double Pay;
System.out.print("Enter Employee payrate:$ ");// prompt
PayRate = input.nextDouble();// read first number from user
if (PayRate <= 0)
{
System.out.println ("Invalid amount, Payrate must be positive");
System.out.print("Please enter valid payrate:$ ");
PayRate = input.nextDouble();
}//end if
System.out.print("Enter Employee hours: ");// prompt
Hours = input.nextDouble();// read second number from user
if (Hours <= 0)
{
System.out.println ("Invalid amount, Hours worked must be positive");
System.out.print("Please enter actual hours worked: ");
Hours = input.nextDouble();
}//end if
Pay = PayRate * Hours;// multiply numbers
System.out.printf("Employee Pay for %s, is $%.2f\n", name,
(PayRate * Hours));// display sum
}//end else
}while (!name.equals("stop"));
Quit();
}// end method main
}// end class Payroll
[5150 byte] By [
Kelelaa] at [2007-11-27 10:36:41]

The Employee class should go in it's own java file. While it is possible to place it in the same java file, I'd say to put it in a seperate java file because:
a) you're new to Java
b) you're task is to extend functionality, in which case the Employee class may later be used by other classes, so it makes sense for it to be it's own seperate file.
> The Employee class should go in it's own java file.
> While it is possible to place it in the same java
> file, I'd say to put it in a seperate java file
> because:
> ) you're new to Java
> b) you're task is to extend functionality, in which
> case the Employee class may later be used by other
> classes, so it makes sense for it to be it's own
> seperate file.
I agree 100% w/ what bheilers is saying. Employee needs to be a separately identifiable class. There potentially is a lot more that it would be doing besides payroll.
Also, I'd clear out your main of all that code. Main should be a very small routine whose job is to call the major program modules and perhaps, perform some preliminary branching logic, but not much. If you do agree to shrink the main, you will need to take much of its previous code and break it down into smaller more managable routines.
bheilers: I haven't seen you before, but I hope to see more of you. I like your advice.
Thanks ya'll!
I really appreciate your input and I will make the files seperate. I think that will be simpler anyway. At least I hope so.
I have been on this today for 18 hours straight, so I can't even read my own code anymore. I will try again tomorrow.
I had the stupid thing working perfectly, now I can't get it to calculate the pay and I'm too tired to see where I'm messing up. Probably a capital or a colon where it should be a semi-colon :(
Yes, you are learning that the compiler is very unforgiving when it comes to spelling or capitalization mistakes. It's a good lesson to learn.
I found all of my upper/lower case mistake, mistakes my instructor years ago would have called "Mickey Mouse Mistakes"
I'm down to one error
C:\>javac Payroll.java
Payroll.java:252: cannot find symbol
symbol : variable getName
location: class Employee
}while (!emp.getName.equals("stop"));
^
1 error
Can I not make the "Not equal to" statement this way? It appears the error is because of the ! in front of the name....
So how do I write "Not Equal To "stop"
I know there is a way to write not equal
Then again I'm sure this assignment was not supposed to take me 20+ hours either.
Maybe I should stick to SQL I seem to have a lot less problems with it ;)
you are correct in using "!" to mean not. The problem in that line of code isn't that, it's that the compiler can't find the getName method for emp. Look closely into that. If you can't find a solution, post the rest of your code and highlight the offending line somehow with a comment (something like // <==== error here **********)
I found it!!!!
}while (emp.getName() != ("stop"));
Thanks for the help! At 3:45 in the morning, just having someone to bounce off of made a HUGE difference :-)
> I found it!!!!
>
> }while (emp.getName() != ("stop"));
>
> Thanks for the help! At 3:45 in the morning, just
> having someone to bounce off of made a HUGE
> difference :-)
No, please don't use this code. You shouldn't use == or != with strings.Strings are objects and you cannot test objects for equality with the == operator (you can only do this for primitive types). You were on the right track before.
It should be: while (!emp.getName().equals("stop"));
I'm not sure if I feel worse that I had it right the first time except for leaving off the ()
or if I feel worse because I made such a silly mistake :(
Thanks again!
I changed that back and the program does compile correctly now :) It did with the
}while (emp.getName() != ("stop"));
But I can see where that is not good programming and you are correct about the testing.
Now I have had to backspace on every word I have typed I think, so I am going to get a little sleep. I still have a bit of a problem with my loop, it doesn't want to start where I want it to. But I have fixed that before I am just too tired to mess with it now.
I really enjoyed your company tonight as much as your help
Thanks!~
you're welcome and good luck
I have worked out all my other Mickey Mouse mistakes, and my program compiles and runs ok. However, I do not like the following:
(I'm ready for the men with white coats over this...ANY help is appreciated!)
C:\>java Payroll
Enter Employee Name or stop to quit: Name
Enter Employee Name : Name ***Is there anyway I can make this work without printing Enter Employee Name or stop to quit: and Enter Employee Name?****Enter Employee payrate:$ 8
Enter Employee hours: 8
Name's weekly gross pay will be $64.00
Enter Employee Name or stop to quit: Enter Employee Name : Name ***I only want Enter Employee Name to print ***Enter Employee payrate:$ 8
Enter Employee hours: 8
Name's weekly gross pay will be $64.00
Enter Employee Name or stop to quit: Enter Employee Name : stop
stop entered, Thank you
Goodbye
C:\>
Here is the code I am working on now:
//Payroll Program Part 3
//Program calculates employee pay
import java.util.Scanner;// program uses class Scanner
import java.text.*;
public class Payroll
{
private static void Quit()
{
System.out.println("Goodbye");
System.exit (0);
}
// main method begins execution of Java application
public static void main(String args[])
{
Employee emp = new Employee();
// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);
do{
System.out.print("Enter Employee Name or stop to quit: "); // prompt for name
emp.setName(input.nextLine());// get name
/* I have tried emp.setName(input.next()); It worked before I added the Employee class*/
if(emp.getName().equals("stop"))
{
System.out.println("stop entered, Thank you");
Quit();
} //end if
else
System.out.print("Enter Employee Name : "); // prompt for name
emp.setName(input.nextLine());// get name
if(emp.getName().equals("stop"))
{
System.out.println("stop entered, Thank you");
Quit();
} //end if
else
System.out.print("Enter Employee payrate:$ "); // prompt
emp.setpayRate(input.nextDouble());// read first number from user
if(emp.getpayRate() <= 0)
{
System.out.println("Invalid amount, Payrate must be positive");
System.out.print("Please enter valid payrate:$ ");
emp.setpayRate(input.nextDouble());
} //end if
System.out.print("Enter Employee hours: ");// prompt
emp.sethours(input.nextDouble());// read second number from user
if(emp.gethours() <= 0)
{
System.out.println ("Invalid amount, Hours worked must be positive");
System.out.print("Please enter actual hours worked: ");
emp.sethours(input.nextDouble());
} //end if
System.out.printf("\n%s's weekly gross pay will be $%.2f\n", emp.getName(), emp.getPay());
}while(!emp.getName().equals("stop"));
Quit();
}// end method main
} // end class Payroll
// Class Employee holds employee information
class Employee
{
private String name;
private double payRate;
private double hours;
//default constructor
public Employee()
{
name = "";
payRate = 0;
hours = 0;
}//end default constructor
//Parameterized Constructor
public Employee(String name, double payRate, double hours)
{
this.name = name;
this.payRate = payRate;
this.hours = hours;
}//end constructor
public void setName(String name) {
this.name = name;
}
String getName()
{
return name;
}
public void setpayRate ( double payRate )
{
this.payRate = payRate;
}
public double getpayRate()
{
return payRate;
}
public void sethours ( double hours )
{
this.hours = hours;
}
public double gethours()
{
return hours;
}
public double getPay()
{
return (payRate * hours);
}
}//end Class Employee
I figured it out :)
What a relief.
On to bigger problems.............