Question

In: Computer Science

Goals Practice conditional statements Description Write a program to simulate a menu driven calculator that performs...

Goals Practice conditional statements Description Write a program to simulate a menu driven calculator that performs basic arithmetic operations (add, subtract, multiply and divide). The calculator accepts two numbers and an operator from user in a given format: For example: Input: 6.3 / 3 Output: 2.1 Create a Calculator class that has a method to choose the right mathematical operation based on the entered operator using switch case. It should then call a corresponding method to perform the operation. In the main method of the testing class create an object of Calculator and accept user’s input using Scanner object, In java .

Solutions

Expert Solution

input code:

output:

code:

import java.util.*;
/*make a class */
class Calculator
{
/*declre the data memberss*/
double num1,num2;
/*parameterise constructor*/
Calculator(double n1,double n2)
{
num1=n1;
num2=n2;
}
/*Addition the numbers*/
double Addition()
{
return num1+num2;
}
/*substraction the number*/
double Substraction()
{
return num1-num2;
}
/*Multipication the numbers*/
double Multipication()
{
return num1*num2;
}
/*divide the numbers*/
double divide()
{
return num1/num2;
}
}
public class Main
{
   public static void main(String[] args)
   {
   /*make object of Scanner*/
   Scanner sc=new Scanner(System.in);
       System.out.print("Addition(+)\nSubstraction(-)\nMultipication(*)\nDivide(/)\nEnter the input: ");
       /*take user input*/
       double a=sc.nextDouble();
       char op=sc.next().charAt(0);
       double b=sc.nextDouble();
       /*make object of Calculator class*/
       Calculator c=new Calculator(a,b);
       /*make switch*/
       switch(op)
       {
       case '+': /*call Addition function*/
       System.out.print(a+" + "+b+" = "+c.Addition());
       break;
       case '-': /*call Substraction function*/
       System.out.print(a+" - "+b+" = "+c.Substraction());
       break;
       case '*': /*call Multipication function*/
       System.out.print(a+" * "+b+" = "+c.Multipication());
       break;
       case '/': /*call divide function*/
       System.out.print(a+" / "+b+" = "+c.divide());
       break;
       }
   }
}


Related Solutions

Goals Practice loops and conditional statements Description Create a game for elementary school students to practice...
Goals Practice loops and conditional statements Description Create a game for elementary school students to practice multiplication. The application should display a greeting followed by a set of mathematical questions.  In each set the player is asked to enter a number from 1 to 9 to test multiplication skills or to press ‘E’ to exit the game.  The application checks if the number entered is indeed between 1 and 9 and if not displays a message inviting the...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Goals: Concepts related to conditional statements in Python. This is a Python program. You DON’T need...
Goals: Concepts related to conditional statements in Python. This is a Python program. You DON’T need loops for this. Below is the programming task. An employee is paid at a rate of $17.25 per hour for up to 40 regular hours worked in a week. If the employee works more than 40 hours, it is considered overtime. Any hours over 40 hours but less than or equal to 60 are paid at the overtime rate ($12.34 per hour). Any hours...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers,...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers, and multi-dimensional dynamic arrays Create a simple makefile Write a robust input validation. The purpose of this lab is to get familiar with the software environment we will be using this term, and review some C++ concepts to get prepared for future projects and labs. This lab might be very time-consuming since there are a lot of concepts you need to pick up. For...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT