Solved

Java Method Question - Returning Multiple Println Statements

Posted on 2011-02-24
7
575 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:gurvinder372
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:
gurvinder372 earned 500 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
Technology Partners: 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:gurvinder372
gurvinder372 earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

749 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