title: Methods and decomposition
body: <p>I'm just starting learning Java after a few years of HTML/CSS coding so hopefully I'm not asking a old or stupid question here but any help explaining this problem would be very much appreciated. </p>
  
  <p>I'm currently working through the Stanford CS106A online material and I've reached week 6, Assignment 2, Question 3 (http://see.stanford.edu/materials/icspmcs106a/13-assignment-2-simple-java.pdf).</p>
  
  <p>As you can see it requires the placement of various objects on the screen to create the Graphics Hierarchy, as described. My plan was to use the centre coordinates to relatively place all the objects on the screen. However I've hit a problem that I can't seem to find an answer to. The course describes how Method Decomposition should allow each method to handle one problem (Single Responsibility Principle, I believe) so I have written the first part of my code as such:</p>
  
  <pre><code>//Import any libraries
  import acm.program.*;
  import acm.graphics.*;
  
      public class GraphicsHierarchy extends GraphicsProgram {
  
  //Define constants
  static final int BOX_WIDTH = 200;
  static final int BOX_HEIGHT = 75;
  
  
  
  public void run() {
      placeGRect();
  }   
  
  //Find centre x &amp; y
  double centre_x = getWidth() / 2; //check this
  double centre_y = getHeight() * 0.5;//and this
  
  //placeGRect method
  public void placeGRect() {
      for (int count = 0; count &lt; 4; count++) {
          GRect box = new GRect (BOX_WIDTH, BOX_HEIGHT);
          add(box);
          switch (count) {
          case 0:
              box.setLocation(centre_x, 75);
              break;
          case 1:
              box.setLocation((centre_x * 0.5), 250);
              break;
          case 2:
              box.setLocation(centre_x, 250);
              break;
          case 3:
              box.setLocation((centre_x * 1.5), 250);
              break;
          }
      }
  }
  }
  </code></pre>
  
  <p>However this doesn't work due to the centre_x &amp; centre_y producing zero values. I discovered this by changing the program to a ConsoleProgram and having the getWidth &amp; getHeight lines inside the run() method (and println their values on screen), which then produced the required values but didn't pass them to the GRect method (so still didn't work). However if I have the getWidth/getHeight lines listed out of the run() then they don't produce any values for relative positioning.</p>
  
  <p>My question is given that each method should handle one task and (as much as possible) methods should be defined out of the run() method, then how I can I get the getWidth/getHeight values to the placeGRect() method without having one big block of code within the run() method. Which I understand is bad practise.</p>
  
  <p>I'm not after any code to solve this, I really need to understand the principles of this so I can write effective code in the future. I prefer understanding to parrot-fashion code copying. </p>
  
  <p>Thanks in advance for any help.</p>