Java : Trying to print average temperature for each of 3 weeks using instantiable classes and arrays





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I need some help please. My task is to develop an application using instantiable classes and arrays to store temperature values of 3 weeks (7 days in a week) and calculate and display average for each week. I think I am close, but my code returns null as value for average, not sure what is wrong! This is the code:



Instantiable class:



public class Temperature {

private double tempData; //array to store temperature readings from 3 weeks
private double avgOfWeek; // array to store avg of each week;

public Temperature () {; // constructor to initialize instance variables with their default values, ie. tempData with null
}

// setter
// setter method to assign / store a 2D array of double elements in the instance variable tempData
public void setTemp (double tempData) {
this.tempData = tempData;
}

// processing
public void calcAvg () {

double avgOfWeek = new double [3];
double sum = new double [3];
int counter = 0;

for (int row=0; row < tempData.length; row ++) {
for (int col=0; col < tempData[row].length; col++) {

sum [row]= sum [row] + tempData [row][col];
}

avgOfWeek [row] = sum[row]/ tempData[row].length;
}

}
// getter method to retrieve the average
public double getAvg () {
return avgOfWeek;
}
}


Application:



public class TemperatureApp {

public static void main (String args) {

// declare a 2D array to store values of temperature

double temp = new double {
{1,2,3,4,5,6,7},
{7,6,5,4,3,2,1},
{1,2,3,4,3,2,1},
};


Temperature myTemp = new Temperature ();

myTemp.setTemp(temp);

myTemp.calcAvg();

double a = myTemp.getAvg();

System.out.println (" The average temperature is " + a);
}
}









share|improve this question




















  • 2





    You have two avgOfWeek variables, remove the one declared in the calcAvg method .

    – Arnaud
    Jan 3 at 15:11








  • 1





    the final print is not going to show what you expect. Check here how to print an array

    – jhamon
    Jan 3 at 15:15











  • thank you! thank really helped!

    – Msza
    Jan 3 at 18:47


















0















I need some help please. My task is to develop an application using instantiable classes and arrays to store temperature values of 3 weeks (7 days in a week) and calculate and display average for each week. I think I am close, but my code returns null as value for average, not sure what is wrong! This is the code:



Instantiable class:



public class Temperature {

private double tempData; //array to store temperature readings from 3 weeks
private double avgOfWeek; // array to store avg of each week;

public Temperature () {; // constructor to initialize instance variables with their default values, ie. tempData with null
}

// setter
// setter method to assign / store a 2D array of double elements in the instance variable tempData
public void setTemp (double tempData) {
this.tempData = tempData;
}

// processing
public void calcAvg () {

double avgOfWeek = new double [3];
double sum = new double [3];
int counter = 0;

for (int row=0; row < tempData.length; row ++) {
for (int col=0; col < tempData[row].length; col++) {

sum [row]= sum [row] + tempData [row][col];
}

avgOfWeek [row] = sum[row]/ tempData[row].length;
}

}
// getter method to retrieve the average
public double getAvg () {
return avgOfWeek;
}
}


Application:



public class TemperatureApp {

public static void main (String args) {

// declare a 2D array to store values of temperature

double temp = new double {
{1,2,3,4,5,6,7},
{7,6,5,4,3,2,1},
{1,2,3,4,3,2,1},
};


Temperature myTemp = new Temperature ();

myTemp.setTemp(temp);

myTemp.calcAvg();

double a = myTemp.getAvg();

System.out.println (" The average temperature is " + a);
}
}









share|improve this question




















  • 2





    You have two avgOfWeek variables, remove the one declared in the calcAvg method .

    – Arnaud
    Jan 3 at 15:11








  • 1





    the final print is not going to show what you expect. Check here how to print an array

    – jhamon
    Jan 3 at 15:15











  • thank you! thank really helped!

    – Msza
    Jan 3 at 18:47














0












0








0








I need some help please. My task is to develop an application using instantiable classes and arrays to store temperature values of 3 weeks (7 days in a week) and calculate and display average for each week. I think I am close, but my code returns null as value for average, not sure what is wrong! This is the code:



Instantiable class:



public class Temperature {

private double tempData; //array to store temperature readings from 3 weeks
private double avgOfWeek; // array to store avg of each week;

public Temperature () {; // constructor to initialize instance variables with their default values, ie. tempData with null
}

// setter
// setter method to assign / store a 2D array of double elements in the instance variable tempData
public void setTemp (double tempData) {
this.tempData = tempData;
}

// processing
public void calcAvg () {

double avgOfWeek = new double [3];
double sum = new double [3];
int counter = 0;

for (int row=0; row < tempData.length; row ++) {
for (int col=0; col < tempData[row].length; col++) {

sum [row]= sum [row] + tempData [row][col];
}

avgOfWeek [row] = sum[row]/ tempData[row].length;
}

}
// getter method to retrieve the average
public double getAvg () {
return avgOfWeek;
}
}


Application:



public class TemperatureApp {

public static void main (String args) {

// declare a 2D array to store values of temperature

double temp = new double {
{1,2,3,4,5,6,7},
{7,6,5,4,3,2,1},
{1,2,3,4,3,2,1},
};


Temperature myTemp = new Temperature ();

myTemp.setTemp(temp);

myTemp.calcAvg();

double a = myTemp.getAvg();

System.out.println (" The average temperature is " + a);
}
}









share|improve this question
















I need some help please. My task is to develop an application using instantiable classes and arrays to store temperature values of 3 weeks (7 days in a week) and calculate and display average for each week. I think I am close, but my code returns null as value for average, not sure what is wrong! This is the code:



Instantiable class:



public class Temperature {

private double tempData; //array to store temperature readings from 3 weeks
private double avgOfWeek; // array to store avg of each week;

public Temperature () {; // constructor to initialize instance variables with their default values, ie. tempData with null
}

// setter
// setter method to assign / store a 2D array of double elements in the instance variable tempData
public void setTemp (double tempData) {
this.tempData = tempData;
}

// processing
public void calcAvg () {

double avgOfWeek = new double [3];
double sum = new double [3];
int counter = 0;

for (int row=0; row < tempData.length; row ++) {
for (int col=0; col < tempData[row].length; col++) {

sum [row]= sum [row] + tempData [row][col];
}

avgOfWeek [row] = sum[row]/ tempData[row].length;
}

}
// getter method to retrieve the average
public double getAvg () {
return avgOfWeek;
}
}


Application:



public class TemperatureApp {

public static void main (String args) {

// declare a 2D array to store values of temperature

double temp = new double {
{1,2,3,4,5,6,7},
{7,6,5,4,3,2,1},
{1,2,3,4,3,2,1},
};


Temperature myTemp = new Temperature ();

myTemp.setTemp(temp);

myTemp.calcAvg();

double a = myTemp.getAvg();

System.out.println (" The average temperature is " + a);
}
}






java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:07









Sofo Gial

515619




515619










asked Jan 3 at 15:08









MszaMsza

83




83








  • 2





    You have two avgOfWeek variables, remove the one declared in the calcAvg method .

    – Arnaud
    Jan 3 at 15:11








  • 1





    the final print is not going to show what you expect. Check here how to print an array

    – jhamon
    Jan 3 at 15:15











  • thank you! thank really helped!

    – Msza
    Jan 3 at 18:47














  • 2





    You have two avgOfWeek variables, remove the one declared in the calcAvg method .

    – Arnaud
    Jan 3 at 15:11








  • 1





    the final print is not going to show what you expect. Check here how to print an array

    – jhamon
    Jan 3 at 15:15











  • thank you! thank really helped!

    – Msza
    Jan 3 at 18:47








2




2





You have two avgOfWeek variables, remove the one declared in the calcAvg method .

– Arnaud
Jan 3 at 15:11







You have two avgOfWeek variables, remove the one declared in the calcAvg method .

– Arnaud
Jan 3 at 15:11






1




1





the final print is not going to show what you expect. Check here how to print an array

– jhamon
Jan 3 at 15:15





the final print is not going to show what you expect. Check here how to print an array

– jhamon
Jan 3 at 15:15













thank you! thank really helped!

– Msza
Jan 3 at 18:47





thank you! thank really helped!

– Msza
Jan 3 at 18:47












1 Answer
1






active

oldest

votes


















4














You're not initializing the right variable double avgOfWeek = new double [3];. remove the code double .



...
public void calcAvg () {

avgOfWeek = new double [3];
double sum = new double [3];
....


Right now you're creating a new local variable with the same name. Whenever you're using it in that method, it only references that one.






share|improve this answer


























  • thank you very much, this works now!

    – Msza
    Jan 3 at 18:48











  • @Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

    – Neijwiert
    Jan 4 at 7:39












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024936%2fjava-trying-to-print-average-temperature-for-each-of-3-weeks-using-instantiabl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You're not initializing the right variable double avgOfWeek = new double [3];. remove the code double .



...
public void calcAvg () {

avgOfWeek = new double [3];
double sum = new double [3];
....


Right now you're creating a new local variable with the same name. Whenever you're using it in that method, it only references that one.






share|improve this answer


























  • thank you very much, this works now!

    – Msza
    Jan 3 at 18:48











  • @Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

    – Neijwiert
    Jan 4 at 7:39
















4














You're not initializing the right variable double avgOfWeek = new double [3];. remove the code double .



...
public void calcAvg () {

avgOfWeek = new double [3];
double sum = new double [3];
....


Right now you're creating a new local variable with the same name. Whenever you're using it in that method, it only references that one.






share|improve this answer


























  • thank you very much, this works now!

    – Msza
    Jan 3 at 18:48











  • @Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

    – Neijwiert
    Jan 4 at 7:39














4












4








4







You're not initializing the right variable double avgOfWeek = new double [3];. remove the code double .



...
public void calcAvg () {

avgOfWeek = new double [3];
double sum = new double [3];
....


Right now you're creating a new local variable with the same name. Whenever you're using it in that method, it only references that one.






share|improve this answer















You're not initializing the right variable double avgOfWeek = new double [3];. remove the code double .



...
public void calcAvg () {

avgOfWeek = new double [3];
double sum = new double [3];
....


Right now you're creating a new local variable with the same name. Whenever you're using it in that method, it only references that one.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 15:17

























answered Jan 3 at 15:11









NeijwiertNeijwiert

723418




723418













  • thank you very much, this works now!

    – Msza
    Jan 3 at 18:48











  • @Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

    – Neijwiert
    Jan 4 at 7:39



















  • thank you very much, this works now!

    – Msza
    Jan 3 at 18:48











  • @Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

    – Neijwiert
    Jan 4 at 7:39

















thank you very much, this works now!

– Msza
Jan 3 at 18:48





thank you very much, this works now!

– Msza
Jan 3 at 18:48













@Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

– Neijwiert
Jan 4 at 7:39





@Msza No problem, glas to help. Could you mark my question as the answer so this question gets closed?

– Neijwiert
Jan 4 at 7:39




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024936%2fjava-trying-to-print-average-temperature-for-each-of-3-weeks-using-instantiabl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith