03/.DS_Store
__MACOSX/03/._.DS_Store
03/A2/.DS_Store
__MACOSX/03/A2/._.DS_Store
03/A2/build.xml
Some useful functions for working with pipes.
__MACOSX/03/A2/._build.xml
03/A2/src/.DS_Store
__MACOSX/03/A2/src/._.DS_Store
03/A2/src/nz/.DS_Store
__MACOSX/03/A2/src/nz/._.DS_Store
03/A2/src/nz/retro_freedom/.DS_Store
__MACOSX/03/A2/src/nz/retro_freedom/._.DS_Store
03/A2/src/nz/retro_freedom/dsa2016/.DS_Store
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/._.DS_Store
03/A2/src/nz/retro_freedom/dsa2016/pair/Pair.java03/A2/src/nz/retro_freedom/dsa2016/pair/Pair.javapackage nz.retro_freedom.dsa2016.pair;
import java.util.*;
/**
* @author
* @version 1.0
*
* A data type representing generic, non-{@code null} pairs.
* Pairs cannot be modified once created - if you need to change a pair,
* make a new one instead.
*/
publicclassPair<T1, T2>{
/**
* Makes a new pair.
*
* @param left the left side of the pair (cannot be {@code null}).
* @param right the right side of the pair (cannot be {@code null}).
* @return a new pair with the given left and right sides.
* @throws NullPointerException if left or right are {@code null}.
*/
publicstatic<T1, T2>Pair<T1, T2> make (T1 left, T2 right){
Objects.requireNonNull(left,"left cannot be null.");
Objects.requireNonNull(right,"right cannot be null.");
returnnewPair<T1, T2>(left, right);
}
/**
* Convenience factory for replacing left side of an existing pair.
*
* @param p a pair (cannot be {@code null}).
* @param left a new left element (cannot be {@code null}).
* @return a new pair which is the same as p, but with a new left element.
* @throws NullPointerException if an argument is {@code null}.
*/
publicstatic<T1, T2>Pair<T1, T2> newLeft (Pair<T1, T2> p, T1 left){
Objects.requireNonNull (p,"p cannot be null.");
return make (left, p.right ());
}
/**
* Convenience factory for replacing right side of an existing pair.
*
* @param p a pair (cannot be {@code null}).
* @param right a new right element (cannot be {@code null}).
* @return a new pair which is the same as p, but with a new right element.
* @throws NullPointerException if an argument is {@code null}.
*/
publicstatic<T1, T2>Pair<T1, T2> newRight (Pair<T1, T2> p, T2 right){
Objects.requireNonNull (p,"p cannot be null.");
return make (p.left (), right);
}
/**
* Compares on both elements.
*
* @param o a thing to compare to.
* @return true if both sides of both pairs match.
*/
@Override
publicboolean equals (Object o){
if(o instanceofPair<?,?>){
Pair<?,?> p =(Pair<?,?>)o;
if(left.equals (p.left)&&
right.equals (p.right)){
returntrue;
}
}
returnfalse;
}
/**
* Hashes on both sides.
*
* @return a unique code representing a pair.
*/
@Override
publicint hashCode (){
int result =1;
resu ...
3. * Makes a new pair.
*
* @param left the left side of the pair (cannot be {@code nu
ll}).
* @param right the right side of the pair (cannot be {@code
null}).
* @return a new pair with the given left and right sides.
* @throws NullPointerException if left or right are {@code
null}.
*/
publicstatic<T1, T2>Pair<T1, T2> make (T1 left, T2 right){
Objects.requireNonNull(left,"left cannot be null.");
Objects.requireNonNull(right,"right cannot be null.");
returnnewPair<T1, T2>(left, right);
}
/**
* Convenience factory for replacing left side of an existing p
air.
*
* @param p a pair (cannot be {@code null}).
* @param left a new left element (cannot be {@code null}).
* @return a new pair which is the same as p, but with a new
left element.
* @throws NullPointerException if an argument is {@code n
ull}.
*/
publicstatic<T1, T2>Pair<T1, T2> newLeft (Pair<T1, T2> p, T1
left){
Objects.requireNonNull (p,"p cannot be null.");
return make (left, p.right ());
}
/**
* Convenience factory for replacing right side of an existing
pair.
4. *
* @param p a pair (cannot be {@code null}).
* @param right a new right element (cannot be {@code null
}).
* @return a new pair which is the same as p, but with a new
right element.
* @throws NullPointerException if an argument is {@code n
ull}.
*/
publicstatic<T1, T2>Pair<T1, T2> newRight (Pair<T1, T2> p, T
2 right){
Objects.requireNonNull (p,"p cannot be null.");
return make (p.left (), right);
}
/**
* Compares on both elements.
*
* @param o a thing to compare to.
* @return true if both sides of both pairs match.
*/
@Override
publicboolean equals (Object o){
if(o instanceofPair<?,?>){
Pair<?,?> p =(Pair<?,?>)o;
if(left.equals (p.left)&&
right.equals (p.right)){
returntrue;
}
}
returnfalse;
}
/**
* Hashes on both sides.
*
5. * @return a unique code representing a pair.
*/
@Override
publicint hashCode (){
int result =1;
result =37* result + left.hashCode ();
result =37* result + right.hashCode ();
return result;
}
/**
* Gives a debugging representation.
* @return an implementation-defined debug string.
*/
@Override
publicString toString (){
return"("+ left.toString ()+", "+ right.toString ()+")";
}
/**
* Fetches the left side of the pair.
* @return the left side of the pair.
*/
public T1 left (){return left;}
/**
* Fetches the right side of the pair.
* @return the right side of the pair.
*/
public T2 right (){return right;}
// Private stuff
privatefinal T1 left;
privatefinal T2 right;
6. privatePair(T1 left, T2 right){
this.left = left;
this.right = right;
}
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pair/._Pair.ja
va
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/._pair
03/A2/src/nz/retro_freedom/dsa2016/pipes/.DS_Store
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._.DS_
Store
03/A2/src/nz/retro_freedom/dsa2016/pipes/Pipes.java03/A2/src/
nz/retro_freedom/dsa2016/pipes/Pipes.javapackage nz.retro_fre
edom.dsa2016.pipes;
import java.util.*;
import nz.retro_freedom.dsa2016.pair.*;
/**
* @author
* @version 1.0
*
* A library for working with pipes.
*/
publicclassPipes{
/**
* Retrieves the first element of a list, in O(1) time.
7. *
* @param list the list (cannot be {@code null} or empty).
* @return the first element of the argument.
* @throws NullPointerException if the list is {@code null}.
* @throws IllegalArgumentException if the list is empty.
*/
publicstatic<T> T head (LinkedList<T> list){
Objects.requireNonNull (list,"list cannot be null.");
try{
return list.getFirst ();
}
catch(NoSuchElementException e){
thrownewIllegalArgumentException("list cannot be empty.");
}
}
/**
* Retrieves all but the first element of the argument, in O(1)
time.
* Will yield an empty list if given a list with just one elemen
t.
*
* @param list the list.
* @return a new list containing everything but the first elem
ent of the
* argument.
* @throws NullPointerException if the list is {@code null}.
* @throws IllegalArgumentException if the list is empty.
*/
publicstatic<T>LinkedList<T> tail (LinkedList<T> list){
Objects.requireNonNull (list,"list cannot be null.");
try{
returnnewLinkedList<>(list.subList(1, list.size ()));
}
catch(IllegalArgumentException e){
thrownewIllegalArgumentException("list cannot be empty.");
8. }
}
/**
* Returns a new list with e as its head, and list as its tail.
*
* @param e an element (cannot be {@code null}).
* @param list a list to serve as the tail (cannot be {@code n
ull}).
* @return a new list with e as its head, and list as its tail.
* @throws NullPointerException if the list is {@code null}.
*/
publicstatic<T>LinkedList<T> cons (T e,LinkedList<T> list){
Objects.requireNonNull (e,"e cannot be null.");
Objects.requireNonNull (list,"list cannot be null.");
LinkedList<T> ll =newLinkedList<>(list);
ll.addFirst (e);
return ll;
}
/**
* Decomposes a list into its head and tail.
*
* @param list a list (cannot be {@code null} or empty).
* @return a pair of the head and tail of the list.
* @throws NullPointerException if the list is {@code null}.
* @throws IllegalArgumentException if the list is empty.
*/
publicstatic<T>Pair<T,LinkedList<T>> uncons (LinkedList<T>
list){
returnPair.make (Pipes.head (list),Pipes.tail (list));
}
/**
* Converts a list into another list by repeated application of
a conversion
9. * rule.
*
* @param trans a transformer for elements of the list (canno
t be
* {@code null}).
* @param list a list (cannot be {@code null}).
* @return a new list, defined as every element of the argume
nt list, with
* the given transformation applied to the elements in the sa
me order.
* @throws NullPointerException if any argument is {@code
null}.
*/
publicstatic<From,To>LinkedList<To> map (Transformer<From
,To> trans,
LinkedList<From> list){
Objects.requireNonNull (trans,"trans cannot be null");
Objects.requireNonNull (list,"list cannot be null");
if(list.isEmpty ()){
returnnewLinkedList<To>();
}
To t = trans.transform (Pipes.head (list));
LinkedList<To> rest =Pipes.map (trans,Pipes.tail (list));
returnPipes.cons (t, rest);
}
/**
* Removes elements from a list if they don't fit a rule.
*
* @param pred a predicate defining the condition for keepin
g elements
* (cannot be {@code null}).
* @param list a list (cannot be {@code null}).
* @return a new list containing only those elements from the
argument
* list for which the given predicate is true.
10. * @throws NullPointerException if any argument is {@code
null}.
*/
publicstatic<T>LinkedList<T> filter (Predicate<T> pred,
LinkedList<T> list){
Objects.requireNonNull (pred,"pred cannot be null");
Objects.requireNonNull (list,"list cannot be null");
if(list.isEmpty ()){
returnnewLinkedList<T>();
}
T e =Pipes.head (list);
LinkedList<T> rest =Pipes.filter (pred,Pipes.tail (list));
if(pred.is (e)){
returnPipes.cons (e, rest);
}
return rest;
}
/**
* 'Boils down' a list into a single value using the given comb
ination rule.
*
* @param start an initial value (cannot be {@code null}).
* @param red a combination rule for combining elements in
the list
* (cannot be {@code null}).
* @param list a list (cannot be {@code null}).
* @return a 'boiled-
down' value based on combining all list elements and
* the starting value in the same order as the argument list.
* @throws NullPointerException if any argument is {@code
null}.
*/
publicstatic<From,To>To reduce (To start,
Reducer<From,To> red,
LinkedList<From> list){
11. Objects.requireNonNull (start,"start cannot be null.");
Objects.requireNonNull (red,"red cannot be null.");
Objects.requireNonNull (list,"list cannot be null.");
if(list.isEmpty ()){
return start;
}
To combined = red.combine(start,Pipes.head (list));
returnPipes.reduce (combined, red,Pipes.tail (list));
}
// Private stuff
// disable constructor
privatePipes(){}
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._Pipes.
java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Predicate.java03/A2/
src/nz/retro_freedom/dsa2016/pipes/Predicate.javapackage nz.re
tro_freedom.dsa2016.pipes;
/**
*
* @version 1.0
*
* An interface for defining predicates (functions which return t
rue or
* false).
*/
publicinterfacePredicate<T>{
/**
* The core driver of this interface.
12. *
* @param in the input element.
* @return true if the element meets the conditions of the pre
dicate.
*/
publicboolean is (T in);
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._Predi
cate.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Reducer.java03/A2/s
rc/nz/retro_freedom/dsa2016/pipes/Reducer.javapackage nz.retr
o_freedom.dsa2016.pipes;
/**
*
* @version 1.0
*
* An interface for defining reducers (functions which take two
items and
* combine them into one).
*/
publicinterfaceReducer<From,To>{
/**
* The core driver of this interface.
*
* @param x the first input element.
* @param y the second input element.
* @return the combination of x and y.
*/
publicTo combine (To x,From y);
19. /**
* The type of flower being represented.
*/
publicfinalFlowerType type;
/**
* Makes a Flower from its representation as a row in a CSV
file.
*
* @param row a CSV file row for a flower (cannot be {@co
de null} or
* empty).
* @return a new Flower from the given data.
* @throws NullPointerException if row is {@code null}.
* @throws IllegalArgumentException if row is empty.
*/
publicstaticFlower fromCSVRow (String row){
Objects.requireNonNull (row,"row cannot be null.");
requireNonEmpty (row,"row cannot be empty.");
try{
String[] divided = row.split (",");
double sepalLength =Double.parseDouble (divided [0]);
double sepalWidth =Double.parseDouble (divided [1]);
double petalLength =Double.parseDouble (divided [2]);
double petalWidth =Double.parseDouble (divided [3]);
FlowerType type =FlowerType.fromString (divided [4]);
returnnewFlower(sepalLength,
sepalWidth,
petalLength,
petalWidth,
type);
}
catch(NumberFormatException e){
thrownewIllegalArgumentException("Could not parse a number
"+
"from this row: "+ row);
20. }
}
/**
* Compares by all sepal and petal parameters and type.
*
* @param o a thing to compare to.
* @return true if the sepal and petal parameters, and type, m
atch exactly.
*/
@Override
publicboolean equals (Object o){
if(o instanceofFlower){
Flower f =(Flower)o;
if(sepalLength == f.sepalLength &&
sepalWidth == f.sepalWidth &&
petalLength == f.petalLength &&
petalWidth == f.petalWidth){
returntrue;
}
}
returnfalse;
}
/**
* Hashes on sepal and petal parameters, and type.
*
* @return a unique code for the given aspects of the Flower.
*/
@Override
publicint hashCode (){
int result =1;
result =37* result + doubleHash (sepalLength);
result =37* result + doubleHash (sepalWidth);
result =37* result + doubleHash (petalLength);
result =37* result + doubleHash (petalWidth);
21. result =37* result + type.hashCode ();
return result;
}
/**
* Emits a CSV representation, which can be read back in wit
h
* fromCSVRow.
*
* @return a CSV row representing this Flower.
*/
@Override
publicString toString (){
StringBuilder sb =newStringBuilder();
sb.append (sepalLength);
sb.append (",");
sb.append (sepalWidth);
sb.append (",");
sb.append (petalLength);
sb.append (",");
sb.append (petalWidth);
sb.append (",");
sb.append (type.toString ());
return sb.toString ();
}
// Private stuff
// private constructor for safety
privateFlower(double sepalLength,
double sepalWidth,
double petalLength,
double petalWidth,
FlowerType type){
this.sepalLength = sepalLength;
this.sepalWidth = sepalWidth;
22. this.petalLength = petalLength;
this.petalWidth = petalWidth;
this.type = type;
}
// Helper for emptiness checking
privatestaticvoid requireNonEmpty (String s,String message){
if(s.length ()==0){
thrownewIllegalArgumentException(message);
}
}
// Helper for hashCode
privatestaticint doubleHash (double d){
long f =Double.doubleToLongBits (d);
return(int)(f ^(f >>>32));
}
}
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/analysis/._Flo
wer.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/FlowerType.java0
3/A3/src/nz/retro_freedom/dsa2016/analysis/FlowerType.javapa
ckage nz.retro_freedom.dsa2016.analysis;
import java.util.*;
/**
* @author
* @version 1.0
*
* The types of flowers in the Iris data set.
*/
public enum FlowerType{
23. SETOSA ("Iris-setosa"),
VERSICOLOUR ("Iris-versicolor"),
VIRGINICA ("Iris-virginica");
/**
* Emits the name of the flower type, as in the sample data se
t.
* @return a representation of this flower type, matching the
way it is
* shown in the sample data set.
*/
@Override
publicString toString (){return rep;}
/**
* Makes a FlowerType from its representation in the sample
data file.
* @param s the textual representation of the type (as per sa
mple data).
* Cannot be {@code null} or empty.
* @return the FlowerType represented by this data.
* @throws NullPointerException if the argument is {@code
null}.
* @throws IllegalArgumentException if the argument is emp
ty.
*/
publicstaticFlowerType fromString (String s){
Objects.requireNonNull (s,"Cannot parse a FlowerType from nu
ll");
if(s.length ()==0){
thrownewIllegalArgumentException("Cannot parse a FlowerTyp
e "+
"from an empty string");
}
switch(s){
24. case"Iris-setosa":return SETOSA;
case"Iris-versicolor":return VERSICOLOUR;
case"Iris-virginica":return VIRGINICA;
default:thrownewIllegalArgumentException("Not a valid Flower
Type "+
s);
}
}
// Private stuff
privatefinalString rep;
// private constructor to take rep into account
privateFlowerType(String s){ rep = s;}
}
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/analysis/._Flo
werType.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/Main.java03/A3/s
rc/nz/retro_freedom/dsa2016/analysis/Main.javapackage nz.retr
o_freedom.dsa2016.analysis;
import java.util.*;
import java.io.*;
import nz.retro_freedom.dsa2016.pipes.*;
/**
* @author
* @version 1.0
*
* Entry point to this program.
*/
publicclassMain{
25. publicstaticvoid main (String[] args){
if(args.length ==0){
System.out.printf ("ERROR: Must give a path to a data file.n");
}
try{
LinkedList<String> data = readAll (args[0]);
LinkedList<Flower> flowers =Pipes.map
(newTransformer<String,Flower>(){
@Override
publicFlower transform (String in){
returnFlower.fromCSVRow (in);
}
}, data);
// YOUR CODE BEGINS HERE
// Use this as an example to start from
int len =Pipes.reduce
(0,
newReducer<Flower,Integer>(){
@Override
publicInteger combine (Integer x,Flower y){
return x +1;
}
}, flowers);
System.out.printf ("Length of result is: %dn", len);
// YOUR CODE ENDS HERE
}
catch(FileNotFoundException e){
System.out.printf ("ERROR: The given file does not exist.n");
}
}
// Private stuff
// disable constructor
privateMain(){}
27. Aims
• Practice using pipeline processing
1 Using pipes
(a) You will work on file A3.
(b) Go to The UC Irvine Machine Learning Database page for
the Iris data set. Read the page
– you find it in file (A3 > data)
– this is the data set you will be processing in this lab.
(c) Incorporate your work on pipes (both the Pair class and the
contents of the pipes folder
they are in file (A2 > src > nz > retro_freedom > dsa2016 ) into
the project. Make sure
that the Ant targets still work.
(d) Using the Javadocs for the code you have in analysis, find
answers to the following ques-
tions. Do not use any methods from LinkedList to do these
tasks.
(i) How many flowers of each kind are there in the data set?
(ii) How many iris setosa with a sepal width of less than 5.0 are
there in the data set?
(iii) How many iris versicolour with a petal length of more than
4.2 are there in the data
set?
(iv) How many iris virginica with a petal width of between 1.5
and 2.0 inclusive are there
28. in the data set?
(v) What are the minimum and maximum values of each of petal
length, petal width, sepal
length and sepal width in the data set? Hint: Consider using the
Pair class for this
task.
(vi) Which kind of flower in the data set has the smallest petal
length? Petal width? Sepal
length? Sepal width?
(vii) What is the average petal length for each kind of flower in
the data set? Average petal
width? Average sepal length? Average sepal length?
(viii) Which kind of flower has the largest petals? Use petal
length times petal width to
determine petal size.
(ix) Which kind of flower has the largest sepals? Use sepal
length times sepal width to
determine sepal size.
1
__MACOSX/03/._read me.pdf
__MACOSX/._03