cs106a – Assignment #3 – Task #1

The complete specification of assignment #3 can be found as part of the stream at iTunes.

Set up the bricks

Before you start playing the game, you have to set up the various pieces. Thus, it probably makes sense to implement the run method as two method calls: one that sets up the game and one that plays it. An important part of the setup consists of creating the rows of bricks at the top of the game, which look like this:

cs106a – assignment #3 – task #1

The number, dimensions, and spacing of the bricks are specified using named constants in the starter file, as is the distance from the top of the window to the first line of bricks. The only value you need to compute is the x coordinate of the first column, which should be chosen so that the bricks are centered in the window, with the leftover space divided equally on the left and right sides. The color of the bricks remain constant for two rows and run in the following rainbow-like sequence: RED, ORANGE, YELLOW, GREEN, CYAN.

Continue reading “cs106a – Assignment #3 – Task #1”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Lecture #10 to #13: More Java

Prerequisites to the tenth lecture is reading chapters 9.1 through 9.3 of “Art and Science of Java”. The lecture provides more insight into using graphics in Java.

Prerequisites to the eleventh lecture is reading chapter 9.4 as well as chapters 10.1 through 10.4 of “Art and Science of Java”. The lecture continues with graphics (including compounds) and introduces event driven programming.

Prerequisites to the twelfth lecture is reading chapters 8.1 through 8.4 of “Art and Science of Java”. The lecture starts with enumeration and explains strings in detail.

Prerequisites to the thirteenth lecture is reading chapter 8.5 of “Art and Science of Java”. The lecture continues with strings in even more detail.

Code for these lectures are available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #6

Douglas Hofstadter’s Pulitzer-prize-winning book Gödel, Escher, Bach contains many interesting mathematical puzzles, many of which can be expressed in the form of computer programs. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements from Chapter 4. The problem can be expressed as follows:

Pick some positive integer and call it n.
If n is even, divide it by two.
If n is odd, multiply it by three and add one. Continue this process until n is equal to one.

On page 401 of the Vintage edition, Hofstadter illustrates this process with the following example, starting with the number 15:
Continue reading “cs106a – Assignment #2 – Task #6”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #5

Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list, as illustrated in this sample run:

cs106a – assignment #2 – task #5

Your program should handle the following special cases:

  • If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest.
  • If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.

Start by printing the program description. Read the fist value, if it is equal to the sentinel print an adequate message, otherwise set two helper variables representing the current smallest and largest numbers equal to the first value. Inside the following infinite loop, read the next number and stop processing if it is equal to the sentinel. Adjust the two helper variables if necessary. And finally print the result:
Continue reading “cs106a – Assignment #2 – Task #5”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #4

In high-school geometry, you learned the Pythagorean theorem for the relationship of the lengths of the three sides of a right triangle:
a2 + b2 = c2
which can alternatively be written as:
c = √(a2 + b2)
Most of this expression contains simple operators covered in Chapter 3. The one piece that’s missing is taking square roots, which you can do by calling the standard function Math.sqrt. For example, the statement

double y = Math.sqrt(x);

sets y to the square root of x.
Write a ConsoleProgram that accepts values for a and b as ints and then calculates the solution of c as a double. Your program should be able to duplicate the following sample run:

cs106a – assignment #2 – task #4

Continue reading “cs106a – Assignment #2 – Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #3

Write a GraphicsProgram subclass that draws a partial diagram of the acm.program class hierarchy, as follows:

cs106a – assignment #2 – task #3

The only classes you need to create this picture are GRect, GLabel, and GLine. The major part of the problem is specifying the coordinates so that the different elements of the picture are aligned properly. The aspects of the alignment for which you are responsible are:

  • The width and height of the class boxes should be specified as named constants so that they are easy to change.
  • The labels should be centered in their boxes. You can find the width of a label by calling label.getWidth() and the height it extends above the baseline by calling label.getAscent(). If you want to center a label, you need to shift its origin by half of these distances in each direction.
  • The connecting lines should start and end at the center of the appropriate edge of the box.
  • The entire figure should be centered in the window.

Continue reading “cs106a – Assignment #2 – Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs106a – Assignment #2 – Task #2

Suppose that you’ve been hired to produce a program that draws an image of an archery target—or, if you prefer commercial applications, a logo for a national department store chain—that looks like this:

cs106a – assignment #2 – task #2

This figure is simply three GOval objects, two red and one white, drawn in the correct order. The outer circle should have a radius of one inch (72 pixels), the white circle has a radius of 0.65 inches, and the inner red circle has a radius of 0.3 inches. The figure should be centered in the window of a GraphicsProgram subclass.

To draw a circle create an oval with using the diameter (twice the radius), and place it at the given position:
Continue reading “cs106a – Assignment #2 – Task #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail