Need Help with Java Payroll Program (if, else, and while statements)

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

50 Replies - 5319 Views - Last Post: 13 August 2009 - 09:19 PM Rate Topic: -----

#1 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 11:01 AM

Hello all,

I am a javanoob, hence my username. I am currently enrolled in a Java Programming class. We have to create a Java payroll program. I completed part 1 just fine, but I'm having issues with part 2. For part 2, I have to modify my application so that it continues to request user information (this portion works). I also have to make it so that when the user enters "stop" as the employee name, the program ends. Also, if a negative number is entered as hourly pay or hours worked, a prompt comes up asking the user to input a positive number. I am so sorry, but this website is my last hope. Unfortunately, my instructor is not much of a help, he expects me to be an expert after only 2 weeks. Can you guys please take a look at my code and help me out or at least point me in the right direction??? I hope I pasted the code right...thanks!


// Payroll Program Part 2
import java.util.Scanner; // class Scanner

public class PayrollProgramTwo
{
	

	//main method begins execution of Java application
		public static void main(String args[])
	{
		

	//create Scanner to obtain input from command window
		Scanner input = new Scanner(System.in);


		
		String cleanInputBuffer; // input
	String employeename; // Employee Name
		float hourly; // hourly pay rate
		float hours; // hours worked this week
		float product; // weekly pay amount
		

	boolean end = false; // is the input name stop?
		while (end = true) // as long as end is false, proceed
		{


			hourly = -1; // this way, both are initiated to be -1;
			hours = -1;


			System.out.print("Enter Name of Employee:");
			employeename = input.nextLine();

			if(employeename.toLowerCase() == "stop")
			end = false;


		System.out.print("Enter hourly pay rate:"); // prompt
			hourly = input.nextFloat(); // input
			
			

		while (hours <= 0) // same as hourly while loop
			{
				

		System.out.print("Enter number of hours worked this week:"); // prompt
				hours = input.nextFloat(); // input
			

		}
			

			product = hourly * hours; // * numbers

			

		System.out.printf("The employee %s was paid $ %.2f this week.\n\n", employeename, product);

		cleanInputBuffer = input.nextLine(); //Read a line of text to clean the input buffer
			

	

		} // end outer while

	} // end method main

} // end class PayrollProgramTwo



Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with Java Payroll Program (if, else, and while statements)

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 11:29 AM

Your first problem lies here
boolean end = false; // is the input name stop?
		while (end = true) // as long as end is false, proceed
		{



You have set you boolean end to false, which means it will never enter the while loop. Why? Because the while loop requires a true. With this advise, see if you can come up with a solution (its only a little change). And it needs to be == as stated below (didnt notice that)

This post has been edited by nick2price: 03 August 2009 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 11:33 AM

You've come to the right place to get help. We won't do the homework for you, but we'll certainly try to help you get your program working (and maybe even teach you something in the process). :)

Some quick observations about your code:

while (end = true) reassigns the value true to end every time. I believe you intend to have while (end == true) (or, better yet, while (end)).

if(employeename.toLowerCase() == "stop") uses == to compare Strings. To properly compare Strings, you must use the equals method: if(employeename.toLowerCase().equals("stop")). Using == actually checks to see if the two strings are the same object (that is, stored in the same place in the computer's memory). That is rarely what you want when comparing strings.

As written, the prompts for hourly pay rate and hours worked will be shown even if the user enters "stop" for the employee name. Eventually, you will probably want to write something to cause those lines to be skipped in the case that the user enters "stop". Similarly for when negative numbers get entered for the pay rate or hours worked. (Ignore that crossed out sentence. I misread the assignment.)

Your while loop for hours seems pretty functional. I would suggest a prompt if the number entered is negative which explains to the user why they are being prompted again. Also, is it really unacceptable for the number of hours worked to be 0? I'm really asking--in real life, a person may not work at all during a week, but your assignment may say it has to be a positive number of hours worked.

That ought to give you some things to work with to nudge you in the right direction. Play with it for a while and let us know if you get stuck again (or if this is totally unhelpful, of course). :)

This post has been edited by smacdav: 03 August 2009 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

#4 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 11:47 AM

View Postsmacdav, on 3 Aug, 2009 - 10:33 AM, said:

You've come to the right place to get help. We won't do the homework for you, but we'll certainly try to help you get your program working (and maybe even teach you something in the process). :)

Some quick observations about your code:

while (end = true) reassigns the value true to end every time. I believe you intend to have while (end == true) (or, better yet, while (end)).

if(employeename.toLowerCase() == "stop") uses == to compare Strings. To properly compare Strings, you must use the equals method: if(employeename.toLowerCase().equals("stop")). Using == actually checks to see if the two strings are the same object (that is, stored in the same place in the computer's memory). That is rarely what you want when comparing strings.

As written, the prompts for hourly pay rate and hours worked will be shown even if the user enters "stop" for the employee name. Eventually, you will probably want to write something to cause those lines to be skipped in the case that the user enters "stop". Similarly for when negative numbers get entered for the pay rate or hours worked.

Your while loop for hours seems pretty functional. I would suggest a prompt if the number entered is negative which explains to the user why they are being prompted again. Also, is it really unacceptable for the number of hours worked to be 0? I'm really asking--in real life, a person may not work at all during a week, but your assignment may say it has to be a positive number of hours worked.

That ought to give you some things to work with to nudge you in the right direction. Play with it for a while and let us know if you get stuck again (or if this is totally unhelpful, of course). :)



***My assignment REALLY says that if a negative number is entered, a prompt should ask for a positive number lol, I don't know why, but it says that!
Was This Post Helpful? 0
  • +
  • -

#5 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 11:53 AM

Given that, I'd say you probably just want < instead of <= since 0 is not a negative number. That's my interpretation, anyway. Maybe your instructor can clarify what he wants.

Of course, it's more important to get everything else working than to worry about this little detail right now. :)
Was This Post Helpful? 1
  • +
  • -

#6 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 01:18 PM

I have updated my code. I am unable to try and compile/run it at work, but can you take a look at it and let me know if it looks ok??


// Payroll Program Part 2
import java.util.Scanner; // class Scanner

public class PayrollProgramTwo
{
	

	//main method begins execution of Java application
		public static void main(String args[])
	{
		

	//create Scanner to obtain input from command window
		Scanner input = new Scanner(System.in);


		
		String cleanInputBuffer; // input
	String employeename; // Employee Name
		float hourly; // hourly pay rate
		float hours; // hours worked this week
		float product; // weekly pay amount
		

	boolean end = false; // is the input name stop?
		while (end == false) // as long as end is false, proceed
		{


			hourly = -1; // this way, both are initiated to be -1;
			hours = -1;


			System.out.print("Enter Name of Employee:");
			employeename = input.nextLine();

			
		if(employeename.toLowerCase().equals("stop"))
			end = true;

else 
{


		System.out.print("Enter hourly pay rate:"); // prompt for input hourly pay rate
			hourly = input.nextFloat(); // read hourly pay rate from user
			
			

		while (hours <= 0) // same as hourly while loop
			{
				

		System.out.print("Enter number of hours worked this week:"); // prompt for input number of hours worked
				hours = input.nextFloat(); // read hours worked from user
			

		}
			

			product = hourly * hours; // product of hourly rate and hours worked

			

		System.out.printf("The employee %s was paid $ %.2f this week.\n\n", employeename, product);

		cleanInputBuffer = input.nextLine(); //Read a line of text to clean the input buffer
			
}
	

		} // end outer while

	} // end method main

} // end class PayrollProgramTwo




Thanks!!! This is just so the program will exit upon entering "stop" as the employee name.
Was This Post Helpful? 0
  • +
  • -

#7 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 01:25 PM

Yep. All you needed was that else clause. There are other ways of doing it, too, but this works great.
Was This Post Helpful? 1
  • +
  • -

#8 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 01:27 PM

View Postsmacdav, on 3 Aug, 2009 - 12:25 PM, said:

Yep. All you needed was that else clause. There are other ways of doing it, too, but this works great.



**So my updated code should compile and run without error? It sucks that I can't download JDK at work, have to wait until I get home.

Thank you soooo much for your help!
Was This Post Helpful? 0
  • +
  • -

#9 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 01:31 PM

I, in fact, copied and pasted it, compiled it, and ran it without error.

You still have a little bit of work to do since you don't have a loop yet for testing the hourly wage to make sure the input is positive (that should be simple since you've already done it for the hours worked), but you have working code that quits as soon as "stop" is entered for the employee name. :)
Was This Post Helpful? 1
  • +
  • -

#10 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 01:35 PM

View Postsmacdav, on 3 Aug, 2009 - 12:31 PM, said:

I, in fact, copied and pasted it, compiled it, and ran it without error.

You still have a little bit of work to do since you don't have a loop yet for testing the hourly wage to make sure the input is positive (that should be simple since you've already done it for the hours worked), but you have working code that quits as soon as "stop" is entered for the employee name. :)



THANK YOU SO MUCH! I am just my 3rd week into this class, and already I feel as if my head is going to implode! I really appreciate the help. You also explained to me the meaning of everything, which definitely helps alot! I already did the loop for the hours worked??? This tells you how boggled I am right now.

Thanks again! I will be in touch :)
Was This Post Helpful? 0
  • +
  • -

#11 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 03 August 2009 - 06:40 PM

I'm glad I could help. I used to teach Java, so I've dealt with my share of boggled students. You were doing better than many to have gotten as far as you did. Hang in there! :)
Was This Post Helpful? 1
  • +
  • -

#12 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 06 August 2009 - 06:29 AM

View Postsmacdav, on 3 Aug, 2009 - 05:40 PM, said:

I'm glad I could help. I used to teach Java, so I've dealt with my share of boggled students. You were doing better than many to have gotten as far as you did. Hang in there! :)


Hello smacdav,

I am working on the next modification for my payroll program. I completed Part 2 which required a prompt to ask the user for a positive number if a negative was entered (this works), and the program will exit upon typing 'stop' as the employee name (this also works). My next phase is quite tricky. I have been reading my text book and while there are some examples, there are none I can apply directly to this program. My text book is very vague, and you are more of a help than my instructor! Can you please look at my full Part 2 code below, and kind of point me in the right direction??

Part 3 calls for this: Modify the payroll program so that it uses a class to store and retrieve the employee's name, hourly rate, and hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Make sure the program maintains all of the current functionality. THIS IS SOOO CONFUSING! I DON'T KNOW WHERE TO BEGIN

Don't worry, not asking you to do it for me, just need as much help as you can give! Thanks :)


// Payroll Program Part 2
import java.util.Scanner; // class Scanner

public class PayrollProgramTwo
{
	

	//main method begins execution of Java application
		public static void main(String args[])
	{
		

	//create Scanner to obtain input from command window
		Scanner input = new Scanner(System.in);


		
		String cleanInputBuffer; // input
	String employeename; // Employee Name
		float hourly; // hourly pay rate
		float hours; // hours worked this week
		float product; // weekly pay amount
		

	boolean end = false; // is the input name stop?
		while (end == false) // as long as end is false, proceed
		{


			hourly = -1; // this way, both are initiated to be -1;
			hours = -1;


			System.out.print("Enter Name of Employee:");
			employeename = input.nextLine();

			
		if(employeename.toLowerCase().equals("stop"))
			end = true;

else 
{


		System.out.print("Enter hourly pay rate:"); // prompt for input hourly pay rate
			hourly = input.nextFloat(); // read hourly pay rate from user
			
			

		while (hourly <= 0) // same as hourly while loop
			{
				

		System.out.print("Please enter a positive number:"); // prompt for input positive hourly rate
				hourly = input.nextFloat(); // read positive hourly rate from user
			

		}


		System.out.print("Enter number of hours worked this week:"); // prompt for input hours worked
		hours = input.nextFloat(); // read hours worked from user

		while (hours <= 0) // same as hours loop
		{

		System.out.print("Please enter a positive number:"); // prompt for input positive hours worked
		hours = input.nextFloat(); // read positive hours worked from user
		}
			

			product = hourly * hours; // product of hourly rate and hours worked

			

		System.out.printf("The employee %s was paid $ %.2f this week.\n\n", employeename, product);

		cleanInputBuffer = input.nextLine(); //Read a line of text to clean the input buffer
			
}
	

		} // end outer while

	} // end method main

} // end class PayrollProgramTwo



This post has been edited by javanoob82: 06 August 2009 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#13 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 06 August 2009 - 10:17 AM

You know, it doesn't have to be me who responds. Other people here can help you, too. ;)

It sounds like you're moving away from procedural programming and into object oriented programming. The first step is to learn how to write a non-executable class, and this assignment is asking you to do that. It sounds like you are expected to write a class that stores all of the employee information (name, hourly rate, hours worked).

For this assignment, I'd say you need two files: one called Employee.java (or something like that) to represent an employee and one that contains just your main method for running the program. Both of these files should live in the same directory so that the main method can find your Employee class when it needs it.

The tutoring process on this site works best if you've already made an attempt at the code writing so we can work with what you've done instead of writing the code for you, so I recommend that you try to write the Employee class and post your code so we can work from there. Don't worry if you don't get far, just do as much as you possibly can.

Also be aware that Dream.In.Code has resources for you other than these forums. One of them, which you might want to check out right now, is a Java tutorial "Intro to Designing Classes" written by Locke. I've only just looked it over briefly, but it looks like Locke has done a pretty darn good job of explaining the process. If it works for you, great! If not then the point at which you get lost gives us a good point to work from.

What book are you using for your class, by the way?
Was This Post Helpful? 1
  • +
  • -

#14 javanoob82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-August 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 06 August 2009 - 10:34 AM

View Postsmacdav, on 6 Aug, 2009 - 09:17 AM, said:

You know, it doesn't have to be me who responds. Other people here can help you, too. ;)

