Results 1 to 3 of 3
  1. #1
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Help with constructors

    Hello, I was asked to make these changes to my assignment:

    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

    My problem is I am not sure if I am doing this correctly. I am writing this in Eclipse and the compiler runs everything in there so it seems the info is being passed between the classes fine, but my problem is when I try to compile in to .class in the command prompt window and run the main class (PayrollInfo) it tells me it cannot find or load the main class PayrollInfo.class

    Am I just compiling this wrong? (I am compiling both classes before trying to run the main class) Or is my coding off?

    Here is my code

    PayrollInfo class

    Java Code:
    //PayrollInfo.java
    //Eric Bowman 02/10/2013
    //Program calculates weekly payroll for employees
    
    import java.util.Scanner; //program uses scanner
    
    public class PayrollInfo 
    {
    	//main method begins program execution
    	public static void main (String[] args)
    	{
    	//create scanner to obtain input from command window
    	Scanner input = new Scanner (System.in);
    	
    	//defines the variables
    	double hoursWorked;
    	double hourlyPay;
    	String empName;
    	
    	System.out.println("\nWelcome to the Payroll Program");
    	System.out.println("This program will calculate weekly pay\n");
    	
    	while (true)
    		{
    		//prompt employee name input
    		System.out.print("Enter employee's name or stop to quit: ");
    		empName = input.nextLine();
    		// breaks the loop if stop is entered
    		if (empName.equalsIgnoreCase("stop"))
    		{
    			break;
    		}//terminates program
    		//prompt hourly pay rate
    		System.out.printf("What is %s's hourly pay rate: ", empName);
    		hourlyPay = input.nextDouble();
    		input.nextLine();
    		//checks for positive number
    		//allowed 0 as input in case employee still needs to be kept on record
    		while (hourlyPay < 0)
    		{
    			System.out.println("You must enter a positive number");
    			System.out.printf("What is %s's hourly pay rate: ", empName);
    			hourlyPay = input.nextDouble();
    			input.nextLine();
    		}//end hourlyPay check	
    		//prompt hours worked
    		System.out.printf("How many hours did %s work: ", empName);
    		hoursWorked = input.nextDouble();
    		input.nextLine();
    		//checks for positive number
    		//allowed 0 as input in case employee still needs to be kept on record
    		while (hoursWorked < 0)
    		{
    			System.out.println("You must enter a positive number");
    			System.out.printf("How many hours did %s work: ", empName);
    			hoursWorked = input.nextDouble();
    			input.nextLine();
    		}//end hoursWorked check
    		//construct Employee class
    		Employee employee = new Employee(empName, hoursWorked, hourlyPay );
    		System.out.printf("%s's weekly pay is $%.2f%n%n",
    			employee.getName(), employee.getWeeklyPay());
    		}//end while
    	}//end main
    }//end class PayrollInfo
    Employee class:

    Java Code:
    public class Employee 
    {
    	private String name;
    	private double hours;
    	private double hourlyPay;
    	private double total;
    	
    	public Employee(String name, double hours, double rate)
    	{
    		this.setName(name);
    		this.hours = hours;
    		this.hourlyPay = rate;
    		setWeeklyPay();
    	}
    	
    	public double getWeeklyPay()
    	{
    		return total;
    	}
    	
    	private void setWeeklyPay()
    	{
    		total = hours * hourlyPay;
    	}
    	
    	public String getName() 
    	{
    		return name;
    	}
    
    	public void setName(String name) 
    	{
    		this.name = name;
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help with constructors

    Can you copy the command prompt console's contents for when you run the javac and java commands and paste it here?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help with constructors

    Quote Originally Posted by Modulus View Post
    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...
    I'm thinking that perhaps Employee shouldn't have a setWeeklyPay method or a total field. If it is supposed to have a getWeekleyPay() method, then the weekly pay should be calculated on the spot inside of the method when the method is called. I'm pretty certain that it should have getters and setters for hours and hourlyPay though.

    My problem is I am not sure if I am doing this correctly. I am writing this in Eclipse and the compiler runs everything in there so it seems the info is being passed between the classes fine, but my problem is when I try to compile in to .class in the command prompt window and run the main class (PayrollInfo) it tells me it cannot find or load the main class PayrollInfo.class
    You will need to show us exactly how you're trying to compile the code. You will want to copy the text from your cmd window and post it here so we can see exactly what you're trying to do. Is your code in any packages? Do you use the -cp . command on the command line?

Similar Threads

  1. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 11:55 AM
  2. using constructors
    By droidus in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 10:30 PM
  3. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  4. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 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
  •