CSSE 220 – Object-Oriented Software Development

Homework 8

Objectives

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

Tasks

  1. Complete the assigned reading for the next session: §7.1–7.5. (See schedule for topics to focus on.) As you read, see if you can answer the self-check questions. If there is something you do not understand, make note of it so you can ask about it.
  2. Complete the assessment exercise over this reading on ANGEL (under Lessons → Assignments).
  3. Programming:
    1. Your programming work for this assignment must be done in the Iteration project inside Eclipse. Use the SVN Repository Exploring perspective to check out this project, then switch back to the Java perspective.
    2. Rates: Write a program named Rates using a for loop that 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%. Use instances 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. Include a drawOn(Graphics2D g) method, that draws an isoceles triangle as 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 an instances of your new Triangle. The main() method is in PyramidSchemeMain.
      2. Add a constructor to TriangleDrawer that takes a total height and width in pixels, and an integer rowCount. Change TriangleDrawer’s paintComponent() method so it draws a pyramid of Triangles as in the figure below for rowCount == 6. (You just have to draw the triangles that point up; the ones pointing down are an illusion.)

        You’ll need to:

        • calculate a base and height to be used for all the Triangles, and
        • write a doubly-nested loop to create and draw the Triangles.
    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.4 of Big Java for an example of using a sentinel loop to get user input.

Turn-in Instructions

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