V22.0002
Intro. to Computers & Programming
Summer 2009
Review Problems
1. Write a program to determine whether a series of purchases comes in over or under a budget. The program starts by reading a budget and a sales tax rate. It then prompts the user for the price (before tax) of each item purchased. After reading all the input, it prints the total cost, the budget and the relationship between the budget and the cost.

Here are two sample runs of the program (user input in italics):

Run 1

(input: JOptionPane):

What is the budget? 1000.00
What is the tax rate?
.10
What is the price of the item (0 to end)?
100.00
What is the price of the item (0 to end)?
200.00
What is the price of the item (0 to end)?
300.00
What is the price of the item (0 to end)?
0

(output: System.out)

```Your total was \$ 660.00
Your budget was \$ 1000.00
You came in under budget by \$ 340.00
```

Run 2:

(input: JOptionPane):

What is the budget? 100.00
What is the tax rate?
.08
What is the price of the item (0 to end)?
100
What is the price of the item (0 to end)?
0

(output: System.out)

```Your total was \$ 108.00
Your budget was \$ 100.00
You came in over budget by \$ 8.00
```

Notes and hints:

• All the values have decimal places.
• Do not worry about formatting the output.
• If you come out exactly on budget, it does not matter what your last line of output states.
• 0.0 is one float/double value which is represented exactly.
2. (25 points) Write a program that that will print an arbitrary number of lines of arbitrary length. First you need to prompt the user for the desired number of lines. The program will then prompt the user for the length of each line. Each line should be printed to the screen using asterisks on a separate line of output. For example (user input in italics):

(input: JOptionPane):

How many lines would you like to print? 4
How long is the next line?
5
How long is the next line?
12
How long is the next line?
3
How long is the next line?
5

(output: System.out)

```*****
************
***
*****
```
3. Write a method that returns the number of digits in a positive integer. Also write main(), which will call the method exactly 100 times using numbers generated by Math.random() in the range 0–10,000.
4. Write a method raiseIntToPower that takes two non-negative integers, n and k, and returns nk. For example:
```raiseIntToPower (2, 4);
```

would return 16.

You only need to write the method declaration (including the body). Do not write main().

Hint: What value should you use to initialize the return value?

5. A palindrome is a word, verse, or sentence that reads the same backward or forward. For example, “able was I ere I saw elba” is a palindrome, as are “abba”, and “a”.

Write a method called isPalindrome that takes one String parameter called str and returns true if str is a palindrome or false if it’s not a palindrome.

• Do not to write a whole program.
• Do not read in a String from the keyboard (this will be done by the calling method); the string is passed to the method.

Hint: With a palindrome, if you look at the beginning and end of the string and work your way one at a time from both ends towards the middle, the character from either end is the same at each step.

6. Write a method firstOccur that takes as parameters a character ch and a string s and returns the index of the first occurrence of ch in s. For example, if s is "abcdefg" and the value of ch is ’d’, your program would return 3. If ch is not found in s, your program should return −1.
7. Write a method pattern that takes an integer n as a parameter and produces the following triangle:
```       x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
```
where the bottom line has n x’s.
8. Write a method sum that takes an integer array x as a parameter and returns the sum of all the elements in the array.

This document was translated from LATEX by HEVEA.