Question

This is a Java beginner class. Write the following exercise to check the user inputs to...

This is a Java beginner class.

Write the following exercise to check the user inputs to be:

  • Valid input

and

  • positive

with using try-catch (and/or throw Exception )

**************************************************************************

Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC)

  1. getInputs
  2. calculatePay
  3. printPay

Create an object of the class and declare the method as the instance methods.

Enter Hours: 45

Enter Rate: 10

Pay: 475.0

475 = 40 * 10 + 5 * 10*1.5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class Main
{
double hour,rate,pay;
void getInput()
{
while(true)
{
try
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Hours: ");
double temphour=sc.nextDouble();
System.out.println("Enter Rate: ");
double temprate=sc.nextDouble();
hour=temphour;
rate=temprate;
break;
}
catch(InputMismatchException e)
{
System.out.println("Invalid input ");
continue;   
}
}   
}
void calculatePay()
{
if(hour<=40)
pay=hour*rate;
else
{
pay=40*rate+(hour-40)*rate*1.5;
}
}
void printPay()
{
System.out.println("Pay: "+pay);
}
   public static void main(String[] args) {
      
       Main ob=new Main();
       ob.getInput();
       ob.calculatePay();
       ob.printPay();
   }
}
Enter Hours: jkk Invalid input Enter Hours: 45 Enter Rate: 10 Pay: 475.0

DON'T FORGET TO HIT LIKE.

THANKS BY HEART.

Add a comment
Know the answer?
Add Answer to:
This is a Java beginner class. Write the following exercise to check the user inputs to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive...

    (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC) getInputs calculatePay printPay Create...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • This is my python assignment You wrote a program to prompt the user for hours and...

    This is my python assignment You wrote a program to prompt the user for hours and rate per hour to compute gross pay. Also your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Enter Hours: 45 Enter Rate: 10 Pay: 475.0 (475 = 40 * 10 + 5 * 15) Rewrite your pay program using try and except so that your program handles non-numeric input gracefully. Enter Hours: 20 Enter Rate:...

  • It's my python assignment. Thank you You wrote a program to prompt the user for hours...

    It's my python assignment. Thank you You wrote a program to prompt the user for hours and rate per hour to compute gross pay. Also your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Enter Hours: 45 Enter Rate: 10 Pay: 475.0 (475 = 40 * 10 + 5 * 15) Rewrite your pay program using try and except so that your program handles non-numeric input gracefully. Enter Hours: 20 Enter...

  • Write a program to prompt the user for hours and rate per hour using input to...

    Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per...

  • This question is about calculating and printing payslips. User inputs his name, number of worked hours...

    This question is about calculating and printing payslips. User inputs his name, number of worked hours and hourly rate. below is the source file for my program...It's supposed calculate salaries. Given that a work week has 40 hours and over time is 1.5xnormalRate for each our of overtime My output is not working. what is wrong with this code? // Calculate and print payslips #include <iostream> #include <iomanip> using namespace std; const float workingHours = 40.0; void getData( string &employeeP,...

  • Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main()...

    Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main() method. Within the same file, create another class named Employee, and do not declare it public. Create a field for each of the following pieces of information: employee name, employee number, hourly pay rate, and overtime rate. Create accessor and mutator methods for each field. The mutator method for the hourly pay rate should require the value to be greater than zero. If it...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll...

    java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions: • An empty string is given for the employee’s name. • An invalid value is given for the employee’s ID number. If you implemented this field as a string, then an empty string would be invalid. If you implemented this field as a numeric variable, then a negative number or...

  • Create the Python code for a program adhering to the following specifications. Write an Employee class...

    Create the Python code for a program adhering to the following specifications. Write an Employee class that keeps data attributes for the following pieces of information: - Employee Name (a string) - Employee Number (a string) Make sure to create all the accessor, mutator, and __str__ methods for the object. Next, write a class named ProductionWorker that is a subclass of the Employee class. The ProductionWorker class should keep data attributes for the following information: - Shift number (an integer,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT