4 Replies - 861 Views - Last Post: 17 April 2012 - 06:49 PM Rate Topic: -----

#1 myster1ous  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 17-April 12

weather program 2d array not performing calculations

Posted 17 April 2012 - 11:29 AM

I've been trying to figure this out for the last few days. The program is to read off a text file and put those values into a 2d array. I have managed to get everything to work but not the calculations to print the highest temp, lowest temp, average low, and average high.

Class
public class Temp {

private double avgHigh;
private double avgLow;
private int highestTemp;
private int lowestTemp;

int [] [] temperature = new int [2][12];

public Temp (){
	avgHigh = 0;
	avgLow=0;
	highestTemp=0;
	lowestTemp=0;
	

	for (int row=0; row<temperature.length; row++)
		for (int col=0; col<temperature[row].length; col++)
			temperature[row][col]=0;
		
}

public Temp (Scanner i, int [][] temp){
for (int row=0; row<temp.length; row++){
	for (int col=0; col<temp[row].length; col++)
		temp[row][col]=i.nextInt();}
}

public Temp (Temp obj){
	avgHigh=obj.avgHigh;
	avgLow=obj.avgLow;
	highestTemp=obj.highestTemp;
	lowestTemp=obj.lowestTemp;
}

public void setAll(double avgHigh, double avgLow, int highestTemp, int lowestTemp){
	this.avgHigh=avgHigh;
	this.avgLow=avgLow;
	this.highestTemp=highestTemp;
	this.lowestTemp=lowestTemp;
}
	
public void setAvgHigh(double avHigh){
	avgHigh=avHigh;
}

public void setAvgLow(double avLow){
	avgLow=avLow;
}

public void setHighest(int hTemp){
	highestTemp=hTemp;
}

public void setLowest(int lTemp){
	lowestTemp=lTemp;
}

public double getavgHigh(){
	return avgHigh;
}

public double getavgLow(){
	return avgLow;
}

public int getHTemp(){
	return highestTemp;
}

public int getLTemp(){
	return lowestTemp;
}



public double avgHigh(){
double sumHTemp = 0;

	for (int i=0; i < temperature[0].length; i++){
	 sumHTemp = sumHTemp + temperature[0][i];    
	 avgHigh = (sumHTemp/temperature[0].length);}
	return avgHigh;
}

public double avgLow(){
	int sumLTemp = 0;
	for (int i=0; i < temperature[1].length; i++)
		 sumLTemp = sumLTemp + temperature[1][i];    
		return avgLow = (sumLTemp/temperature[1].length);		
}

public int highTemp(){
		highestTemp = temperature[0][0];
		for (int col =1; col< temperature[0].length; col++)
			if (highestTemp < temperature[0][col])
				highestTemp = temperature[0][col];
		return highestTemp;
	}

public int lowTemp(){
		int lowestTemp = temperature[0][0];
		for (int col =1; col< temperature[1].length; col++)
			if (lowestTemp > temperature[1][col])
				lowestTemp = temperature[1][col];
		return lowestTemp;
		}


	

public String toString(){

	
	String data = "\nAverage High: " + this.avgHigh + "\nAverage Low: " + this.avgLow + "\nHighest Temp: " + this.highestTemp + "\nLowest Temp: " + this.lowestTemp;
	return data;
}

}


main class
public class MainClass {

	public static void main(String[] args) throws FileNotFoundException {

		
		Scanner inFile = new Scanner(new FileReader("temp.txt"));
		PrintWriter pr = new PrintWriter("outdata.txt"); 
		
		int [] [] temperature = new int [2][12];
		Temp Temps = new Temp(inFile, temperature);
		
		for (int row=0; row<temperature.length; row++){
			for (int col=0; col<temperature[row].length; col++)
				System.out.printf("%-4d", temperature[row][col]);
			
			System.out.println();
			
			}

		System.out.println(Temps);}
	
}


This is my output

30  40  45  60  70  90  89  95  79  90  70  40  
10  -10 20  30  50  75  85  79  50  80  30  20  

Average High: 0.0
Average Low: 0.0
Highest Temp: 0
Lowest Temp: 0


Is This A Good Question/Topic? 0
  • +

Replies To: weather program 2d array not performing calculations

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8034
  • View blog
  • Posts: 13,747
  • Joined: 19-March 11

Re: weather program 2d array not performing calculations

Posted 17 April 2012 - 11:47 AM

You have two arrays called temperature. The one that's local to the main method is never given values, so it's full of zero.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: weather program 2d array not performing calculations

Posted 17 April 2012 - 11:47 AM

No really sure what you want to do with your different constructors and even more lost in the way you use them

public class Temp {

private double avgHigh;
private double avgLow;
private int highestTemp;
private int lowestTemp;

int [] [] temperature = new int [2][12];

// constructor called without parameter
public Temp (){

// all the folowing initilization are useless 
// double are intialized to 0 at object instantition
// but they will not hurt you can leave them there
	avgHigh = 0;
	avgLow=0;
	highestTemp=0;
	lowestTemp=0;
	
// your int [] [] temperature = new int [2][12];
// has initialized the array with 0 already but again it won't hurt
// usually we initialize instance variables only if their values are 
// different than 0
// any how
	for (int row=0; row<temperature.length; row++)
		for (int col=0; col<temperature[row].length; col++)
			temperature[row][col]=0;
		
}

// this constructor though is weird
// you fill an array received as parameter with the values read from a file
// this array as nothing to do with the array temperature[][] in the instance
// variables with the class
public Temp (Scanner i, int [][] temp){
for (int row=0; row<temp.length; row++){
	for (int col=0; col<temp[row].length; col++)
		temp[row][col]=i.nextInt();}
// when this constructor exits, your Temp object variables are exactly in the same
// state (all filled with 0) as if you had call the constructor without parameter
}

public Temp (Temp obj){
	avgHigh=obj.avgHigh;
	avgLow=obj.avgLow;
	highestTemp=obj.highestTemp;
	lowestTemp=obj.lowestTemp;
// you miss temperature = obj.temperature here
// or more probably copying all the values from obj.temperature to temperature
}




Now in your main()
Your create a local array that you fill from the file calling the second constructor
That does not change the values in the array temperature[][] of the Temps object

And nowhere you actually call the method that effectivelly perform the calculation

You shouldn't have a temperature[][] array in your main
and your second constructor should be

public Temp (Scanner i){
for (int row=0; row<temperature.length; row++){
	for (int col=0; col<temperature[row].length; col++)
		temperature[row][col]=i.nextInt();}
}
}


Now yoiu can call the methods that will work with the internal array of temps
Hope this help

This post has been edited by pbl: 17 April 2012 - 12:04 PM

Was This Post Helpful? 1
  • +
  • -

#4 myster1ous  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 17-April 12

Re: weather program 2d array not performing calculations

Posted 17 April 2012 - 06:43 PM

Wow, thanks alot!

I did learn a lot. Thank you!
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: weather program 2d array not performing calculations

Posted 17 April 2012 - 06:49 PM

That's great :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1