PHP calculator problem
Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. It doesn't need an HTML form, all the arguments are put in through the method.
The class must check for a correct operator (+,*,-,/), and a number (integer) for the second and third argument entered. The calculator cannot divide by zero, that will be an error.
Separate error messages will be displayed for all errors. Also, separate success messages will be displayed with each successful answer. This script will run using a browser. The output of the script will display after the web address is entered into the browser navigation window. There will be a separate file that will require the class. That file will be used to instantiate the class and call the appropriate method, while the class will do the heavy lifting.
You will have two files.
One will be the file that is your class.
The other will be a file that requires your class and call the
appropriate method.
You have a file with a class named $Calculator. You will have another class that uses the calculator class and displays the information.
<?php
require_once "Calculator.php";
$Calculator = new Calculator();
echo $Calculator->calc("/", 10, 0); //will output Cannot divide
by zero
echo $Calculator->calc("*", 10, 2); //will output The product of
the numbers is 20
echo $Calculator->calc("/", 10, 2); //will output The division
of the numbers is 5
echo $Calculator->calc("-", 10, 2); //will output The difference
of the numbers is 8 echo $Calculator->calc("+", 10, 2); //will
output The sum of the numbers is 12
echo $Calculator->calc("*", 10); //will output You must enter a
string and two numbers echo $Calculator->calc(10); //will output
You must enter a string and two numbers
?>
The browser will output (based upon the above methods):
Cannot divide by zero
The product of the numbers is 20
The division of the numbers is 5
The difference of the numbers is 8
The sum of the numbers is 12
You must enter a string and two numbers You must enter a string and
two numbers
NOTE: You are to use the code shown below as your test code.
NOTE: For this assignment you can write the following code within a PHP block within the HTML body element. You do not need to write the comments I did that to show you what needs to be outputted depending on what is sent as arguments.
Code
Calculator.php
<?php
class Calculator
{
// method declaration
public function calc()
{
if(func_num_args()<3)
{
return "You must enter a string and two
numbers</br>";
}
$op=func_get_arg(0);
$num1=func_get_arg(1);
$num2=func_get_arg(2);
if($op=="+")
{
return "The sum of the numbers is
".($num1+$num2)."</br>";
}
if($op=="-")
{
return "The difference of the numbers is
".($num1-$num2)."</br>";
}
if($op=="*")
{
return "The product of the numbers is
".($num1*$num2)."</br>";
}
if($op=="/")
{
if($num2!=0)
return "The product of the
numbers is ".($num1*$num2)."</br>";
else return "Cannot divide by
zero<br>";
}
}
}
?>
TestCalculator.php
<html>
<head>
<title>
Calculator
</title>
</head>
<body>
<?php
require_once
"Calculator.php";
$Calculator = new
Calculator();
echo $Calculator->calc("/", 10,
0); //will output Cannot divide by zero
echo $Calculator->calc("*", 10,
2); //will output The product of the numbers is 20
echo $Calculator->calc("/", 10,
2); //will output The division of the numbers is 5
echo $Calculator->calc("-", 10,
2); //will output The difference of the numbers is 8 echo
$Calculator->calc("+", 10, 2); //will output The sum of the
numbers is 12
echo $Calculator->calc("*", 10);
//will output You must enter a string and two numbers
echo $Calculator->calc(10);
//will output You must enter a string and two numbers
?>
</body>
<html>
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.