CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   conditions error (http://www.codingforums.com/showthread.php?t=282015)

clue< 11-12-2012 03:55 PM

conditions error
 
Hi!
I'm very new to javascript and i'm trying to write a script that allows me to:
-add to and substract from a value called temperature through methods warmer and cooler
-read temperature
-set a maximum and minimum value for temperature

this is what i've come up with:
Code:

public class Heater
{
    private int temperature;
    private int min;
    private int max;
    private int increment;
    private int recMin;
    private int recMax;
   
    public Heater()
    {
        temperature = 15;
        min = recMin;
        max = recMax;
        increment = 5;
    }
   
    public void insrtMin(int minimum)
    {
        recMin = minimum;
    }
   
    public void insrtMax(int maximum)
    {
        recMax = maximum;
    }
   
    public void warmer()
    { 
        if ((temperature + increment) > max)
        {

        }
        else
        {
            temperature = temperature + increment;
        }
    }
   
    public void cooler()
    { 
        if ((temperature - increment) < min)
        {

        }
        else
        {
            temperature = temperature - increment;
        }
    }   

    public int returnTemp()
    {
        return temperature;
    }
}

The code worked fine until I added the conditions to 'warmer' and 'cooler'. Now, however, only the cooler method seems to work. What did I miss?

thanks!

DanInMa 11-12-2012 03:59 PM

you need the "Java" forum not javascript

Philip M 11-12-2012 04:01 PM

This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

clue< 11-12-2012 04:34 PM

Thanks! Sorry about that..

vwphillips 11-12-2012 04:56 PM

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title></title>
</head>

<body>
 <input name="" id="tst" /> <br />
 <input type="button" name="" value="Warmer" onmouseup="heater.heat('tst',1);"/>
 <input type="button" name="" value="Colder" onmouseup="heater.heat('tst',-1);"/>
 <input type="button" name="" value="Set Min" onmouseup="heater.setmin('tst',0);"/>
 <input type="button" name="" value="Set Max" onmouseup="heater.setmax('tst',300);"/>

<script  type="text/javascript">
/*<![CDATA[*/

var heater={

 init:function(o){
  heater[o.id]={
  obj:document.getElementById(o.id),
  temp:o.temperature,
  min:o.min,
  max:o.max,
  i:o.increment
  }
 },

 heat:function(id,ud){
  var o=heater[id],ud=typeof(ud)=='number'&&ud==0?0:ud<0?-1:1;
  if (o){
  o.obj.value=Math.min(Math.max(o.temp+=(ud*o.i),o.min),o.max);
  }
 },

 setmin:function(id,nu){
  var o=heater[id];
  if (o&&typeof(nu)=='number'){
  o.min=Math.min(nu,o.max);
  this.heat(id,0);
  }
 },

 setmax:function(id,nu){
  var o=heater[id];
  if (o&&typeof(nu)=='number'){
  o.max=Math.max(nu,o.min);
  this.heat(id,0);
  }
 }

}

heater.init({
 id:'tst',
 temperature:100,
 min:10,
 max:200,
 increment:2
});

/*]]>*/
</script>

</body>

</html>



All times are GMT +1. The time now is 10:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.