Sunday, November 15, 2009

Work in Progress: Program To Draw A Pyramid

Problem: Write a "GraphicsProgram" subclass that draws a pyramid consisting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid.

The pyramid should be centered in the window and should use named constants for the following parameters:


BRICK_WIDTH: The width of each brick
BRICK_HEIGHT: The height of each brick
BRICKS_IN_BASE: The nuber of bricks in the base

(Roberts Ch 4, exercise 11).


What's making this difficult:

1) Centering the pyramid. I can use getWidth/2 and getHeight/2 methods to find the center of the screen, and add or subtract to place the brick at a location in reference to the center. However, the bricks are sometimes above the center, sometimes below, sometimes to the right of the center, sometimes to the left. Either I'm missing something (quite likely), or it requires a clunky solution (ie, if four sepeate nested loops for each quadrant of the pyramid).

2) Variables inside the nested "for" statement: The outer "i" loop draws all of the rows, and the inner loop "j" draws all of the bricks inside the rows. The first row has 1 brick, the second row has two bricks, and so on, so I make the inner loop condition sound like this: "From j equals 1 to j equals i, draw a brick", making "i" the endpoint since "i" is the nth row that we're on. However, Eclipse seems to get mad when I reference "i" inside the "j" loop; I don't know why, but it has an angry red line under my i's within the inner loop.

(Click to enlarge)

Stanford Editor - Pyramid.java - Eclipse SDK - _Applications_eclipse_Eclipse.app_Contents_MacOS_cs106a_workspace
Uploaded with plasq's Skitch!


I ignored these problems for the time being and tried to see whether I could *just draw* the first brick of the first row. Below is the code for that part; I think it's in the right place. How can I modify it to get to the rest of the pyramid?


/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
*/

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class Pyramid extends GraphicsProgram {

public void run() {

//Starting from the bottom, build rows
for (int i = BRICKS_IN_BASE; i > 1; i --); {

//For each row; starting from the left, lay bricks
for (int j = 1; j <= 14; j++); {
double x = getWidth()/2 - BRICK_WIDTH * 14/2;
double y = getHeight()/2 + BRICK_HEIGHT * 14/2;
GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
add(brick);
}
}
}
/** Width of each brick in pixels */
private static final int BRICK_WIDTH = 30;

/** Width of each brick in pixels */
private static final int BRICK_HEIGHT = 12;

/** Number of bricks in the base of the pyramid */
private static final int BRICKS_IN_BASE = 14;

}



What it looks like? One sad brick :(


Sunday, November 1, 2009

Program to Reverse the Digits in an Integer

The problem: "Rewrite the "Digit Sum" program given in Figure 4-6 so that instead of adding the digits in the number, it generates the number that has the same digits in the reverse order, as illustrated by this sample run:

'This program reverses the digits in an integer.
Enter a positive integer: 1729
The reversed number is 9271'

The idea in this exercise is not to take the integer apart character by character, which you will not learn how to do until Chapter 8. Instead, you need to use arithmetic to compute the reversed integer as you go. In this example, the new integer will be 9 after the first cycle of the loop, 92 after the second, 927 after the third, and 9271 after the fourth." (Robers ch 4, exercise 6).

Here's my code:



/*
* File: ReverseIntegers.java
* Name: Renee
* Section Leader: Chubacca
* -----------------
*This program follows the insight from the Digital Root problem that the last digit in an integer is the
*same thing as the remainder of that number when divided by 10. We use a while loop to divide the imput
*by 10 repeatedly. Each time, we take the last digit of the integer, house it in the variable "temp",
*and add it to the cumulating variable "reverse." Then when n can't be divided by 10 any more, we print
*the number housed in "reverse."
*/


import acm.program.*;


public class ReverseIntegers extends ConsoleProgram {
public void run() {

println("This program reverses the integers in an integer.");

int n = readInt("Enter a positive integer: ");

//If there's only one digit, print; we're done
if (n <= 9) { println("The reversal is " + n); } else { int temp = 0; int reverse = 0; while (n > 0) {
temp = n % 10;
reverse = 10*reverse + temp;
n/=10;
}
println ("The reverse is " + reverse);
}
}
}








What the output looks like:


What made it tough: This was actually quite easy to figure out, having just worked on the sister problems for digital root and fib sequence. It took about 10 minutes to sketch out. Jason and Annie's examples for fib sequence, which used temp variables, made it easy to think of using temp variables for this problem.

Time to complete: 20 minutes.