Reputation: 607
I am creating a program that calculates rainfall for the year etc. I had the first block of code below working with the user input, perfectly. However, I am trying to change the program now, so that the array values are specified (I'm basically trying to eliminate the user input).
Why isn't the second block of code working? I am getting errors at the bottom for r.getTotalRainFall, r.getAverageRainFall etc.
Please note that I had to introduce the array thisYear (this is required).
CODE BLOCK #1:
import java.util.*;
public class Rainfall {
Scanner in = new Scanner(System.in);
int month = 12;
double total = 0;
double average;
double months[];
public Rainfall() {
months = new double[12];
}
public void enterMonthData() {
for (int n = 1; n <= month; n++) {
System.out.print("Enter the rainfall (in inches) for month #" + n + ": ");
months[n - 1] = in.nextDouble();
// Input Validation - Cannot accept a negative number
while (months[n - 1] < 0) {
System.out.print("Rainfall must be at least 0. Please enter a new value.");
months[n - 1] = in.nextDouble();
}
}
}
public double getTotalRainFall() {
total = 0;
for (int i = 0; i < 12; i++) {
total = total + months[i];
}
return total;
}
public double getAverageRainFall() {
average = total / 12;
return average;
}
/**
* Returns the index of the month with the highest rainfall.
*/
public int getHighestMonth() {
int highest = 0;
for (int i = 0; i < 12; i++) {
if (months[i] > months[highest]) {
highest = i;
}
}
return highest;
}
/**
* Returns the index of the month with the lowest rainfall.
*/
public int getLowestMonth() {
int lowest = 0;
for (int i = 0; i < 12; i++) {
if (months[i] < months[lowest]) {
lowest = i;
}
}
return lowest;
}
public static void main(String[]args) {
Rainfall r = new Rainfall();
r.enterMonthData();
System.out.println("The total rainfall for this year is " + r.getTotalRainFall());
System.out.println("The average rainfall for this year is " + r.getAverageRainFall());
int lowest = r.getLowestMonth();
int highest = r.getHighestMonth();
System.out.println("The month with the highest amount of rain is " + (highest+1) + " with " + r.months[highest] + " inches");
System.out.println("The month with the lowest amount of rain is " + (lowest+1) + " with " + r.months[lowest] + " inches");
}
}
CODE BLOCK #2:
package rain;
public class Rain {
int month = 12;
double total = 0;
double average;
double getRainAt[];
public Rain {
getRainAt = new double[12];
}
double getTotalRainFall() {
total = 0;
for (int i = 0; i < 12; i++) {
total = total + getRainAt[i];
}
return total;
}
double getAverageRainFall() {
average = total / 12;
return average;
}
int getHighestMonth() {
int high = 0;
for (int i = 0; i < 12; i++) {
if (getRainAt[i] > getRainAt[high]) {
high = i;
}
}
return high;
}
int getLowestMonth() {
int low = 0;
for (int i = 0; i < 12; i++) {
if (getRainAt[i] < getRainAt[low]) {
low = i;
}
}
return low;
}
public static void main(String[] args) {
// Create an array of rainfall figures.
double[] thisYear = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7,
3.9, 2.6, 2.9, 4.3, 2.4, 3.7 };
int high; // The high month
int low; // The low month
// Create a RainFall object initialized with the figures
// stored in the thisYear array.
Rainfall r = new Rainfall(thisYear);
// Display the statistics.
System.out.println("The total rainfall for this year is " +
r.getTotalRainFall();
System.out.println("The average rainfall for this year is " +
r.getAverageRainFall());
high = r.getHighestMonth();
System.out.println("The month with the highest amount of rain " +
"is " + (high+1) + " with " + r.getRainAt(high) +
" inches.");
low = r.getLowestMonth();
System.out.println("The month with the lowest amount of rain " +
"is " + (low+1) + " with " + r.getRainAt(low) +
" inches.");
}
}
}
Upvotes: 0
Views: 1338
Reputation: 564
Not sure why you are creating a getRainAt class just to initialise it, try using the Rain class constructor to do this.
Replace this:
public class getRainAt {
public getRainAt() {
getRainAt = new double[12];
}
}
With:
public Rain() {
getRainAt = new double[12];
}
and since you are using Rain instead of Rainfall now, in the main method it should be: Rain r = new Rain();
Upvotes: 1
Reputation: 4886
I have re-factored the 2 classes
now Rain class only contains the main method whereas all the other logic is contained in the Rainfall class
Rainfall class has a method - getRainAt() to get rain base don the given month in Rainfall class has a constructor that takes a double array as an argument so it has to be instantiated with this argument provided.
take a look at the classes now and see if this fits your requirement.
import java.util.*;
public class Rainfall {
Scanner in = new Scanner(System.in);
int month = 12;
double total = 0;
double average;
double months[];
public Rainfall(double newmonths[]){
months = newmonths;
}
public void enterMonthData() {
for (int n = 1; n <= month; n++) {
System.out.print("Enter the rainfall (in inches) for month #" + n
+ ": ");
months[n - 1] = in.nextDouble();
// Input Validation - Cannot accept a negative number
while (months[n - 1] < 0) {
System.out
.print("Rainfall must be at least 0. Please enter a new value.");
months[n - 1] = in.nextDouble();
}
}
}
public double getTotalRainFall() {
total = 0;
for (int i = 0; i < 12; i++) {
total = total + months[i];
}
return total;
}
public double getAverageRainFall() {
average = total / 12;
return average;
}
/**
* get rain given the month number
*/
public double getRainAt(int month){
double rainValue = 0;
for (int i = 0; i < months.length; i++) {
if(month == i){
rainValue = months[i];
break;
}
}
return rainValue;
}
/**
* Returns the index of the month with the highest rainfall.
*/
public int getHighestMonth() {
int highest = 0;
for (int i = 0; i < 12; i++) {
if (months[i] > months[highest]) {
highest = i;
}
}
return highest;
}
/**
* Returns the index of the month with the lowest rainfall.
*/
public int getLowestMonth() {
int lowest = 0;
for (int i = 0; i < 12; i++) {
if (months[i] < months[lowest]) {
lowest = i;
}
}
return lowest;
}
}
Rain class only has the main method now
public class Rain {
public static void main(String[] args) {
// Create an array of rainfall figures.
double[] thisYear = { 1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3,
2.4, 3.7 };
int high; // The high month
int low; // The low month
// Create a RainFall object initialized with the figures
// stored in the thisYear array.
Rainfall r = new Rainfall(thisYear);
// Display the statistics.
System.out.println("The total rainfall for this year is "
+ r.getTotalRainFall());
System.out.println("The average rainfall for this year is "
+ r.getAverageRainFall());
high = r.getHighestMonth();
System.out.println("The month with the highest amount of rain " + "is "
+ (high + 1) + " with " + r.getRainAt(high) + " inches.");
low = r.getLowestMonth();
System.out.println("The month with the lowest amount of rain " + "is "
+ (low + 1) + " with " + r.getRainAt(low) + " inches.");
}
}
hope this helps
Upvotes: 2
Reputation: 11
I'm not sure if this was just a copying error, but in the second block you've called the class Rain
, but then you declared r as Rainfall
.
Upvotes: 1