Question

PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers....

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
PHP script that will take a set of two numbers and output them as a nested...
PHP script that will take a set of two numbers and output them as a nested list one number is for the main list items the other is the sub list items PHP functionality should be above the doctype display the resulting HTML string via a variable in the echo statement within the body element of the webpage script should be in PHP/HTML
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide and clear. Test your calculator by writing a main program incorporating the test code below: Calculator mycalc; mycalc.clear(); mycalc.add(4.52); mycalc.add(3.789); mycalc.divide(2.6); mycalc.multiply(3.12); mycalc.subtract(2.678); cout << mycalc.display() << endl;       // prints out "7.2928" mycalc.clear(); mycalc.add(5.0); cout << mycalc.display() << endl;       // prints out "5" //advanced stuff #1: add a constructor Calculator calc1; cout << calc1.display() << endl;  //prints out 0 //advanced stuff #2: add a parameterized...
<?php    if(isset($_GET['submit'])){ //sanitize the input        /* Check the error from the input: if...
<?php    if(isset($_GET['submit'])){ //sanitize the input        /* Check the error from the input: if input from user is empty -> get an error string variable if input is not empty -> use preg_match() to match the pattern $pattern = "/^[1-9][0-9]{2}(\.|\-)[0-9]{3}(\.|\-)[0-9]{4}$/"; -> if it's a matched, get a success string variable */           } ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link...
You have one Question, Which have four parts, 1st... PHP scriptIn a HTML document, use PHP...
You have one Question, Which have four parts, 1st... PHP scriptIn a HTML document, use PHP to obtain the following :The user will write a sentence as a value of a predefined variable.The output should look like this :The sentence you entered is :This is my sentenceIt is composed of :• 4 different words• 19 characters (including spaces)Note : Your program should make sure the final output is in lower case with the first letter in upper case. 2nd... PHP...
For this assignment, create an html page that has a login form. The form should have...
For this assignment, create an html page that has a login form. The form should have 3 input elements -- 1. This should have a type text for the user to enter UserName 2. This should have a type password (like text except cannot see the text that is typed in), for the user to enter password. 3. Submit button (type submit, as we did in class on 2/6). The form method should be set to POST and the action...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS OR SWITCH STATEMENTS IN THE JAVASCRIPT. Even though there is stylesheet in the HTML file do no create a new one. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building...
For this assignment you'll be creating an application that has the user input a subtotal, tax...
For this assignment you'll be creating an application that has the user input a subtotal, tax rate and tip percentage and then displays the sales tax, tip amount and the total. You'll use JQuery instead of the getElementByX functions AND you will display all messages on the page (no alert or prompt boxes) The starter file is based off of the Lab 2 sales_tax application. Feel free to borrow code from your lab solution but realize you will need to...
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
Subject: Shell Scripting Practice A File: practice-script-a Create File practice-script-a that: 1. Accepts any number of...
Subject: Shell Scripting Practice A File: practice-script-a Create File practice-script-a that: 1. Accepts any number of userids on the command line 2. For each userid, display the userid, PID, CPU time, and current command for each process owned by that userid Your output should look similar to this example: practice-script-a doug.jones User: doug.jones PID: 8748 TIME: 00:00:00 COMMAND: /usr/lib/systemd/systemd --user User: doug.jones PID: 8749 TIME: 00:00:00 COMMAND: (sd-pam)    User: doug.jones PID: 8750 TIME: 00:00:00 COMMAND: sshd: doug.jones@pts/5 User: doug.jones...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT