SWE 510: Object-Oriented Programming in JavaFAll 2010Muhammad Abdulla |
General Information | Textbooks | Schedule & Notes | Projects | Policies |
Homework 1
Due: 11:59 PM, Oct. 7, 2010 Reminders: Follow the submission instructions. Remember to comment your code! Total Points: 60 0) Prime number generation. (30 pts) Prime numbers play a central role in number theory, and they also have applications in modern cryptography. A prime number is defined as a natural number that has exactly two distinct natural number divisors: 1 and itself. The number 1 is not a prime number by definition. A sequence of the first ten prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. Implement a tool to generate and print a sequence of prime numbers. By default, your program should print all prime numbers below 100. Assuming that the name of your Java file is PrimeNumbers.java, the following is a sample session: $ javac PrimeNumbers.java $ java PrimeNumbers 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 $ Augment your program to take a command line parameter specifying that all the prime numbers smaller than or equal to that number are printed. For example: $ java PrimeNumbers 7 2 3 5 7 $ java PrimeNumbers 1000 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 $ Make sure your program does something sensible if the supplied argument is not a valid integer. There should not be an arbitrarily imposed upper limit to this number (in other words, do not have an artificial cap on the value of the argument, except as limited by the value range of the int type in Java). Since it is easy to forget how to use programs, it is often useful to provide a well-known command line switch (or parameter) that tells the user how to invoke the program. Augment your program to recognize two more command line arguments, '-h' and '--help' that prints out the usage information for the program. For example, $ Java PrimeNumbers -h and $ Java PrimeNumbers -help should output the following usage information: Java PrimeNumbers [-h | -help] : output this usage message. Java PrimeNumbers [n] : print out prime numbers smaller than or equal to n Finally, augment your program to print out its version. Don't forget to add this usage case to your help dialog. $ java PrimeNumbers -version PrimeNumbers, Version 0.1 $ java PrimeNumbers -v PrimeNumbers, Version 0.1 $ Note: Your program is supposed to exit after printing help or version information. ----------------------------------------------------------------------------- 1) Change Calculator (30 pts) Write a program that calculates the minimum number of dollar bills and coins for a given amount. That is, a smaller denomination is used only when a larger denomination is too large for the remaining change. The dollar bills are available in the following denominations: 1, 5, 10, 20, 50, and 100 (in dollars). The denominations for coins are: 1, 5, 10, and 25 (in cents). Your program should require an argument, and should check that the argument is a sensible currency value. Sample sessions might look like this: # javac ChangeCalculator.java # java ChangeCalculator 79.87 $50 notes: 1 $20 notes: 1 $5 notes: 1 $1 notes: 4 c25 coins: 3 c10 coins: 1 c1 coins: 2 # java ChangeCalculator -12.0 Amount should be larger than 0. # java ChangeCalculator 1010.117 Amount rounded to: 1010.12 $100 notes: 10 $10 notes: 1 c10 coins: 1 c1 coins: 2 As shown above, an error message is given for negative values, and values smaller than a cent should be rounded to the closest cent value. __________________________________________________________________________ Notes: You may want use use the "int Integer.parseInt(String num)" method for String to int conversion. And "double Double.parseDouble(String num)" method for String to double conversion. Even though we have not covered exceptions yet, you may follow the following template for in your code: try { val = Integer.parseInt(s); } catch (NumberFormatException nx) { System.out.println("Enter a valid integer!"); } |
Date & Time
![]() ![]()
What is New?
|