Tom Tom - 5 days ago 5
Java Question

How to pass the correct value when calling a method?

I'm doing an assignment in which I have created an Appliance class that has a

timePasses()
method within it. This method re-directs some values that need to be stored within another method that is inside of another class. Here is where I am up to on this:

Appliance

public class ElectricCooker extends Cooker {

public int isOn = -1;
public int isOff = 0;
public int incrementTime;
public int varPass = -1;

@Override
public int currentState() {
if (varPass == 0) {
return isOff;
} else {
return isOn;
}
}

@Override
public void useTime(int defaultTime) {
defaultTime = 15;
incrementTime = 4;
}

@Override
public void timePasses() {
if (varPass == isOff) {
varPass = 0;
} else {
ElectricMeter.getInstance().incrementConsumed(electricityUse);
GasMeter.getInstance().incrementConsumed(gasUse);
WaterMeter.getInstance().incrementConsumed(waterUse);
}
}

ElectricCooker(int electricityUse, int gasUse, int waterUse, int timeOn) {
super(electricityUse, gasUse, waterUse, timeOn);

this.electricityUse = 5 * incrementTime;
this.gasUse = 0 * incrementTime;
this.waterUse = 0 * incrementTime;
this.timeOn = 15 * incrementTime;
}
}


Meter

public class ElectricMeter {

ElectricMeter() {
}
private static ElectricMeter instance = new ElectricMeter();

public static ElectricMeter getInstance() {
return instance;
}

public void incrementConsumed(int value) {
System.out.println(value);
}

public int incrementGenerated() {
}

public boolean canGenerate() {
}

public String getConsumed() {
}

public String getGenerated() {
}
}


Main method

public class MainCoursework {

public static void main(String[] args) {
ElectricMeter a = new ElectricMeter();
a.incrementConsumed(//what goes here?);
}
}


So the value from
timePasses()
has been redirected into an
ElectricMeter
instance but now I need to return that value to the
increentConsumed()
method in the meter class and I'm stuck on how to do this. Since the value of
electricityConsumed
is 20, the output should be
20
. But instead I have to pass a parameter into
a.incrementConsumed(//pass parameter here)
and what ever is passed gets printed out onto the screen instead of the
20
from
electrictyUse
. Any help on how to do this is appreciated, thanks.

Answer

Actually, the incrementConsumed method is indeed implemented as you described:

public void incrementConsumed(int value)  
{
    System.out.println(value);     
}

A method called incrementXXX shouldn't really output anything, should it? It should increment a variable/field:

private int electricityUsed = 0;
public void incrementConsumed(int value)  
{
    electricityUsed += value;    
}

You should declare another method that returns electricityUsed:

public int getElectricityUsed() {
    return electricityUsed;
}

Now let's fix your main method.

In your main method, you didn't even create anything that consumes electricity! How can the electric meter incrementConsumed? So remove everything from the main method and create a cooker:

// your constructor looks weird. So I passed in some random arguments..
ElectricCooker cooker = new ElectricCooker(20, 0, 0, 60);

Now call timePasses to simulate that some time passed:

cooker.timePasses();

And print the electricity used:

System.out.println(ElectricMeter.getInstance().getElectricityUsed());