Results 1 to 13 of 13
Thread: 2D Array. method problem.
 11102011, 10:39 AM #1Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
2D Array. method problem.
making one temperature class for my homework and I'm lost is outer space now
Troubble is to get the methods done as they should be now.
getAverageEachDay() shall be taking the hours at the day, add them up and divide by the hours length. That iknow i should be doing. but the troubble is to execute it. :P
Java Code:class Temperature { private String name; private double[][] temp; public Temperature(String name, double[][] temp2) { this.name = name; int day = temp2.length; int hour = temp2[0].length; temp =new double[day][hour]; for(int i=0;i<day;i++){ for(int j=0;j<hour;j++){ temp[i][j] = temp2[i][j]; } } } public int getHours() { return temp[0].length; } public int getDays(){ return temp.length; } public double[] getAverageEachDay() { int Days = getDays(); int Hours = getHours(); double[] middelTemp = new double[Days]; for (int i=0; i<Days; i++) { for (int j=0;j<Hours;j++) { middelTemp[Days] = temp[i][j]; } } middelTemp = middelTemp / Hours; return middelTemp; } }
 11102011, 11:23 AM #2Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
Re: 2D Array. method problem.
Java Code:middelTemp = middelTemp / Hours;
And, should you not also be using the "day" index (i) in the inner loop rather than using the length (Days) of the array as the index?
 11102011, 12:19 PM #3Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
How do this method look like ?
Java Code:public double[] getAverageEachDay() { double Average = 0; int Days = getDays(); int Hours = getHours(); double[] middelTemp = new double[Days]; for (int i=0; i<Hours; i++) { for (int j=0;j<Days;j++) { Average += temp[j][i]; } Average = Average / Hours; middelTemp[i] = Average; } return middelTemp; }
 11102011, 12:25 PM #4Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
Re: 2D Array. method problem.
You need to reinitiate "Average" for every iteration of the outer loop, of course, or the daily average will just get higher and higher, right?
Talk about global warming.
Edit: P.S. just as a note /= also exists (just like the += you used earlier).
 11102011, 12:35 PM #5Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
Thanks again! Global warming is just a kitten compared to what's gonna happen in the next weeks in this program :D
Java Code:public double[] getAverageEachDay() { double Average = 0; int Days = getDays(); int Hours = getHours(); double[] middelTemp = new double[Days]; for (int i=0; i<Hours; i++) { for (int j=0;j<Days;j++) { Average += temp[j][i]; } [B]Average /= Hours;[/B] middelTemp[i] = Average; [B]Average = 0;[/B] } return middelTemp; }
 11112011, 12:27 AM #6Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
Working on a new methos now. I'm gonna get each columm and then get the average of those.
I'm not sure how the for loops should be at all either.
Java Code:public double getHourAverage(){ int Days = getDays(); int Hours = getHours(); double avgHours = 0; double[] avgHours2 = new double[Days];; for(int i=0;i<Days;i++){ for(int j=0;j<Hours;j++){ } } return avgHours;
the seccond loop will take each hour in that day, witch is wrong.
I want hour[i] of all days and make the average out of that.
 11112011, 09:47 AM #7Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
Re: 2D Array. method problem.
Well, when the outer loop is days you are iterating over the rows, what do you think would happen if the outer loop used the hours (with the respective trading of which position the index is entered in).
 11112011, 10:09 AM #8Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
something like this =
Java Code:public double getHourAverage(){ int Days = getDays(); int Hours = getHours(); double avgHours = 0; double[] avgHours2 = new double[Days];; for(int i=0;i<Days;i++){ for(int j=0;j<temp[i].length;j++){ avgHours += temp[i][j]; } avgHours /=temp[i].length; avgHours2[i] = avgHours; avgHours = 0; } return avgHours; }
 11112011, 10:44 AM #9Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
Re: 2D Array. method problem.
No. You need to use "Hours" in the outer loop and "Days" in the inner loop and swap the index references in the += line. Your result array will also need to be the correct length, of course (hint, the outer loop variable as it should be).
 11112011, 01:09 PM #10Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
Perfect!
Finally closin in on the task now =)
 11112011, 01:31 PM #11Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
Last method. to make one toString() method..
Java Code:public String toString(){ String result = ""; result +=name; result +=temp; return result; }
 11112011, 02:08 PM #12Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
Re: 2D Array. method problem.
Another double loop, of course.
 11112011, 02:31 PM #13Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: 2D Array. method problem.
yeh, ofcourse. never that easy is it ? :P
Java Code:public String toString(){ String result = ""; String result2 = ""; result +=name+"\n"; int Days = getDays(); int Hours = getHours(); for (int i=0; i<Days; i++) { for (int j=0;j<Days;j++) { result2 += temp[i][j]; result2 +=" "; } result +=result2+"\n"; result2 = ""; } return result; }
Similar Threads

problem with using a method's object/variable in another method. Please help!!!
By biodunalfet in forum New To JavaReplies: 1Last Post: 10272011, 07:16 AM 
problem in a method which return array format
By elenora in forum Advanced JavaReplies: 13Last Post: 09212011, 09:43 AM 
Array method problem
By insanepenguin in forum New To JavaReplies: 2Last Post: 12042010, 05:58 PM 
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09272010, 11:40 AM 
Problem using the toUpperCase method with character array...
By lisalala in forum New To JavaReplies: 2Last Post: 02242009, 05:32 PM
Bookmarks