Introduction

The Java language provides many options to create an application, such as many techniques to display a value. In this exercise, we will continue our payroll application.

Practical LearningPractical Learning: Creating the Application

  1. Start Visual Studio Code
  2. On the main menu, click File and click New File...
  3. In the New File text box, type Exercise.java and press Enter
  4. In the Create File dialog box, ccreate a folder named PayrollEvaluation and display it in the top combo box
  5. Set the file name to Exercise.java and press Enter
  6. In the empty document, type the following:
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
    
            System.out.print("Payroll Evaluation\n");
            System.out.print("===========================================================\n");
            System.out.print("Enter the following pieces of information\n");
            System.out.print("-----------------------------------------------------------\n");
            System.out.print("Employee Information\n");
            System.out.print("-----------------------------------------------------------\n");
            
            String firstName, lastName;
            System.out.print("First Name:\t\t");
            firstName = scnr.next();
            System.out.print("Last Name:\t\t");
            lastName = scnr.next();
            System.out.print("Hourly Salary:\t\t");
    
            double hSalary = 0.00;
            hSalary = scnr.nextDouble();
            System.out.print("\n===========================================================");
            System.out.print("\nTime worked - First Week");
            System.out.print("\n-----------------------------------------------------------");
    
            System.out.print("\nMonday:\t\t\t");
            double wk1Monday = scnr.nextDouble();
    
            System.out.print("Tuesday:\t\t");
            double wk1Tuesday = scnr.nextDouble();
    
            System.out.print("Wednesday:\t\t");
            double wk1Wednesday = scnr.nextDouble();
    
            System.out.print("Thursday:\t\t");
            double wk1Thursday = scnr.nextDouble();
    
            System.out.print("Friday:\t\t\t");
            double wk1Friday = scnr.nextDouble();
            System.out.print("\n===========================================================");
            System.out.print("\nTime worked - Second Week");
            System.out.print("\n-----------------------------------------------------------");
    
            System.out.print("\nMonday:\t\t\t");
            double wk2Monday = scnr.nextDouble();
    
            System.out.print("Tuesday:\t\t");
            double wk2Tuesday = scnr.nextDouble();
    
            System.out.print("Wednesday:\t\t");
            double wk2Wednesday = scnr.nextDouble();
    
            System.out.print("Thursday:\t\t");
            double wk2Thursday = scnr.nextDouble();
    
            System.out.print("Friday:\t\t\t");
            double wk2Friday = scnr.nextDouble();
    
            scnr.close();
    
            double wk1TimeWorked = wk1Monday + wk1Tuesday + wk1Wednesday + wk1Thursday + wk1Friday;
            double wk2TimeWorked = wk2Monday + wk2Tuesday + wk2Wednesday + wk2Thursday + wk2Friday;
    
            double wk1RegTime  = wk1TimeWorked;
            double wk1Overtime = 0.00;
            double wk1RegPay   = hSalary * wk1TimeWorked;
            double wk1OverPay  = 0.00;
    
            double wk2RegTime  = wk2TimeWorked;
            double wk2Overtime = 0.00;
            double wk2RegPay   = hSalary * wk2TimeWorked;
            double wk2OverPay  = 0.00;
            
            if(wk1TimeWorked > 40.00)
            {
                wk1RegTime  = 40.00;
                wk1Overtime = wk1TimeWorked - 40.00;
                wk1RegPay   = hSalary * 40.00;
                wk1OverPay  = hSalary * 1.50 * wk1Overtime;
            }
            
            if(wk2TimeWorked > 40.00)
            {
                wk2RegTime  = 40.00;
                wk2Overtime = wk2TimeWorked - 40.00;
                wk2RegPay   = hSalary * 40.00;
                wk2OverPay  = hSalary * 1.50 * wk2Overtime;
            }
    
            double wk1NetPay = wk1RegPay + wk1OverPay;
            double wk2NetPay = wk2RegPay + wk2OverPay;
            double netPay = wk1NetPay + wk2NetPay;
    
            System.out.print("\n===========================================================");
            System.out.print("\nPayroll Evaluation");
            System.out.print("\n===========================================================");
            System.out.print("\nEmployee Information");
            System.out.print("\n-----------------------------------------------------------");
            System.out.print("\nFull Name:\t\t");
            System.out.print(firstName);
            System.out.print(" ");
            System.out.print(lastName);
            System.out.print("\nHourly Salary:\t\t");
            System.out.print(String.format("%.2f", (double)hSalary));
            System.out.print("\n===========================================================");
            System.out.print("\nTime Worked Summary");
            System.out.print("\n--------+--------+---------+-----------+----------+--------");
            System.out.print("\n        | Monday | Tuesday | Wednesday | Thursday | Friday");
            System.out.print("\n--------+--------+---------+-----------+----------+--------\n");
            System.out.print(String.format(" Week 1 | %5.2f  |%6.2f   |%8.2f   |%7.2f   |%6.2f",
                             (double)wk1Monday, (double)wk1Tuesday, (double)wk1Wednesday, (double)wk1Thursday, (double)wk1Friday));
            System.out.print("\n--------+--------+---------+-----------+----------+--------");
            System.out.print(String.format("\n Week 2 | %5.2f  |%6.2f   |%8.2f   |%7.2f   |%6.2f",
                             (double)wk2Monday, (double)wk2Tuesday, (double)wk2Wednesday, (double)wk2Thursday, (double)wk2Friday));
            System.out.print("\n========+========+=========+===========+==========+========");
            System.out.print("\n\t\t\t\tPay Summary");
            System.out.print("\n-----------------------------------------------------------");
            System.out.print("\n\t\t\t\t\tTime      Pay");
            System.out.print("\n-----------------------------------------------------------");
            System.out.print(String.format("\n\tFirst Week\tRegular:\t%4.2f\t%8.2f", wk1RegTime, wk1RegPay));
            System.out.print("\n-----------------------------------------------------------");
            System.out.print(String.format("\n\t\t\tOver Time:\t%5.2f\t%8.2f", wk1Overtime, wk1OverPay));
            System.out.print("\n-----------------------------------------------------------");
            System.out.print(String.format("\n\t\t\tWeekly Pay:\t%16.2f", wk1NetPay));
            System.out.print("\n==========================================================");
            System.out.print(String.format("\n\tSecond Week\tRegular:\t%4.2f\t%8.2f", wk2RegTime, wk2RegPay));
            System.out.print("\n-----------------------------------------------------------");
            System.out.print(String.format("\n\t\t\tOver Time:\t%5.2f\t%8.2f", wk2Overtime, wk2OverPay));
            System.out.print("\n-----------------------------------------------------------");
            System.out.print(String.format("\n\t\t\tWeekly Pay:\t%16.2f", wk2NetPay));
            System.out.print("\n===========================================================");
            System.out.print(String.format("\n\t\t\tNet Pay:%24.2f", netPay));
            System.out.print("\n===========================================================");
        }
    }
  7. To execute the project, on the main menu of Visual Studio Code, click Run -> Start Without Debugging
  8. In the Terminal window, click the right side of First Name, type each of the following values and press Enter each time:
    First Name:    Michael
    Last Name:     Carlock
    Hourly Salary: 28.46
    Week 1
    Monday:         7
    Tuesday:        8
    Wednesday:      6.5
    Thursday:       8.5
    Friday:         7.5
    Week 2
    Monday:         9.5
    Tuesday:        8.5
    Wednesday:     10.5
    Thursday:       9
    Friday:         8
    Here are are the results:
    Payroll Evaluation
    ===========================================================
    Enter the following pieces of information
    -----------------------------------------------------------
    Employee Information
    -----------------------------------------------------------
    First Name:             Michael
    Last Name:              Carlock
    Hourly Salary:          28.46
    
    ===========================================================
    Time worked - First Week
    -----------------------------------------------------------
    Monday:                 7
    Tuesday:                8
    Wednesday:              6.5
    Thursday:               8.5
    Friday:                 7.5
    
    ===========================================================
    Time worked - Second Week
    -----------------------------------------------------------
    Monday:                 9.5
    Tuesday:                8.5
    Wednesday:              10.5
    Thursday:               9
    Friday:                 8
    
    ===========================================================
    Payroll Evaluation
    ===========================================================
    Employee Information
    -----------------------------------------------------------
    Full Name:              Michael Carlock
    Hourly Salary:          28.46
    ===========================================================
    Time Worked Summary
    --------+--------+---------+-----------+----------+--------
            | Monday | Tuesday | Wednesday | Thursday | Friday
    --------+--------+---------+-----------+----------+--------
     Week 1 |  7.00  |  8.00   |    6.50   |   8.50   |  7.50
    --------+--------+---------+-----------+----------+--------
     Week 2 |  9.50  |  8.50   |   10.50   |   9.00   |  8.00
    ========+========+=========+===========+==========+========
                                    Pay Summary
    -----------------------------------------------------------
                                            Time      Pay
    -----------------------------------------------------------
            First Week      Regular:        37.50    1067.25
    -----------------------------------------------------------
                            Over Time:       0.00       0.00
    -----------------------------------------------------------
                            Weekly Pay:              1067.25
    ==========================================================
            Second Week     Regular:        40.00    1138.40
    -----------------------------------------------------------
                            Over Time:       5.50     234.80
    -----------------------------------------------------------
                            Weekly Pay:              1373.20
    ===========================================================
                            Net Pay:                 2440.45
    ===========================================================
  9. Close your programming environment

Home Copyright © 2001-2025, FunctionX Sunday 22 December 2024, 21:02 Home