I am trying to implement the strategy pattern to find the middle point of arraylists that hold different types of numerical data including paired numerical data(coordinates). I am having some difficulty 1) with the pairs and 2) my strategy method 'findMiddle' is not implementing. How exactly do I implement the strategy pattern and how do I work in Java using pairs?
package Strat;
import java.util.List;
import java.util.ArrayList
import java.util.Arrays;
public class Strat {
public static void main(String[] args) {
IntegerData intdata = new IntegerData(20); //adds and removes elements
FloatData floatdata = new FloatData(3.1415f); //adds and removes elements
coordinates pair = new coordinates(3.4, 5.2); //adds and removes elements
intdata.insertInt(30);
intdata.insertInt(57);
intdata.insertInt(22);
intdata.removeInt(57);
//intdata.findMiddle(new midInt());
//floatdata.findMiddle(new midFlo());
floatdata.insertFlo(5.55f);
floatdata.insertFlo(6.14f);
floatdata.removeFlo(5.42f);
pair.insertP(5.0, 4.7);
pair.insertP(2.4, 1.3);
pair.removeP(4.1, 4.7);
}
}
class coordinates {
public class Pair<X,Y> {
private final X x;
private final Y y;
public Pair(X x, Y y){
this.x = x;
this.y = y;
}
}
List<Pair<Double,Double>> paired;
public coordinates(double x, double y){
this.paired = new ArrayList<>();
}
public void insertP(double newdub, double newdub2){
Pair<Double,Double> pair = new Pair<>(newdub, newdub2);
paired.add(pair);
System.out.println(pair + " added to Coordinate ArrayList");
}
public void removeP(double newdub, double newdub2){
Pair<Double, Double> pair = new Pair<>(newdub, newdub2);
paired.removeAll(Arrays.asList(pair));
System.out.println(pair + " removed from Coordinate ArrayList");
}
}
interface Middle{
public void findMiddle();
}
class midInt implements Middle{
@Override
public void findMiddle(){
List Int = new ArrayList<> ();
int middle = (Int.size()/2+1);
System.out.println(middle + " is the middle element in the Int "
+ "ArrayList");
}
}
class midFlo implements Middle{
@Override
public void findMiddle(){
List Flo = new ArrayList<> ();
float middle = (Flo.size()/2+1);
System.out.println(middle + " is the middle element in the Float"
+ "ArrayList");
}
}
class midCoor implements Middle {
@Override
public void findMiddle(){
List pair = new ArrayList<>();
double middle = (double) pair.get((pair.size()/2)+1);
System.out.println(middle);
}
}
When it comes to the Strategy Pattern, I get a 'cannot find symbol' error for the lines that say
intdata.findMiddle(new midInt());
floatdata.findMiddle(new midFlo());
And when it comes to the pairs, the main method outputs this...
30 added to Integer ArrayList
57 added to Integer ArrayList
22 added to Integer ArrayList
57 removed from Integer ArrayList
5.55 added to Float ArrayList
6.14 added to Float ArrayList
5.42 not in Float ArrayList
strat.coordinates$Pair@2a139a55 removed from Coordinate ArrayList
What is going on in this last line of output?
Aucun commentaire:
Enregistrer un commentaire