Java static variables problems...?
-
Create a class named Household that includes data fields for the household name, number of occupants and the total monthly household income. In your class, include the following methods: A method to calculate and return the average income per occupant. Use the following method header: double computeAverageIncome() A method to display the data field values. Note: Below is the part I don't understand... (Using static variables and methods) Suppose you want to store the national average total monthly household income. Note that this value is revised every year. Modify the Household class accordingly. ______________________________________… This is the code I have so far... public class Household { // three fields String houseName; int occupantNum; double houseIncome; static double average; // calculate average method double computeAverageIncome() { average = houseIncome/occupantNum; return average; } public void output() { average = computeAverageIncome(); System.out.printf("\nAverage income per occupant is: %1.2f", average); } } ______________________________________… So, what does it means by it will revised every year. And does my calculations for the average is wrong? Very thanks you.
-
Answer:
First of all , yes ! your average calculation is perfect :) and revised every year means that your data for national average household income to be refreshed and recalculated each year again ! let that national average be stored in this variable : static double NationalAverage = anything ; (or lets simply assume Rs. 50000) Now lets compare your household income to this national one : if(average < NationalAverage) System.out.println("BELOW NATIONAL Average"); else if(average > NationalAverage) System.out.println("Above N.A"); else System.out.println("Same as N.A");
Siaw at Yahoo! Answers Visit the source
Related Q & A:
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How to Link Another Static Library to my Static Library in XCode?Best solution by Stack Overflow
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- Does Java have global variables?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.