Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week2;
- import java.util.Scanner;
- public class Day12B {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- double num1=0, num2=0, result;
- String userInput, op = "";
- String[] opList = {"+","-","*","/","^"};
- boolean isValid = false;
- // while (!isValid) {
- while (isValid == false) {
- System.out.print("Enter num A: ");
- userInput = sc.nextLine();
- isValid = isNumeric(userInput);
- if (isValid == true) {
- num1 = Double.parseDouble(userInput);
- } else {
- System.out.println("please try again");
- }
- }
- isValid = false;
- while (isValid == false) {
- System.out.print("Enter operation(+,-,*,/,^): ");
- userInput = sc.nextLine();
- for (String string : opList) {
- if (userInput.equals(string)) {
- isValid = true;
- }
- }
- if (isValid == true) {
- op = userInput;
- } else {
- System.out.println("please try again");
- }
- }
- isValid = false;
- while (isValid == false) {
- System.out.print("Enter num B: ");
- userInput = sc.nextLine();
- isValid = isNumeric(userInput);
- if (isValid == true) {
- num2 = Double.parseDouble(userInput);
- } else {
- System.out.println("please try again");
- }
- }
- result = compute(num1, num2, op);
- System.out.printf("%.2f %s %.2f = %.2f %n", num1, op, num2, result);
- }
- static boolean isNumeric(String txtIn) {
- boolean isNumber = true;
- if (txtIn.trim().isEmpty()) {
- isNumber = false;
- }
- try {
- double d = Double.parseDouble(txtIn);
- } catch (Exception e) {
- isNumber = false;
- }
- return isNumber;
- }
- static double compute(double num1, double num2, String op){
- double result;
- switch (op) {
- case "+" :
- result = num1 + num2;
- break;
- case "-" :
- result = num1 - num2;
- break;
- case "*" :
- result = num1 * num2;
- break;
- case "/" :
- if (num2 > 0) {
- result = num1 / num2;
- } else {
- System.out.println("cannot divide by zero");
- result = 0;
- }
- break;
- case "^" :
- result = Math.pow(num1, num2);
- break;
- default:
- result = 0;
- }
- return result;
- }
- }
- //----- ------------ ------- ----- --
- package week2;
- abstract class Day12CalcuAbstract {
- abstract double enterNumber();
- abstract String enterOperator();
- static boolean isNumeric(String txtIn) {
- boolean isNumber = true;
- if (txtIn.trim().isEmpty()) {
- isNumber = false;
- }
- try {
- double d = Double.parseDouble(txtIn);
- } catch (Exception e) {
- isNumber = false;
- }
- return isNumber;
- }
- static double compute(double num1, double num2, String op){
- double result;
- switch (op) {
- case "+" :
- result = num1 + num2;
- break;
- case "-" :
- result = num1 - num2;
- break;
- case "*" :
- result = num1 * num2;
- break;
- case "/" :
- if (num2 > 0) {
- result = num1 / num2;
- } else {
- System.out.println("cannot divide by zero");
- result = 0;
- }
- break;
- case "^" :
- result = Math.pow(num1, num2);
- break;
- default:
- result = 0;
- }
- return result;
- }
- }
- //-------------- ------- ------- ----- ------
- package week2;
- import java.util.Scanner;
- public class Day12D extends Day12CalcuAbstract{
- Scanner sc = new Scanner(System.in);
- String userInput = "";
- String[] opList = {"+","-","*","/","^","x"};
- public static void main(String[] args) {
- Day12D callMe = new Day12D();
- double num1=0, num2=0, result;
- String op = "";
- System.out.println("type x for operator to end");
- num1 = callMe.enterNumber();
- while (!op.equalsIgnoreCase("x")) {
- op = callMe.enterOperator();
- if (op.equalsIgnoreCase("x")) {
- break;
- }
- num2 = callMe.enterNumber();
- if (!op.equalsIgnoreCase("x")) {
- result = compute(num1, num2, op);
- System.out.printf("%.2f %s %.2f = %.2f %n", num1, op, num2, result);
- num1 = result;
- }
- }
- }
- double enterNumber(){
- double numOut = 0;
- boolean isValid = false;
- while (isValid == false) {
- System.out.print("Enter number: ");
- userInput = sc.nextLine();
- isValid = isNumeric(userInput);
- if (isValid == true) {
- numOut = Double.parseDouble(userInput);
- } else {
- System.out.println("please try again");
- }
- }
- return numOut;
- }
- String enterOperator(){
- String opr = "X";
- boolean isValid = false;
- while (isValid == false) {
- System.out.print("Enter operation(+,-,*,/,^): ");
- userInput = sc.nextLine();
- for (String string : opList) {
- if (userInput.equalsIgnoreCase(string)) {
- isValid = true;
- }
- }
- if (isValid == true) {
- opr = userInput;
- } else {
- System.out.println("please try again");
- }
- }
- return opr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment