BEGINNER Java payroll

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5

64 Replies - 11697 Views - Last Post: 21 June 2009 - 06:56 PM Rate Topic: -----

#53 inf4mi5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 09

Re: BEGINNER Java payroll

Posted 17 June 2009 - 04:15 PM

Pill, I get errors while trying to compile. Could be my fault though. Also, I recently ordered that book. I've read that it's great a lot of places, so I went ahead and bought it. Should be here soon, but I'm in school, and this has to be done by Sunday, and my professor isn't very helpful. Not at all actually.

These are the errors:
class PayRoll is public, should be declared in a file named PayRoll.java
class PayRoll2 is public, should be declared in a file named PayRoll2.java



And this is my code when I get those errors.
import java.util.Scanner;

public class PayRoll {
   
	public static void main(String args[]) {
		PayRoll2 runPayRoll =  new PayRoll2();
		runPayRoll.getDataFromUser();
	}
}
public class PayRoll2
{
   
public void getDataFromUser() {
   
	Scanner stringInput = new Scanner( System.in );
	Scanner dubInput = new Scanner(System.in);
	boolean keepGoing = true;
	System.out.print("\nType the word STOP for name to quit");
	System.out.println();
   
	while(keepGoing){
	   
		System.out.print( "\nPlease Enter Employee Name: " );
	   
		String name  = stringInput.nextLine();

		if (name.equals("STOP") || name.equals("Stop") || name.equals("stop"))
		{
			keepGoing = false;
			return;
		}
	   

		System.out.print( "\nPlease Enter Total Number of Hours Worked This Week: ");
		double hoursWorked = dubInput.nextDouble();

		while( hoursWorked < 0.00 )
		{

			System.out.print( "\nInput must be a positive number, reenter hours worked: ");
			hoursWorked = dubInput.nextDouble();
		}


		System.out.print( "\nPlease Enter Employees Hourly Rate: ");
		double hourlyRate = dubInput.nextDouble();

		while ( hourlyRate < 0.00 )
		{
			System.out.print( "\nInput must be a positive number, reenter hourly rate: ");
			hourlyRate = dubInput.nextDouble();
		}

		double pay = hourlyRate * hoursWorked;

		System.out.print( "\n\nEmployee Name: " + name );

		System.out.print( "\nTotal Pay for the Week: $" + pay);
		System.out.println();
	}
}
}



And Knowledge, were you talking about me missing curly brackets, or Pill?

This post has been edited by inf4mi5: 17 June 2009 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#54 pillpusher   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: BEGINNER Java payroll

Posted 17 June 2009 - 04:24 PM

View Postinf4mi5, on 17 Jun, 2009 - 05:15 PM, said:

These are the errors:
class PayRoll is public, should be declared in a file named PayRoll.java
class PayRoll2 is public, should be declared in a file named PayRoll2.java



Make sure you save the files with the file names "PayRoll.java" and "PayRoll2.java"

Capitalization is important and the extension .java is necessary too.

Just to clarify, you'll need to compile both files by typing:

javac PayRoll.java
javac PayRoll2.java


Then run the file with the "main" method (which is PayRoll.class) by typing:

java PayRoll 


Notice that you type the .java extension when you compile, but you do not type the .class extension when you run the program. Also, you have to make two different files because the class name needs to match the file name (including capitalization). (ex. "public class PayRoll" matches "PayRoll.java"

It compiles and runs for me; I'm curious to know if renaming the files doesn't work for you.

This post has been edited by pillpusher: 17 June 2009 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#55 inf4mi5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 09

Re: BEGINNER Java payroll

Posted 18 June 2009 - 06:45 PM

Okay. I'll try that as soon as I get a chance. Is there any way to do this with just a single file? And to clarify, The two pieces of code you gave me are two completely separate files?
Was This Post Helpful? 0
  • +
  • -

#56 pillpusher   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: BEGINNER Java payroll

Posted 18 June 2009 - 06:59 PM

Below is the code to put it all in one file, but yes the previous code was for two separate files. It's just habit to put the "main" method in a separate class or file for me.

The code below will need to be saved in a file named "PayRoll.java" and compiled like this:

javac PayRoll.java 


and run like this:

java PayRoll 


Good Luck!

import java.util.Scanner;

public class PayRoll {
	
	public static void main(String args[]) {
		Scanner stringInput = new Scanner( System.in );
		Scanner dubInput = new Scanner(System.in);
		boolean keepGoing = true;
		System.out.print("\nTo quit, type the word STOP instead of an Employee Name");
		System.out.println();

		while(keepGoing){

			System.out.print( "\nPlease Enter an Employee Name: " );

			String name  = stringInput.nextLine();

			if (name.equals("STOP") || name.equals("Stop") || name.equals("stop")) 
			{
				keepGoing = false;
				return;
			}


			System.out.print( "\nPlease Enter Total Number of Hours Worked This Week: ");
			double hoursWorked = dubInput.nextDouble();

			while( hoursWorked < 0.00 )
			{

				System.out.print( "\nInput must be a positive number, reenter hours worked: ");
				hoursWorked = dubInput.nextDouble();
			}


			System.out.print( "\nPlease Enter Employees Hourly Rate: ");
			double hourlyRate = dubInput.nextDouble();

			while ( hourlyRate < 0.00 )
			{
				System.out.print( "\nInput must be a positive number, reenter hourly rate: ");
				hourlyRate = dubInput.nextDouble();
			}

			double pay = hourlyRate * hoursWorked;

			System.out.print( "\n\nEmployee Name: " + name );

			System.out.print( "\nTotal Pay for the Week: $" + pay);
			System.out.println();
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#57 inf4mi5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 09

Re: BEGINNER Java payroll

Posted 18 June 2009 - 08:46 PM

That compiles and works great, but I don't see where it stores their names,hours worked, and total pay. Am I missing it? If so, I'm going to feel very unintelligent. xD
Was This Post Helpful? 0
  • +
  • -

#58 pillpusher   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: BEGINNER Java payroll

Posted 19 June 2009 - 06:07 AM

View Postinf4mi5, on 14 Jun, 2009 - 02:29 PM, said:

... The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency....


Sorry. I didn't realize you needed to save the data. How do you want to save it (to a .txt file or database)? If you want to save the information, it's going to require a bit more complex coding. I found this web page that explains how to save items to a text file.
Was This Post Helpful? 0
  • +
  • -

#59 inf4mi5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 09

Re: BEGINNER Java payroll

Posted 19 June 2009 - 06:26 AM

Use 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 as the employee name, the application should terminate.

From my understanding, you can just temporarily store the information in the program instead of storing it in a text file or database. Please correct me if I'm wrong, and I'll start going in that direction.
Was This Post Helpful? 0
  • +
  • -

#60 pillpusher   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: BEGINNER Java payroll

Posted 19 June 2009 - 06:54 AM

View Postinf4mi5, on 19 Jun, 2009 - 07:26 AM, said:

Use 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 as the employee name, the application should terminate.


I think you're right. They want you to store the information in the computer's memory until the program terminates. When they say "use a class," they mean they want you to store the information in an Employee object. To do that, you will be creating more than one .java file.

Can't just give you the code, so you probably want to read up on Constructors and Objects. If you put something together, I'll be glad to help you get it working if you need it.
Was This Post Helpful? 0
  • +
  • -

#61 inf4mi5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 09

Re: BEGINNER Java payroll

Posted 19 June 2009 - 09:43 AM

Okay. So I read up on Objects and Constructors. From my understanding, I need to create an Object, then store the information in that object. And lastly, call that Object with a Constructor. Am I getting closer?

This is my first attempt at creating the Object "Employee". Am I missing anything?
import java.util.Scanner;
	public class Employee {
		Object Employee = new Employee			
	
	}	


This post has been edited by inf4mi5: 19 June 2009 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#62 pillpusher   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 13-May 09

Re: BEGINNER Java payroll

Posted 19 June 2009 - 11:22 AM

View Postinf4mi5, on 19 Jun, 2009 - 10:43 AM, said:

Okay. So I read up on Objects and Constructors. From my understanding, I need to create an Object, then store the information in that object. And lastly, call that Object with a Constructor. Am I getting closer?


Closer. An object is "instantiated" from a class (in other words it is an instance of a class). What you should do is create the "Employee" objects from your main class.

This is how your Employee class might look:

public class Employee {

		   Employee(String nameIn, double hoursIn, double rateIn) {
				   String name = nameIn;
				   double hours = hoursIn;
				   double rate = rateIn;
				   double totalPay = hours * rate;
			} //end constructor

} // end Employee class



To create one of these "Employee" objects, you put the following code in your PayRoll.java file

Employee someNewEmployee = new Employee("John Doe", 40.00, 10.00);



Now you have one new "Employee" object with a name, hours, rate, and total pay. You access those variables by using the object name then a "dot" then the variable name. ex:

someNewEmployee.totalPay

will give you the totalPay for that employee.

Now your next problem is that in order to store all the employees, you'll need to put them in an array of Employee objects or a Vector or an ArrayList.

Good Luck!

This post has been edited by pillpusher: 19 June 2009 - 08:29 PM

Was This Post Helpful? 1
  • +
  • -

#63 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: BEGINNER Java payroll

Posted 19 June 2009 - 06:31 PM

62 replies for a "Beginner Java Payroll"
you guys should start to think about openening your own forum :D
Was This Post Helpful? 0
  • +
  • -

#64 inf4mi5   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-June 09

Re: BEGINNER Java payroll

Posted 21 June 2009 - 09:41 AM

hahaha the reason for so many replies is probably because I'm such a slow learner. ^_^
Was This Post Helpful? 0
  • +
  • -

#65 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: BEGINNER Java payroll

Posted 21 June 2009 - 06:56 PM

View Postinf4mi5, on 21 Jun, 2009 - 08:41 AM, said:

hahaha the reason for so many replies is probably because I'm such a slow learner. ^_^

May be but nobody will have the patience to scan +60 posts tring to help you
Topic closed... if you haven't understood in 60 topics you will never :D
and there is no "pedagogic" reason to keep a topic open for more than 3 pages
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5