hts.StevenWood.com

Lab Exercises

Home ] Up ] How to use Methods in Java ] Methods and Parameters in Java ] Creating User-Defined Methods ] Common Programming Errors ] Good Programming Practices ] [ Lab Exercises ] Labs 1 ] Labs 2 ] Rubric ][ Last Page ]

Lab Exercises

1. a) Write a method named evaluate which accepts values for a, b, and c and returns the value of the equation:
  b) Use the above method in a program named Solve that will allow the user to enter in values for a, b, and c.

2. a)

Create a method that will calculate the value of the equation:

and return "positive", "negative", or "zero" based on the value of the equation.

  b) Create a main method that will allow the user to enter in a value for x and see the return value of the method.

3. a) Write a double method named wages that takes the payrate/hour and hours worked and then calculates and returns the wages earned. "Time and a half" is paid (150% of your normal wage rate) for every hour worked over 40 hours.
b) Write a program called Payroll that calculates the wage for ONE employee. Enter in the hours worked and the payrate, calculate the amount of pay, and display the result.
c)

Write a program called Payroll1 to calculate the wage for 3 employees. Use a for loop to enter in the hours worked and payrate for each. Use the wages method to find and display the amount to be paid. Information on the for Loop.

Enter hours worked: 20
Enter pay rate: 6.00
Wages earned: $120.00

Enter hours worked: 50
Enter pay rate: 12.00
Wages earned: $660.00
....

d) Write a program called Payroll2 to calculate the wages for 3 employees using the method wages. Have the program reject negative hours or wages and ask for correct information as shown below. You should use a while loop to repeat the question until a "good" number has been entered. Information on the while Loop.

Enter hours worked: 10
Enter pay rate: 5.00
Wages earned: $50.00

Enter hours worked: -10
Bad input
Enter hours worked: 45
Enter pay rate: 10.00
Wages earned: $475.00
....


4. a) Write a method of type long (think of it as a large whole number - no decimals), named factorial that returns the factorial of an int parameter. The factorial of a number is the product of all positive integers less than or equal to that number. For example, the factorial of 5 (written 5!) is:

5! = 1 * 2 * 3 * 4 * 5 = 120

Hint #1: use a loop to do the multiplication.
Hint #2: when we add to a number we can use the following format:

x += 3;  // add 3 to the variable x
        
the Java way to multiply numbers together replaces the "+" sign in the above command with the multiply sign.
 

NOTE: the code will give you some crazy values if you don't use type long.

  b) Write a program named MathCalculation that will allow the user to enter in a value to use with the factorial method. Have the code repeat until the user enters a 'Q' to quit the program.

5. a) Write separate double methods to calculate each of the following conversions:
i) A method named kilogram which converts pounds to kilograms. There are 2.2 pounds per kilogram.
ii) A method named inches which converts centimetres to inches. There are  0.39370 inches in a centimetre.
iii) A method named squareFeet which converts square metres to square feet. There are 10.76 square feet in a square metre.
b) Write a program that combines these three functions so that the user can select one of them.

1. Pound to kilograms
2. Centimetres to inches
3. Square metres to square feet 
Enter choice (1, 2, or 3 ): 2

Enter centimetres: 1000
1000 centimetres equals 393.70 Inches


6. a) A prime number is any integer that is evenly divisible only by itself and 1. For example, the numbers 2, 3, 5, and 7 are prime while 9, 10, and 15 are not. Write a boolean method named prime that accepts an integer greater than 1 and then returns true if the integer is prime or false if it is not. The computer tests an integer by repeatedly dividing it by integers smaller than itself but larger than 1, and checking whether the remainder is 0. If so, the integer is not prime. 

Hint: you used a structure to do this in Question #2 of the Looping Assignment. It is called "mod". Go back and review it for this question.

b) Write a program that class prime with all integers between 2 and 100 and prints only those that are prime.

7. a) Create a method that will return an int value based on the roll of a die.

See How to get a random number here.

  b) Have the main method call the method in a) two times. This will simulate rolling 2 dice. Display the total value of the roll.
e.g.

  c) Update the program so that you ask the user how many times to roll the 2 dice and display the total of each roll.
e.g.:

  d) Update the program so that you keep track of how many times you roll 7 OR some other number that you enter. (You roll 2 dice.)
e.g.


8.   Use the rolling dice idea to roll two dice 100 times and count the number of times EACH total comes up. That is, create a variable for each possible total and when you roll that number, add one to it.
e.g.:


9.   Make a NEW program, similar to the question above. In this one, you indicate how many sides are on the dice. (You roll 2 dice.)

e.g.

 
Home ] Up ] How to use Methods in Java ] Methods and Parameters in Java ] Creating User-Defined Methods ] Common Programming Errors ] Good Programming Practices ] [ Lab Exercises ] Labs 1 ] Labs 2 ] Rubric ][Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+