Getting started
Start with a couple of Java classes.
One to implement a lollypop.
package edu.unca.XXXXX;
/**
*
* @author XXXXX
*/
public class Lollypop {
int[] start ;
int[] loop ;
int startLength ;
int loopLength ;
public Lollypop(int[] start, int[] loop) {
this.start = start ;
this.loop = loop ;
startLength = start.length ;
loopLength = loop.length ;
}
public int get(int n) {
if (n < startLength) {
return start[n] ;
} else {
return loop[(n-startLength) % loopLength] ;
}
}
}
One to test the lollypop.
package edu.unca.XXXX;
/**
*
* @author XXXX
*/
public class LollypopTest {
public static void main(String[] args) {
int[] start = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9} ;
int[] loop = {10,11,12,13,14,15,16,17,18,19} ;
Lollypop expected = new Lollypop(start, loop) ;
printTest(5, expected) ;
printTest(13, expected) ;
printTest(2019, expected) ;
}
private static void printTest(int n, Lollypop l) {
System.out.println("@" + n + " is " + l.get(n)) ;
}
Action Α: Modifying the input arrays
Modify the start array after you have created
the Lollypop. Notice that this changes the lollypop.
printTest(5, expected) ;
start[5] = 431 ;
printTest(5, expected) ;
Fix your class by making copies of the start and loop
arrays in your Lollypop.
You can use either an array or an ArrayList.
Action Β: The empty loop
Modify your code to pass in an empty loop: That is a loop that has no elements. This should result in your program performing a divide by 0.
Passing an empty loop makes no sense. It’s a stick,
not a lollypop. Modify your constructor to raise the
IllegalArgumentException
when the loop is empty.
Action Γ: Changing the implementation
We’re going to start the process of making a generic
Lollypop.
Start by modifying the implementation.
Make start and loop be of type
ArrayList<Integer>. (There may be better
ways of obtaining the ultimate goal, but this is some Java
practice.)
This will not change the signature of the
Lollypop class.
Action Δ: Using Lists
Add a second constructor that can receive start
and loop arguments of type
List<Integer>.
Do not try to be clever. Just make a copy of the existing constructor
and change it as little as possible.
Action Ε: Getting more generic
Make you class into a generic Lollypop<T>
with constructor arguments of type List<T>.
For now, you should just remove the constructor arguments for the integer array. We will get back to that one later.
Action Ζ: Testing generic
Test out your program by adding a second type, I suggest
List<String>. You might find the following
useful for your two arrays:
String[] startNames =
{"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" } ;
String[] loopNames =
{"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eightteen", "nineteen" } ;
Be sure to use both Integer and String
in your program.
Action Η: Converting array to ArrayList
Get on the net and
look up information about converting an array into an
ArrayList.
There are some wild answers out there.
See if you can get any to work.
Be sure to save your old program before trying out these newfangled JDK 8+ features.
Action ?:
Next class we will look at some new things in JDK 8:
Stream and Collectors.
Look at the whole tutorial on Nested Classes and pay attention to the section on Lambda Expressions. Also, read the Tutorial on Collections with a focus on Aggregate Operations. For more information look at the documentation for java.util.stream.