Version 2 of my monthly rainfall program, with edits and suggestions applied. Thanks so much for your help it really made me think. Wonderful thing about programming is how complete strangers can come together to solve problems.
// Rainfall Class Start
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Arrays;
/**
*
* @author Adrian
*/
public class Rainfall{
DecimalFormat twoDecimals = new DecimalFormat("##0.00");
private final String[] Months={"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};;
private final double[] Rainfall;
public Rainfall() throws IOException
{
Rainfall = new double[12];
}
public double getAverageRainFall()
{
return getTotalRainFall() / 12 ;
}
public void outputToFile()throws IOException
{
try (PrintWriter outputFile = new PrintWriter("Months.txt")) {
Scanner kb=new Scanner(System.in);
System.out.println("For each month enter a rainfall amount");
for (int i=0;i<12;i++)
{
System.out.println(Months[i]+"?");
Rainfall[i]=kb.nextDouble();
outputFile.println(Months[i]+" "+Rainfall[i]);
System.out.println(Months[i]+" "+Rainfall[i]);
}
}
}
/**
*
* @return
*/
public double getTotalRainFall()
{
double total = 0.0; // Accumulator initialized to 0
// Accumulate the sum of the rain array elements.
for (int i = 0; i < Rainfall.length; i++)
total += Rainfall[i];
// Return the sum.
return total;
}
public double getLowestMonth()
{
int lowest = 0;
// Find the element with the lowest value.
for (int i = 1; i < Rainfall.length; i++)
{
if (Rainfall[i] < Rainfall[lowest])
lowest = i;
}
// Return the element number.
return lowest+1;
}
public int getHighestMonth()
{
int highest = 0;
// Find the element with the highest value.
for (int i = 1; i < Rainfall.length; i++)
{
if (Rainfall[i] > Rainfall[highest])
highest = i;
}
// Return the element number.
return highest;
}
public void displayMessage(){
System.out.println("----------------------");
System.out.println("Monthly Rainfall Totals For 2014");
System.out.println(Arrays.toString(Months));
System.out.println(Arrays.toString(Rainfall));
System.out.println("Average Rainfall = " +getAverageRainFall());
System.out.println("Total Rainfall = " +getTotalRainFall());
System.out.println("Highest Month = " +getHighestMonth());
System.out.println("Lowest Month = " +getLowestMonth());
System.out.println("----------------------");
}
public double getRainAt(int e)
{
return Rainfall[e];
}
}
/*public void inputFromFile() throws IOException
{
File myFile=new File ("Months.txt");
Scanner inputFile=new Scanner(myFile);
//loop through the file
for(int i=0;i<12;i++){
Months[12] = inputFile.nextLine();
inputFile.close();
}
}*/
// End Class // Start of Main Method
import java.io.*; import java.text.DecimalFormat;
public class Rainfall2{
public static void main(String[] args) throws IOException
{
// The low month
DecimalFormat twoDecimals = new DecimalFormat("##0.00");
Rainfall myData=new Rainfall();
myData.outputToFile();
myData.displayMessage();
}
}
Version 2 of my monthly rainfall program, with edits and suggestions applied. Thanks so much for your help it really made me think. Wonderful thing about programming is how complete strangers can come together to solve problems.