Question

I need this java program to contain methods that do some of the work that can...

I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. Continue to take input for every employee in a company, and display their information until a sentinel value is entered, that exits the loop and ends the program.

import java.util.Scanner;

public class SalaryCalc {
double Rpay = 0, Opay = 0;

void calPay(double hours, double rate) {
if (hours <= 40) {
Rpay = hours * rate;
Opay = 0;
} else {
double Rhr, Ohr;
Rhr = 40;
Ohr = hours - Rhr;
Rpay = Rhr * rate;
Opay = Ohr * (1.5 * rate);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
int shift = 0;
Double rate, hours;
System.out.println("Pay Calculator");
String ch = "";
//added loop here to take the inputs repetedly
do {
System.out.println("Enter Your Name");
name = sc.next();
System.out.println("Enter Your Shift, Enter 0 for Day, Enter1 for Night");
System.out.println("0=Day, 1= Night");
shift=sc.nextInt();
System.out.println("Enter Number of Hours Worked");
hours = sc.nextDouble();
System.out.println("Enter Hourly Pay");
rate = sc.nextDouble();
SalaryCalc c = new SalaryCalc();
c.calPay(hours, rate);
Double Tpay = c.Rpay + c.Opay;
System.out.println();
System.out.println("Calculate Pay");
System.out.println("Employee Name: " + name);
System.out.println("Employee Regular Pay: " + c.Rpay);
System.out.println("Employee Overtime Pay: " + c.Opay);
System.out.println("Employee Total Pay: " + Tpay);
if (shift == 0) {
System.out.println("Employee PayPeriod is Friday");
} else {
System.out.println("Employee PayPeriod is Saturday");
}
//asking user if they want to continue to enter another employee data
System.out.println("Press Y to continue.Other key to exit ");
ch=sc.next();
} while (ch.equalsIgnoreCase("y"));
}
}

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

import java.util.Scanner;

public class SalaryCalc {
   double Rpay = 0, Opay = 0, hours = 0;
   String name;
   int shift;
//calculates the pay for user
   void calPay(double hours, double rate) {
       if (hours <= 40) {
           Rpay = hours * rate;
           Opay = 0;
       } else {
           double Rhr, Ohr;
           Rhr = 40;
           Ohr = hours - Rhr;
           Rpay = Rhr * rate;
           Opay = Ohr * (1.5 * rate);
       }
   }

   //reads the info from user
   public void readInfo(Scanner sc, SalaryCalc c) {
       System.out.println("Enter Your Name");
       name = sc.next();
       System.out.println("Enter Your Shift, Enter 0 for Day, Enter1 for Night");
       System.out.println("0=Day, 1= Night");
       shift = sc.nextInt();
       System.out.println("Enter Number of Hours Worked");
       hours = sc.nextDouble();
       System.out.println("Enter Hourly Pay");
       double rate = sc.nextDouble();
       c.calPay(hours, rate);
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       SalaryCalc c = new SalaryCalc();

       String name;
       int shift = 0;
       Double rate, hours;
       System.out.println("Pay Calculator");
       String ch = "";
       // added loop here to take the inputs repetedly
       do {
           c.readInfo(sc, c);
           Double Tpay = c.Rpay + c.Opay;
           c.displayInfo(c.name, c.Rpay, c.Opay, Tpay, shift);
           // asking user if they want to continue to enter another employee
           // data
           System.out.println("Press Y to continue.Other key to exit ");
           ch = sc.next();
       } while (ch.equalsIgnoreCase("y"));
   }

   //prints the info for user
   private void displayInfo(String name, double Rpay, double Opay, double Tpay, int shift) {
       System.out.println();
       System.out.println("Calculate Pay");
       System.out.println("Employee Name: " + name);
       System.out.println("Employee Regular Pay: " + Rpay);
       System.out.println("Employee Overtime Pay: " + Opay);
       System.out.println("Employee Total Pay: " + Tpay);
       if (shift == 0) {
           System.out.println("Employee PayPeriod is Friday");
       } else {
           System.out.println("Employee PayPeriod is Saturday");
       }
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
I need this java program to contain methods that do some of the work that can...
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
  • Chapter 4 Loop logic and using loop logic for File Input/0utput. 1. Read Chapter 4 sections...

    Chapter 4 Loop logic and using loop logic for File Input/0utput. 1. Read Chapter 4 sections 4.10 until end of chapter. 2. Review Questions and Exercises, short answers. 3. [20 points] submit SalaryCalcLoopFile.java Let's update our program, SalaryCalcLoop.java, from last week, to now take input about each employee from a file and write their information and salary/pay information to a file. use input file, where each employee has name,shift,hours worked,hourly rate: EmployeePayroll.txt output file should be named: EmployeePayStubs.txt EmployeePayroll.txt Rashid...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • I have this java program that I need to add an abstract method and polymorphism to...

    I have this java program that I need to add an abstract method and polymorphism to it : import java.util.Scanner; //class and encapsulation class BookTheTicket { private String movieName; private String theatreName; private int ticketCost; void myAllmovies() { System.out.println("-------Listing the movies:------"); System.out.println(" 1.DDLJ ------------ $40 \n 2.kkr---------$.50 \n 3.game-movie --------$60 \n 4.fun movie ----- $.70 "); } } // inheritence class theater extends BookTheTicket{ private int numOfTickets; void theater() { System.out.println("*******Listing the theatre:******* \n 1.coco cola tld \n 2.koi gandhi...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • This is not my entire line of code but the errors I'm getting are from this...

    This is not my entire line of code but the errors I'm getting are from this section. Can you please correct it and tell me where I went wrong? Thank you! package comJava; Vimport java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.Scanner; //Lis public Driver { static List<RescueAnimal> list = new ArrayList<>(); public static void main(String[] args) { // Class variables // Create New Dog Dog d = new Dog(); // Create New Monkey Monkey m = new Monkey(); // Method...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

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