0

I'm doing these free online Stanford classes and learning Java. I'm stuck on something and haven't been able to figure it out. I think there must be something wrong with my logic. Please check out the code below. I commented it, so hopefully you can understand what I'm trying to do there.

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

public class Pyramid extends GraphicsProgram {

/** 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;

/** The Width of the Base in px  */ 
    double baseInPx = BRICKS_IN_BASE * BRICK_WIDTH;

/** Taking the width of the window minus the width of the base and dividing by two 
 * to find the x axis starting point)   
 */
    double firstBrick = (getWidth() - baseInPx) / 2;

/* giving the y axis a variable name  */
        double baseHeight = getHeight();

    public void run() {

        add(new GRect(firstBrick,baseHeight,BRICK_WIDTH, BRICK_HEIGHT));

    }   

}

I'm thinking I must be doing something wrong in my formatting in this line:

double firstBrick = (getWidth() - baseInPx) / 2;

The problem is that my variable in the x axis is not working. If I hard code a number there, the rect shows up, but not with firstBrick

Thanks for your help!

EDIT: Thank you all for your help! Pretty much every one of you were right. I just learned something!

5 Answers 5

2

Move both firstBrick and baseHeight into the run method and see if that works.

public void run() {
    double firstBrick = (getWidth() - baseInPx) / 2;
    double baseHeight = getHeight();

    add(new GRect(firstBrick,baseHeight,BRICK_WIDTH, BRICK_HEIGHT));

}

My guess is your getWidth() and getHeight() are instance methods, not static methods. So you can not call on them in your object initialization block (in your example code: the inits outside any methods) because the Pyramid object hasn't been completely initialized.

You can also declare the variables where they are and initialize them in the Pyramid constructor, which is the preferred approach. That's what constructors are for.

Sign up to request clarification or add additional context in comments.

2 Comments

This did seem to do the trick. What is confusing to me about this though s my understanding is that getHeight and getWidth are used to call the property of the actual graphical box. Is this still considered part of the instance method?
That's correct, but until the object has been fully constructed you can't depend on it having all properties initialized. The SCJP book has a section on object initialization sequence and is a great book for learning the details of Java. amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060/…
2

I'm going to try and help you help yourself. Try moving the calculation into the run method and printing out the value to see how it differs from what you expect when you hard code the value.

2 Comments

I appreciate that. Putting the calculation in the run method did do the trick. I'm now trying to understand why :-D...ah-is it because "baseInPx" is referring to the instance method?
baseInPx is computed using the instance method getWidth() - which does not make sense outside of the context of an instance. The run method is an instance method, associated with an instance of a Pyramid.
0

Take a look at this code

public class Assignment {

    private String a = setString();
    private String b;

    public Assignment() {
        b = "set in constructor";
    }

    private String setString() {
        return b;
    }

    private String getA() {
        return a;
    }

    public static void main(String[] args) {
        System.out.println(new Assignment().getA());
    }

}

This will print null;

My guess is that something similar is happening. Maybe the width and height of the pyramid are set during the constructor so getWidth and getHeight default to 0.0 when called there.

Comments

0

Try setting firstBrick and baseHeight in the run method or maybe in the constructor.

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

public class Pyramid extends GraphicsProgram {

    private static final int BRICK_WIDTH = 30;
    private static final int BRICK_HEIGHT = 12;
    private static final int BRICKS_IN_BASE = 14;
    double baseInPx = BRICKS_IN_BASE * BRICK_WIDTH;

    double firstBrick;
    double baseHeight;

    public void run() {
       firstBrick = (getWidth() - baseInPx) / 2;
       baseHeight = getHeight();

       add(new GRect(firstBrick,baseHeight,BRICK_WIDTH, BRICK_HEIGHT));

    }   

}

Comments

0

Your problem is this line:

double firstBrick = (getWidth() - baseInPx) / 2;

At the point in time this line gets executed, getWidth() returns 0; The operation results in a negative number that won't be painted.

As already said, you have to call the function after the UI has been built. -> run()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.