Bye bye Karel, hello Java. I am thinking that the graphics windows will work with the newer Java and Eclipse. For now, as my vm’s working, I’ll leave it set up as is.

Packaged Jar files.  Extract zipped contents and run the various jar files for each of the programs below (make sure you have java installed on your system).

### Problem 1: Draw Pyramid

Here, I based the initial code attempts around the checkerboard example in Handout 12 – Control Statements.

I could see the code in my head and by this point I was also dreaming code.

The main thing to wrap my head around here was that the getHeight and getWidth commands used to determine the size of an “object” are actually used to find the size of the graphics window.  After that was groked, things went well.

Here’s the output once I figured out how to write text into a graphics program.

While letting what I’m going to do with Problem 3 percolate at the back of my head and thinking towards Assignment 3 – Breakout, I’ve tweaked the above program a touch to get the output seen below.

### Problem 2: Draw Target

After a whole lots of crappy code with a huge amount of commenting, a quick Internet search reminded me of my Basic days.  Circles do not draw from the cenre, they draw from the top left “shoulder”.  Once I had the algorithm for determining the start point, things were easy peasy.

GOval target = new GOval (startX – x, startY – y,2*x,2*y);

I wrote the code to be pretty sequential.  I am thinking that though, not meeting the dimensions of the final target exactly, a divide starting radius by circle number could produce any number of cicles in a target by using a loop.  A boolean determine if n is odd or even could be used to paint the circle red or white.  If I do come up with version 2 code, I’ll put up version 1.

By this stage I am putting constants up the top, variables at the bottom and have figured out if anything is assigned outside the run method, like “double startX”, in the program one can use “startX”.

One thing to be aware of, coordinates need to be doubles when specifying them using GRect, or GOval etc.

### Problem 3: Write a Graphics Program

Here one had to draw a chart (something like what might be done in Visio).  What is interesting me here is how to put text into a graphics window.  So far, I’ve been trying to funkify my programs with the following.  You can see my issue in the code comments.

private void whatProgramDoes () { // this works when ONLY acm.program is imported.  When acm.graphics, prints to console only.  Something to figure out at later date.
println(“What does this program do?”);
println(“It draws a red and white target mid screen”);
}

A ha!.  This is the code needed to print text when using the graphics module of Java:

add (new GLabel (“What does this program do?”), x, 20);  // 1st number is x coord, second is y //NOTE x = 0 here

Wow!  I spent forever on this problem.  I think I over complicated what I wanted to do: set up a loop that would draw the boxes bases on a boolean check (1 box, 3 boxes) and label the boxes automatically and then draw connecting lines.  In the end I wrote pretty basic sequential code that could do with being cleaned up – there’s lots of repeated code in the draw GRect, GLabel and Gline modules.

What you see below is what I came up with.  Lots of manual centering of graphical elements and the second line of boxes taking the origin point based on the top box and then visually tweaking – not what you’d want to do in “real life”.  I’ve left in the execution of some additional code that was used to visually determine the center of the graphics window.  A fair bit of paper was used to scribble out where specific points were.  It brought back memories of my Commodore 64 / Basic days, where I would plot out graphical element coordinates using graph paper before converting to code.

I was riding my bike after finishing the above code and it struck me.  The program can draw other objects by referencing another object.  So the next day I recoded the program pretty much from scratch (half to try out my idea, half to practice coding.

The code’s better but GLabels do not centre how I think they should. As an example, here are two different outputs.

Tye Y axis is correct. X is giving me headaches.  Program works, but that’s aligned to the centre of the main screen.  When using this code with the Console, Graphics and Dilaog boxes, everything’s off the the right (see the second image below).

To center the text vertically, I should also be using getAscent.

There’s a lot of duplicated code, but when the code into private voids, the variables could not be referenced so maybe I needed to set up private doubles or ints properly.  As I’ve already spent a week on this program it’s onto Problem 4.

### Problem 4: Pythagorean Theorem

Write a Console Program that accepts values for a and b as ints and then calculates the  solution of c as a double and replicate a sample run.

The code for this took about 10 minutes to write and it worked first go .

So, I figured I’d expand on it so if someone entered anything other than a positive number, it would display an error and loop back to the input prompt.  This caused a prompt repeat issue if a letter was entered, as the acm.program already appears to include an inbuilt error check for this.  Otherwise, the program works as I intended.

I’m not concerned with fixing it as I’m sure future assignments will cover it.  If not, by the time I get to the end of CS106A, I’ll go back and see if I can fix the code. This goes for any programs that are a tad iffy.

### Problem 5: Find the Smallest and Largest Number

Write a Console Program that takes in numbers and outputs the highest and lowest after zero is input.

Post conditions:

• if zero was input first, the program is meant to end and inform the user that there are no values
• when a single number is entered and then zero, the program is meant to end and report the high and low number as the same
• reproduce the output shown in a provided sample run screen shot

The second image shows I was able to reproduce the sample run.

In the opposite of Problem 4, it took me a few hours to get this right.  The code right off the bat was correct, but the while, ifs and else’s were in the wrong places.  I needed to print the code, grab a pen and draw lines and then scratch my head and whack in trouble shooting comments before I could trace the bug.

As this course is mean to teach good program design, rather than code this in one big body, I’ve used top down design.  From the first image below showing the output of my final three test runs, and the first bit of my code in the program, you should be able to develop the rest.

#### Program run code:

public void run() {
// Asks for values until the SENTINEL_VALUE is entered.
whatProgramDoes();
firstEntryAction();
if (valueEntered == SENTINEL_VALUE) {
println(“You have not entered any values yet”);
}
else {
determineValueStatus();
outputLowestAndHighest();
}
}

### Problem 6: An Escher Mathematical Puzzle

Write a Console Program that shows the output from a Hailstone sequence which is:

• if the number at the start and as the calc goes on is even, have it
• if the number is odd, times it by 3 and then add one.

The program needed to track how many turns it takes to get to 1.

The sample run starts with 17 and takes 12 steps.

This program included one bug in the odd and even calcs.  The incorrect code for the even cal was “nManipulatedResult = nManipulated = (nManipulated / 2);

The output with the bug was

After fixing the two lines of code with the error, the two outputs are as follows.  I wanted to make sure an answer was also given if one was entered at the start.

### Summary

I found that the programs that asked for an input and gave a result or set of calculated results the easiest and quickest.  The program hierarchy took the longest, but I think that was due to trying to program it using loops rather than a simple, draw box, draw text, draw lines.

The hardest thing to get right in the above programs was the looping.  While vs if and where to use else.  I did not use for loops.

As well as the bare bones “make it work according to the sample run”, I thought about trying to error-proof the code and add features.

Despite the frustrating nature of the hierarchy problem, I found assignment 2 fun and after the bugs were iron out, very pleasing.

Save

Save

Save

Save

Save

Save

Save

Save