CSSE 220 – Object-Oriented Software Development

Homework 7

Objectives

Practice with loops (while, for) in Java. Practice with debugging. More practice implementing classes based on a given specification.

Tasks

  1. Complete the assigned reading for today's session (Session 7: Big Java, Ch. 6), according to the course schedule.
  2. Complete the assessment exercise over this reading on ANGEL (under Lessons → Assignments).
  3. Programming:
    1. In Eclipse, checkout the Iteration project.

    2. Rates: Create a new class named Rates that has the usual main method. That method should use a for loop to prints results like the following table:
          Rate  Years to Double
          ----  ---------------
          0.1%    694
          0.2%    347
          0.3%    232
          0.4%    174
          0.5%    139
          0.6%    116
          0.7%    100
          0.8%     87
          0.9%     78
          1.0%     70
          1.1%     64
          1.2%     59
          1.3%     54
          1.4%     50
          1.5%     47
          1.6%     44
          1.7%     42
          1.8%     39
          1.9%     37
      
      but for rates up to 8.0% (in increments of 0.1 percent, as shown in the above example). Use an instance of the provided Investment class to do the calculation for each line of the table.
    3. Whack-a-bug: The WhackABug class contains several methods with bugs in them. Use the Java debugger in Eclipse to find the bug in each method. Add a comment to each method identifying the bug. Fix each bug.
    4. Pyramid Scheme:
      1. Create a class Triangle whose constructor takes x- and y-coordinates, a base width, and a height. Have Triangle implement a drawOn(Graphics2D g) method.
        • Triangle's drawOn method should draw a single isoceles triangle as shown in the figure below. (You just have to draw the outline of the triangle, not the labels.)
        • For testing, you can modify TriangleDrawer to create and draw different instances of your new Triangle class (different locations and sizes). The main() method is in PyramidSchemeMain.
        Shows how the constructor arguments for Triangle relate to the figure to be drawn.
      2. Add a second constructor to TriangleDrawer that takes a total window  height and total window width in pixels, and an integer rowCount. Change TriangleDrawer’s paintComponent() method so it draws a pyramid of Triangles as in the first figure below (in that figure,  rowCount == 6, but your program should work for any reasonable value of rowCount) You just have to draw the triangles that point up; the ones that point down are an illusion.

        You’ll need to:

        • calculate a base and a height to be used for each of the Triangles, and
        • write a doubly-nested loop to create and draw the Triangles at appropriate locations.
        • Note that the pyramid should mostly fill the window, as in the picture below.  
        • Test your program by running it several times with different values of the three arguments passed to the TriangleDrawer constructor.

        Test your code by modifying PyramidSchemeMain as needed.

        Sample output for PyramidScheme with rowCount == 6     Sample output for PyramidScheme with rowCount == 10
    5. Retirement Plan: Write a program that prompts the user for an interest rate, then for a series of annual investments. After each entry, the program should print the total value of the investment. Here’s an example session using the program:
      Enter the annual interest rate earned: 5.0
      Enter the annual investment, or Q to quit: 100
      New balance: 100.00
      Enter the annual investment, or Q to quit: 50
      New balance: 155.00
      Enter the annual investment, or Q to quit: 100
      New balance: 262.75
      Enter the annual investment, or Q to quit: q
      Final balance: 262.75
      
      Other requirements:
      • Put your main() method in a class named RetirementPlan.
      • Create an AnnualInvestment class to handle the calculations. This class’s constructor should take an annual interest rate. It should have an addAnnualInvestment(double amount) method that calculates the annual interest earned and adds the given amount to the investment.
      • Create a JUnit test class AnnualInvestmentTest, to test your AnnualInvestment class. You might need to add method or methods to AnnualInvestment to make it testable.
      • Don’t forget to handle the user input as described above. See §6.3.6 of Big Java for an example of using a sentinel loop to get user input.

Remember, in all your code:

Here is the grading rubric for this assignment.

Turn-in Instructions

Turn in your programming work by committing it to your SVN repository.