Create a non-GUI-based Java application that calculates weekly pay for an employ
ID: 3763341 • Letter: C
Question
Create a non-GUI-based Java application that calculates weekly pay for an employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.
(The application compiles and runs.)
(The application displays text that requests the user input the name of the employee, the hourly rate, and the number of hours worked.)
(The application prints out the name of the employee and the correct weekly pay amount in dollar format.)
(The source code is readable and well documented)
Explanation / Answer
WeeklyPay.java
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class WeeklyPay{
public static void main(String []args){
Scanner scnr = new Scanner(System.in);//scanner to read user input from keyboard
String name; //variable to capture name of the employee
float num_hours,hourly_rate; //variables to capture number of hours worked, hourly rate
System.out.print("Please input the name of the employee: ");
name = scnr.next();//captures name of the employee input from keyboard
System.out.print("Please input the hourly rate: ");
hourly_rate = scnr.nextFloat();//captures hourly rate input from keyboard
System.out.print("Please input the number of hours worked: ");
num_hours = scnr.nextFloat();//captures number of hours worked input from keyboard
double weeklypayInUSD = num_hours * hourly_rate;//calculates weekly pay and saves in double variable weeklypayInUSD
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);//set Locale US currency format
System.out.printf("Weekly pay: %s %n", currencyFormat.format(weeklypayInUSD));//print weeky pay in USD currency format
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.