Kody-Burg
asked on
Java Method Question - Returning Multiple Println Statements
Hello Everyone,
I am learning to program in Java. Right now, I am learning the basics and creating programs that run off of the command line (accept input through scanner, return values through println). My latest project involves a program that computes employee payroll. It involves two files, EmployeePayroll, which contains the methods that make everything work, and EmployeePayrollUser, which accepts input from the scanner method and displays an employee payroll report. I have the entire program working, but the teacher of the class would like me to create a method in EmployeePayroll called displayEmployeeData() to display my payroll report by calling on that method in EmployeePayrollUser instead of just putting my println lines in EmployeePayrollUser itself. I think that I just need to know what type my method needs to be. Here is the code for the method:
1) how do I make this work?
2) how to I call upon this in my EmployeePayrollUser.java file?
I can attach both full java files if you need to see them to help.
Thank you for your help in advance!
Kody
I am learning to program in Java. Right now, I am learning the basics and creating programs that run off of the command line (accept input through scanner, return values through println). My latest project involves a program that computes employee payroll. It involves two files, EmployeePayroll, which contains the methods that make everything work, and EmployeePayrollUser, which accepts input from the scanner method and displays an employee payroll report. I have the entire program working, but the teacher of the class would like me to create a method in EmployeePayroll called displayEmployeeData() to display my payroll report by calling on that method in EmployeePayrollUser instead of just putting my println lines in EmployeePayrollUser itself. I think that I just need to know what type my method needs to be. Here is the code for the method:
public ??? displayEmployeeData()
{
System.out.println("\nHere is the payroll report for " + account.getName() + ":" );
System.out.println(" ");
System.out.println(account.getName() + " works " + account.getHours() + " hour(s) per week.");
System.out.println(" ");
account.calculateWeeklyPay();
System.out.println(account.getName() + " is paid $" + account.getWeeklyPay() + " per week before taxes are withheld.");
System.out.println(" ");
account.addYearlyHours();
System.out.println(account.getName() + " works " + account.getYearlyHours() + " hour(s) per year (based on 52 weeks).");
System.out.println(" ");
account.calculateGrossPay();
System.out.println(account.getName() + " grosses $" + account.getGrossPay() + " per year.");
System.out.println(" ");
account.calculateNetPay();
System.out.println(account.getName() + " nets $" + account.getNetPay() + " per year.");
System.out.println(" ");
}
1) how do I make this work?
2) how to I call upon this in my EmployeePayrollUser.java file?
I can attach both full java files if you need to see them to help.
Thank you for your help in advance!
Kody
ASKER
Here is the .java file for EmployeePayroll:
EmployeePayroll.java
Here is the .java file for EmployeePayrollUser:
EmployeePayrollUser.java
As of right now, if you run EmployeePayrollUser, it will print the report to the screen. My objective is to move those println statements to EmployeePayroll under the displayEmployeeData() method and tell EmployeePayrollUser to get the report from there.
EmployeePayroll.java
Here is the .java file for EmployeePayrollUser:
EmployeePayrollUser.java
As of right now, if you run EmployeePayrollUser, it will print the report to the screen. My objective is to move those println statements to EmployeePayroll under the displayEmployeeData() method and tell EmployeePayrollUser to get the report from there.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
EmployeePayroll compiled just fine, but EmployeePayrollUser bombed with the following error:
EmployeePayrollUser.java:4 9: displayEmployeeData(Employ eePayroll) in EmployeePayroll cannot be applied to ()
account.displayEmployeeDat a();
^
1 error
EmployeePayrollUser.java:4
account.displayEmployeeDat
^
1 error
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Typically, the proper way is to implement toString in the appropriate class. In this case, it's EmployeePayrollUser.
The calling class would execute
System.out.println(employe e);
where 'employee' is an instance of EmployeePayrollUser
Here's a stub implementation you can fill out
The calling class would execute
System.out.println(employe
where 'employee' is an instance of EmployeePayrollUser
Here's a stub implementation you can fill out
public String toString() {
final String LF = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append(getName()).append(":");
sb.append(LF);
sb.append(getName()).append(" works ").append(getHours())
.append(" hour(s) per week.");
return sb.toString();
}
ASKER
Thank you. For some reason when I tried the same thing yesterday, I couldn't get it to work. You have saved me from my frustration.
And yes, seeing both files can help more