Reputation: 3
I have a project where I have four methods. The 2nd and 3rd methods (with, and without respectively) both have arrays in them where the user inputs data.
An example of the 2nd method is below. The 3rd is much like it
public static void billWithoutGardens()
{
Scanner input = new Scanner(System.in);
double withoutGardens[] = new double[12];
System.out.println("Please enter energy bills for year without rooftop gardens");
// 12 part array, one for each month
System.out.print("Please enter energy bill for January: ");
withoutGardens [0] = input.nextDouble();
System.out.print("Please enter energy bill for February: ");
withoutGardens [1] = input.nextDouble();
System.out.print("Please enter energy bill for March: ");
withoutGardens [2] = input.nextDouble();
System.out.print("Please enter energy bill for April: ");
withoutGardens [3] = input.nextDouble();
System.out.print("Please enter energy bill for May: ");
withoutGardens [4] = input.nextDouble();
System.out.print("Please enter energy bill for June: ");
withoutGardens [5] = input.nextDouble();
System.out.print("Please enter energy bill for July: ");
withoutGardens [6] = input.nextDouble();
System.out.print("Please enter energy bill for August: ");
withoutGardens [7] = input.nextDouble();
System.out.print("Please enter energy bill for September: ");
withoutGardens [8] = input.nextDouble();
System.out.print("Please enter energy bill for October: ");
withoutGardens [9] = input.nextDouble();
System.out.print("Please enter energy bill for November: ");
withoutGardens [10] = input.nextDouble();
System.out.print("Please enter energy bill for December: ");
withoutGardens [11] = input.nextDouble();
System.out.println(Arrays.toString(withoutGardens));
return;
I am trying to use the 4th method to calculate the difference between each of the elements of the 2 arrays. I do not know how to do this and after much searching around I am turning to the good folks at Stackoverflow.
the 4th method is as such
public static void calculateSavings(double withoutGardens[], double withGardens[])
{
}
Is that the correct way to start out that method in order to achieve my goal of calculating difference?
EDIT - how would I call the 4th method back into the main?
calculateSavings(withoutGardens[], withGardens[]);
that is how I have it as of now and I get errors on it that it "cannot be resolved to a variable".
Upvotes: 0
Views: 90
Reputation: 303
I believe I can help answer your question.
Before I answer the question, let me point out that instead of the following parameters:
double withoutGardens[], double withGardens[]
You should instead use:
double[] withoutGardens, double[] withGardens
To ensure that the type of the parameter is an array, and the brackets aren't just in the name.
Now, to determine the difference between each of the elements, you must loop through them. This can be done with a simple for loop or with a foreach loop. In my opinion, a for loop would be easier than a foreach loop since you're using 2 arrays, and a for loop can give you an integer for an index to use for both. If you used a foreach loop, you would have to use a method to hunt down the current index.
With a for loop, it would look something like this:
public static double[] calculateSavings(double[] withGardens, double[] withoutGardens) {
double[] difference = new double[];
for (int i = 0; i < withGardens.size(); i++) {
difference[i] = withGardens[i] - withoutGardens[i];
}
return difference;
}
I hope this helps!
Upvotes: 0
Reputation: 8598
I know an answer has already been accepted, still, there are couple of things I'd like to mention, that you/others might benefit from. First, let billWithoutGardens() return an array of doubles that the user has entered, so let's change it's signature to:
public static double[] billWithoutGardens() {
}
Next, you can greatly simplify data entry by using a loop in your method, eg.:
//an array of months
String[] months = {"January", "February", "March", "April", "May", "June", "July", "September", "October", "November", "December"};
System.out.println("Please enter energy bills for year without rooftop gardens");
//loop through the months and store the values in your array
for(int i=0; i<months.length; i++) {
System.out.print("Please enter energy bill for " + months[i] + ": ");
withoutGardens[i] = input.nextDouble();
//!!!!!!!!!!!!!
//also remember to skip newline character added when user pressed ENTER
//you can consume it with this call, which will read newline character
input.nextLine();
}
Then, return the array with the values by adding this as the last line in your method:
return withoutGardens;
Next, to calculate savings, let's also return an array in calculateSavings(), so change it's signature to:
public static double[] calculateSavings(double[] withoutGardens, double[] withGardens)
And to calculate the difference, create an array that will hold the difference for each month, and loop through the months:
double[] difference = new double[withoutGardens.length];
for (int i=0; i<difference.length; i++) {
difference[i] = withoutGardens[i] - withGardens[i];
}
At the end, return the array:
return difference;
In similar fashion, you can rewrite your other methods. Then, to use them in main(), you can have the following:
public static void main(String[] args) {
double[] wGardens = billWithGardens();
double[] woGardens = billWithoutGardens();
double[] savings = calculateSavings(woGardens, wGardens);
System.out.println(Arrays.toString(savings));
}
Upvotes: 0
Reputation: 1
double[] diff = new double[12];
for(int i = 0; i < 12; i++){
diff[i] = withoutGardens[i] - withGardens[i];
}
return diff;
Upvotes: 0
Reputation: 11137
Yes, you are correct you have to pass the 2 array to the forth function and you can return the result array to
public static double[] calculateSavings(double withoutGardens[], double withGardens[])
{
double difference_array[withoutGardens.length()];
//.....some code goes here for calculations
return difference_array;
}
Upvotes: 0
Reputation: 3822
Create a new array the same size as withoutGardens
, then do a loop and set the elements of the new array to the difference of the elements of withoutGardens
and withGardens
.
Also in your methods you want to return the created arrays, i.e. withoutGardens
, as it is a local variable inside billWithoutGardens()
and is not available outside that method.
Change the method signature to:
public static double[] billWithoutGardens()
and the last line to:
return withoutGardens;
Upvotes: 2