In: Computer Science
Calculator Class Instructions
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.
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. This is the file you will provide the web
address for.
For example:
If you have a file with a class named $Calculator. You will have another class that uses the calculator class and displays the information. That file will have the code shown below.
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.
<?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
in PHP/HTML
<!DOCTYPE html>
<html>
<head>
<title>Calculator Using PHP and
HTML</title>
<style>
#d
{
border:solid 2px black;
margin-left:500px ;
margin-top:200px;
width: 500px;
height: 300px;
background-color:darkgreen;
}
#p
{
float:left;
width: 200px;
height: 150px;
color:white;
margin-top: 100px;
padding-left: 10px;
font-size:25pt;
}
#c
{
float:right;
font-size:15pt;
color:white;
width:200px;
height:150px;
margin-top: 100px;
</style>
</head>
<body style= "background-color:Skyblue;">
<div id="d">
<div
id="p">
<p>Php Calculator</p>
</div>
<div
id="c"class= "screen-body-item">
<div class="app-form">
<form
method="POST">
<div class="app-form-group">
<input type="text" name="num1"
class="app-form-control" placeholder="Enter
number"></div>
<div class="app-form-group">
<input type="text" name=
"num2"class="app-form-control" placeholder="Enter
number"></div>
<div class="select-style">
<select
name="operation">
<option
value="add">Add</option>
<option
value="sub">Sub</option>
<option
value="mult">Mult</option>
<option
value="div">Div</option>
</select>
</div>
<div>
<input type="submit"
name="submit" value="submit" class="app-form-button">
</div>
</form>
</div>
<div
class="app-form-group showdata">
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$operation =
$_POST['operation'];
switch($operation){
case "add":$sum=
$num1+$num2;
echo"The Addition of two
numbers.{$sum}";
break;
case "sub":$sub=
$num1-$num2;
echo"The Subtraction of two
numbers.{$sub}";
break;
case "mult":$mult=
$num1*$num2;
echo"The Multiplication of
two numbers.{$mult}";
break;
case "div":$div=
$num1/$num2;
echo"The Division of two
numbers.{$div}";
break;
default: echo"Sorry Does not
Exist";
}
}
?>
</p>
</div>
</div>
</div>
</body>
</html>