Question

In: Computer Science

Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers....

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

Solutions

Expert Solution

<!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>


Related Solutions

Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator. For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the...
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator: The calculator has one “accumulator”, which holds the result of all prior calculations. The calculator is set to 0.0 when the calculator is turned on (i.e., instantiated). On the console, the user then enters an operator (+, -, *, or /) and a number. Your console will then apply the operator and operand to the number in the accumulator, and...
How to add, multiply, and divide numbers in flowgorithm
How to add, multiply, and divide numbers in flowgorithm
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
Briefly explain why there are no immediate multiply or divide integer ALU instructions for MIPS.
Briefly explain why there are no immediate multiply or divide integer ALU instructions for MIPS.
Tip Calculator Instructions For this project you are to create a tip calculator that will take...
Tip Calculator Instructions For this project you are to create a tip calculator that will take a decimal/or non decimal amount and calculate the different tip values of that amount: 10% 15% 20% And display them appropriately. The values will be set to two decimal places. If you have an empty value, then a message will display informing the user of such. When the device is rotated the error message or tip information must continue to be displayed. in Android/Java
C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping...
C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping cart and get the total price of purchases made. Items are simply described by an Item class as follows: class Item {   public:      std :: String description;      float price; }; The ShopCart class must be able to add and remove items and display an invoice. This class must use a dynamically allocated array of items whose capacity is fixed in advance to the build....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT