Lost Password?


  #1 (permalink)  
Old 10-12-2008, 08:49 AM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Object-Orientated-Programming Calculator tutorial

This time I have decided to take a different approach in my tutorial. I have decided that I will add comments and the parts that need a lot of explaining I will post below. In this case it's the pow function in the class.

Also this is my VERY first OO script in any language so please bare with me.
Here we are;

calc.class.php
PHP Code:
<?php
//This class will take the two numbers supplied by the user and perform all the functions with them
cl*** Calc {
    
    
//First declare the variables that will be used in the cl***
    
private $x;
    private 
$y;
    private 
$opt;
    private 
$ans;
    
    function 
__construct($opt) {
        
//Checks to see if a user has chose a option, if not it shows an error message
        
if (empty($opt)) {
            throw new 
Exception("You must select an operator before continuing.");
        } else {
            
//if they have entered a option, it will set the variable
            
$opt $_POST['opt'];
        }
    }
    
    function 
addNum($x$y) {
        
//add two numbers supplied by the user
        
return $this->ans $x $y;
    }
    
    function 
subNum($x$y) {
        
//subtract two numbers supplied by the user
        
return $this->ans $x $y;
    }
    
    function 
mulNum($x$y) {
        
//multiply two numbers supplied by the user
        
return $this->ans $x $y;
    }
    
    function 
divNum($x$y) {
        
//check to see if y == 0, if it show an error message
        
if ($y == 0) {
            die(
"You can't divide by zero. Please enter a new number.");
        } else {
            return 
$this->ans $x $y;
        }
    }
    
    function 
pow($x$y) {
        
//this might look confusing, but it's not. It's explained below
        
return $this->ans round(pow(abs($y), $x), 2);
    }
}
?>
form.php
PHP Code:
<?php
//This is the form code which will be shown on index.php
function formInput() {                                                                
?>
    <center>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">             <!-- The PHP code in this long tells the form that the action is itself -->
        <p>
            First/Base number: <input type="text" name="x" /> <br />                <!-- Make them enter the values -->
                Second/Exponent number: <input type="text" name="y" /><br />
    
                <input type="radio" name="opt" value="+"/> Addition <br />            <!-- Offer them the functions that they would like to use -->
                <input type="radio" name="opt" value="-"/> Subtract <br />
                <input type="radio" name="opt" value="*"/> Multiply <br />
                <input type="radio" name="opt" value="/"/> Divide <br />
                <input type="radio" name="opt" value="^"/> Power
            </p>
                <input type="submit" value="Submit" name="submit" />                <!-- Straight forward, this submits the forms information to the action -->
        </form>
    </center>
<?php
}                                                                                    //Re-open the PHP tags to close the function from above                                                        
?>
index.php
PHP Code:
<?php
require "form.php";
require 
"calc.cl***.php";

if (!isset(
$_POST['submit'])) {                            //Here if the user has just visited the link they will be shown the formInput() which is in form.php
    
formInput();
} else {
    
$opt $_POST['opt'];                                //If they have, declare the variables
    
$x $_POST['x'];
    
$y $_POST['y'];
    
$calc = new Calc($opt);
    
    switch(
$opt) {                                        //Now this is basic PHP, it switches through the possibilites of $opt and does the correct function with the declard variables
        
case "+":
            echo 
"$x + $y <br />";
            echo 
$calc->addNum($x$y);                    //Here for example. It will use the cl*** Calc and find the addNum function. In that parameters is the given values we wish to use, retrieved from formInput()
            
break;
            
        case 
"-":
            echo 
"$x - $y <br />";                        //Here on down is the same as above.
            
echo $calc->subNum($x$y);
            echo 
"<br /> $y - $x <br />";
            echo 
$calc->subnum($y$x);
            break;
        
        case 
"*":
            echo 
"$x * $y <br />";
            echo 
$calc->mulNum($x$y);
            break;
        
        case 
"/":
            echo 
"$x / $y <br />";
            echo 
$calc->divNum($x$y);
            echo 
"<br /> $y / $x <br />";
            echo 
$calc->divNum($y$x);
            break;
            
        case 
"^":
            echo 
"$x <sup>$y</sup> <br />";
            echo 
$calc->pow($x$y);
            echo 
"<br /> $y <sup>$x</sup><br />";
            echo 
$calc->pow($y$x);
            break;
    }
    echo 
"<br /><a href='index.php'>Another Calculation</a>";    //Offer the user to do another calculation
}
?>

