Java Gator is in town, and everyone knows that the Java Gator is addicted to num
ID: 3634698 • Letter: J
Question
Java Gator is in town, and everyone knows that the Java Gator is addicted to numbers.If someone doesn't develop a simple calculator for the
"Calcu-Gator" he just might throw a fit!
Write a program that simulates a simple calculator. The calculator has the ability to have two real numbers inputted and then:
*multiply the two numbers
*divide the first number by the second number (be sure to check for division by 0 and provide an error message if this occurs)
*add the two numbers together
*subtract the second number from the first number
Example: User inputs 10 and 5, Output would be:
Multiplied = 50, Divided = 2, Added = 15, Subtracted = 5
Create a method for each of the four tasks. The main method should ask for the numbers and display the results of each of the four tasks. Have the program repeat until the user tells the program to stop.
****ALSO: Java Gator Says he will stop coming by if someone would just write him a personal Class called Calc.java so that he can make his own calculator objects! this class would contain 4 behaviors for each of the methods, and two data fields to hold the users input, it could also have more behaviors for input and output and a menu, but that's all up to you.
Explanation / Answer
import java.util.*; public class Calc { public static void main(String[] args) { double a,b; Scanner sc = new Scanner(System.in); while (sc.hasNextDouble()){ a = sc.nextDouble(); b = sc.nextDouble(); if (b == 0){ System.out.println("Can't be divided by zero."); System.out.printf("Multiplied = %d, Divided = NA, Added = %d, Subtracted = %d", multiple(a,b), add(a,b), subtract(a,b)); }else { System.out.printf("Multiplied = %d, Divided = %.2f, Added = %d, Subtracted = %d", multiple(a,b), divide(a,b), add(a,b), subtract(a,b)); } } } public static double multiple(double a,double b){ return a*b; } public static double divide(double a,double b){ return a/b; } public static double add(double a,double b){ return a+b; } public static double subtract(double a,double b){ return a-b; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.