Question

Could someone re-write this code so that it first prompts the user to choose an option...

Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things.

------

import java.util.*;

abstract class CalculatorNumVals
{
int num1,num2;

CalculatorNumVals(int value1,int value2)
{
num1=value1;
num2=value2;
}

public String toString()
{
return "Numbers are "+num1+"\t"+num2+"\n";
}

abstract void add();
abstract void subtract();
abstract void multiply();
abstract void division();
}

class CalFunctions extends CalculatorNumVals
{
CalFunctions(int value1,int value2)
{
super(value1,value2);
}

void add()
{
int ans;
ans=num1+num2;
System.out.println("Addition of two numbers is "+ans);
System.out.println();
}

void subtract()
{
int ans;
ans=num1-num2;
System.out.println("Subtraction of two numbers is "+ans);
System.out.println();
}

void multiply()
{
int ans;
ans=num1*num2;
System.out.println("Multiplication of two numbers is "+ans);
System.out.println();
}

void division()
{
double ans;
ans=(double)num1/num2;
System.out.println("Division of two numbers is "+ans);
System.out.println();
}
}
class CalculatorDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int val1,val2,choice;

System.out.print("\nEnter number 1 : ");
val1=sc.nextInt();
System.out.print("Enter number 2 : ");
val2=sc.nextInt();
System.out.println("\nOperations of Calculator :");
System.out.println("-----------------------------------------------------------------");
System.out.println("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
System.out.println("-----------------------------------------------------------------\n");
CalFunctions cal=new CalFunctions(val1,val2);
System.out.println(cal);

System.out.print("Enter your choice : ");
choice=sc.nextInt();

System.out.println();

switch(choice)
{
case 1:cal.add();
break;
case 2:cal.subtract();
break;
case 3:cal.multiply();
break;
case 4:cal.division();
break;
default:System.out.println("Invalid case\n");
break;
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

abstract class CalculatorNumVals {

int num1, num2;

CalculatorNumVals(int value1, int value2) {

num1 = value1;

num2 = value2;

}

public String toString() {

return "Numbers are " + num1 + "\t" + num2 + "\n";

}

abstract void add();

abstract void subtract();

abstract void multiply();

abstract void division();

}

class CalFunctions extends CalculatorNumVals {

CalFunctions(int value1, int value2) {

super(value1, value2);

}

void add() {

int ans;

ans = num1 + num2;

System.out.println("Addition of two numbers is " + ans);

System.out.println();

}

void subtract() {

int ans;

ans = num1 - num2;

System.out.println("Subtraction of two numbers is " + ans);

System.out.println();

}

void multiply() {

int ans;

ans = num1 * num2;

System.out.println("Multiplication of two numbers is " + ans);

System.out.println();

}

void division() {

double ans = 0;

try {

ans = (double) num1 / num2;

} catch (Exception e) {

System.out.println(e.getMessage());

}

System.out.println("Division of two numbers is " + ans);

System.out.println();

}

}

class CalculatorDemo {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

int val1, val2, choice = 0;

while (choice != -1) {

System.out.println("\nOperations of Calculator :");

System.out.println("-----------------------------------------------------------------");

System.out.println("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n 5 Exit");

System.out.println("-----------------------------------------------------------------\n");

System.out.print("Enter your choice : ");

choice = sc.nextInt();

if (choice == -1)

break;

System.out.print("\nEnter number 1 : ");

val1 = sc.nextInt();

System.out.print("Enter number 2 : ");

val2 = sc.nextInt();

System.out.println();

CalFunctions cal = new CalFunctions(val1, val2);

System.out.println(cal);

switch (choice) {

case 1:

cal.add();

break;

case 2:

cal.subtract();

break;

case 3:

cal.multiply();

break;

case 4:

cal.division();

break;

case 5:

choice = -1;

break;

default:

System.out.println("Invalid case\n");

break;

}

}

}

}

Console X CalculatorDemo [Java Application] C:\Program FilesJavare1.8.0_144\binjavaw.exe (Nov 22, 2018, 9:42:13 AM) Enter your choice: 1 Enter number 1 10 Enter number 2 20 Numbers are 10 20 Addition of two numbers is 30 Operations of Calculator: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5 Exit Enter your choice: 1 Enter number 1 10 Enter number 2 12 Numbers are 10 Addition of two numbers is 2 12 Operations of Calculator: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5 Exit Enter your choice:

Add a comment
Know the answer?
Add Answer to:
Could someone re-write this code so that it first prompts the user to choose an option...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • ram reads user input with command line argument, two inte ment, two integers and in between...

    ram reads user input with command line argument, two inte ment, two integers and in between tor. The programme will determine whether the input is valid, an rompt info if not. Read the codes, design the isNumber0 method t progran an operator. The program. (5 marks) public class ArithmeticTest ( public static void main(Stringl] args) ( if (isNumber(args[e]) & isNumber(args[2])) ( int value1 Integer.parseInt (args[e]); int value Integer.parseInt (args [2]); char op args[1].charAt (e); calcAndDisplay (value1, value2, op); ) else...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    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;   ...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // DebugFive2.java // Decides if two numbers are evenly divisible import java.util.Scanner; public class DebugFive2 { public static void main(String args[]) { int num; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter a number "); num = input.nextInteger() System.out.print("Enter another number ");...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT