created 08/01/99

# Chapter 11 Programming Exercises

## Exercise 1

Write a program that calculates the annual cost of running an appliance. The program will ask the user for the cost per kilowatt-hour and the number of kilowatt-hours the appliance uses in a year:

```Enter cost per kilowatt-hour  in cents
8.42
Enter kilowatt-hours used per year
653
Annual cost: 54.9826
```

## Exercise 2

When a brick is dropped from a tower, it falls faster and faster until it hits the earth. The distance it travels is given by

```d = (1/2)gt2
```

Here d is in feet, t is the time in seconds, and g is 32.174. Write a program that asks the user for the number of seconds and then prints out the speed.

```Enter the number of seconds: 5.4
Speed of the brick: 469.092 feet per second
```

Use the program to determine how lonk it would take a brick to fall 100 feet.

## Exercise 3

The base 2 logarithm of a number is defined by:

```log2 X = n  if 2n = X
```

For example

```log2 32 = 5,  because  25 = 32
```
```log2 1024 = 10,  because 210 = 1024
```

Write a program that inputs a number and outputs its base 2 logarithm. Use floating point input. This problem would be easy, but the Math package does not have a base 2 logarithm method. Instead you have to do this:

```log2 X = (loge X) / (loge 2)
```

Here, `loge X` is the natural logarithm of X. Use this function in the Java Math package:

```Math.log( X )
```

When you use this, X must be a double. Write the program so that the user can enter floating point numbers.

```Enter a double:
998.65
Base 2 log of 998.65 is 9.963835330516641
```

## Exercise 4

The harmonic mean of two numbers is given by:

```H = 2 / ( 1/X + 1/Y )
```

This is sometimes more useful than the more usual average of two numbers. Write a program that inputs two numbers (as floating point) and writes out both the usual average (the arithmetic mean) and the harmonic mean.

```Enter X:
12
Enter Y:
16
Arithmetic mean: 14.0
Harmonic   mean: 13.714285714285715
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.8764597,"math_prob":0.9857651,"size":2142,"snap":"2020-34-2020-40","text_gpt3_token_len":575,"char_repetition_ratio":0.121141255,"word_repetition_ratio":0.113300495,"special_character_ratio":0.28804857,"punctuation_ratio":0.11276596,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99961644,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-30T19:03:44Z\",\"WARC-Record-ID\":\"<urn:uuid:ec36ba7c-e7cc-401c-b634-874b65ff5104>\",\"Content-Length\":\"4785\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b37aa94b-ec54-4cec-870e-e25fbd0c49f4>\",\"WARC-Concurrent-To\":\"<urn:uuid:fb5867a8-d3cf-413d-a09e-b9f39e85bf43>\",\"WARC-IP-Address\":\"192.124.249.114\",\"WARC-Target-URI\":\"http://compsciprep.com/material/chortle/Notes/chap11/progExercises11.html\",\"WARC-Payload-Digest\":\"sha1:JMGDPJEADTYVYH2LG27PPW6MZUZZBGO2\",\"WARC-Block-Digest\":\"sha1:ECNY6FU37PYXM5JB6NGHIQGOTDAZPME2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600402127397.84_warc_CC-MAIN-20200930172714-20200930202714-00681.warc.gz\"}"}