computer

profileete08
mrwjavaweek04example.java

/************************************************/ /* Your Name */ /* Week 4 Programming Example */ /* Due Date: ______________________________ */ /* CS 151- Introduction to Programming */ /************************************************/ //This is a copy of Lab 2 redone with functions to enhance portability //in the source code. import java.util.*; import java.text.*; public class mrwJavaWeek04Example { //Custom Format - Allows for the formatting of the output with commas //and fixed decimal places. static public String customFormat (String pattern, double value) { DecimalFormat myFormatter = new DecimalFormat(pattern); String output = myFormatter.format(value); //String right_output = new Format(output); return output; } //calcGrossPay: accepts hoursWorked and hourlyRate - returns the product for Gross Pay static public double calcGrossPay (double hoursWorked, double hourlyRate) { return (hoursWorked * hourlyRate); } //calcFederalTaxes: accepts the GrossPay and returns 15% of the gross pay as Federal Taxes static public double calcFederalTaxes (double grossPay) { return (0.15 * grossPay); } //calcStateTaxes: accepts the GrossPay and returns 8% of the gross pay as State Taxes static public double calcStateTaxes (double grossPay) { return (0.08 * grossPay); } //calcNetPay: accepts the GrossPay, Federal Taxes, and state taxes and returns the Net Pay static public double calcNetPay (double grossPay, double fedTaxes, double stateTaxes) { return (grossPay - fedTaxes - stateTaxes); } public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); //Class reference to allow input from the console String firstName; //First Name of the employee String lastName; //Last Name of the employee double hoursWorked; //Hours Worked of an employee double hourlyRate; //Hourly Rate of an employee double grossPay; //Gross Pay of an employee double fedTaxes; //Federal taxes of an employee double stateTaxes; //State Taxes of an employee double netPay; //Net Pay of an employee //Inputs did not change in terms of what you are accepting from the end user in the process. System.out.print("Input your First Name: "); firstName = stdIn.nextLine(); System.out.print("Input your Last Name: "); lastName = stdIn.nextLine(); System.out.print("Input the amount of hours worked: "); hoursWorked = stdIn.nextDouble(); System.out.print("Input the hourly rate: "); hourlyRate = stdIn.nextDouble(); //The next four lines of code will be the function calls grossPay = calcGrossPay(hoursWorked, hourlyRate); fedTaxes = calcFederalTaxes(grossPay); stateTaxes = calcStateTaxes(grossPay); netPay = calcNetPay(grossPay, fedTaxes, stateTaxes); System.out.println(); System.out.println("Payroll Information for " + lastName + ", " + firstName + ":"); System.out.println(); System.out.printf("Hours Worked: %20s\n", customFormat("###,###,##0.00", hoursWorked)); System.out.printf("Hourly Rate: %20s\n", customFormat("###,###,##0.00", hourlyRate)); System.out.printf("\n"); System.out.printf("Gross Pay: %20s\n", customFormat("###,###,##0.00", grossPay)); System.out.printf("Federal Taxes: %20s\n", customFormat("###,###,##0.00", fedTaxes)); System.out.printf("State Taxes: %20s\n", customFormat("###,###,##0.00", stateTaxes)); System.out.printf("Net Pay: %20s\n", customFormat("###,###,##0.00", netPay)); } }