Computer Science 15-100 (Sections T & U), Spring 2008
Homework 4
Due:  Fri 8-Feb-2008 at 10:00am (online submission) and at recitation (physical copy)
(no late submissions accepted).



  1. Do the following Exercises from Chapter 3 (pp 152-153):
    Exercises 3.1 - 3.9 and 3.11.

     
  2. Write a program that uses the drawing methods we have covered to draw each of the following graphics.  Important note:  the graphics must fill the entire window, even as the window is being resized by the user.  In this case, you may get dimensions that make the flag look peculiar, but nonetheless it should adjust reasonably to the window's dimensions.  Also, this week you should color-match, rather just using built-in colors.
    1. Hw4Q2a.java
        (Solomon Islands, larger image)

      Hint:
        One way to find the exact color of blue in this flag (on a Windows PC, at least) is to copy the screen using PrtScr (print screen).  Then run the Paint program, and paste the screen's image into it.  Then use the eyedropper tool and click on the light blue in the flag.  Finally, from the Colors menu, select "Edit Colors", then "Define Custom Colors", and there you will see the RGB values of this shade of blue are 154, 176, and 217 respectively.  Now, to create that exact color of blue in Java, just do this:
           page.setColor(new Color(154,176,217));
      Even better, to give this color a name, store it in a well-named variable like this:
           Color solomonLightBlue = new Color(154,176,217);
           page.setColor(solomonLightBlue);

      Another Hint:
        After you draw one star with a Polygon instance, you should repeatedly use the Polygon's translate method to move this star into the other positions to draw the other stars.  Pretty handy!

       
    2. Hw4Q2b.java
        (Israel, larger image)

      Hint:  study BasicGraphicsDemo to see how to draw, rather than fill, a polygon, and also how to change the line thickness.

       
    3. Hw4Q2c.java
      (Optical Illusion:  the horizontal lines and vertical lines are the same length!)

       
  3. Write a program, Hw4Q3.java, that reads in four doubles from the file in.txt representing the coefficients of two linear equations (y = mx+b), where the two lines are guaranteed to intersect in one point, and writes their point of intersection (with both x and y values rounded to the nearest 1/10th) to the file out.txt.  This program should work like this:
         in.txt contents:
              2.0 3.0 4.0 1.0
         Program output:
              <none>
         out.txt contents (after running program):
              1.0 5.0 
    Explanation:  the input represents the lines y=2x+3 and y=4x+1, and the output indicates that these two lines intersect at the point (1,5).  Note, however, that the output is not "1" but "1.0", as the output should use printf to round the value to the nearest 1/10th.

    Hint: The lines y1 = m1*x + b1 and y2 = m2*x+b2 intersect where y1 = y2.  That is, where:
         m1*x + b1 = m2*x + b2
    Now solve for x, then plug that value of x back into either equation to solve for y.

     
  4. Write a program, Hw4Q4.java, that reads in two Strings from the file in.txt and writes "true" to the file out.txt if either of the strings contains exactly one copy of the other string (without regard to case), and false otherwise.  This program should work like this:
     
    in.txt contents program output out.txt contents explanation
    AbCdEf Bc <none> true Bc is contained once in AbCdEf
    BC abcdef <none> true BC is contained once in abcdef
    efg abcdef <none> false neither string is contained in the other
    ab abcabc <none> false ab occurs twice in abcabc
    bb bbb <none> false bb occurs twice in bbb
    bb bb <none> true bb occurs once in itself

     

  5. Write a program, Hw4Q5.java, that reads in one even non-negative integer from the file in.txt representing a number of years, and using Moore's Law, writes how many times faster computers will operate that many years from now to the file out.txt.  This program should work like this:
         in.txt contents:
              10
         Program output:
              <none>
         out.txt contents (after running program):
              32
    Explanation:  the input represents 10 years, which by Moore's Law includes 5 doublings in speed, so computers in 10 years will be about 25 or 32 times faster.  Hence the output is the number 32.

    Hint: As the input is an even integer, you are assured the result will also be an integer.  Be sure to output the correct integer, taking double math into account.

     
  6. Write a program, Hw4Q6.java, that performs the opposite calculation of the previous problem -- that is, it reads in one non-negative integer that is a power of 2 from the file in.txt representing how many times faster computers will be in the future as compared to today's computers, and using Moore's Law, writes how many years from now computers will achieve that speed to the file out.txt.  This program should work like this:
         in.txt contents:
              32
         Program output:
              <none>
         out.txt contents (after running program):
              10
    Explanation:  the input represents 32 times faster, which is 25 times faster, which is 5 doublings of speed, which by Moore's Law will require about 5*2, or 10 years.  Hence the output is the number 10.

    Hint: As the input is a power of 2, you are again assured the result will also be an integer.  Be sure to output the correct integer, taking double math into account.
     

Carpe diem!