CSCI 1320 (Principles of Computer Science I), Fall 2014:
Homework 2

Credit:
30 points.

Reading

Be sure you have read chapters 3 and 4.

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 2'' or ``CS1 hw2''). 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) Write a Scala script (program) to show the binary representation of an integer. The program should ask the user for an integer and print the integer and its binary equivalent. Here are some sample executions, assuming you called your program show-binary.scala. Text in boldface is what you type; text in typewriter font is what the program prints.
    [bmassing@xena02]$ scala show-binary.scala
    enter an integer:
    10
    you entered 10
    in binary that is 1010

    [bmassing@xena02]$ scala show-binary.scala
    enter an integer:
    -10
    you entered -10
    in binary that is 11111111111111111111111111110110
    For extra credit (up to 2 points), make it always print exactly 32 digits.

    Note: This program can be very short if you take advantage of a method mentioned in chapter 3. If you find yourself writing a lot of complicated code, or even more than a few lines, stop and look for a simpler approach.

  2. (5 points) Write a Scala script (program) to convert a Fahrenheit temperature to Celsius. The rule for converting Fahrenheit temperature F to Celsius temperature C is

    $\displaystyle C = \frac{5}{9}(F - 32) $

    The program should ask the user for the Fahrenheit temperature and print the equivalent Celsius temperature. You can use integers or floating-point numbers for this problem.

  3. (10 points) Write a Scala script (program) to compute U.S. income tax given taxable income. The instructions for Form 1040 for 20111are as follows (for single taxpayers):

    Your program should first prompt the user for a taxable income (in whole dollars), and then do one of three things: If the amount is negative, it should tell the user that income cannot be negative. If the amount is no more than $100,000, it should tell the user to use the IRS's tax table. Otherwise it should use the above formulas to compute and print the tax due, rounded to the nearest dollar (round 50 cents upward). For example, the tax on $110,000 is 28% of $110,000 minus $6,383; computing and rounding, this gives $24,417. (Don't worry about printing amounts with commas; for this assignment it's fine to just print, for the example, $24417.)

    You could use floating-point numbers for the calculation, but the result will likely be more accurate if you instead do everything with integers, calculating tax in pennies and converting back to dollars at the end. (An easy way to round cents to the nearest dollar is to add 50 cents and then use integer division to convert to dollars.)

  4. (10 points) Write a Scala script (program) that asks the user for three integers and prints them in order from smallest to largest. (Sounds simple -- but be sure you consider all possible cases. The user could enter the numbers from smallest to largest, or vice versa, or in several other orders. Your mission is to give the right answer for all possibilities. Notice that the numbers do not have to be distinct -- for example, the user could enter three values of 0, in which case the program should print three values of 0.)



Footnotes

... 20111
If you wonder why this problem doesn't use the most recent version of the form, it's because the most recent version (2013) makes use of non-integer numbers, and I want you to be able to easily solve the problem with integers only.


Berna Massingill
2014-09-16