**********NEEDS TO BE IN JAVA 1.4.2!!!!! NO SCANNER OR PRINTF PLEASE!***********
ID: 3584260 • Letter: #
Question
**********NEEDS TO BE IN JAVA 1.4.2!!!!! NO SCANNER OR PRINTF PLEASE!********************** 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
/*100% working code*/
import java.io.*;
public class Calc {
static int value1;
static int value2;
static int add(int value1, int value2) {
return value1 + value2;
}
static int subtract(int value1, int value2) {
return value1 - value2;
}
static int multiply(int value1, int value2) {
return value1 * value2;
}
static double divide(int value1, int value2) {
return (double) value1 / (double) value2;
}
public static void main(String[] args) {
int flag1 = 0;
int flag2 = 0;
String choice = null;
InputStreamReader console = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(console);
do {
try {
do {
flag1 = 0;
System.out.print("Enter the First number:");
String str1 = br.readLine();
try {
value1 = Integer.parseInt(str1);
} catch (Exception e) {
System.out.print("Invalid number ");
flag1 = 1;
}
} while (flag1 == 1);
do {
flag2 = 0;
System.out.print(" Enter the Second number:");
String str2 = br.readLine();
try {
value2 = Integer.parseInt(str2);
} catch (Exception e) {
System.out.print("Invalid number ");
flag2 = 1;
}
} while (flag2 == 1);
} catch (IOException e) {
}
System.out.println("Addition is:" + add(value1, value2));
System.out.println("Subtraction is :" + subtract(value1, value2));
System.out
.println("Multiplication is :" + multiply(value1, value2));
if (value2 == 0)
System.out.println("Division not possible");
else
System.out.println("Division is:" + divide(value1, value2));
System.out.print("Do You want to continue (y/n) :");
try {
choice = br.readLine();
} catch (Exception e) {
}
} while (choice.charAt(0) == 'y' || choice.charAt(0) == 'Y');
}
}
Output:
Enter the First number:34
Enter the Second number:454
Addition is:488
Subtraction is :-420
Multiplication is :15436
Division is:0.07488
Do You want to continue (y/n) :n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.