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

Credit:
10 points.

Reading

Be sure you have read chapter 3.

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.

    Hint: This program can be very short if you take advantage of a method mentioned in chapter 3.

  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.



Berna Massingill
2012-09-12