Starting from:
$30

$24

Lab Assignment 3 Solution

PURPOSE: The purpose of this laboratory assignment is to familiarize students with the mechanics of constructing a program from an algorithm presented in pseudo-code or flowchart form. This lab also introduces basic concepts in iteration.




This laboratory assumes introductory familiarity with the concepts of:

Semantics, Syntax, and Style
Program construction/decomposition
Variables
Arithmetic and Relational Operators and Assignments
Simple flow control with if and while statements
Input using the Scanner class
Output using System.out.println() and System.out.printf()



ASSIGNMENT: [10 pts.] Constructing code from an algorithm




Consider the following problem, algorithm, and implementation (as a Java program)




Initial Problem: Calculate the weekly gross pay for an hourly employee. Overtime rates (for hours worked over 40 per week) are x1.5 standard rate. Allow for multiple calculations.

























































































































Figure 1: Algorithm as a flow chart













Page 1 of 2



WageCalculator

While (forever)



Input hourly wage



Input number of hours worked



Determine gross pay



if number of hours worked does not exceed 40









6
then calculate
gross pay with no overtime
7
if number of hours
worked exceeds
40
8
then calculate
gross pay with
overtime



Output gross pay



end-While






Figure 2: Algorithm as pseudo-code










import java.util.Scanner;







public class WageCalculator {







public static void main (String[] args) {







Scanner keyboard = new Scanner (System.in);







double hourlyWage = 0.0;







double hoursWorked = 0.0;







double grossPay = 0.0;







while (true) {







// Get hourly wage







System.out.println("Enter hourly rate of pay: "); hourlyWage = keyboard.nextDouble();







Get number of hours worked System.out.println("Enter number of hours worked: "); hoursWorked = keyboard.nextDouble ();



Determine gross pay






if (hoursWorked
<= 40) {
grossPay =
hoursWorked * hourlyWage;
} // end if no
overtime
if (hoursWorked
40)
{
grossPay =
1.5 *
hourlyWage * (hoursWorked - 40)



hourlyWage * 40;



} // end if overtime







// Output gross pay







System.out.printf("Gross pay is: $%.2f\n", grossPay);







} // end while




} // end method main
















Page 2 of 2




} // end class WageCalculator




Figure 3: An implementation of the algorithm in Java







Modifying existing code: Often, in practice, program specifications change over time. In this case, let us assume that after using the program, we now determine a need to separately list normal pay, overtime pay (if any), and total pay (the sum of normal pay and overtime pay). Also, let us say that instead of running forever, we would like to ask the user after we complete the wage for each employee if they would like to calculate the wage for another employee. The user is to answer ‘y’ (for yes, process another employee) or ‘n’ (for no, exit the program).




[5 pts] Construct a flowchart and pseudo-code for the modified problem. Ask your TA to check your work before moving forward.
[5 pts] Implement the modified algorithm in Java. Demonstrate your implementation to your TA.






ASSIGNMENT: [10 pts.] Sum of series calculator




The goal of this assignment is to develop an algorithm and implement code to calculate the series sum of reciprocals S(n) = (1/1 + 1/2 + 1/3 + 1/4 + … + 1/n) for a user specified value of n. Note: a second part of this assignment is explained below.










Enter n (the number of terms): 3







The series sum of reciprocals is: 1.8333333333333333




Example I/O (for reference: your input/output should look similar to this!)







[1 pts] Create a flowchart OR pseudocode for your implementation to this problem. Ideally, you should show (and get feedback on) your flow chart OR pseudocode to your TA before the end of the second lab period. If you do not, then you must include your flowchart OR pseudocode as part of your assignment upload (however is appropriate: scanned, printed as PDF, as a powerpoint, etc).



[3 pts] Correctness of sum of series calculator for various values of n. Ideally, you should demonstrate your implementation to your TA before the end of the second lab period.



[1 pts] Documentation, style (refer to style guidelines!), clarity of program



Second part:




[1 pt] Create a flowchart or pseudocode for a second (related) problem. This problem is to ask the user to enter a positive decimal value (for example, 1.5 or 5.0) and then have your program calculate the smallest number n for which the series sum of reciprocals S(n) EQUALS OR EXCEEDS the entered the value. If the user enters 1.5 (for example), your program should return the value n=2 (since 1/1 + 1/2 equals 1.5).



[3 pt] Copy your original sum of series calculator program and modify the new program so that instead of taking the value n from the user, it uses a loop to compute the lowest integer value n for which the series sum of reciprocals equals or exceeds the user provided value.



[1 pts] Documentation, style (refer to style guidelines!), clarity of program



Save your lab 3 assignment by creating a zip file. Show you lab assignments to your TA. Upload your lab assignments to the Pilot drop box before the lab due date. Ask your TA for help if you have any questions.


































Page 3 of 2

More products