Solved

Java Method Question - Returning Multiple Println Statements

Posted on 2011-02-24
7
572 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
  • 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now