CSSE 220 -- Object-oriented Software Development

Homework 3 Due at 8:05 AM on Day 4. 
Written problems due at the beginning of your class period.

  1. Complete the assigned reading for the next session (see the course schedule). If there is something you do not understand, make note of it so you can ask about it.
    Estimated time: Textbook: 40 minutes.
  2. Look at the Key Concepts from the end of chapter 3.  This is a good place to quickly notice things you might have missed from the chapters. At some point we will have a "Key concepts quiz" over chapters 1-4.
  3. Complete the Angel quiz over this reading. You'll find this on the course Angel page, under Lessons → Assignments → Reading Quizzes → Quiz 3.
    Estimated time (after reading the material): 20 minutes.
  4. Try out command-line arguments within the Eclipse environment.  Instructions for doing this are on one of the Day 3 PowerPoint Slides. Estimated time: 10 minutes.
  5. Do the written exercises that are listed below.  You may write them neatly by hand or do them on your computer and print them. Turn in hard copy at the beginning of the Day 4 session. Include your name and section number at the top of your paper.  Estimated time: 30 minutes,
  6. Write code for the following two Java classes, as described below.  The first one's main( ) method expects input from the keyboard, while the second one expects two command-line arguments.  Each should be in its own .java file, with exactly  the same name I indicate for the class.  You should copy both of your .java files, and nothing else, into the Homework3 folder in your turnin folder.  Estimated time: 90 minutes if you came into this class retaining most of the CSSE 120 material.
  7. Log in to addiatior using SecrureCRT or puTTY and run the grading script to check and make sure you have written and submitted things correctly.  This is what you should do and see if everything is correct:
addiator 6:22am > cd /class/csse/csse220/200820/
addiator 6:23am > ./check Homework3 
Checking Homework3
Clearing /afs/rh/class/csse/csse220/200820/turnin/<your-username>/Homework3/extract/
Copying *.java... done

Compiling project...
No compile errors found
<your-username> - Summary for Reverser
Graded on Sat Dec 01 06:23:22 EST 2007

Test                   Reversed  Points  Your Answer
                       Line      1/1     
                       Word      1/1     
Word                   Line      1/1     droW
Word                   Word      2/2     droW
case-insensitive test  Line      2/2     tset evitisnesni-esac
case-insensitive test  Word      3/3     evitisnesni-esac tset
This `is a' test       Line      2/2     tset 'a si` sihT
This `is a' test       Word      3/3     sihT si` 'a tset
CapItalIZAtIOn teST    Line      2/2     TSet nOItAZIlatIpaC
CapItalIZAtIOn teST    Word      3/3     nOItAZIlatIpaC TSet
!@#$% ^&*().           Line      2/2     .)(*&^ %$#@!
!@#$% ^&*().           Word      3/3     %$#@! .)(*&^

Points earned: 25/25

<your-username> - Summary for Sieve
Graded on Sat Dec 01 06:23:22 EST 2007

Test             Points  Your Answer
(1     ,1     )  1/1     
(14    ,16    )  1/1     
(51    ,59    )  3/3     53 59 
(1     ,100   )  5/5     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 
(500   ,600   )  5/5     503 509 521 523 541 547 557 563 569 571 577 587 593 599 
(600000,600200)  5/5     600011 600043 600053 600071 600073 600091 600101 600109 600167 600169 
(2     ,999   )  5/5     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 

Points earned: 25/25

 

Classes to write for the programming part

Class Description
Reverser Reads lines of input from the keyboard (It does not print any kind of prompt).  After reading each line, it first prints the line with all of the characters reversed.  Then it prints the line with the characters of each "word" reversed, but the order in which the words come in the line is the same as the original.  By "word" here, we mean a sequence of characters with no spaces.  You may want to use String.split( )or the StringTokenizer class to pick out the words.  For example, if an input line is:
The "words" can contain punctuation.
then the output should be
.noitautcnup niatnoc nac "sdrow" ehT
ehT "sdrow" nac niatnoc .noitautcnup

Your program should keep reading and reversing input lines until the user signals end of input (ctrl-z in Windows, ctrl-d in UNIX/Linux ).  It should not print anything except the two "reversed" lines for each input line.
Sieve The Sieve of Eratosthenes is a famous way of finding all prime numbers in a range reasonably efficiently.  It has been known for a couple of millennia.  A prime number is an integer that is greater than 1 and has no positive integer factors besides 1 and itself.  Your program (classname: Sieve) must take two positive integers as its command-line arguments.  Both arguments should be positive integers, and the first one should be smaller than the second one.  Your program is to print (on one line, with a single space after each number) all prime numbers that are in the closed interval bounded by these two numbers.  For example, if the command-line arguments are 53 103, then the output should be:
53 59 61 67 71 73 79 83 89 97 101 103
For another example,  if the command-line arguments are 10000000 10000200, then the output should be:
10000019 10000079 10000103 10000121 10000139 10000141 10000169 10000189

Here is how the algorithm works, assuming that the two numbers are lower and upper.

  1. allocate an array, primes of upper + 1 booleans, set all of them to true.
  2. Start with base = 2.  "Cross off" (by setting the corresponding array element to false) all multiples of base that are  greater than or equal to base*base, and less than or equal to upper.  Can you see why it is okay to start with the more efficient  base*base rather than base*2
  3. Find the next base (i.e. the next prime) by finding the next true position in the array after the current position.
  4. Repeat steps 2 and 3 until base*base > = upper.
  5. Finally, loop through from lower to upper, printing each number whose array position is still true.

Written problems

  1. (5 points)  Weiss exercise 1.14.
  2. (4 points)  Weiss exercise 2.6.  In addition to telling what each prints, explain why it prints what it prints.
  3. (5 points) Weiss exercise 2.9.  (Replace "routine" by "static method".)  You do not have to do this on your computer, but you are welcome to do so if you wish.