CS11a: Intro to Programming in Java and C
HW3 (10 points + 6 points extra credit )

· General Information

· Notes

  (printable)

 

HW3

Due: Monday 6/8/2009.
  1. Do all of the javabat problems in the Warmup-1 section at http://www.javabat.com and hand in a list of any questions you might have about how to solve those problems. If you understand all the solutions then there is nothing to hand in for this part.
  2. Take one of the javabat problems and convert it into a Java program which runs all of the tests in the main method and which then goes into a loop allowing the user to test the function on his/her own input values. It should ask the user to enter the values, then it should read the values, then it should send the values to the method solving the javabat problem and report the result to the user. Finally it should ask the user whether they want to continue with another set of input values or quit, and proceed accordingly.
  3. Write a java program which will ask the user for a number N and will print out all of the divisors of N and ask for another number. If the user enters zero then it will exit. You can test for divisibility using (N % m)==0 where % is the "remainder" operator in Java. This will return true if N is evenly divisible by m. Here is a sample interaction
    Enter a number:
      28
    The divisors of 28 are 1 2 4 7 14 28
    
    Enter a number:
      5
    The divisors of 5 are 1 5
    
    Enter a number:
      0
    Good bye!
    
  4. Write a java program which will ask the user for a number N and will then print the first N prime numbers. Your program will need to have a boolean method
       public static boolean isPrime(int n)
    
    which returns true if the parameter n is a prime and false otherwise. The algorithm should test all numbers m less than the square root of n to see if they divide n evenly. If any do, then return false; in none do, then return true.
  5. Write a program which will ask the user for a number of dollars D and a number of cents C and will make change for that amount of money, e.g.
    Enter the number of dollars:
      87
    Enter the amount under one dollar:
      87
    The change for this is
     4 twenty dollar bills
     1 five dollar bill
     2 one dollar bills
     3 quarters
     1 dime
     2 pennies
    
  6. Finally, write a one paragraph description of what you would like to get out of this course and your future plans, if any, to continue in Computer Science.
2009 © Copyright Tim Hickey, Some Rights Reserved