Question

(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)

  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

  • Put at least two outputs (two different numbers and results)  (results after you run your code) at the end of your code as a multi-line comment.

/*

Your result

*/

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

SOURCE CODE IN JAVA:

import java.util.Scanner;

class Main

{

//variables to store user inputs and calculated values

int hours;

double rate, pay;

//function to get input from user and throw Exception for wrong input

void getInputs()throws Exception

{

    Scanner in=new Scanner(System.in);

    System.out.print("Enter hours: ");

    hours=in.nextInt();

    if(hours<0)

    throw new Exception("Hours cannot be negative!");

    System.out.print("Enter rate: $");

    rate=in.nextDouble();

    if(rate<=0)

    throw new Exception("Rate cannot be negative or zero!");

}

//function to calculate pay

void calculatePay()

{

    pay=hours*rate;

    if(hours>40)

    pay+=(hours-40)*rate*0.5;

}

//function to print pay

void printPay()

{

    System.out.println("Pay: $"+pay);

}

public static void main(String[] args)

{

    try

    {

      //calling the functions

      Main ob=new Main();

      ob.getInputs();

      ob.calculatePay();

      ob.printPay();

    }

    catch(Exception e) //catching any Exception thrown

    {

      System.out.println(e);

    }

}

}

OUTPUT:

Enter hours: 45 Enter rate: $10 Pay: $475.0

Regards!

Add a comment
Know the answer?
Add Answer to:
(Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive...
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
  • 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...

  • (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...

  • 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,...

  • Design an algorithm to calculate the weekly pay for employees. The user will prompted to enter...

    Design an algorithm to calculate the weekly pay for employees. The user will prompted to enter the number of hours worked and their job code. Hours in excess of 40 are paid at 1.5 times the hourly pay. Job code hourly rates are: P - $10, Q - $12.50, R - $15.00, and S - $20.00. Use both a case and a selection structure.

  • Exercise 1 A program was created that outputs the following unto the console: Hello! What is...

    Exercise 1 A program was created that outputs the following unto the console: Hello! What is your name: Hello, Jane Doe! Using this program, we can help you determine you pay for the week! Please enter the hours you worked this week: 20 Next, please enter your rate of pay: 10 Calculating… you will make $ 200 by the end of the week! Here is the code for that program (you should have written a close variant of this last...

  • Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...

    Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...

  • 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...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

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