HarrJ

Day 12 calcu More

Feb 13th, 2024 (edited)
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1. package week2;
  2.  
  3. import java.util.Scanner;
  4. public class Day12B {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         double num1=0, num2=0, result;
  8.         String userInput, op = "";
  9.         String[] opList = {"+","-","*","/","^"};
  10.         boolean isValid = false;
  11.        
  12. //        while (!isValid) {
  13.         while (isValid == false) {
  14.             System.out.print("Enter num A: ");
  15.             userInput = sc.nextLine();
  16.             isValid = isNumeric(userInput);
  17.             if (isValid == true) {
  18.                 num1 = Double.parseDouble(userInput);
  19.             } else {
  20.                 System.out.println("please try again");
  21.             }
  22.         }
  23.         isValid = false;
  24.         while (isValid == false) {
  25.             System.out.print("Enter operation(+,-,*,/,^): ");
  26.             userInput = sc.nextLine();
  27.             for (String string : opList) {
  28.                 if (userInput.equals(string)) {
  29.                     isValid = true;
  30.                 }
  31.             }
  32.             if (isValid == true) {
  33.                 op = userInput;
  34.             } else {
  35.                 System.out.println("please try again");
  36.             }
  37.         }
  38.         isValid = false;
  39.         while (isValid == false) {
  40.             System.out.print("Enter num B: ");
  41.             userInput = sc.nextLine();
  42.             isValid = isNumeric(userInput);
  43.             if (isValid == true) {
  44.                 num2 = Double.parseDouble(userInput);
  45.             } else {
  46.                 System.out.println("please try again");
  47.             }
  48.         }
  49.         result = compute(num1, num2, op);
  50.         System.out.printf("%.2f %s %.2f = %.2f %n", num1, op, num2, result);
  51.     }
  52.    
  53.     static boolean isNumeric(String txtIn) {
  54.         boolean isNumber = true;
  55.         if (txtIn.trim().isEmpty()) {
  56.             isNumber = false;
  57.         }
  58.         try {
  59.             double d = Double.parseDouble(txtIn);
  60.         } catch (Exception e) {
  61.             isNumber = false;
  62.         }
  63.         return isNumber;
  64.     }
  65.    
  66.     static double compute(double num1, double num2, String op){
  67.         double result;
  68.         switch (op) {
  69.             case "+" :
  70.                 result = num1 + num2;
  71.                 break;
  72.             case "-" :
  73.                 result = num1 - num2;
  74.                 break;
  75.             case "*" :
  76.                 result = num1 * num2;
  77.                 break;
  78.             case "/" :
  79.                 if (num2 > 0) {
  80.                     result = num1 / num2;
  81.                 } else {
  82.                     System.out.println("cannot divide by zero");
  83.                     result = 0;
  84.                 }
  85.                 break;
  86.             case "^" :
  87.                 result = Math.pow(num1, num2);
  88.                 break;
  89.             default:
  90.                 result = 0;
  91.         }
  92.         return result;
  93.     }
  94. }
  95. //----- ------------ ------- ----- --
  96. package week2;
  97.  
  98. abstract class Day12CalcuAbstract {
  99.     abstract double enterNumber();
  100.     abstract String enterOperator();
  101.        
  102.     static boolean isNumeric(String txtIn) {
  103.         boolean isNumber = true;
  104.         if (txtIn.trim().isEmpty()) {
  105.             isNumber = false;
  106.         }
  107.         try {
  108.             double d = Double.parseDouble(txtIn);
  109.         } catch (Exception e) {
  110.             isNumber = false;
  111.         }
  112.         return isNumber;
  113.     }
  114.    
  115.     static double compute(double num1, double num2, String op){
  116.         double result;
  117.         switch (op) {
  118.             case "+" :
  119.                 result = num1 + num2;
  120.                 break;
  121.             case "-" :
  122.                 result = num1 - num2;
  123.                 break;
  124.             case "*" :
  125.                 result = num1 * num2;
  126.                 break;
  127.             case "/" :
  128.                 if (num2 > 0) {
  129.                     result = num1 / num2;
  130.                 } else {
  131.                     System.out.println("cannot divide by zero");
  132.                     result = 0;
  133.                 }
  134.                 break;
  135.             case "^" :
  136.                 result = Math.pow(num1, num2);
  137.                 break;
  138.             default:
  139.                 result = 0;
  140.         }
  141.         return result;
  142.     }
  143. }
  144.  
  145. //-------------- ------- ------- ----- ------
  146. package week2;
  147.  
  148. import java.util.Scanner;
  149. public class Day12D extends Day12CalcuAbstract{
  150.     Scanner sc = new Scanner(System.in);
  151.     String userInput = "";
  152.     String[] opList = {"+","-","*","/","^","x"};
  153.    
  154.     public static void main(String[] args) {
  155.         Day12D callMe = new Day12D();
  156.         double num1=0, num2=0, result;
  157.         String op = "";
  158.        
  159.         System.out.println("type x for operator to end");
  160.         num1 = callMe.enterNumber();
  161.         while (!op.equalsIgnoreCase("x")) {
  162.             op = callMe.enterOperator();
  163.             if (op.equalsIgnoreCase("x")) {
  164.                 break;
  165.             }
  166.             num2 = callMe.enterNumber();
  167.             if (!op.equalsIgnoreCase("x")) {
  168.                 result = compute(num1, num2, op);
  169.                 System.out.printf("%.2f %s %.2f = %.2f %n", num1, op, num2, result);
  170.                 num1 = result;
  171.             }
  172.         }
  173.     }
  174.    
  175.     double enterNumber(){
  176.         double numOut = 0;
  177.         boolean isValid = false;
  178.         while (isValid == false) {
  179.             System.out.print("Enter number: ");
  180.             userInput = sc.nextLine();
  181.             isValid = isNumeric(userInput);
  182.             if (isValid == true) {
  183.                 numOut = Double.parseDouble(userInput);
  184.             } else {
  185.                 System.out.println("please try again");
  186.             }
  187.         }
  188.         return numOut;
  189.     }
  190.    
  191.     String enterOperator(){
  192.         String opr = "X";
  193.         boolean isValid = false;
  194.         while (isValid == false) {
  195.             System.out.print("Enter operation(+,-,*,/,^): ");
  196.             userInput = sc.nextLine();
  197.             for (String string : opList) {
  198.                 if (userInput.equalsIgnoreCase(string)) {
  199.                     isValid = true;
  200.                 }
  201.             }
  202.             if (isValid == true) {
  203.                 opr = userInput;
  204.             } else {
  205.                 System.out.println("please try again");
  206.             }
  207.         }
  208.         return opr;
  209.     }
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment