This Java code is a simple console-based calculator that allows users to perform various mathematical operations. The calculator supports addition, subtraction, multiplication, division, square, cube, square root, and power operations. Users can navigate through the calculator’s functionalities by selecting options from a main menu displayed in the console.
The code structure includes a class named “Calculator” with static variables for input values and a user interface (UI) method to display the main menu. The main functionality is implemented through separate methods for each mathematical operation, ensuring a modular and organized approach. Input validation is incorporated to handle potential errors, such as entering invalid characters or excessively long input strings.
The calculator code serves as a good starting point for a novice programmer to understand the basics of modular programming and user input handling. It offers a practical example of implementing a simple calculator with various mathematical operations. The novice programmer can further enhance the code by addressing the mentioned issues and possibly adding more features or improving the user interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | import java.io.*; import java.math.*; public class Calculator { static double numAdd1 = 0, numAdd2 = 0; static double numSub1 = 0, numSub2 = 0, numMul1 = 0; static double numMul2 = 0, numDiv1 = 0, numDiv2 = 0; static double numSqr1 = 0, numCube1 = 0, numPow1 = 0; static double numPow2 = 0, numSqrt1 = 0; static int choice; static String myString; //makes the full user interface at start up public static int UI() throws Exception { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("\n\n****************************\nWel Come to Java Language"); System.out.println(" CALCULATOR \n*****************************"); System.out.println("0:\tEXIT()"); System.out.println("1:\tAdd two Numbers"); System.out.println("2:\tSubtract two Numbers"); System.out.println("3:\tMultiply two Numbers"); System.out.println("4:\tDivide two Numbers"); System.out.println("5:\tSquare of a number"); System.out.println("6:\tCube of a number"); System.out.println("7:\tFind the SQUARE-ROOT of a Number"); System.out.println("8:\tFind the X power Y"); choice = Integer.parseInt(input.readLine()); return choice; } //Calculate the Addition of two numbers public static double add(double numAdd1, double numAdd2) { return numAdd1 + numAdd2; } //Calculate the subtraction of two numbers public static double sub(double numSub1, double numSub2) { return numSub1 - numSub2; } //Calculate the multiplication of two numbers public static double multiply(double numMul1, double numMul2) { return numMul1 * numMul2; } //Calculate the Division of two numbers public static double divide(double numDiv1, double numDiv2) { return numDiv1 / numDiv2; } //Calculate the Square of a numbers public static double square(double numSqr1) { return numSqr1 * numSqr1; } //Calculate the Cube of a numbers public static double cube(double numCube1) { return numCube1 * numCube1 * numCube1; } //Calculate the SQUARE-ROOT of a numbers public static double squareRoot(double numSqrt1) { return Math.sqrt(numSqrt1); } //Calculate the power of numbers public static double power(double numpow1, double numPow2) { return Math.pow(numPow1, numPow2); } //press any key to Goto Main Menu public static void mainMenu() { System.out.print("Press Enter key....."); try { System.in.read(); } catch (IOException e) { return; } } //Function to check the input validation public static boolean checkInput(String str) { int stringLength = str.length(); if (stringLength >= 300) { return false; } for (int i = 0; i < stringLength - 1; i++) if (str.charAt(i) <= 0 || str.charAt(i) >= 9) return false; return true; } //main function public static void main(String[] args) throws Exception { boolean isValidInput; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); choice = UI(); while (choice != 0) { switch (choice) { case 0: return; case 1: //Add two numbers code Code System.out.println("\nEnter First Number"); String str = input.readLine(); isValidInput = checkInput(str); if (isValidInput == true) { numAdd1 = Double.parseDouble(str); } else { System.out.println("\n****************\nInput ERROR\n************** "); } System.out.println("\nEnter second Number"); str = input.readLine(); isValidInput = checkInput(str); if (isValidInput == true) { numAdd2 = Double.parseDouble(str); double numAddSum = add(numAdd1, numAdd2); System.out.println("\n**********\nThe Sum is= " + numAddSum + "\n********** "); } else { System.out.println("\n****************\nInput ERROR\n************** "); } mainMenu(); UI(); break; case 2: //Code //subtract two numbers code Code System.out.println("\nEnter First Number"); numSub1 = Double.parseDouble(input.readLine()); System.out.println("\nEnter second Number"); numSub2 = Double.parseDouble(input.readLine()); double numSub = sub(numSub1, numSub2); System.out.println("\n**********\nThe Difference is= " + numSub + "\n********** "); mainMenu(); UI(); break; case 3: //Code //subtract two numbers code Code System.out.println("\nEnter First Number"); numMul1 = Double.parseDouble(input.readLine()); System.out.println("\nEnter second Number"); numMul2 = Double.parseDouble(input.readLine()); double numMul = multiply(numMul1, numMul2); System.out.println("\n**********\nThe Multiplication is= " + numMul + "\n********** "); mainMenu(); UI(); break; case 4: //Code //Divide two numbers code Code System.out.println("\nEnter First Number"); numDiv1 = Double.parseDouble(input.readLine()); System.out.println("\nEnter second Number"); numDiv2 = Double.parseDouble(input.readLine()); double numDiv = divide(numDiv1, numDiv2); System.out.println("\n**********\nThe Division is= " + numDiv + "\n********** "); mainMenu(); UI(); break; case 5: //Code //square of a number code System.out.println("\nEnter a Number"); numSqr1 = Double.parseDouble(input.readLine()); double numSqr = square(numSqr1); System.out.println("\n**********\nThe SQUARE is= " + numSqr + "\n********** "); mainMenu(); UI(); break; case 6: //Code //cube of a number code System.out.println("\nEnter a Number"); numCube1 = Double.parseDouble(input.readLine()); double numCube = cube(numCube1); System.out.println("\n**********\nThe CUBE is= " + numCube + "\n********** "); mainMenu(); UI(); break; case 7: //Code //square-root of a numbver System.out.println("\nEnter a Number"); numSqrt1 = Double.parseDouble(input.readLine()); double numSqrt = squareRoot(numSqrt1); System.out.println("\n**********\nThe SQUARE-ROOT is= " + numSqrt + "\n********** "); mainMenu(); UI(); break; case 8: //Code //Divide two numbers code Code System.out.println("\nEnter a Number"); numPow1 = Double.parseDouble(input.readLine()); System.out.println("\nEnter a 2nd Number"); numPow2 = Double.parseDouble(input.readLine()); double numPow = power(numPow1, numPow2); System.out.println("\n**********\nThe " + numPow1 + " power " + numPow2 + " is= " + numPow + "\n********** "); mainMenu(); UI(); break; default: UI(); break; } } } } |