[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Java Method Question - Returning Multiple Println Statements

Posted on 2011-02-24
7
Medium Priority
?
579 Views
Last Modified: 2012-05-11
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:

 
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(" ");
}

Open in new window


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
0
Comment
Question by:Kody-Burg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 34972686
you will have to pass the employee identification (id) to displayEmployeeData() method in EmployeePayrollUser class, and it will return you the EmployeePayrollUser object. Then you will print its values just like you would have done in EmployeePayrollUser class


And yes, seeing both files can  help more
0
 
LVL 6

Author Comment

by:Kody-Burg
ID: 34972728
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.
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 34972769
try this

public class EmployeePayroll
{

	private String employeeName;
	private double hoursWorked;
	private double payRate;
	private double totalPay;
	private double fedTax;
	private double stateTax;
//	private double totalState;
//	private double totalFed;
//	private double totalNetPay;
//	private double totalWeeklyPay;

public EmployeePayroll( double newPay )
{
	totalPay = newPay;
}
public void setName( String Value )
{
	employeeName = Value;
}
public String getName()
{
	return employeeName;
}
public void setHoursWorked( double Amount )
{
	hoursWorked = Amount;
}
public void setPayRate( double Amount )
{
	payRate = Amount;
}
public double getHours()
{
	return hoursWorked;
}
public void calculateWeeklyPay()
{
	totalPay = (payRate * hoursWorked);
}
public double getWeeklyPay()
{
	return totalPay;
}
public void addYearlyHours()
{
	hoursWorked = (hoursWorked * 52);
}
public double getYearlyHours()
{
	return hoursWorked;
}
public void calculateGrossPay()
{
	totalPay = (payRate * hoursWorked);
}
public double getGrossPay()
{
	return totalPay;
}
public void setFedTax( double Amount )
{
	fedTax = Amount;
}
public void setStateTax ( double Amount )
{
	stateTax = Amount;
}

public void calculateNetPay()
{
	totalPay = totalPay -(totalPay) * (stateTax + fedTax);
}
public double getNetPay()
{
	return totalPay;
}



public void displayEmployeeData(EmployeePayroll account)
{
	System.out.println(" ");
		System.out.println("\nThe program will now compile a payroll report.");
		
		System.out.println(" ");
		System.out.println(" ");
		System.out.println(" ");
		
//		System.out.println( account.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(" ");
		
		System.out.println( "\nThank you for using the Employee Payroll Program.");
		System.out.println(" ");
		System.out.println( "\nThe program will now terminate. Good bye.");
}

}


import java.util.Scanner;

public class EmployeePayrollUser
 {

	public static void main( String args[] )
	 {
		EmployeePayroll	account;
		String			name;
		int				hours;
		double			currentRate;
		double			fedTaxRate;
		double			stateTaxRate;
		double			totalPay;
		
		Scanner keyboard = new Scanner(System.in);
		totalPay = 0.0;
		account = new EmployeePayroll(totalPay);
		
		System.out.print("\nEnter the Employee's name: ");
		name = keyboard.nextLine();
		account.setName( name );
		
		System.out.println("\nYou are now working on the payroll account for " + account.getName() );
		
		System.out.print("\nEnter the hourly pay rate for this employee: ");
		currentRate = keyboard.nextDouble();
		account.setPayRate( currentRate );
		
		System.out.print("\nEnter the amount of hours that this employee works per week: ");
		hours = keyboard.nextInt();
		account.setHoursWorked( hours );
		
		System.out.print("\nEnter the federal tax rate (normally .075): ");
		fedTaxRate = keyboard.nextDouble();
		account.setFedTax( fedTaxRate );
		
		System.out.print("\nEnter the state tax rate (ex: .015): ");
		stateTaxRate = keyboard.nextDouble();
		account.setStateTax ( stateTaxRate );
		
		account.displayEmployeeData() ;
	}
	
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:Kody-Burg
ID: 34972857
EmployeePayroll compiled just fine, but EmployeePayrollUser bombed with the following error:

EmployeePayrollUser.java:49: displayEmployeeData(EmployeePayroll) in EmployeePayroll cannot be applied to ()
      account.displayEmployeeData();
                   ^
1 error
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 2000 total points
ID: 34972906
replace that line with

account.displayEmployeeData(account) ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34972947
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(employee);

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();
    }

Open in new window

0
 
LVL 6

Author Closing Comment

by:Kody-Burg
ID: 34973055
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.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video teaches viewers about errors in exception handling.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question