Hello, I am a student and I'm working on a problem.
I have written other small class and method assignments without a problem, but I'm getting confused as to what's wrong with my code.
I'm doing a simple assignment where I'm asked to create a class, and then populate and display it in another. I can compile without any errors, so it makes it a little harder to troubleshoot.
Here is my code from the first class.
public class PayrollML { private String name; //employee's first and last name private int idNumber; //Employee's ID number private double rate; //Employee's hourly rate private double hours; //employee's hours worked public void setName( String name ) { name = (name != null ? name : "No name" ); } public void setIdNumber( int idNumber ) { idNumber = ( idNumber >=0? idNumber : 0 ); } public void setRate( double rate ) { rate = ( rate >= 0? rate :0 ); } public void setHours( double hours ) { hours = ( hours >= 0? hours : 0 ); } public String getName() { return name; } public int getIdNumber() { return idNumber; } public double getRate() { return rate; } public double getHours() { return hours; } }
Here is the second class to create and instance, populate it and then display the information.
import javax.swing.*; class PayrollMLInput { public static void main( String [] args ) //fill in the information for the employees { String input; String payName; int payIdNumber; double payRate; double payHours; double payGross; //creates an object from the payroll class. PayrollML Payee1 = new PayrollML(); [COLOR="Orange"]// I assume this area is where the problem lies. Setting the data. I cannot tell for sure, since it all compiles without errors.[/COLOR] [COLOR="Red"]input = JOptionPane.showInputDialog( "Please enter employee name."); payName = input; Payee1.setName(payName); input = JOptionPane.showInputDialog( "Please enter employee ID."); payIdNumber = Integer.parseInt(input); Payee1.setIdNumber(payIdNumber); input = JOptionPane.showInputDialog( "Please enter employee rate of pay in dollars per hour."); payRate = Double.parseDouble(input); Payee1.setRate(payRate); input = JOptionPane.showInputDialog( "Please enter employee hours worked."); payHours = Double.parseDouble(input); Payee1.setHours(payHours); payGross = (Payee1.getRate() * Payee1.getHours());[/COLOR] [COLOR="Orange"]//Unless this is the issue and I'm not calling it correctly. Either way, I cannot tell for sure where my error is. To me it looks like I've named the class and parameters correctly.[/COLOR] JOptionPane.showMessageDialog( null, "Employee is " + Payee1.getName() + "\n" + "Id Number: " + Payee1.getIdNumber() + "\n" + "Makes " + Payee1.getRate() + " Per Hour\n" + "Worked " + Payee1.getHours() + " hours this week\n" + "Earned " + payGross + " dollars"); System.exit(0); } }
When I run the second program, after all the JOptionPane promts are completed, I get nulls and zero's for the displayed data.
Any help in pointing out my error would be greatly appreciated.
Thank you.