CS21 Spring 2006
Homework 1
Due Tuesday, January 24 before 11:30pm


For each homework assignment, you should create a hwX subdirectory of your cs21 subdirectory and keep your homework X solution files in this directory. For example, here is what I would do to create a hw1 subdirectory:
% cd 
% cd cs21
% mkdir hw1
% cd hw1
% pwd		
  /home/your_user_name/cs21/hw1/
Also, you can use the in-class Java source code files as a starting point for assigments (just copy them from your week1 subdirectory into your hw1 subdirectory into a file with the approriate name).
# For example, from your hw1 subdirectory:
% pwd
  /home/your_user_name/cs21/hw1/
% cp ../week1/InputExample.java  Volume.java 
Now just change the class name to Volume inside Volume.java file and modify the contents of the file to implement your solution to the Volume program.
Answer the following Review Questions from the book on your own, but do not turn them in. The answers are a just few pages later in the textbook.

Please complete the following 3 programs and turn them in using cs21handin. You should work on this assignment by yourself (no partners).

  1. Write a program, in a file named Volume.java (it is important that your file name exactly matches the one listed in the assignment), that asks the user for the radius of a sphere, and then computes the volume of that sphere (V) using the formula:



    where pi is approximately 3.14159. Note: there is no "raise to a power" operator in Java. Given the arithmetic operators from Chapter 2, how can you write an expression that achieves the desired result?

    Remember to print out a prompt to the user to enter the input values, and then to print out the result in a way that would make sense to a user of your program. For example, here is the output of my program when a user enters 2.3 as the radius:

    % java Volume
    
    This program computes the volume of a sphere.
    Enter the radius: 2.45
    The volume of a sphere with radius 2.45 is 61.60082031833334
    
    Remember to get your program to read input, you need to create and use a new Scanner object:
    import java.util.*;     // add this to the top of your file before the class
                            // definition
    
    
    Scanner reader;         // inside main, declare a Scanner reference variable
    
    reader = new Scanner(System.in);  // create a new Scanner object to read from stdin
    
    // then you can use methods of Scanner class to read in values
    r = reader.nextDouble();	// read in a double and assign it to r
    x = reader.nextInt();		// read in an int and assign it to x
    f = reader.nextFloat();		// read in a float and assign it to f
    
    
    To compile a Java program:
    --------------------------
    % javac Volume.java
    
    To run a Java program:
    ----------------------
    % java Volume
    

  2. Write a program, in a file named PiggyBank.java, that asks the user to enter the number of quarters, the number of dimes, the number of nickels and the number pennies in a piggy bank, and the computes the total amount of money in the piggy bank. Output the result in the form $dolars.cents. For example, 4 quarters, 2 dimes and one penny would be $1.21
  3. Write a program, in a file named Convert.java, that asks the user to enter a total time value in seconds, and then converts the value to days:hours:minutes:seconds. For example, here are three runs of my solution:
    % java Convert
    
    This program reads in a total number of  seconds time value entered
    by the user and converts it to days:hours:mins:secs
    Enter the total number of seconds: 61  
    
    61 seconds is 0 days 0 hours 1 mins 1 secs 
    
    % java Convert
    
    This program reads in a total number of  seconds time value entered
    by the user and converts it to days:hours:mins:secs
    Enter the total number of seconds: 432001
    
    432001 seconds is 5 days 0 hours 0 mins 1 secs 
    
    % java Convert
    
    This program reads in a total number of  seconds time value entered
    by the user and converts it to days:hours:mins:secs
    Enter the total number of seconds: 443108
    
    443108 seconds is 5 days 3 hours 5 mins 8 secs 
    

Please use the cs21handin command to turn in your homework.