Witam, piszę trochę z nietypowym problem, niestety dość mocno "kłującym" mnie w oczy, przez co nie mogę przejść obok niego obojętnie. Otóż mam do napisania program liczący szereg parametrów dla danego zbioru którym jest zbiór irysów. Wyliczeń mam dokonać dla poszczególnych cech (długość, szerokość itd) dla wszystkich gatunków oraz dla każdego z osobna. Oto przykładowy kod, który obecnie z bólem serca umieszczam, bo strasznie mi za niego wstyd, w poniższej liście przechowuje wszystkie obiekty irysów wczytane z pliku
ArrayList<Iris> irisData;
a to deklaracja klasy Iris:
public class Iris {
private double sepalLength;
private double sepalWidth;
private double petalLength;
private double petalWidth;
private String irisClass;
public Iris(double sepL, double sepW, double petL, double petW, String iriC) {
this.sepalLength = sepL;
this.sepalWidth = sepW;
this.petalLength = petL;
this.petalWidth = petW;
this.irisClass = iriC;
}
}
Poniżej umieszczam kod przykładowej metody liczącej średnią arytmetyczną, jak widać jest tu powtarzanie kodu na praktycznie każdym miejscu:
public ArrayList<Double> getSepalWidthsOfSpiece(String spiece) {
ArrayList<Double> result= new ArrayList<>();
for(Iris iris : irisData){
if(spiece.equals(iris.getIrisClass())){
result.add(iris.getSepalWidth());
}
}
return result;
}
public ArrayList<Double> getSepalLengthsOfSpiece(String spiece) {
ArrayList<Double> result= new ArrayList<>();
for(Iris iris : irisData){
if(spiece.equals(iris.getIrisClass())){
result.add(iris.getSepalLength());
}
}
return result;
}
public ArrayList<Double> getPetalLengthsOfSpiece(String spiece) {
ArrayList<Double> result= new ArrayList<>();
for(Iris iris : irisData){
if(spiece.equals(iris.getIrisClass())){
result.add(iris.getPetalLength());
}
}
return result;
}
public ArrayList<Double> getPetalWidthsOfSpiece(String spiece) {
ArrayList<Double> result= new ArrayList<>();
for(Iris iris : irisData){
if(spiece.equals(iris.getIrisClass())){
result.add(iris.getPetalWidth());
}
}
return result;
}
public void arithmeticAvarageForAll() {
double resultSepalL = 0;
double resultSepalW = 0;
double resulPetalL = 0;
double resultPetalW = 0;
for(Iris iris : irisData){
resultSepalL += iris.getSepalLength();
resultSepalW += iris.getSepalWidth();
resulPetalL += iris.getPetalLength();
resultPetalW += iris.getPetalWidth();
}
System.out.println("Arithmetic avarage for each feature:" + "\nsepal length: " + resultSepalL/irisData.size() +
"\nsepal width: " + resultSepalW/irisData.size() + "\npetal length: " + resulPetalL/irisData.size() + "\npetal width: " +
resultPetalW/irisData.size() + "\n");
arithmeticAvarageForSpieces("Iris-setosa");
arithmeticAvarageForSpieces("Iris-versicolor");
arithmeticAvarageForSpieces("Iris-virginica");
}
public void arithmeticAvarageForSpieces(String spiece) {
double[] resultSepalL = getSepalLengthsOfSpiece(spiece).stream().mapToDouble(Double::doubleValue).toArray();
double[] resultSepalW = getSepalWidthsOfSpiece(spiece).stream().mapToDouble(Double::doubleValue).toArray();
double[] resulPetalL = getPetalLengthsOfSpiece(spiece).stream().mapToDouble(Double::doubleValue).toArray();
double[] resultPetalW = getPetalWidthsOfSpiece(spiece).stream().mapToDouble(Double::doubleValue).toArray();
System.out.println(spiece + "\nArithmetic avarage for each spieces:" + "\nsepal length: " + DoubleStream.of(resultSepalL).sum()/resultSepalL.length +
"\nsepal width: " + DoubleStream.of(resultSepalW).sum()/resultSepalW.length + "\npetal length: " +
DoubleStream.of(resulPetalL).sum()/resulPetalL.length + "\npetal width: " +
DoubleStream.of(resultPetalW).sum()/resultPetalW.length+ "\n");
}
Będę bardzo wdzięczny jeśli ktoś mógł podrzucić jakiś pomysł jak zminimalizować takowe operacje do minimum, gdyż nie ukrywam że cały program liczący 12 różnych wartości zajął mi prawie 700 linijek co wydaję mi się że jest okropnie złą praktyką. Pozdrawiam i dziękuję z góry za wskazówki.