OK now time for the explaination of the pow function used.
This code;
PHP Code:
    function pow($x$y) {
        
//this might look confusing, but it's not. It's explained below
        
return $this->ans round(pow(abs($y), $x), 2);
    } 
So let's break this down.
It will round anything in the parameters to two decimal places, in this case;
PHP Code:
pow(abs($y), $x
Now here comes the PHP function pow. The first value is the base and the second is the exponent. But you see abs. What that does is will return the absolute value of the value in the parameters in this case;
PHP Code:
$y 
Now what you have is;
PHP Code:
pow($y$x
But you must remember that PHP will round the answer of this to two decimals places and also that $y is at it's absolute value. Now it's simple. If you still don't understand just ask.


Anything else that you see wrong with the script or you see I explained the above code wrong please tell me.

Good luck!

+rep if you like please.

Last edited by John; 10-21-2008 at 11:18 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-12-2008, 09:09 AM
kresh7's Avatar   
kresh7 kresh7 is offline
Programming Expert
 
Join Date: Jun 2007
Location: Kosovo
Posts: 472
Rep Power: 10
kresh7 will become famous soon enoughkresh7 will become famous soon enough
Send a message via MSN to kresh7
Default Re: Object-Orientated-Programming Calculator tutorial

Great Tutorial
__________________

The quiter you become,the more you able to hear.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-12-2008, 09:14 AM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Re: Object-Orientated-Programming Calculator tutorial

Thanks mate. Really glad you liked the tutorial, if you notice carefully, not in the actual script, I will be adding more features to it. You will know which ones just need to look out for which ones.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-21-2008, 10:32 PM
mixpage65's Avatar   
mixpage65 mixpage65 is offline
Learning Programmer
 
Join Date: Oct 2008
Posts: 38
Rep Power: 1
mixpage65 is on a distinguished road
Default Re: Object-Orientated-Programming Calculator tutorial

thanks! very useful for the novice php programmer. In a simple script you covered many things of PHP.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-21-2008, 11:20 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default Re: Object-Orientated-Programming Calculator tutorial

What is the point of this? The class never uses $opt...
PHP Code:
    function __construct($opt) {
        
//Checks to see if a user has chose a option, if not it shows an error message
        
if (empty($opt)) {
            throw new 
Exception("You must select an operator before continuing.");
        } else {
            
//if they have entered a option, it will set the variable
            
$opt $_POST['opt'];
        }
    } 
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-31-2008, 11:32 AM
amrosama's Avatar   
amrosama amrosama is offline
Guru
 
Join Date: Aug 2007
Location: egypt
Age: 20
Posts: 2,021
Last Blog:
OO Life
Rep Power: 21
amrosama is a jewel in the roughamrosama is a jewel in the roughamrosama is a jewel in the roughamrosama is a jewel in the rough
Send a message via MSN to amrosama
Default Re: Object-Orientated-Programming Calculator tutorial

ive started learning php, and i find this tut very useful
good job +rep
__________________
A good programmer is someone who always looks both ways before crossing a one-way street
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-31-2008, 05:02 PM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Re: Object-Orientated-Programming Calculator tutorial

Thanks Amrosama. Opt is the option.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-31-2008, 05:07 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,583
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Object-Orientated-Programming Calculator tutorial

Nice one! You can have a massive dose of +rep from you... that's 70 points for you.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-31-2008, 05:12 PM
Brandon W's Avatar   
Brandon W Brandon W is offline
Guru
 
Join Date: Sep 2008
Location: Australia
Age: 15
Posts: 1,588
Rep Power: 15
Brandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really niceBrandon W is just really nice
Send a message via MSN to Brandon W
Default Re: Object-Orientated-Programming Calculator tutorial

Thanks Xav But you only have rep power of 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-31-2008, 05:14 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,583
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of