• 07-07-2011, 09:53 AM
YLTO
Hello folks, I'm new member here and I got stumble to this forum when I google out the CS106A problem , so pardon me if I do make some mistake in this forum.

So here is my problem ,

I teach myself programming from CS106A free courses.
And I try to do the assignment but sadly I failed to complete it correctly.

Whenever I try to decompose the project, I'm somehow hard to figure out especially the coordinate of the object and when it comes to argument, I'm totally screwed.
So does anyone know how to decompose problem effectively, I mean what aspect we should take care first?

So here is my work for assignment number 3.
http://see.stanford.edu/materials/ic...imple-java.pdf

Code:

```/*  * File: ProgramHierarchy.java  * Name:  * Section Leader:  * ---------------------------  * This file is the starter file for the ProgramHierarchy problem.  */ import acm.graphics.*; import acm.program.*; import java.awt.*; public class ProgramHierarchy extends GraphicsProgram {        /*        Dimension of the box */         private static final double BOX_WIDTH = 200;         private static final double BOX_HEIGHT = 50; /*        Width of space between boxes */         private static final double BOX_SPACE = 25;         /*        The height of space between boxes */         private static final double BOX_CONNECTOR = 50;                         double x = (getWidth()/2) - (BOX_WIDTH/2);                 double y = (getHeight()/2) - (BOX_HEIGHT/2);                 double dx = BOX_SPACE + BOX_WIDTH;                 double dy = BOX_CONNECTOR;                                 public void run(){                 drawProgram(x, y);                 drawConsole(x, y + dy);                 drawGraphics(x - dx , y + dy);                 drawDialog(x + dx , y + dy);         }         private void drawProgram(double x, double y){                 drawBox(x, y);                 drawLabel("Program" , x, y );         }                 private void drawConsole(double x, double y){                 drawBox(x, y);                 drawLine(x + BOX_WIDTH/2 , y + BOX_HEIGHT, x, y + dy/2 );                 drawLabel("ConsoleProgram" ,x ,y);                }         private void drawGraphics(double x, double y){                 drawBox(x,y);                 drawLine(x + BOX_WIDTH/2, y + BOX_HEIGHT, x + BOX_WIDTH - dx, y + BOX_HEIGHT + dy);                 drawLabel("GraphicsProgram" ,x, y);         }                 private void drawDialog(double x, double y ){                 drawBox(x,y);                 drawLine(x + BOX_WIDTH/2, y + BOX_HEIGHT, x + BOX_WIDTH + dx, y + BOX_HEIGHT + dy );         }         private void drawLine(double x1, double x2, double y1, double y2){                 add(new GLine(x1, x2, y1, y2));         }                 private void drawLabel(String text, double x, double y) {                 GLabel label = new GLabel(text, x, y);                 x = BOX_WIDTH/2 - label.getWidth()/2;                 y = BOX_HEIGHT/2 - label.getAscent()/2;                 add(label);         }                 private void drawBox(double x, double y){                 add(new GRect(x, y));         } }```
The code looks messy, isn't it? However it took my half an hour to figure out the code.
By the way when I try to run the code, the code can't run. Your help will be appreciated.

Thanks :)
• 07-07-2011, 09:58 AM
Dark
Whats the error message you get when you try to compile/run it?
• 07-07-2011, 10:04 AM
YLTO
Quote:

Originally Posted by Dark
Whats the error message you get when you try to compile/run it?

I do not know , my eclipse just directly turn the perspective into debug perspective.