a. You must use proper error and exception handling (try, catch) in the application. [5 marks]
b. Accept the users first and last name store in two SEPARATE variables. [2 marks]
c. Accept two numbers:
i. validate that the user has entered numbers for each input. [3 marks]
ii. For invalid numbers ask the user to re-enter the number [2 marks]
iii. If the user enters a number greater than 500 throw an exception, display a message that numbers should be less than 1000. [3 marks]
d. Give the user the option to do any one of the following calculations (i.e prompt for user input):
· add both numbers
· multiply both numbers
· divide the second number by the first.
· subtract the first number from the second.
You must use an if-Else-Elseif to evaluate the user selection. [3 marks]
e. After the calculation is done if the result is greater than 50 multiply the result by two and subtract 4. If the result is less than 50 but greater than 10 multiply the result by 7 and add 2. If the result is less than 10 do nothing. You must use a switch statement to get full marks. [5 marks]
f. Display the users first and last name along with the calculation result on the screen in this format> “Hello <first name> <last name> your answer is <answer> [2 marks]
The user should have the option to repeat the process of entering two numbers until they enter the word “END” to exit the application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ErrorHandling
{
class Program
{
static void Main(string[] args)
{
//Two seperate string variables to store user's first and last names
string firstName, lastName;
//Two seperate integer variable to store two numbers
double num1 = 0, num2 = 0;
bool validNum1 = false,validNum2 = false;
Console.WriteLine("=====================================");
Console.WriteLine("\tPROGRAM EXECUTION");
Console.WriteLine("=====================================");
//Read user's first name and last name
Console.Write("Enter your first name : ");
firstName = Console.ReadLine();
Console.Write("Enter your last name : ");
lastName = Console.ReadLine();
Console.WriteLine();
//Accept two numbers
//Validate that the user has entered numbers for each input.
string choice = "";
//Loop until the user want's to exit
while (!choice.Equals("END", StringComparison.InvariantCultureIgnoreCase))
{
//Re-prompt the user to enter number1 if invalid number is entered
while (validNum1 == false)
{
//Using try catch statements
try
{
//Read first number
Console.Write("Enter first number : ");
string n1 = Console.ReadLine();
//If the entered input is a number
if (double.TryParse(n1, out num1))
//If it is greater than 500 throws exception
if (num1 > 500) { throw new Exception(); }
//else set flag to true
else { validNum1 = true; }
else
//If input is not a number throws formatted exception
throw new FormatException();
}
catch (FormatException) { Console.WriteLine("Please enter number's only...Try again"); }
catch (Exception) { Console.WriteLine("Numbers should be less than 1000."); }
}
while (validNum2 == false)
{
try
{
//Read second number
Console.Write("Enter second number : ");
string n2 = Console.ReadLine();
//If the entered input is a number
if (double.TryParse(n2, out num2))
//If it is greater than 500 throws exception
if (num2 > 500){ throw new Exception(); }
//else set flag to true
else { validNum2 = true; }
else
//If input is not a number throws formatted exception
throw new FormatException();
}
catch (FormatException) { Console.WriteLine("Please enter number's only...Try again"); }
catch (Exception) { Console.WriteLine("Numbers should be less than 1000."); }
}
int option = 1;
double answer = 0;
//Display optiona to user
Console.WriteLine("\n=====================================");
Console.WriteLine("Select one of the following operations : ");
Console.WriteLine("1. Add both numbers\n2. Multiply both numbers");
Console.WriteLine("3. Divide the second number by the first\n4. Subtract the first number from the second.");
//Read user option
Console.Write("Enter your choice: ");
option = int.Parse(Console.ReadLine());
//Validate user option
while (option != 1 && option != 2 && option != 3 && option != 4)
{
Console.WriteLine("Invalid Choice...Try again...");
Console.Write("Enter your choice : ");
option = int.Parse(Console.ReadLine());
}
//Perform operation based on the option
if (option == 1) { answer = num1 + num2; }
else if (option == 2) { answer = num1 * num2; }
else if (option == 3) { answer = num2 / num1; }
else if (option == 4) { answer = num2 - num1; }
//Switch for further calculation
int ch = (answer > 50) ? 1 : ((answer > 10 && answer < 50) ? 2 : 3);
switch (ch)
{
case 1:
answer = (answer * 2) - 4;
break;
case 2:
answer = (answer * 7) + 2;
break;
case 3:
break;
}
//Display output
Console.WriteLine("Hello " + firstName + " " + lastName + " your answer is " + answer);
validNum1 = false; validNum2 = false;
Console.WriteLine("=====================================\n");
//Prompt user whether the user wants to proceed or not
Console.Write("Press END to exit...Any other key to continue...");
choice = Console.ReadLine();
}
}
}
}
Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...
SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){ int num1,num2; cout << "Enter two numbers "<< endl; cout << "First :"; cin >> num1; cout << "Second :"; cin >>num2; int result=num1+num2; cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result; ...
Purpose: To familiarize yourself with variables and arithmetic. Directions: Convert the following problems below to c++ equivalent code. Each problem should be done within the same file and all within the same main function. After each step is performed the user should be shown the updated value that was changed. Turn in the finished c++ (.cpp) file for problem 0-6 and the image file for problem 7. Remember to reset or use unique variables for each problem (you cannot declare...
Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place...
in a java application need to create an application which prompts the user numerator and denominator values then show the result of the division. The application will use exception handling to verify the user has entered valid input and will continue to prompt the user for input as long as the value they enter is invalid. Once the user has entered valid numerator and denominator values, the result is shown and the application exits. had some issues when I entered letters instead of...
Write a C program requesting the user to input two integer numbers and then requesting the user to either add, subtract, or multiply the two numbers (choose 0 to add, 1 to subtract, or 2 to multiply. Declares three separate functions for these choices to come after main(). Program needs to use a pointer to these three functions to perform the requested action. Must print to output.
C++
developing involves ut from a file a person's first name, last name, phone number and birth a menu driven database application. You need to 4) This program accept as input ogram will be menu driven with the following options: 1) Find a person's information 2) Add a person to the database 3) Edit a person's information 4) Display all records to the screen 5) Quit Option 1 allows the user to enter a name after which you will search...
Please make a JAVA program for the following using switch structures Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the...
Create a windows form application in c# The form must look like a calculator and do the following. Add, subtract, multiply, and divide. Account for division by zero. Show at least two decimal places. Retain the last number on the window so that, when the user opens the calculator the next time, the number that was in the window at the last close appears. Have a memory, so that users can store values and recall them. Operate with a mouse...
This is Python
The program should accept input from the user as either 7-digit
phone number or 10-digit. If the user enters 7 characters, the
program should automatically add "512" to the beginning of the
phone number as the default area code. Dash (hyphen) characters do
not count in input character count, but must not be random. If the
user enters dashes the total character count must not exceed
12.
The program should not crash if the user enters invalid...