CS 171 & 171L Introduction to Computer Science I & Lab Fall 2019 

Homework Assignment 4

Write java methods to implement the following computations:

  1. (4 points) Create a function named hms that takes three int parameters representing hours, minutes, and seconds. Use the values of these parameters to compute the total number of seconds represented then return an integer representing this value. For example, hms(2, 10, 45) should return 2*60*60 + 10*60 + 45 = 7200+600+45 = 7845.
  2. (4 points) Create a function named pay that takes one float parameter representing the number of hours worked. Compute the pay based on Michigan's current minmum wage rate of $7.40 per hour and any amount worked over 40 hours will receive 1.5 times the base rate. Return a float representing the total earnings.

    One way to think about this is as follows:
    if hours worked is greater than 40 hours, then the earnings are 40*$7.40 plus (hours-40)*1.5*$7.40;
    otherwise the amount of pay is simply hours*$7.40.
    return earnings.

  3. (4 points) A prime number p is a positive integer greater than one that is evenly divisible by positive integers 1 and p. Create a Java function named isPrime that takes an int parameter and returns a boolean that is true if the parameter is a prime number and false otherwise.
  4. (4 points) A Harshad number is an integer that is evenly divisible by the sum of its digits. For example, the numbers 10, 12, 42, 144, and 156 are a few of these numbers. Create a Java function named harshad that takes an int parameter and returns a boolean that is true if the parameter is a Harshad number and false otherwise.

    Hint: Recall that digits can be extracted from an integer using % and /. What happens when you repeatedly divide an integer variable by 10? Try it!! What happens when you determine the remainder when dividing an integer variable by 10 (say a%10)? Try it!!
  5. (4 points) One way to numerically solve equations is with the Newton-Raphson root-finding algorithm. A very simple application of this algorithm is to find the square root of a number. Let f(x) = x2 - a, then x = sqrt(a) is a solution to the equation f(x) = 0. You may have used this algorithm in a calculus course, but don't panic if you have never seen it because it is very easy. Here are the basic steps:

    1. let x = a/2
    2. compute t = x - (x2 - a)/(2 x)
    3. assign the value of t back to x.
    4. repeat steps b and c until the answer converges, that is the absolute value of difference between t and x, simply |x-t|, is less than some tolerance or the number of iterations exceeds a preset maximum. Typically a good answer can be obtained in 10 iterations.

    Create a Java method squareRoot that takes a double parameter and computes its square root using the Newton-Raphson method with 10 iterations. Because you need to repeat several steps until a condition is met, this is an ideal example of what loops can do.

    For example, to compute the square root of 16, we start with a = 16.

    • x = 16/2 = 8
    • t = x - (x2 - a)/(2 x) = 8 - (82 - 16)/(2*8) = 8 - (64-16)/(16) = 8 - 48/16 = 8 - 3 = 5
    • x = 5
    • t = x - (x2 - a)/(2 x) = 5 - (52 - 16)/(2*5) = 5 - (25-16)/(10) = 5 - 9/10 = 5 - .1 = 4.1
    • x = 4.1
    • t = x - (x2 - a)/(2 x) = 4.1 - (4.12 - 16)/(2*4.1) = 4.00121951219512195122
    • x = 4.0012195121951220
    • x = 4.0000001858445895
    • x = 4.0000000000000040
    • x = 4.0000000000000000

In addition to writing these methods, create several test cases that demonstrate your functions are correct.

Other required information

You must use comments in your program. At the top, put your name and assignment number in a comment. Label each method with a comment. Clearly comment your test cases. Use whitespace (a blank line or two) to also separate section of your code to improve its readability.

Send me (dreimann@albion.edu) the following in a .java file as an attachment to an email message with Homework 4 as the subject line.

Have fun!


Copyright © 2019, David A. Reimann. All rights reserved.