Computer Science 15-100 (Sections T & U), Fall 2007
Homework 3
Due:  Thu 20-Sep-2007 at 3:00pm



  1. As you learn to program computers, you should realize the social and ethical implications of the technologies you may someday create.  To that end, this is a reading and writing question (and a thinking question, for that matter, but hopefully they all are!).  No coding here!  First, carefully read these two articles on Google Library:
         Tim O'Reilly's "NY Times Op Ed on Author's Guild Suit Against Google"
    and
       Nick Taylor (Author's Guild President)'s "Washington Post Op-Ed: Not at the Writer's Expense"
    Now they can't both be right!  Your task:  first, think deeply about these two opposing arguments, and begin to form your own opinion.  Then, find several more online sources that eloquently argue each side of the issue.  Finally, write your own Op-Ed piece.  Take a position and argue it as forcefully and clearly as you can (and you must take one or the other position, no splitting-the-difference or pox-on-both-their-houses arguments here).  You must cite at least 4 sources, two pro and two con (again, that's the minimum), and not that quantity equals quality, but you must have at least 350 words (and at most 700 words), or about half of what Tim O'Reilly wrote.  Microsoft Word (and most other word processors) has a word count feature which may be handy here.  Have fun, but write a compelling piece.  To receive full credit, you may take any position you like (of course!), but your writing must reflect that you understand the important aspects of this debate.
     
  2. Write a program that reads in a positive integer less than 1000 and prints out the sum of its digits.  It should work like this:
         Enter an integer between 0 and 999:  342
         The sum of the digits of 342 is 3 + 4 + 2 which equals 9.
    Of course, your program should work for any legal input!
     
  3. Write a program that reads in a positive integer N and prints out the sum of the integers from 1 to N (that is, 1 + 2 + ... + N).  It should work like this:
         Enter a positive integer:  8
         The sum 1 + ... + 8 equals 36.
    As usual, your program should work for any legal input!
    Hint:  As a child prodigy, the great mathematician Gauss figured out the math needed here.  The sum from 1 to N equals N times (N+1) divided by 2.  So, for example, the sum from 1 to 8 equals 8 * 9 / 2, or 72/2, which is 36.  Check that this equals 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8.  It does!
     
  4. Fabric must be purchased in whole yards.  So if you require 24 inches (2 feet) of fabric, you still must purchase one yard, leaving a foot of scrap left over.  Write a program that reads in the number of inches of fabric that are required, and prints out both the number of yards that must be purchased and the number of inches of scrap that will be left over.  It should work like this:
         Enter the inches of fabric required:  74
         74 inches equals 6 feet and 2 inches, requiring 3 yards of fabric, with 34 inches of scrap left over.
    As usual, your program should work for any legal input!
    Hint:  Be sure your program works when the input is a multiple of 36!
    Note: You may not use an "if" statement or any kind of conditional here.  You must solve this using a numeric expression!

     
  5. Write a program that reads in an 4-digit integer where the digits are all guaranteed to be either 0 or 1, and prints out the decimal equivalent of that integer as if it were in binary:
         Enter a 4-digit binary number: 1101
         1101 in binary equals 13 in decimal.

    Hint: even though the prompt says “binary”, read in the input as a decimal number, as such:
         int x = scanner.nextInt();
    Then, extract the 4 digits of x, all guaranteed to be 1 and 0, and use these to compute the answer.

     
  6. Imaginary High School has exactly 300 students in each of grades 9-12, or 1200 total students, and student ids are assigned by groups of 3 students from each grade in turn. So we have:
         Students 1, 2, and 3 are in 9th grade.
         Students 4, 5, and 6 are in 10th grade.
         Students 7, 8, and 9 are in 11th grade.
         Students 10, 11, and 12 are in 12th grade.
         Students 13, 14, and 15 are in 9th grade.
         Students 16, 17, and 18 are in 10th grade.
         and so on…
    Write a program that reads in a student id (between 1 and 1200) and prints out the grade of that student, as such:
         Student id: 31
         Student 31 is in 11th grade.

     
  7. Write a program that reads in four integers representing the coefficients of two linear equations (y = mx+b), and prints out their intersection, as such:
         Enter m1, b1, m2, and b2: 2 3 4 1
         The lines y=2x+3 and y=4x+1 intersect at (1,5).

    Hint #1: You can assume the lines do intersect, and then that they intersect at a point where both x and y are integers.
    Hint #2: 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.

     
  8. Write a program that uses the drawing methods we have covered to draw each of the following flags.  Important note:  the flag 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.
     
    1. :Panama


      Note:  unlike last week, now you must draw the stars as actual stars and not just circles.

      Note #2:  If it helps with the math, the outer points of each star all lie on a circle that shares the center with the star (think about this!).  The points are equally spaced, and so the angle between each pair of points is 360/5, or 72 degrees.  Now you can use trig to find the x,y coordinates of each point.

      Note #3:  for this problem, you must use the exact same code to paint both stars.  Seeing as we have not yet learned about methods (the preferred way to do this), your code will have to use some variables that you may set to different values in order to make this work.  Remember:  do not use a method here, but actually cut-and-paste your code (yuck!).
       
    2. Grenada:


      Note #1:  A larger version can be found at:  https://www.cia.gov/library/publications/the-world-factbook/flags/gj-flag.html

      Note #2:  Again, reuse code as much as possible.  There are 7 stars, and you should cut-and-paste your star drawing code for all 7 (the challenge here is that one of the stars is a different size than the others, and you need to encode this in your variables to allow for cut-and-pasting).

      Note #3:  You can replace that yellow-and-red nutmeg pod (yes, that is what it is!) in the left green triangle with a yellow oval..
       
    3. European Union


      Note:  Unlike last week, this time you do have to paint the stars as stars.  The point is to combine your code from last week and your code from above, which when combined in the right way should get this done fairly easily.
       
    4. BONUS (optional).  ROTATED STAR
      Write a program that draws a 5-pointed star rotated counterclockwise by some number of degrees.  The star should be centered in the window and should fill half its width as the window gets resized.  The exact number of degrees will be determined based on the width of the window, as such:
           int degrees = (5*width % 360);
      Thus, as you drag the window, you should be able to watch the star rotate as it grows or shrinks.
       
    5. BONUS (optional):  St Pierre and Miquelon


      Here is the enlarged version:  https://www.cia.gov/library/publications/the-world-factbook/flags/sb-flag.html

      Ok, it's improbable, but it was too interesting a flag not to include here!  :-)   Partial credit will be given if you give it the ol' college effort and make some reasonable compromises to get something close-ish to the actual design.

Carpe diem!