i an writing a room class java code that returns the temperature status of the room and return if a lightbulb is on or off.

how do i write the method for the temperature?

public class SittingRoom {

     String SRlightBulb;
    int   SRTemperature; 

    public SittingRoom()
    {String InitialStatus = null;int InitialTemp;
        SRlightBulb = InitialStatus;
        SRTemperature = InitialTemp;
    }


    void switchLightOn()
    {
        SRlightBulb = "ON";
    }
   void switchLightOff()
   {
       SRlightBulb = "OFF";
   }
  public  String checkSRLightBulbStatus()
   {
      return SRlightBulb; 
   }

     public int checkTemperature()
  {
      return SRTemperature;
  }

public static void main (String [] arg)
{
SittingRoom n = new SittingRoom();
System.out.println(n.checkTemperature());

}

}

Dani AI

Generated

Immediate problem and quick fix: the constructor declares a local int InitialTemp; but never assigns it. Java will fail to compile with "variable InitialTemp might not have been initialized". Instance fields are default-initialized (objects -> null, int -> 0), but assigning from an uninitialized local variable is illegal. Fix that by assigning a value directly to the field or initializing the local variable before use.

A cleaner, safer design:

  • make fields private,
  • use a boolean (or an enum) for bulb state,
  • provide constructors that set sensible defaults,
  • validate temperature in the setter,
  • add convenience methods (increase/decrease and a human-friendly status).

Example (refactor, not the same as the snippets already shown in the thread):

public class SittingRoom {
    private boolean lightOn;
    private int temperatureC = 20; // default

    public SittingRoom() {
        this(false, 20);
    }

    public SittingRoom(boolean lightOn, int temperatureC) {
        this.lightOn = lightOn;
        setTemperature(temperatureC);
    }

    public boolean isLightOn() { return lightOn; }
    public void setLightOn(boolean on) { lightOn = on; }

    public int getTemperature() { return temperatureC; }
    public void setTemperature(int temp) {
        if (temp < -50 || temp > 60) throw new IllegalArgumentException("temperature out of range");
        temperatureC = temp;
    }

    public void changeTemperature(int delta) { setTemperature(temperatureC + delta); }

    public String temperatureStatus() {
        if (temperatureC < 16) return "Cold";
        if (temperatureC <= 24) return "Comfortable";
        return "Hot";
    }
}

Short usage example:

SittingRoom r = new SittingRoom(true, 22);
System.out.println(r.temperatureStatus()); // "Comfortable"
r.changeTemperature(3);
System.out.println(r.getTemperature());    // 25

Notes: prefer descriptive names (lightOn, temperature), document units (Celsius vs Fahrenheit), choose double if fractional temps are needed, and add unit tests. This follows suggestion to avoid public instance variables and to prefer a boolean for the bulb; to fix the immediate compile problem apply the constructor change described above (or initialize the local InitialTemp before use). 's BB-code note is noted for posting format.

Recommended Answers

All 2 Replies

get2tk,

Source program must be surrounded with BB code tags: See # icon at toolbar and also read How to use bb code tags?.

Firstly, get in the habit of making your instance variables private.

Secondly, not that it's going to make a whole lot of difference, but wouldn't it be easier to make SRlightBulb a boolean? True means on, False means off?

Thirdly, when you call the constructor of your class, which parameters are being passed? Using the default constructor (no arguments), the light bulb status is going to be null, but what will the temperature instance variable be set to?

If you're trying to modify the room temperature, I would do something like...

public void setTemp(int n)
{
SRTemperature = n;
}

and to access that method...

n.setTemp(<temp>);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.