インターフェイスを実装し、ArrayとListの2つの異なるクラスで試してみる必要があります。我々はインターフェイスに見られるように、異なる方法を追加する必要があり、これらのクラスでは - >インターフェイスを実装し、ArrayとListで試す -
public interface IStatistics {
public void addSample(String name, GregorianCalendar date, double value);
public double getMeanTemperature(int month);
public double getMedianTemperature(int month);
public double getVariance(int month);
public double getStandardDeviation(int month);
public void printSamples(int month);
}
私はすでにそれのほとんどをやったが、それは唯一の両方の方法では動作しない特定の月からの値を使用する必要があります。この練習では、1年間(名前、日付、気温)毎日一定の価値が得られます。私はそれを1ヶ月の値だけを使うようにする方法を知らない(両方のクラスではうまくいかない)。リストではすでに試してみたが、全く動かない。
@Override
public double getMeanTemperature(int month) {
MeasurementValue m = this.head;
double result = 0;
while(m.date.get(GregorianCalendar.MONTH)==month){
while(m!=null){
result = result + m.temp;
m=m.getNext();
}
result = result/this.num;
}
return result;
}
そして、配列を持つクラスでは、起動方法がわかりません。
@Override
public double getMeanTemperature(int month) {
double sum = 0.0;
for(double a : values)
sum += a;
return sum/numValues;
}
また、日付が正しく印刷されません。たぶん、誰もがその解決策を知っているかもしれません。
MeasurementValuepublic void output(){
GregorianCalendar date = this.date;
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
System.out.print(sdf.format(date.getTime()));
}
そしてMeasurementListで出力するコードで出力するコードです
import java.util.GregorianCalendar;
import kwm.statistics.IStatistics;
import kwm.statistics.MeasurementArray;
import kwm.statistics.MeasurementList;
public class TestStatistics {
public static void main(String[] args) {
GregorianCalendar date = new GregorianCalendar(2017,05,12);
GregorianCalendar date1 = new GregorianCalendar(2017,05,02);
GregorianCalendar date2 = new GregorianCalendar(2017,05,22);
GregorianCalendar date3 = new GregorianCalendar(2017,06,06);
GregorianCalendar date4 = new GregorianCalendar(2017,05,18);
// ***TESTLIST***
IStatistics statistics1 = new MeasurementList();
statistics1.addSample("Lisa", date, 12);
statistics1.addSample("Lisa", date1, 17);
statistics1.addSample("Lisa", date2, 14);
statistics1.printSamples(5);
// ***TESTARRAY***
IStatistics statistics2 = new MeasurementArray();
statistics2.addSample("Lisa", date, 12);
statistics2.addSample("Lisa", date1, 5);
statistics2.addSample("Lisa", date2, 40);
statistics2.addSample("Lisa", date3, 31);
statistics2.addSample("Lisa", date4, 23);
statistics2.printSamples(5);
}
}
。
public void printSamples(int month) {
MeasurementValue m = this.head;
// while(m.date.get(GregorianCalendar.MONTH)==month){
while(m != null){
if(month>=0 && 11>=month){
System.out.println("Name: "+m.getName());
System.out.println("Value: "+m.getTemp());
System.out.print("Date: ");m.output();
System.out.print("\n");
System.out.print("*****************\n\n");
m = m.getNext();
}
}
// }
System.out.print("\n");
}
ありがとうございました!リンクリストのバージョンについては
コードが多すぎます。 [MCVE](https://stackoverflow.com/help/mcve)を作成してください。 ** minimal **を重視しています。 – Turing85
ここに誰もコードの壁を読むつもりはありません。 – OldProgrammer
またはそれをデバッグしてください。 – redFIVE