It sounds like you're moving away from procedural programming and into object oriented programming. The first step is to learn how to write a non-executable class, and this assignment is asking you to do that. It sounds like you are expected to write a class that stores all of the employee information (name, hourly rate, hours worked).

For this assignment, I'd say you need two files: one called Employee.java (or something like that) to represent an employee and one that contains just your main method for running the program. Both of these files should live in the same directory so that the main method can find your Employee class when it needs it.

The tutoring process on this site works best if you've already made an attempt at the code writing so we can work with what you've done instead of writing the code for you, so I recommend that you try to write the Employee class and post your code so we can work from there. Don't worry if you don't get far, just do as much as you possibly can.

Also be aware that Dream.In.Code has resources for you other than these forums. One of them, which you might want to check out right now, is a Java tutorial "Intro to Designing Classes" written by Locke. I've only just looked it over briefly, but it looks like Locke has done a pretty darn good job of explaining the process. If it works for you, great! If not then the point at which you get lost gives us a good point to work from.

What book are you using for your class, by the way?



This book called "Java: How to Program"

I tried creating an employee.java file. I'm not even sure if I did this right. I need the employee.java file to work with my main payrollprogram file right?? Do the declared variables need to be the same in both files? i.e. hourly, hours, product, etc.

Here is my so called employee.java code


class Employee
{
	// private variables
	private String p_name;
	private int p_age;

	// contructor to create a person object
	// with name, hourly pay, hours worked, weekly pay
	public Employee(String employeename, float hourly pay, float hours worked, float product)
	{
		 this.p_employeename = employeename;
		 this.p_hourly = hourly;
	 this.p_hours = hours;
	 this.p_product = product;
	}

	 // now you would need some functions 
	 // allowing you to retrive this information
	 public String getName()
	 {
		  return this.p_employeename; // return the name
	 }

	 public float getHourly()
	 {
		  return this.p_hourly; // return the hourly rate
	 }

	 public float getHours()
	 {
		  return this.p_hours; // return the hours worked
	 }

	 public float getProduct()
	 {
		  return this.p_product; // return the product of hourly rate and hours worked
	 }


}




This is frustrating...
Was This Post Helpful? 0
  • +
  • -

#15 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Need Help with Java Payroll Program (if, else, and while statements)

Posted 06 August 2009 - 11:05 AM

View Postjavanoob82, on 6 Aug, 2009 - 09:34 AM, said:

This book called "Java: How to Program"


Not too surprisingly, I don't know that one. I just thought I'd check.

Quote

I tried creating an employee.java file. I'm not even sure if I did this right. I need the employee.java file to work with my main payrollprogram file right?? Do the declared variables need to be the same in both files? i.e. hourly, hours, product, etc.


Since the class is called Employee, the file name should be Employee.java and not employee.java. This can make a difference on some systems, especially if you are defining a public class (done simply by putting "public" before the word "class" at the beginning). You do need this file before you can write the main program file, yes. The variables, however, do not need to be the same.

As for your code, I think you've done okay for a first try. I'll go through line-by-line and make some comments. The line numbers I use assume that "class Employee" appears on line 1.

Line 1: I know Locke doesn't do so in his tutorial, but most books teach you to write public classes (and to specify that they are public). Double-check the example(s) in your book and/or any examples your instructor may have given you, but my guess is that they all start with "public class ClassName". If so, then to make your instructor happy I would recommend putting "public" in there.

Lines 3-5: Declaring private variables is good. These variables (also called "fields") need to represent the things you want the class (really instances of this class, but that comes later) to remember. In our case, we want the employee's name, hourly pay rate, and number of hours worked, so you need a variable for each of these.

Line 9: The parameters (variables) used in your constructor declaration must be legal variables. In other words, they must start with a letter and may not contain spaces. Specifically, "hourly pay" and "hours worked" are illegal variable names, so you'll want to change those. Do not include the "product" variable. That is a value to be computed, not provided during construction.

Lines 11-14: In here, you should be assigning the values provided in the parameter list (line 9) to the variables you defined before. An example line that works with what you have is this.p_name = employeename. In this way, the p_name variable is assigned the value of employeename.

Lines 19-32: These accessor methods are good--just make sure that the variable name on the return line is the same as the name of the variable that stores the information you want to return. In your code, you have return this.p_employeename, but you don't have a variable called p_employeename. You do have a variable called p_name. Perhaps that should be the returned value here.

Lines 34-37: What you are really wanting to do here is compute the pay, right? If so then I recommend two things: rename the method to something that describes what you are doing (such as computePay) and return the calculation that you did in the first version of this program. You do not have a variable to store the "product" because that is not in the specs for the class as given by the problem, but you can compute the product easily enough.

Quote

This is frustrating...


For now. Once you learn it, you will see that it is not as difficult as it seems. See if what I've written here gets you a bit farther along.
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »