Introduction

The primary type of application you can create with the Java language is a console or teminal applicaiton. In this exercise, we will have an introduction to Java by creating a simple small console application.

As Java is a free language, you can download and install it on your computer. There are various ways you can create a Java application, including the Command prompt or a terminal. You can also use a programming environment such as Eclipse (which is free). For our example, we will use Visual Studio Code (which also is free).

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 PayrollEvaluation1 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("----------------------------------------------");
            System.out.print("\nTime worked");
            System.out.print("\n---------------------------------------------");
    
            System.out.print("\nMonday:\t\t\t");
            double mon;
            mon = scnr.nextDouble();
    
            System.out.print("Tuesday:\t\t");
            double tue = scnr.nextDouble();
    
            System.out.print("Wednesday:\t\t");
            double wed = scnr.nextDouble();
    
            System.out.print("Thursday:\t\t");
            double thu = scnr.nextDouble();
    
            System.out.print("Friday:\t\t\t");
            double fri = scnr.nextDouble();
    
            scnr.close();
    
            double timeWorked = mon + tue + wed + thu + fri;
    
            double regTime  = timeWorked;
            double regPay   = hSalary * timeWorked;
            double overtime = 0.00;
            double overPay  = 0.00;
            
            if(timeWorked > 40.00)
            {
                regTime  = 40.00;
                regPay   = hSalary * 40.00;
                overtime = timeWorked - 40.00;
                overPay  = hSalary * 1.50 * overtime;
            }
    
            double netPay = regPay + overPay;
    
            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(hSalary);
            System.out.print("\n==============================================");
            System.out.print("\nTime Worked Summary");
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\tMonday:\t\t");
            System.out.print(String.format("%.2f", (double)mon));
            System.out.print("\n\tTuesday:\t");
            System.out.print(String.format("%.2f", (double)tue));
            System.out.print("\n\tWednesday:\t");
            System.out.print(String.format("%.2f", (double)wed));
            System.out.print("\n\tThursday:\t");
            System.out.print(String.format("%.2f", (double)thu));
            System.out.print("\n\tFriday:\t\t");
            System.out.print(String.format("%.2f", (double)fri));
            System.out.print("\n==============================================");
            System.out.print("\nPay Summary");
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\t\t\tTime\tPay");
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\tRegular:\t");
            System.out.print(String.format("%.2f", (double)regTime));
            System.out.print("\t");
            System.out.print(String.format("%.2f", (double)regPay));
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\tOvertime:\t");
            System.out.print(String.format("%.2f", (double)overtime));
            System.out.print("\t");
            System.out.print(String.format("%.2f", (double)overPay));
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\t\tNet Pay:\t");
            System.out.print(String.format("%.2f", (double)netPay));
            System.out.print("\n==============================================");
        }
    }
  7. To execute the project, on the main menu, click Debug -> Start Without Debugging
  8. When requested, type each of the following values and press Enter each time:
    First Name: Michael
    Last Name: Carlock
    Hourly Salary: 28.25
    Monday 7
    Tuesday: 8
    Wednesday: 6.5
    Thursday: 8.5
    Friday: 6.5
    Payroll Evaluation
    ----------------------------------------------
    Enter the following pieces of information
    ----------------------------------------------
    Employee Information
    ----------------------------------------------
    First Name:             Michael
    Last Name:              Carlock
    Hourly Salary:          28.46
    ----------------------------------------------
    Time worked
    ---------------------------------------------
    Monday:                 7
    Tuesday:                8
    Wednesday:              6.5
    Thursday:               8.5
    Friday:                 6.5
    
    ==============================================
    Payroll Evaluation
    ==============================================
    Employee Information
    ----------------------------------------------
    Full Name:              Michael Carlock
    Hourly Salary:          28.46
    ==============================================
    Time Worked Summary
    ----------------------------------------------
            Monday:         7.00
            Tuesday:        8.00
            Wednesday:      6.50
            Thursday:       8.50
            Friday:         6.50
    ==============================================
    Pay Summary
    ----------------------------------------------
                            Time    Pay
    ----------------------------------------------
            Regular:        36.50   1038.79
    ----------------------------------------------
            Overtime:       0.00    0.00
    ----------------------------------------------
                    Net Pay:        1038.79
    ==============================================

Using an Opposite Conditional Statement

