CS 111: Fundamentals of Programming 1

Lab 2

Goals

The goals for this lab are learn how to

Step 1 - Copy Lab2 folder

Log on and copy the Lab2 folder from the course folder on the P drive to your directory on the W drive.

For all of the programs, be sure to put your name at the top.

Step 2 - The Weekly Pay Program

This exercise is essentially Programming Problem 4 of Chapter 3. An employee's total weekly pay is the sum of his/her regular pay and overtime pay. The regular pay is computed as the product of the hourly wage and the number of regular hours. The overtime pay is the 1.5 times the hourly wage times the number of overtime hours. Your program should get three inputs from the user (prompt for each one separately): the hourly wage, the number of regular hours, and the number of overtime hours. Your program should print out the total pay for the employee. For this you should open the Pay project and insert the code. Again all of your work takes place in the run method. I have provided the following variables for your use: hourlyWage, regularHours, overtimeHours, regularPay, overtimePay, and totalPay. You may declare other variables if needed.

Step 3 - Integer Division

In this exercise you are to complete a Java program that allows the user to enter two positive integers. Then the program should tell how many times the second divides into the first (whole number) and tell what the remainder would be from this division. You should make use of the integer division operation and the modulus operation. For example, if the user enters 33 and 5, the output should be, exactly,

The number 33 divided by 5 gives a quotient of 6 with a remainder of 3.

  1. Start BlueJ
  2. Open the Division project
  3. Open the editor and complete the run method
  4. Test your program
  5. When you have it working correctly, close the project

Step 4 - Vending Machine

In this exercise you are to complete a program that lets the user enter a positive integer from 1 to 99. This is to represent the cost of vending machine item, in cents. The program assumes that payment is made with a dollar and specifies the number of quarters, dimes, nickels and pennies the machine should give back in change. The program should return the minimal number of coins. Note: You do not need and should not use if-statements. Unless I am convinced otherwise, you should not use *,+ or - (with the exception of using subtraction to find the amount of change to return).

  1. Open the VendingMachine project
  2. Open the editor and complete the run method
  3. Test your program
  4. When you have it working correctly, close the project

Step 5 - Exploring the If-else statement

In this exercise, you will explore the if-else statement by making changes in a working program to see the effects. This is the next statement to be covered in class.

  1. Open the PosNeg project
  2. Open the editor and study the code there
  3. Execute the program a few times to see how it works
  4. Now take out the first pair of braces after "else". Try compiling and executing the program now and report on the Worksheet what happens. Try to explain why.
  5. Now put those braces back and take out the first pair after "if". Try compiling and executing the program now and report on the Worksheet what happens. Try to explain why.
  6. Now put the braces back. Now change the program so that rather than telling whether the number is positive or negative, it tells whether the number is even or odd. Hints will be available if needed, but think about it first.
  7. Test your program.
  8. When working properly, tell on the Worksheet what changes you made.
  9. Close the project

Step 6 - Turn in your work.

Before leaving

  1. Copy your Lab2 folder to your turnin folder on the Q drive.
  2. Log off