86.6k views
0 votes
Write a programm that has been generalized to read a user's input value for hourlyWage.Generalize the program to get user input values for workHoursPerWeek and workWeeksPerYear (change those variables' initializations to 0).

1 Answer

3 votes

Answer:

The simple way to get user input values is:

in C++:

Suppose hourlyWage, workHoursPerWeek and workWeeksPerYear are int type variable. So first declare and initialize these variables to 0

int workHoursPerWeek = 0;

int workWeeksPerYear = 0;

int hourlyWage = 0;

Now a print statement can be used to prompt user to enter the values for hourlyWage, workHoursPerWeek and workWeeksPerYear as:

cout<<"Enter hourly wage: ";

cout<<"Enter work hours per week: ";

cout<<"Enter work hours per year: ";

Next, to read the user input value cin statement is used which reads the values for the above variables as:

cin>>hourlyWage;

cin>>workHoursPerWeek;

cin>>workWeeksPerYear;

In JAVA:

First declare the three variables and initialize the variables to 0:

int hourlyWage = 0;

int workHoursPerWeek = 0;

int workWeeksPerYear = 0;

Next, prompt the user to enter the values for these variables and read the input values. We use Scanner class to take input from user.

Scanner scnr = new Scanner(System.in); //creates an object of Scanner class named scnr

System.out.println("Enter hourly wage: "); //prompts user to enter the value for hourly wage

hourlyWage = scnr.nextInt(); //reads the value of hourly wage from user

System.out.println("Enter work hours per week: "); //prompts user to enter the value for work hours per week

workHoursPerWeek = scnr.nextInt(); //reads the value of work hours per week from user

System.out.println("Enter work hours per year: "); //prompts user to enter the value for work hours per year

workWeeksPerYear = scnr.nextInt(); //reads the value of work hours per year from user

Step-by-step explanation:

Here is the complete JAVA program:

import java.util.Scanner; // used to take input from user

public class Salary {

public static void main (String [] args) { //start of main function

Scanner scnr = new Scanner(System.in); //creates object of Scanner class

//declare and initialize variables

int hourlyWage = 0;

int workHoursPerWeek = 0;

int workWeeksPerYear = 0;

final int monthsPerYear = 12; //Declares as final and use standard naming

int annualSalary = 0;

int monthlySalary = 0;

/*prompts user to enter the value for hourly wage, work hours per week and work hours per year and reads the values from user. nextInt() is used to scan the next token of the input as a Int */

System.out.println("Enter hourly wage: ");

hourlyWage = scnr.nextInt();

System.out.println("Enter work hours per week: ");

workHoursPerWeek = scnr.nextInt();

System.out.println("Enter work hours per year: ");

workWeeksPerYear = scnr.nextInt();

annualSalary = hourlyWage * workHoursPerWeek * workWeeksPerYear; //computes annual salary

System.out.print("Annual salary is: ");

System.out.println(annualSalary); //displays annual salary

monthlySalary = (hourlyWage * workHoursPerWeek * workWeeksPerYear) / monthsPerYear; //computes monthly salary

System.out.print("Monthly salary is: ");

System.out.println(monthlySalary); //displays value of monthly salary

return; } }

The output and program screenshots are attached.

Write a programm that has been generalized to read a user's input value for hourlyWage-example-1
User Seeiespi
by
5.2k points