CSCI 1320 (Principles of Computer Science I), Fall 2012:
Homework 4

Credit:
30 points.

Reading

Be sure you have read chapters 5 and 6.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu, with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., ``csci 1320 homework 4'' or ``CS1 hw4''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.

  1. (5 points) For the previous assignment you wrote a Scala program to compute U.S. income tax given taxable income. It probably involved some rather repetitive code, since the calculations for all the income tax brackets are similar (multiply by a percentage and then subtract something) but using different rates and subtraction amounts. Your mission for this problem is to reduce the amount of near-duplicate code in your program by using a function to compute the tax. This function (you choose its name) should take as parameters taxable income, the rate to multiply by, and the amount to subtract, and it should return the amount of tax. It probably makes the most sense for the function to do the whole computation, including rounding.

    To the user this program will look the same as the previous program (prompting the user for taxable income and then printing either an amount of tax or a message about using the tax table), but the code should be simpler and with less repetition.

  2. (5 points) Write a Scala program that uses a recursive function to compute b to the e-th power (b multiplied by itself e times), where b and e are Ints and e is not negative. The program should prompt for b and e and print the result of computing b to the e-th power. It should print an error message if e is less than zero. Examples:

  3. (10 points) Write a Scala program that uses a recursive function to ``count'' up or down, specifying a starting value start, an ending value end, and an increment incr. (All three values should be Ints.) The function should then count starting at start, incrementing by incr, and stopping when the next value would go beyond end. Examples:

  4. (10 points) Write a Scala program that prompts the user for lines of input, ending with a line ``quit'', and prints the lines in reverse order. (Yes, you can do this with a recursive function! You do not need arrays or lists.) Sample execution (text in boldface is what you type; text in typewriter font is what the program prints):
    [bmassing@xena02]$ scala reverse.scala
    enter lines of text, quit to end
    hello world
    more text
    still more
    another line
    quit

    here are the lines you entered, in reverse order:
    another line
    still more
    more text
    hello world
    You can use the library function readLine to read a line of text; for example, the statement
    val s = readLine
    makes s a String that contains a line of text typed by the user.



Berna Massingill
2012-10-03