super
in Methods and PolymorphismDue Friday, April 27 at 11:59 PM
By the time you have completed this work, you should be able to:
super
in methods to call a superclass' implementation of a methodUseSuperBase.java
UseSuperSub.java
UseSuperMain.java
Circle.java
CircleDynamicArea.java
CirclePrecomputedArea.java
CircleMain.java
collaborators.txt
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.
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.
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.
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:
CircleDynamicArea.java
).
This is wasteful if we end up needing the area a lot, since it requires recomputation each time.
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 Circle
s, 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 Circle
s.
Overall, you need to download and edit three files, namely:
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
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:
UseSuperSub.java
Circle.java
CircleDynamicArea.java
CirclePrecomputedArea.java
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.