Results 1 to 11 of 11
  1. #1
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Question Need help ASAP with Payroll Program Part 2

    I don't know what I'm doing wrong. Can anyone help me out with my code? Thanks!

    import java.util.Scanner;
    //This line allows the user to interact with the application.
    import java.text.NumberFormat;
    //This line is used by the application as instructions to format currency.

    public class Payroll{//Payroll class
    private String Employee;
    // This lines declares Employee as character variable in the application.
    private Double Rate,Hours,Salary;
    // This lines declares Payroll, Rate and Hours as numeric variables in the application.

    public static void main(String args[]){

    Scanner WeeklyPayroll = new Scanner(System.in);
    //This line tells the application expects input from the user.
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    //This line is used to format currency.

    }
    public void getEmployee(){

    System.out.println("Welcome to the Payroll Calculator Application.");
    //This line welcomes the user to the application.
    System.out.println("Please enter the employee's name here");
    //This line prompts the user to input the employee's name.
    System.out.println("To quit enter stop");
    //This line prompts the user to enter the word stop to quit the application
    Employee = WeeklyPayroll.nextLine();
    //This line tells the program to expect Employee input from user.
    ProgramPayroll payrollObject = new ProgramPayroll();
    //Creates object payrollObject
    payrollObject.testEmployee();//calls module testEmployee
    }//Ends getEmployee module
    public void getRate(){
    System.out.println("Please enter the hourly rate for " + Employee);
    // This line prompts the user to input the hourly rate for that employee.
    Rate = WeeklyPayroll.nextDouble();
    //This line tells the program to expect Rate input from user.
    ProgramPayroll payrollObject = new ProgramPayroll();
    payrollObject.testRate();
    }//Ends getRate module
    public void getHours(){
    System.out.println("Please enter how many hours " + Employee + " worked this week");
    // This line prompts the user to input the hours worked by that employee.
    Hours = WeeklyPayroll.nextDouble();
    // This line tells the program to expect Hours input from the user.
    }//Ends getHours module
    public void Calculation(){
    Salary = Rate * Hours;
    // This is the calculation the program needs to do to get the total amount.
    System.out.print("The payroll for " + Employee + " is " + nf.format (Salary) + ". ");
    System.out.print(Employee + " worked " + Hours + " hours with an hourly rate of " + nf.format (Rate)+ ".");
    //These lines output the user the Employee's name, the total payroll for that week,the hours worked and the hourly rate for that employee.

    WeeklyPayroll.close();

    }//End main method
    }//End class



    public class ProgramPayroll {

    public void testEmployee(String name){
    //Create Module testEmployee with a string argument name

    Employee=name;
    //gives the variable Employee the value of name
    if (name == "stop");
    System.out.println("Are you sure you want to exit the Payroll Application?");
    System.exit(0);
    }//Ends testEmployee module

    public void testRate(double setRate){
    //Creates Module testRate with a double argument setRate
    Rate=setRate;//gives the variable Rate the value of setRate

    if (Rate >0);
    System.out.println("Please enter a possitive amount.");
    //Prompts the user to enter a positive amount
    }//Ends testRate module

    public void testHours(double setHours){
    //Creates Module testHours with a double argument setHours
    Hours=setHours;//gives the variable Hours the value of setHours

    if (Hours > 0);
    System.out.println("Please enter a positive amount.");
    }//Ends testHours module

    }// Ends class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) please use code tags so your code is readable. right now it's not.
    2) Please detail just what errors you are receiving. The more information you can give us, the better we can help you.

  3. #3
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    I'm sorry I'm new to this stuff what do you mean with tags? :S thanks for responding so quickly

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Post Code correction

    Java Code:
    // Program: Payroll Program Part 2
    // File:	Payroll.java
    // Summary: Payroll Calculator that interacts with user to calculate the employee's payroll with the hourly rate and hours worked by the employee.
    // Author:	Charlene Arreche
    // Date:	October 30,2009.
    
    import java.util.Scanner;//This line allows the user to interact with the application.
    import java.text.NumberFormat;//This line is used by the application as instructions to format currency.
    
    public class Payroll{//Payroll class
    	private String Employee; // This lines declares Employee as character variable in the application.
    	private Double Rate,Hours,Salary; // This lines declares Payroll, Rate and Hours as numeric variables in the application.
    
    	public static void main(String args[]){			
    		Scanner WeeklyPayroll = new Scanner(System.in);//This line tells the application expects input from the user.
    		NumberFormat nf = NumberFormat.getCurrencyInstance();//This line is used to format currency.		
    	}
    	public void getEmployee(){		
    		System.out.println("Welcome to the Payroll Calculator Application.");//This line welcomes the user to the application.
    		System.out.println("Please enter the employee's name here");//This line prompts the user to input the employee's name.
    		System.out.println("To quit enter stop"); //This line prompts the user to enter the word stop to quit the application
    			Employee = WeeklyPayroll.nextLine();//This line tells the program to expect Employee input from user.
    			ProgramPayroll payrollObject = new ProgramPayroll();//Creates object payrollObject
    			payrollObject.testEmployee();//calls module testEmployee
    	}//Ends getEmployee module
    	public void getRate(){
    		System.out.println("Please enter the hourly rate for " + Employee);// This line prompts the user to input the hourly rate for that employee.
    			Rate = WeeklyPayroll.nextDouble();//This line tells the program to expect Rate input from user.
    			ProgramPayroll payrollObject = new ProgramPayroll();
    			payrollObject.testRate();
    	}//Ends getRate module
    	public void getHours(){
    		System.out.println("Please enter how many hours " + Employee + " worked this week");// This line prompts the user to input the hours worked by that employee.
    			Hours = WeeklyPayroll.nextDouble();// This line tells the program to expect Hours input from the user.
    	}//Ends getHours module
    	public void Calculation(){	
    			Salary = Rate * Hours;// This is the calculation the program needs to do to get the total amount.
    			System.out.print("The payroll for " + Employee + " is " + nf.format (Salary) + ". ");
    			System.out.print(Employee + " worked " + Hours + " hours with an hourly rate of " + nf.format (Rate)+ ".");
    				//These lines output the user the Employee's name, the total payroll for that week,
    				// the hours worked and the hourly rate for that employee.
    			
    		WeeklyPayroll.close();	
    	}//End main method
    }//End class

    Java Code:
    // Program: Payroll Program Part 2
    // File:	ProgramPayroll.java
    // Summary: Payroll Calculator that interacts with user to calculate the employee's payroll with the hourly rate and hours worked by the employee.
    // Author:	Charlene Arreche
    // Date:	November 7,2009.
    
    public class ProgramPayroll {
    
    	public void testEmployee(String name){//Create Module testEmployee with a string argument name
    		Employee=name;//gives the variable Employee the value of name
    		
    		if (name == "stop");
    		 System.out.println("Good bye!");
    		 System.exit(0);
    		}//Ends testEmployee module
    	public void testRate(double setRate){//Creates Module testRate with a double argument setRate
    		Rate=setRate;//gives the variable Rate the value of setRate
    	
    		if (Rate >0);
    			System.out.println("Please enter a possitive amount.");//Prompts the user to enter a positive amount
    	}//Ends testRate module
    	public void testHours(double setHours){//Creates Module testHours with a double argument setHours
    		Hours=setHours;//gives the variable Hours the value of setHours
    		
    		if (Hours > 0);
    			System.out.println("Please enter a positive amount.");
    	}//Ends testHours module
    	
    }// Ends class
    oK here we go. Right now is not giving me any errors but nothing is comming up either :S

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    first thing. Don't use == for comparing strings. Use the .equalsIgnoreCase() method attached within string objects.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  7. #7
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    ok I changed that. but still I cannot get both classes to use the vatiables Employee, Rate, Hours and Salary. I thought creating an object and call the module in the other class will solve the issue but it does not.

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    You haven't created an object since there is no constructor. Are you trying to get the payroll classes to treate Rate, Hours, and Salary as values of an Employee object? If that's the case you need a new Employee class.

    Java Code:
    public class Employee{
    	private double hours;
    	private double salary;
    	private double rate;
    	private String name;
    	
    	Employee(){
    		this.hours = 0;
    		this.salary = 0;
    		this.rate = 0;
    		this.name = "";
    	}
    	
    	Employee(String n, double h, double s, double r){
    		this.name = n;
    		this.hours = h;
    		this.salary = s;
    		this.rate = r;
    	}
    	
    	//getter and setter methods to retrieve and set object values
    	
    }
    I think that may be what you're looking for. I just wrote the constructors for you. You still need to add the get/set methods in order to use the objects properly.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Also this line ProgramPayroll payrollObject = new ProgramPayroll(); does nothing because you don't have a constructor.

    You seem to understand in theory how the objects should work and interact with each other but you don't have a grasp of the key components of it. Perhaps you should re-read about working with objects.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  10. #10
    arrech326 is offline Member
    Join Date
    Nov 2009
    Posts
    13
    Rep Power
    0

    Default

    ok so even if I have an object but no constructor the program will not run? did I get this right? I'm so confused. Thank you for responding to my thread!
    -Charlene

  11. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    You can't have an object without having a constructor for that object. Here's an example.

    Whenever you do String something = new String() //without anything inside
    you end up calling the default Constructor which would be
    Java Code:
    String(){
         this.name = "";
    }
    however doing String something = new String("Java rocks!")//with a parameter
    you end up calling this constructor
    Java Code:
    String(String n){
         this.name = n;
    }
    And yes you're correct. Trying to construct an object without having a constructor for that objects class will throw an error.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 02:30 AM
  2. Part of program not printing ?
    By graz in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 04:37 AM
  3. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 06:57 AM
  4. Payroll Program exits at wrong time
    By jsand2 in forum Java Applets
    Replies: 13
    Last Post: 01-26-2009, 04:10 AM
  5. Payroll Part 2, Java
    By lplopez92 in forum New To Java
    Replies: 2
    Last Post: 03-24-2008, 02:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •