Results 1 to 3 of 3
Thread: Help with constructors
- 02-10-2013, 10:27 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
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
Employee 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
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; } }
- 02-11-2013, 12:44 AM #2
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.
-
Re: Help with constructors
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.
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?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
Similar Threads
-
Constructors
By cups in forum New To JavaReplies: 1Last Post: 02-15-2012, 11:55 AM -
using constructors
By droidus in forum New To JavaReplies: 1Last Post: 09-18-2011, 10:30 PM -
Need help with constructors
By tpfaff in forum New To JavaReplies: 10Last Post: 10-22-2010, 04:33 AM -
Constructors
By suresh.sa in forum New To JavaReplies: 5Last Post: 10-20-2010, 12:10 AM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks