Working with OOP, constructors, arrays? help....
hi all,
If you think this thread looks familiar, you have probably seen it before. Thousands of students come to this forum every year looking for help because, lets face it, its much easier to see code and ask questions than to learn from some old textbook.
So here I am, I need some help with my payroll program and anyone that knows more than me is welcome to answer (which should be almost anyone on here) Here is my payroll code and below that I will list the new instructions and my progress. Thank you.
import java.util.Scanner;
publicclass Payroll
{
publicstaticvoid main( String args[] )
{
Scanner input =new Scanner ( System.in );
float number1;
float number2;
float sum;
System.out.println();
System.out.print("Payroll Calculation Program\nPlease type STOP to end program");
System.out.println();
System.out.println();
System.out.println();
System.out.print("Enter Employee Name:");
String empName = input.nextLine();
while ( !empName.equalsIgnoreCase("stop") )
{
System.out.printf("Enter Hourly Rate for %s: $",empName );
number1 = input.nextFloat();
while (number1 < 0.00){
System.out.print("Please re-enter using positive numbers only: $");
number1 = input.nextFloat();
}
System.out.print ("Enter Hours Worked: ");
number2 = input.nextFloat();
while (number2 < 0.00){
System.out.print ("Please re-enter using positive numbers only: ");
number2 = input.nextFloat();
}
sum = number1 * number2;
System.out.printf ("%s's weekly pay is:$%.2f\n",empName,sum);
System.out.println();
input.nextLine();
System.out.print("Enter Employee Name:");
empName = input.nextLine();
}
}
}
I need to modify the program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. I also need to use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay.
So here is what I have done, aside from building the program above. I have begun to build what I think is a constructor within my payroll.java file, and it looks like this empName =new Employee (number1,number2, sum);
and I have also begun work on my employee.java file which looks like thispublicclass Employee{
float number1;
float number2;
float sum;
Employee (float number1,float number2,float sum){
number1 = ;
Yah, I know none of this is probably correct, so please MTV, pimp my....no wait, fix my code!
Thank you guys (sorry, got to through in a few jokes)
[4034 byte] By [
Js_Sola] at [2007-11-27 10:18:40]

> If you think this thread looks familiar, you have probably seen it before.
You're right. Which means that it still exists. Which means search for it.
Can you post the entire *.java file here so we can take a look?
I doubt this:
public class Employee {
float number1;
float number2;
float sum;
Employee (float number1, float number2, float sum) {
number1 = ;
is your entire code.
< Yah, I know none of this is probably correct, so please MTV, pimp my....no wait, fix my code!
for starters:
1) close your brackets.
2) number1 = ; ?
3) Employee (float number1, float number2, float sum) , this is probably OK but when i make my constructers i make them public.
edit
probably i would make something like this:
public class Employee {
float number1;
float number2;
float sum;
public Employee (float number1, float number2, float sum) {
number1 = ; //not sure what you want to do with this...
}
}
Message was edited by:
deAppel
Message was edited by:
deAppel
Ahhh, the old "Here's my code fix it for me because I'm a lazy doofus who is off to play xbox" trick.
> Ahhh, the old "Here's my code fix it for me because
> I'm a lazy doofus who is off to play xbox" trick.
Aren't you being a little hard on him and presumptuous (It's probably a Wii)?
> > Ahhh, the old "Here's my code fix it for me
> because
> > I'm a lazy doofus who is off to play xbox" trick.
>
> Aren't you being a little hard on him and
> presumptuous (It's probably a Wii)?
I need a Wii and I always wash my hands afterwards.
to "Navycoder"-- thanks for nothing, your post is neither constructive, nor insightful. The forum rules state to search for previous answers, i did. The forum states to leave constructive and educated answers - you did not. Of course i have seen it posted before but their programs were built completely different than mine. I would not have posted this if I was able to learn from their post...... i don't make a habit of wasting my time, or others.
To deAppel, thank you, thank you, thank you..... It's nice to have someone who wants to help.
Anyway, unfortunently, this is my entire Employee.java file so far. I have built it based off of prior written threads (see note above :) )but I just don't quite understand where I am going with it. For example this is from a prior thread (written by someone else):
public class Employee {
private String name;
private double hourlyRate;
private double hoursWorked;
// Constructor to initialize the employee with the information provided.
public Employee(String name, double hourlyRate, double hoursWorked) {
setName(name);
setHourlyRate(hourlyRate);
setHoursWorked(hoursWorked);
}
// Method to get the employee name.
public String getName() {
return this.name;
}
// Method to set the employee name.
private void setName(String name) {
this.name = name;
}
// Method to set the hourly rate.
private void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
// Method to set the hours worked.
private void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
// Method to compute the weekly pay.
public double getWeeklyPay() {
return this.hourlyRate *this.hoursWorked;
}
}
I had my hourlyrate and hoursworked coded as number1 and number2. I also used float instead of double. So I started building my code my translating those. It's just getting to a point where I do not recognize the code (i don't know the terms such as: private and set) I am trying to find them in the text book but there are so many chapters and I am completely lost. Thank you to all those who help!
I havn't had time to play my wii, or xbox since I started this class. .... but in regards to your reply, isn't this forum for helping people like me?
I'm not asking you to write the code for me, just point me in the right direction.....
If i wanted just another school credit, I would have taken an easier class. I want to understand the subject of java and the textbook just isn't much help.
I've never been hashed this bad for trying to honestly learn something in my entire life, give me a break guys....
> to "Navycoder"-- thanks for nothing, your post is
> neither constructive, nor insightful.
That's right, throwing a temper tantrum is so gunna want us to help you.
> Of course i have seen it
> posted before but their programs were built
> completely different than mine. I would not have
> posted this if I was able to learn from their
> post
Right! So when you were at school learning to count, your teacher taught you that 1 + 1 = 2. But when asked what 2 + 2 equals, you wail "but you never taught us that"
> isn't this forum for helping people like me?
Yes this forum is for helping people. We do not do their work for them. And it is apparent from this statement in your first post "...no wait, fix my code!" that is exactly what you think help is, us doing your work for you.
Alright guys,
There are so many negative replies, I can only assume I have not presented my problem correctly. I never intended for any of you to build my code or do my work for me.
To flounder- after reviewing your post on other threads, I see you enjoy your superior intellect so much that you will not even share your knowlage with others. Why even take the time to post these negative statements? Everyone else take a look as well, his 5000+ replies have really done a lot of good to improving the information and communication of this entire forum.
....and don't bother replying. I am off this forum for a while. Hopefully the next time I come looking for helpful advice, you'll be spewing your negativity and insults on myspace.
Thank you again to deappel for beginning to help me.
I will bother to reply. You haven't read many of my posts then have you? I have helped plenty of people. Those that get help are people who post their code and ask a specific question and display a genuine desire to work the problem out for themselves and most importantly, use their brain.
Those, like you, who simply plonk their code on the table (so to speak) and expect others to debug it for them without the slightest bit of information from the originator, get nothing but ridicule. Also those that continue to argue with the regulars and think they no better get nbo help at all.
So you make the choice, act like a ****-face pratt and get no help or acknowledge that your initial post was the wrong way to get help, amend your posting strategy and you will no doubt get plenty of help.
Or you can simply ran away and never return which I'm sure nobody here gives a flying **** as to whether you do or don't.
Let's look at those parameters: float, float, float. Hmmmmm, not very useful since the requirement wants you to keep employee name and it's a String. Too bad.
Poor choice of variable names: number1, number2. What is this, VB? Why not hoursWorked?
Just what the forum needs - another "prizes for everybody" kid who can't bear criticism, knows no Java, and just wants everyone to do their homework for them. Great.
%
> Let's look at those parameters: float, float, float.
> Hmmmmm, not very useful since the requirement wants
> you to keep employee name and it's a String. Too
> bad.
>
> Poor choice of variable names: number1, number2.
> What is this, VB? Why not hoursWorked?
>
> Just what the forum needs - another "prizes for
> everybody" kid who can't bear criticism, knows no
> Java, and just wants everyone to do their homework
> for them. Great.
>
> %
Take a look at reply 6, he did a pretty good job there.
> Take a look at reply 6, he did a pretty good job
> there.
Take a closer look at reply 6. That's not his code, and he doesn't understand it yet. His studying it though is a start.
> Take a closer look at reply 6. That's not his code...
for whatever reason, I found that overwhelmingly amusing... I'm actually still laughing...
> > Take a look at reply 6, he did a pretty good job
> > there.
>
> Take a closer look at reply 6. That's not his code,
> and he doesn't understand it yet. His studying it
> though is a start.
aww my bad, i assumed he wrote that himself.
Anyway I'm just wondering why the OP wrote this code:
public Employee (float number1, float number2, float sum) //etc...
instead of continuing with what he already had?
> for whatever reason, I found that overwhelmingly
> amusing... I'm actually still laughing...
I dunno, we all started out as ignorant snot-nosed brats. Why should he be any different. I remember I was terrified of my first Pascal program, and it was nothing but a bunch of writelines.
> > Take a closer look at reply 6. That's not his
> code...
>
> for whatever reason, I found that overwhelmingly
> amusing... I'm actually still laughing...
Sucks to be me for only looking at the code ;(
> instead of continuing with what he already had?
Ever heard of the deer-in-the-headlights syndrome? He's totally overwhelmed. He needs to step back, take a breath, and work this out.
> > instead of continuing with what he already had?
>
> Ever heard of the deer-in-the-headlights syndrome?
> He's totally overwhelmed. He needs to step back,
> take a breath, and work this out.
let's hope he come's back and read this.
and while im at it, i suggest him to read this as well:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/