1 An employer hires two students a CS major and an XX major

1. An employer hires two students, a CS major and an XX major to do handyman work under the following conditions:

- The duration of the job is between 21 and 30 days, each choice is equally likely

- There are two options of payment offered to the workers: the employer pays

(i)$1,000.00 for each day of work

(ii)1 penny for the first day and for each other day the payment is twice as many pennies as paid for the previous day’s work

The XX major took option 1, the CS major chose the second option. Did XX or CS make a better deal? What is the minimum, the maximum and the average money each student can make considering the possible cases of 21 to 30 working days? You have to write a program to answer these question.

2. The main method has the following responsibilities:

- Creates a Scanner object to read from the console

- Solicits an integer number between 21 and 30

- Validates the required bounds for the input (we assume that the input is a number). Use a loop for solicitation until input is admissible.

- Saves the input in a variable named days

- Declares integral type variable pennies, xxMin, xxMax, xxAverage, csMin, csMax, csAverage, sumOfPennies

- Runs a for loop that iterates days many times

- For the first day, pennies is assigned 1, for each subsequent day pennies is doubled; you must not use the Math,pow( ) method to compute the values for the pennies variable.

- At each turn of the iteration the day index and the pennies value are displayed on the console according to the template (notice the comma separation), day 20: xyz,abc

   Use the printf( ) method with the comma separator for the display

Hint: see pp 170 – 171 (168 – 169 in Edition 5) for using the comma separator flag

- At each turn of the loop the sumOfPennies variable accumulates the daily payments pennies from day 1 to the current day

- When the iteration is finished, declare a double variable wages and assign it the dollar value that corresponds to sumOfPennies

- Display the dollar total on the console. The display shall follow the template

In 10 days the CS major earned $5.12

Use the comma separator again.

- Run a for loop that iterates 30 times. Use this loop

(i)to compute csMin (the sumOfPennies value at day 21)

(ii)to compute csMax (the sumOfPennies at day 30)

(iii)to compute csAverage (add the sumOfPennies values of days 21, 22, …, 30 and divide by 10).

- After the loop compute xxMin, xxMax, xxAverage (no loop needed)

- Display all the min, max and average values on the console

3. Create a testing plan (it does not have to be documented): the input validation must work correct as well as the penny iteration; run the program for various input values to see that it is free of bugs

4. Sum up your experience in a comment attached to the code. In this comment analyze if the choice made by the CS major was justifiable, which may depend on the number of days worked.

5. Save your comments (the whole text) in a String variable named comment

6. Use Notepad to create a file named wages.txt and save it in you project folder. Type into the file a title line as follows: My Comments on the Penny-run Project

7. Declare and instantiate a PrintWriter object (choose a name for it) such that it can write to the filewages.txt. Do not forget the necessary import and the exception clause in the main header

8. Use your PrintWriter to print the comment to the file and close the file.

9. Use the Windows Explorer to open up the file and verify that the title line disappeared

10. Instantiate your PrintWriter object again such that it can write to the file wages.txt in the append mode.

11. Print the comment string to the file and close the file.

12. Open the file and verify that the title line is there.

13. Notice that Notepad ignores the \ characters your may have written in your text, however after a call of the println method the next printing goes in a new line. If you use doc extension rather than txt, the \ character works as expected.

Solution

1. For option 1 if the number of days are 21 then the amount will 21,000 dollars and if the days are 30 then the amount will be 30,000 dollars

For option 2  if the number of days are 21 then the amount will 20971.51 dollars and if the days are 30 then the amount will be 10737418.23 dollars

Both the options are good for 21 days but for 30 days option 2 is better than option 1.

2. Program

import java.util.Scanner;

public class SalaryCalculator {
   public static void main(String args[]){
       Scanner sc=new Scanner(System.in);
       int days=sc.nextInt(); // Number of days are taken

       int xxMin, xxMax, xxAverage;
       float csMin, csMax, csAverage;
       int sumOfPennies =1;

// for 21 days and option 1
       if(days == 21 ){
           xxMin = 21*1000;
           System.out.println(xxMin);
       }else if (days == 30){

// for 30 days and option 1
           xxMax = 30 * 1000;
       }else{
           System.out.println(\"Invalid input\");
       }

         // for 21 days and option 2
       if(days == 21 ){
           int d = 0;
           int e = 1;
           for(int i = 0 ; i< 20; i++){
               sumOfPennies = sumOfPennies+ d*2;
           d= 2*e;
           System.out.println(\"for day\"+(i+1)+\"amount is\"+sumOfPennies);
           e=e*2;
           }
           csMin = sumOfPennies;
           System.out.println(csMin);
       }else if (days == 30){

// for 30 days and option 2
           int d = 0;
           int e = 1;
           for(int i = 0 ; i< 29; i++){
               sumOfPennies = sumOfPennies+ d*2;
           d= 2*e;
           System.out.println(\"for day\"+(i+1)+\"amount is\"+sumOfPennies);
           e=e*2;
           }
           csMax= sumOfPennies;
           System.out.println(csMax);
       }else{
           System.out.println(\"Invalid input\");
       }
   }

}

1. An employer hires two students, a CS major and an XX major to do handyman work under the following conditions: - The duration of the job is between 21 and 30
1. An employer hires two students, a CS major and an XX major to do handyman work under the following conditions: - The duration of the job is between 21 and 30
1. An employer hires two students, a CS major and an XX major to do handyman work under the following conditions: - The duration of the job is between 21 and 30

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site