CS10, 09S, UCSB

H15: Nested loops (section 6.3) and Recursion (section 13.1) Total points: ?

Accepted: on paper, in lecture (1-1:50pm) on Friday, May 27th only.
No email submission allowed.

Name One: (5 pts)______________________________   UCSBNetID (5 pts)  _____________________

Section (5 pts) Circle one:         Thu 4pm                 Fri 10am                       Fri 11am                  Fri noon


  1. (25 pts) Re-read Section 6.3 (or if you didn't read it the first time, read it now).

    In particular, read the code on p. 245 and 246, and understand how the toString method for the Triangle class works.

    Then, consider the following class. Write the code that would go inside the "stub" for the toString method to make the class work correctly.

    If you want to do this online, you can find a BlueJ project for the code at this link that will allow you to check your answers with JUnit tests: http://www.cs.ucsb.edu/~pconrad/cs10/09S/homework/H15/H15Project/

    You should still write out your answer by hand on this sheet to turn it in, however.
    (Experience indicates this will help you be ready for the final exam—I'm not doing this just to be a pain.)

    /**
       This class describes triangle objects that can be displayed
       as shapes like this:
       [][][]
       [][]
       []
       
       @author P. Conrad, based on code by Horstman, Big Java 3rd edition, Section 6.3
       @version for CS10, Spring 2009, Homework 15
    */
    public class UpperLeftTriangle
    {
    
       private int width;
       
       /**
          Constructs a triangle.
          @param aWidth the number of [] in the first row of the triangle.
       */
       public UpperLeftTriangle(int aWidth)
       {
          width = aWidth;
       }
    
       /**
          Computes a string representing the triangle.
          @return a string consisting of [] and newline characters
       */
       public String toString()
       {
          String r = "stub"; // @@ implement this
         
          return r;
       }
    
    }
          
  2. (10 pts) Now read Section 13.1, looking especially at this code for the getArea() on p. 589.
    Which specific line of code in this method makes it a "recursive" method? Write that line of code here.
End of H14