In the previous section, we use a Greater Than operator to check a condition. That operator has an opposite, which is the Less Than Or Equal operator. We will uase apply as an example.

Practical LearningPractical Learning: Using an Opposite Conditional Statement

  1. Change the document as follows:
    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("----------------------------------------------");
            System.out.print("\nTime worked");
            System.out.print("\n---------------------------------------------");
    
            System.out.print("\nMonday:\t\t\t");
            double mon;
            mon = scnr.nextDouble();
    
            System.out.print("Tuesday:\t\t");
            double tue = scnr.nextDouble();
    
            System.out.print("Wednesday:\t\t");
            double wed = scnr.nextDouble();
    
            System.out.print("Thursday:\t\t");
            double thu = scnr.nextDouble();
    
            System.out.print("Friday:\t\t\t");
            double fri = scnr.nextDouble();
    
            scnr.close();
    
            double timeWorked = mon + tue + wed + thu + fri;
    
            double regTime  = 40.00;
            double regPay   = hSalary * 40.00;
            double overtime = timeWorked - 40.00;
            double overPay  = hSalary * 1.50 * overtime;
            
            if(timeWorked <= 40.00)
            {
                regTime  = timeWorked;
                regPay   = hSalary * timeWorked;
                overtime = 0.00;
                overPay  = 0.00;
            }
    
            double netPay = regPay + overPay;
    
            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(hSalary);
            System.out.print("\n==============================================");
            System.out.print("\nTime Worked Summary");
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\tMonday:\t\t");
            System.out.print(String.format("%.2f", (double)mon));
            System.out.print("\n\tTuesday:\t");
            System.out.print(String.format("%.2f", (double)tue));
            System.out.print("\n\tWednesday:\t");
            System.out.print(String.format("%.2f", (double)wed));
            System.out.print("\n\tThursday:\t");
            System.out.print(String.format("%.2f", (double)thu));
            System.out.print("\n\tFriday:\t\t");
            System.out.print(String.format("%.2f", (double)fri));
            System.out.print("\n==============================================");
            System.out.print("\nPay Summary");
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\t\t\tTime\tPay");
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\tRegular:\t");
            System.out.print(String.format("%.2f", (double)regTime));
            System.out.print("\t");
            System.out.print(String.format("%.2f", (double)regPay));
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\tOvertime:\t");
            System.out.print(String.format("%.2f", (double)overtime));
            System.out.print("\t");
            System.out.print(String.format("%.2f", (double)overPay));
            System.out.print("\n----------------------------------------------");
            System.out.print("\n\t\tNet Pay:\t");
            System.out.print(String.format("%.2f", (double)netPay));
            System.out.print("\n==============================================");
        }
    }
  2. To execute the project again, on the main menu, click Debug -> Start Without Debugging
  3. When requested, type each of the following values and press Enter each time:
    First Name: Catherine
    Last Name: Busbey
    Hourly Salary: 24.37
    Monday 9.50
    Tuesday: 8
    Wednesday: 10.50
    Thursday: 9
    Friday: 10.50
    Payroll Evaluation
    ----------------------------------------------
    Enter the following pieces of information
    ----------------------------------------------
    Employee Information
    ----------------------------------------------
    First Name:             Catherine
    Last Name:              Busbey
    Hourly Salary:          28.37
    ----------------------------------------------
    Time worked
    ---------------------------------------------
    Monday:                 9.5
    Tuesday:                8
    Wednesday:              10.5
    Thursday:               9
    Friday:                 10.5
    
    ==============================================
    Payroll Evaluation
    ==============================================
    Employee Information
    ----------------------------------------------
    Full Name:              Catherine Busbey
    Hourly Salary:          28.37
    ==============================================
    Time Worked Summary
    ----------------------------------------------
            Monday:         9.50
            Tuesday:        8.00
            Wednesday:      10.50
            Thursday:       9.00
            Friday:         10.50
    ==============================================
    Pay Summary
    ----------------------------------------------
                            Time    Pay
    ----------------------------------------------
            Regular:        40.00   1134.80
    ----------------------------------------------
            Overtime:       7.50    319.16
    ----------------------------------------------
                    Net Pay:        1453.96
    ==============================================
  4. Press T to close the window and return to your programming environment

Home Copyright © 2001-2025, FunctionX Sunday 22 December 2024, 15:39 Home