CS-207-1: Programming II
Spring 2020
Northeastern Illinois University
Homework #9: Due Monday, 04/13/20 at 5:00 p.m.
Abstract Classes and Interfaces
Getting Started
1. Download the .zip file that contains the code needed for this assignment. Unzip it in a
location on your computer where you want your homework files to go.
2. Launch IntelliJ and click "Open". Select the unzipped folder.
3. Check the following:
• Go to File −→ Project Structure. Under Project Settings, click on Project and verify that
the Project SDK is set to JDK 11 or higher. If it is not, select the co
ect SDK from the
drop-down.
Submitting your assignment to D2L
1. Every java file in the project should begin with a comment block that includes your name,
CS-207-1, and Spring 2020. Here is an example that uses my name (Note: Use your own
name instead!):
Rachel Trana
CS-207-1, Spring 2020
2. Once you have completed all the problems, in IntelliJ, go to File, select Export to Zip File.
This will a create a zip file of the entire project directory.
3. Submit the .zip file to the Homework 9 Assignment folder on D2L.
Problem 1
1. All code for this problem shoudl go in the src −→ edu.neiu.p2 −→ problem1 directory.
2. Create an interface named Filterwith the following:
• An abstract method named accept that does not take any parameters and returns a
oolean.
• A static method named count that takes a 1D a
ay of Filter objects and returns an
integer. The method should iterate over the a
ay of Filter objects to count the total
objects that return true when the acceptmethod is called on them.
3. Create a class named BankAccountwith the following:
• A properly encapsulated double instance variable named balance. (Yes, we’re using a
double even though normally we would use a BigDecimal for finances - this is to make
this problem slightly more straightforward.)
1
• A private static variable named CAP of type double.
• A constructor that takes one parameter and sets the instance variable.
• A getter for the instance variable.
• Implement the Filter interface and then ove
ide the accept method to return true if
the balance is greater than CAP and false otherwise.
4. In the DemoBankAccounts class, you have been provided with an a
ay of BankAccounts.
Uncomment this code.
5. Call the countmethod from the Filter interface (remember how you call static methods - it
works the same way for interfaces that it does for regular classes) and print out the result.
Problem 2
1. All code for this problem shoudl go in the src −→ edu.neiu.p2 −→ problem2 directory.
2. Create an interface named Drawable that has the following:
• An abstract method named draw that takes a char parameter named fill and does not
eturn anything.
• An abstract method named scale that takes a double parameter named factor and
does not return anything.
• Compile the interface.
3. Create an abstract class named Shape that implements the Drawable interface and has the
following:
• An encapsulated String instance variable named name.
• A constructor that takes a String parameter and sets the instance variable.
• A getter for the instance variable.
• An abstract method named getArea that does not take any parameters and returns a
double.
4. Create a class named Rectangle that inherits from Shape and has the following:
• Two encapsulated double instance variables named width and height.
• A constructor that takes two double parameters. It should pass the String "Rectangle"
to the superclass and then set the subclass instance variables co
ectly.
• Implement the getAreamethod to co
ectly calculate the area of a rectangle.
• Implement the scalemethod to throw an IllegalArgumentException if factor is less
than or equal to 0. Give it the message: "Factor must be greater than 0". Then
it should update each of the instance variables to be the result of multiplying thei
individual values by factor.
• Implement the draw method so that it prints out a rectangle that has width rows and
height columns made up of the fill parameter (see sample output below). Because
width and height are doubles, round their values down to the nearest integer and then
add 1 (so, 4.5 becomes 5, 3.1 becomes 4, etc.).
2
• Compile your code. Uncomment the first set of block commented code in the ShapeDemo
class - your output must match the first half of the sample output below.
5. Create a class named RightTriangle that inherits from Shape and has the following:
• Two encapsulated double instance variables named base and height.
• A constructor that takes one double parameter. It should pass the String "Right
Triangle" to the superclass and then set both of the subclass instance variables to the
constructor parameter.
• Implement the getAreamethod to co
ectly calculate the area of a right triangle.
• Implement the scalemethod to throw an IllegalArgumentException if factor is less
than or equal to 0. Give it the message: "Factor must be greater than 0". Then
it should update each of the instance variables to be the result of multiplying thei
individual values by factor.
• Implement the draw method so that it prints out a right triangle that has height rows
and base columns made up of the fill parameter (see sample output below). Because
ase and height are doubles, round their values down to the nearest integer and then
add 1 (so, 4.5 becomes 5, 3.1 becomes 4, etc.).
• Compile your code. Uncomment the second set of block commented code in the
ShapeDemo class - your output must match the second half of the sample output below.
6. Your output format must match the sample output format.
Shape: Rectangle
Area: 200.0
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
Scaled by 0.5:
Area: 50.0
- - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
Factor must be greater than 0
3
Shape: Right Triangle
Area: 40.5
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
# # # # # # # # #
# # # # # # # # # #
Scaled by 0.5:
Area: 10.125
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
4