Lab 19: super in Methods and Polymorphism


Due Friday, April 27 at 11:59 PM

Goals for This Lab

By the time you have completed this work, you should be able to:

Provided files:

Step-by-Step Instructions

Step 1: Download and Open the UseSuperBase.java File

Download the UseSuperBase.java file, and open it in jGrasp (or a text editor of your choice). Note that you should not edit this file, though you will need to be aware of the code it contains.

Step 2: Download and Open the UseSuperMain.java File

Download the UseSuperMain.java file, and open it in jGrasp (or a text editor of your choice). Note that you should not edit this file, though you will need to be aware of the code it contains.

Step 3: Edit UseSuperSub.java

Download the UseSuperSub.java file, and open it in jGrasp (or a text editor of your choice). You will need to add code to this file so that it compiles and UseSuperMain.java ends up producing the correct output. The comments in the file provide more details. Once you have your code working correctly, UseSuperMain.java should produce the output All ok when run.

Step 4: Get CircleMain.java Working

Download the CircleMain.java file, and open it in jGrasp (or a text editor of your choice). While you will not edit this file, you need to be familiar with the code in this file. This file is part of a larger codebase involving different approaches to getting the area of a circle. Specifically, this codebase has classes that implement two approaches:

  1. Compute the area each time we are requested to compute the area (in CircleDynamicArea.java). This is wasteful if we end up needing the area a lot, since it requires recomputation each time.
  2. Compute the area ahead of time and save it elsewhere (in CirclePrecomputedArea.java). Return this saved value instead of computing the area on demand. The area needs to be recomputed if the radius of the circle ever changes. This approach allows us to avoid needing to recompute the area each time we need it, but there is a tradeoff - we end up recomputing the area each time the radius changes, and we need to store space for an extra instance variable to keep track of the area.

Both of these above approaches are unified under a single abstract class, namely Circle.java, which has an abstract getArea method. As to which of the above approaches is better depends on exactly how the code is used. If we never need the area, then CircleDynamicArea.java is best. If we need the area quite frequently, then CirclePrecomputedArea.java is best. Thanks to polymorphism, we can define code that works with Circles, which will work no matter the particular implementation (either CircleDynamicArea.java or CirclePrecomputedArea.java). This allows to to experiment with both versions and see which is best, without any changes to code that operates on Circles.

Overall, you need to download and edit three files, namely:

  1. Circle.java
  2. CircleDynamicArea.java
  3. CirclePrecomputedArea.java

You will need to write code in each of these files in order to make CircleMain.java compile and produce the correct output. As an example, under the command-line arguments 11.1 99.9, this should produce the following output:

Initial dynamic area: 387.07563084879837
Initial precomputed area: 387.07563084879837
Reset dynamic area: 31353.126098752677
Reset precomputed area: 31353.126098752677

Step 5: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110L class. Click “Assignments” on the left pane, then click “Lab 19”. From here, you can upload your code. Specifically, you must turn in the following four files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other four files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.