Results 1 to 13 of 13
  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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;
    	}
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: 2D Array. method problem.

    Java Code:
    middelTemp = middelTemp / Hours;
    middelTemp is an array, right? So would it not be better if that line (adding the index of course) be better within the outer loop, but after the inner loop?

    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?

  3. #3
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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;
    	}

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: 2D Array. method problem.

    You need to re-initiate "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).

  5. #5
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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;
    	}

  6. #6
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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 first loop will take the day and walk trough the days.
    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.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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).

  8. #8
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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;
    	}

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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).

  10. #10
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: 2D Array. method problem.

    Perfect!
    Finally closin in on the task now =)

  11. #11
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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;
    	}
    Need to get one 2D array into string here, probably simple task.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: 2D Array. method problem.

    Another double loop, of course.

  13. #13
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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

  1. Replies: 1
    Last Post: 10-27-2011, 08:16 AM
  2. problem in a method which return array format
    By elenora in forum Advanced Java
    Replies: 13
    Last Post: 09-21-2011, 10:43 AM
  3. Array method problem
    By insanepenguin in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 05:58 PM
  4. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  5. Replies: 2
    Last Post: 02-24-2009, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •