Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

From: Java Programming: From Problem Analysis to Program Design, 5th ed. Ch. 11,

ID: 3691662 • Letter: F

Question

From: Java Programming: From Problem Analysis to Program Design, 5th ed.

Ch. 11, page 782, Problem Exercise 7 (an extension of 6PE):

The Calculator programming example of this chapter is designed to perform operations on integers. Write a similar program that can be used to perform operations on decimal numbers. (Note: If division by zero occurs with values of the int data type, the program throws a division by zero exception. However, if you divide a decimal number by zero, Java does not throw the division by zero exception; it returns the answer as infinity. However, if division by zero occurs, your calculator program must output the message ERROR: / by zero.)

Explanation / Answer

import java.util.Scanner; class Calculator { public static void main(String[] args) { double x, y; String operation; Scanner input = new Scanner(System.in); while (true) { System.out.println("please enter the first number"); x = input.nextDouble(); System.out.println("please enter the second number"); y = input.nextDouble(); Scanner op = new Scanner(System.in); System.out.println("Please enter the operation you want to use"); operation = op.next(); if (operation.equals("+")) { System.out.println("The answer is " + (x + y)); } else if (operation.equals("-")) { System.out.println("The answer is " + (x - y)); } else if (operation.equals("*")) { System.out.println("The answer is " + (x * y)); } else if (operation.equals("/")) { if (y == 0) System.out.println("ERROR: / by zero."); else System.out.println("The answer is " + (x / y)); } else { System.out.println("Invalid operation!"); } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote