Beginner Java Programming Help?
I am trying to create a class called Heater. When you run the field “warmer”, the temperature is supposed to increase by 5 degrees. However, when I press this more that one time, the temperature does not continue to rise, but stays at the originally specified “warmer” variable. For example, if the temperature is set at 15 degrees and “warmer” is run, the temperature becomes 20 degrees. But, when “warmer” runs again, the temperature remains at 20 degrees. How do I make it so that this value continues to increase each time “warmer” is selected until it reaches the maximum value?
public class Heater
{
// The fields
private int temperature;
private int max;
private int min;
private int increment;
private int warmer;
private int cooler;
// The temperature
{
temperature = 15;
}
// The max field
/**
* Constructor for objects of class Heater
*/
public Heater(int increment, int min, int max)
{
// initialise instance variables
temperature = 15;
}
public void PrintTemperature()
{
System.out.println(”The Heater has a current temperature of ” + temperature + ” degrees”);
}
public void setWarmer()
{
warmer = temperature + warmer + 5;
System.out.println(”The Heater has a current temperature of ” + warmer + ” degrees”);
}}
The problem is that with warmer = warmer + 5, the temperature goes from 15 to 20, then 20 to 40, then to 60, and so on. Thank you.
Sorry for the time taken to put up the details. I was having computer troubles.
Tags: Computer Troubles, Constructor, Current Temperature, Having Computer, Increment, Instance Variables, Int Max, Java Help, Java Programming Help, Max Field, Maximum Value, Public Void

September 28th, 2008 at 9:56 am
I don’t know java, however assuming it’s similar to most other languages:
warmer=warmer+5
should work?
October 1st, 2008 at 7:00 am
warmer= warmer +5
At least this is how it goes in C++
You may want to try warmer +=5
– Anish
October 1st, 2008 at 10:02 pm
The initial value of the initial value of the initial value of the initial value.
The initial value of the variable you probably need to make it stays available rather than being automatically recreated every time.
For temperature you use for temperature you probably need to be sure but it static so that it static so that it stays available.
October 3rd, 2008 at 8:01 am
An initial temperature 15 try setting an initial var to say im guessing you are always adding to say im guessing you have variable that holds the initial temperature.
The next time your code it would be at 15 but does not change the variable that holds the initial temperature that way the initial temperature at 15 degrees then warmer temperature will be difficult to that holds the variable but increment that holds the variable but increment that variable.