前提・実現したいこと
まず、DatasetObj.java を書きました。それから、Scannerクラスを使って、LinReg.java を書いて、DatasetObj.java の #bestFit と、#rsq と、#meanDistance を表示したいのですが、どのようにすればいいのでしょうか。DatasetObj と LinReg をどうやって結びつけるのかよく分かりません。
DatasetObj
Java
1public class DatasetObj implements Dataset { 2 3 private DPointObj[] a; 4 5 public DatasetObj(DPointObj[] a) { 6 if (a == null) { 7 throw new IllegalArgumentException(); 8 } 9 for (int i = 0; i < a.length; i++) { 10 if (a[i] == null) { 11 throw new IllegalArgumentException(); 12 } 13 } 14 this.a = a; 15 } 16 17 18 public int numObs() { 19 return a.length; 20 } 21 22 23 public boolean isEmpty() { 24 return this.numObs() == 0; 25 } 26 27 28 public double sumX() { 29 double sum = 0; 30 for (int i = 0; i < a.length; i++) { 31 sum += a[i].getX(); 32 } 33 return sum; 34 } 35 36 37 public double sumY() { 38 double sum = 0; 39 for (int i = 0; i < a.length; i++) { 40 sum += a[i].getY(); 41 } 42 return sum; 43 } 44 45 46 public Double meanX() { 47 if (this.isEmpty()) { 48 return null; 49 } else { 50 return this.sumX() / this.numObs(); 51 } 52 } 53 54 55 public Double meanY() { 56 if (this.isEmpty()) { 57 return null; 58 } else { 59 return this.sumY() / this.numObs(); 60 } 61 } 62 63 64 public Line bestFit(){ 65 double sumXY = 0; 66 double sumXX = 0; 67 double m = (a.length * sumXY - this.sumX() * this.sumY()) / (a.length * sumXX - this.sumX() * this.sumX()); 68 double b = (this.sumY() * sumXX - this.sumX() * sumXY) / (a.length * sumXX - this.sumX() * this.sumX()); 69 70 if (a.length <= 1) { 71 return null; 72 } else { 73 for (int i = 0; i < a.length; i++){ 74 sumXY += a[i].getX() * a[i].getY(); 75 sumXX += a[i].getX() * a[i].getX(); 76 } 77 return new NVLineObj(m, b); 78 } 79 } 80 81 82 public Double meanDistance(Line n){ 83 if (this.isEmpty() || n == null) { 84 return null; 85 } else { 86 double m = n.getM(); 87 double b = n.getB(); 88 double sum = 0; 89 for(int i = 0; i < a.length; i++){ 90 sum += Math.abs(b + m * a[i].getX() - a[i].getY()) / Math.sqrt(1 + m * m); 91 } 92 return sum / a.length; 93 } 94 } 95 96 97 public Double rsq() { 98 if (this.isEmpty()) { 99 return null; 100 } else { 101 double varX = 0.0; 102 double varY = 0.0; 103 double meanXY = a[0].getX() * a[0].getY() / this.numObs(); 104 double sigX = Math.sqrt(varX); 105 double sigY = Math.sqrt(varY); 106 double r = (meanXY - this.meanX() * this.meanY()) / (sigX * sigY); 107 for (int i = 0; i < a.length; i++) { 108 meanXY = a[i].getX() * a[i].getY() / this.numObs(); 109 varX += a[i].getX() * a[i].getX() - this.meanX() * this.meanX(); 110 varY += a[i].getY() * a[i].getY() - this.meanY() * this.meanY(); 111 } 112 return r * r; 113 } 114 } 115 116 117}
LinReg
Java
1import java.util.Scanner; 2 3public class LinReg { 4 5 public static void main(String[] args) { 6 Scanner scn = new Scanner(System.in); 7 while (scn.hasNext()) { 8 String line = scn.nextLine();
LinReg.java を提示してください
すいません、提示はしたのですが、よく分からなくなり、途中であきらめてしまっている状態です。