###前提・実現したいこと
かなり長いコードになると思いますよろしくお願いします。
湖の上にかかった雲の面積のデータを集計するプログラムです
まずインポートしたデータの高さに合わせる2次元配列を作りたいと思っています。
インポートするデータの幅はすべて同じですが、高さは変わります。
以下がデータの一部です。
CloudCover1.dat
2009 66.06 68.89 56.16 57.53 50.87 55.17 57.74 53.13 44.27 77.45 66.37 78.61
2010 70.58 63.21 45.45 46.40 49.61 64.50 47.65 49.35 62.07 49.35 70.03 78.35
2011 78.52 65.07 60.16 57.90 59.16 54.70 45.32 41.65 53.77 56.39 68.00 77.52
一列目はデータの年で、となりの数字は1月のデータ(%)、その隣が2月のデータ、、、12月分まで続きます。
データファイルはもう一種類あって、2000年から2010年までの分のものがあります。
また、別のjavaファイルで、データの集計をするプログラムがあります。
集計プログラムの太文字の部分は与えられているので間違いはありません。
2次元配列のプログラムは
private void grow( int row )
public Object get( int row, int column )
public void put( int row, int column, Object value )
public int getWidth( )
public int getHeight( )
public String toString( )
public Dynamic2dArray( int numRows, int numColumns )
上記のメソッドを使用することのみ制限されています。
###動的2次元配列のソースコード
import java.util.Arrays;
public class Dynamic2dArray {
private Object[][] array = null;
/** * 高さを+10追加するメソッドです */ private void grow( int row ) { Object[][]temp = new Object [array.length + 10][]; for (int i=0; i < array.length; i++){ temp[i] = array[i]; } } /** * array[row][column]の値を返すコードです *もし行が、配列の長さを超える場合はgrowメソッドで、縦の配列を増やします * @param row : 行 * @param column : 列 * @return : array[row][column]の値を返す。 */ public Object get( int row, int column ) { Object[][] temparray = new Object[array.length][array[0].length]; for (int i = 0; i < array.length; i++){ for( int j = 0 ; j < array[0].length ; j++){ temparray[i][j] = array[i][j]; } } return array; } public void put( int row, int column, Object value ) { if( row > array.length){ grow(row); } } public int getWidth( ) { return width; } public int getHeight( ) { return height; } /** * 配列を表示します */ public String toString( ) { String result = null; for (Object[] v: array){ result = "[ " + v +" ]"; } return result; } public Dynamic2dArray( int numRows, int numColumns ) { } }
}
###集計プログラム
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//先ほど作った動的2次元配列を使ってでーたの集計をします。
public class CloudCover {
private enum Month {
JANUARY, FEBRUARY, MARCH, APRIL,
MAY, JUNE, JULY, AUGUST,
SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;}
/**
* 湖の面積です
*/
** private final double SURFACE_AREA = 81925.0; //km^2 **
/**
* 先ほど作った2次元配列のプログラムを使用します。
*/
** private Dynamic2dArray array = new Dynamic2dArray( 1, 13 );**
/**
* datファイルを引用します
*/
** public void loadData( String filename ) {**
Scanner scan = null; try { scan = new Scanner( new File(filename) ); while (scan.hasNext()){ double value = scan.nextDouble( ); array.put(row, column, value); } } catch ( FileNotFoundException e ) { e.printStackTrace( ); } finally { if ( scan != null ) { scan.close(); }}
}
/**
* それぞれの月の平均
/
** public double getAve( Month month ) {*
int sum = 0;
for (int i = 0; i < 12 ; i++){
sum += Month;
}
double Month = sum/12;
return Month;
}
/**
すべてのデータの平均を出します。
/
** public double getAve( int year ) {
int sum = 0;
for (int i = 0; i < 12 ; i++){
sum += 12;
}
year = sum/12;
return year;
}
/**
* それぞれの月の一番大きい数値を出します。例えば5年分のデータがあった場合、5年間分の1月のデータで一番大きい数値を返します。
*/
** public double getMax( Month month ) {**
double[] month;
double max = month;
for (int i = 1; i < 12; i++){ if(max < month[i]){ max = month[i]; } } return month = -1;
}
/**
* その年の一番多きい数値を返します
/
** public double getMax( int year ) {*
int max = year;
for (int i = 1; i < 12; i++){ if(max < 3){ max = year; } } return max =-1;
}
/**
* 全体で一番大きい数値
*/
** public double getMax( ) {**
return max;
}
/**
* それぞれの月の一番小さい数値
*/
** public double getMin( Month month ) {**
Month min = month;
for (int i = 1; i < 12; i++){ if(min > 12){ min = month; }
}
return month= -1;
}
/**
*その年の一番小さい数値
*/
** public double getMin( int year ) {**
int min = year;
for (int i = 1; i < 12; i++){ if(min > 12){ min = year; }
}
return year= -1;
}
/**
* すべてのデータの一番小さい数値
/
**
** public double getMin( ) {***
return min;
}
/**
* 湖の上に雲がかかっていたパーセンテージを面積に変換
*/
** public double coverageToArea ( double coverage ) {**
coverage = 82,100 * %;
return coverage;
}
/**
* データファイルに書かれている最初の年
/
** public int beginYear( ) {*
return 0;
}
/**
* 最後の年
*/
** public int endYear( ) {**
return 0;
}
public static void main ( String [ ] args ) {
CloudCover self = new CloudCover( );
// 表示させたい月を選びます
Month month = Month.MARCH;
//表示させたい年を選びます.
int year = 2010;
** // データのロード self.loadData( "CloudCover1.dat" ); **System.out.printf( "平均 %s 値:\t%.2f\n", month, self.getAve( month ) ); System.out.printf( "最大値 %s 値:\t%.2f\n", month, self.getMax( month ) ); System.out.printf( "最小値 %s 値:\t%.2f\n", month, self.getMin( month ) ); System.out.println( );** **System.out.printf( "平均 %d 値:\t%.2f\n", year, self.getAve( year ) ); System.out.printf( "最大値 %d 値:\t%.2f\n", year, self.getMax( year ) ); System.out.printf( "最小値 %d 値:\t%.2f\n", year, self.getMin( year ) ); System.out.println( ); ** **System.out.printf( "全体平均:\t%.2f\n", self.getAve( ) ); System.out.printf( "全体最大値:\t%.2f\n", self.getMax( ) ); System.out.printf( "全体最小値:\t%.2f\n", self.getMin( ) ); System.out.println( );** **System.out.printf( "平均面積:\t%,.2f\n", self.coverageToArea( self.getAve( ) ) ); System.out.println( );;** **System.out.printf( "開始年\t%d\n", self.beginYear( ) ); System.out.printf( "終了年\t%d\n", self.endYear( ) ); System.out.println( );**
}
}
お恥ずかしい話ですがかなりの初心者でセンスもないため授業にもついていけず、先生に質問したのですが見放されてしまいました。
ショックと恥ずかしさででほかの人にも聞けなくなってしまったので、こちらで質問させていただきます。
ほぼゼロに近いコードなのは招致しておりますが、お力添えをいただけるととてもうれしいです…。
せめて2次元配列でも動かしたいのですが、配列の表示ができていないので、思っているように動いているかどうかもわかりません。 似たようなサンプルコードや、ウェブサイトなど、わずかでもヒントになるような情報があれば十分ありがたいです。
###補足情報(言語/FW/ツール等のバージョンなど)
java, Eclipse