Disclaimer: This document is meant to supplement other studying materials, not replace them
- Interfaces
- Abstract Classes
- Exams are cumulative. You should have a strong understanding of past material(References, Polymorphism, etc.). If not, ask for help!
If you need more practice with interfaces and abstract classes & methods, go to the EMP site: https://cs199emp.netlify.app/dist/s21/2021-03-19.html#1
You can use a code playground on the CS 125 website to follow along and test the code! https://cs125.cs.illinois.edu/
When a class implements an interface, what is the promise that is made?
Can a class implement an interface and extend a class?
How many interfaces can we implement?
How do you declare an interface?
Can you instantiate an abstract class?
What are abstract methods?
- Create an interface called
KleeneStar
. TheKleeneStar
interface has one method calledkleenify
that takes in one string and anint
. Create a new class of your choice that implements this interface. Assert the passed in string is not null. Your Kleene star function should print out the string the amount of times as the int. See the example below:
kleenify("CS", 0) \\ prints out nothing
kleenify("CS", 1) \\ Prints out CS
kleenify("CS", 2) \\ prints out CSCS
A Kleene star is actually an important topic in computational theory. If this interests you Click Here
public interface KleeneStar {
void kleenify(String arg1, int arg2);
}
public class Stargirl implements KleeneStar {
public void kleenify(String arg1, int arg2) {
assert (arg1 != null);
for (int i = 0; i < arg2; i++) {
System.out.print(arg1);
}
}
}
- Create a class called
ArraySum
that implementsComparable
. TheArraySum
constuctor takes in an Int Array which you shouldassert
is notnull
. It should implement a methodsum
that sums the stored array. You should implement thecompareTo
method based off of which array has the larger sum.
public class ArraySum implements Comparable {
int[] myArray;
int summer;
public ArraySum(int[] j) {
assert j != null;
myArray = j;
}
public void sum() {
int sum = 0;
for (int i = 0; i < myArray.length; i++) {
sum += myArray[i];
}
summer = sum;
}
public int compareTo(Object o) {
assert (o instanceof ArraySum);
ArraySum other = (ArraySum) o;
sum();
other.sum();
if (summer < other.summer) {
return -1;
}
if (summer == other.summer) {
return 0;
}
return 1;
}
}
- Shorter: Can you create a
Utility
class that has a class methodisGreaterThan
. This takes in two objects that implementComparable
and returns the larger one.
public class Utility {
public static Comparable isGreaterThan(Comparable one, Comparable two) {
if (one.compareTo(two) < 0) {
return two;
}
return one;
}
}
- Create an abstract
Pet
class and implement it in two different classes. Use your imagination and think about some methodsPet
should have, sprinkling in some abstract methods. What behavior do you notice?
public abstract class Pet {
public abstract String name();
public abstract String type();
public int numberOfFeet() {
return 4;
}
}
public class Cat extends Pet {
public String name() {
return "Josh"
}
public String type() {
return "Cat"
}
}
- Bonus: We will have a little bit more fun with Kleene stars. Create an interface called
KleeneStarDetector
that has one methoddetector
. Create a class that implements this interface.detector
takes in three arguments, the final string, the original string, and the amount of times it was repeated. If the original string repeated x times equals the first argument return true, else return false.
Example:
detector("CSCSCS","CS",3) \\return true
detector("","CS",0) \\return true
detector("CS","CS",2) \\return false
public class Off implements KleeneStarDetector {
public boolean detector(String mutated, String original, int multiplier) {
assert mutated != null;
assert original != null;
int originalLength = original.length();
int newLength = mutated.length();
if (newLength != originalLength * multiplier) {
return false;
}
int currentStart = 0;
int currentEnd = original.length();
for (int i = 0; i < multiplier; i++) {
System.out.println(mutated.substring(currentStart, currentEnd));
if (!mutated.substring(currentStart, currentEnd).equals(original)) {
return false;
}
currentStart += original.length();
currentEnd += original.length();
}
return true;
}
}
Feedback: https://forms.gle/yJLaYFaBtthPf3AP6