Sunday, January 30, 2011

php - temperature conversion

<?php
class Temperature
{
    private $tk;
    private $tc;
    private $tf;


    function  __construct(){
        $this->init_t();
    }

    private function init_t(){
        $this->tk = 0;
        $this->tc = 0;
        $this->tf = 0;
    }
    public function set_tc($t){
        $this->tk = $t + 273.15;
    }
    public function set_tf($t){
        $this->tk = (($t - 32)*5/9)+273.15;
    }
    public function set_tk($t){
        $this->tk = $t;
    }
    public function get_tc(){
        return $this->tk - 273.15;
    }
    public function get_tf(){
        return (($this->tk - 273.15)*9/5)+32;
    }
    public function get_tk(){
        return $this->tk;
    }
}


$tmp = new Temperature();
$tmp->set_tc(40);
print "If the temperaute is 40 degrees C,<br />";
$t = $tmp->get_tc();
print "Temperature in C is: $t degrees.<br />";
$t = $tmp->get_tf();
print "Temperature in F is: $t degrees.<br />";
$t = $tmp->get_tk();
print "Temperature in K is: $t degrees.<br />";

?>

No comments:

Post a Comment