Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

---------------------------------------------------------------- HERE IS A SHELL

ID: 3849430 • Letter: #

Question

----------------------------------------------------------------

HERE IS A SHELL THAT I WAS PROVIDED

---------------------------------------------------------------

HERE IS THE CODE THAT I ATTEMPTED:

  1 import java.util.Scanner;
2 public class CaculatorShell
3 {
4 // the code for the main method is complete. once you implement all other methdos you can the program
5 public static void main(String[] args)
6 {
7
8
9 Scanner kb = new Scanner(System.in);
10 System.out.print("Welcome to my calculator website");
11
12 description();
13 for(int count =1; count <= 10; count++)
14 {
15 System.out.print("Enter your expression:");
16 String exp = kb.nextLine();
17 char semi = exp.charAt(exp.length() -1);
18 if(validFormat(exp) && semi != -1)
19 {
20 String op1 = token(exp);
21 //int operand1 = Integer.parseInt(op1);
22
23 exp = removeToken(exp);
24
25 String operation = token(exp);
26 exp = removeToken(exp);
27
28 String op2 = token(exp).substring(0,exp.length() -1);
29 //first we need to check if it is a valid character 0-9
30 if (validCharacter(op1) && validCharacter(op2))
31 {
32 int operand1 = Integer.parseInt(op1);
33 int operand2 = Integer.parseInt(op2);
34 //checking to see if it is a single digit number
35 if (validOp(operand1) && validOp(operand2) && validOperation(operation))
36 {
37 int result = calculate(operand1, operand2, operation);
38 output(operand1, operand2, operation, result);
39 }
40 else
41 {
42 System.out.println("please check your expression");
43 description();
44 }
45 }
46 else
47 {
48 System.out.println("please check your expression");
49 description();
50 }
51
52 }
53 else
54 {
55 System.out.println("please check your expression.");
56 description();
57 }
58 }
59
60 }
61
62 //****************************
63 public static boolean validCharacter(String operand)
64 {
65 int zero = (int)'0';
66 int nine = (int)'9';
67 char op = operand.charAt(0);
68 if((int)op >= zero && (int)op <= nine)
69 return true;
70 else
71 //your code
72 return false;
73 }
74 //*****************************
75 public static void description()
76 {
77 System.out.println();
78 System.out.println(" your operands must be between 0-9");
79 System.out.println("The operations can only be +, -, *, /, ^, %");
80 System.out.println("Each token must be separated with exactly one space");
81 System.out.println("No space at the beginning of your expression");
82 System.out.println("You must end your expression with a ;");
83 System.out.println();
84 }
85
86 //********************************
87 public static boolean validFormat(String expression)
88 {
89 if (expression.length() != 6)
90 return false;
91 else
92 {
93 int count = 0;
94 for (int i = 0; i < expression.length()-1; i++)
95 {
96 if (expression.charAt(i) == ' ')
97
98 count++;
99
100 }
101 int words = 0;
102
103 for (int i = 0; i < expression.length()-1; i++)
104 {
105 if (expression.charAt(i) == ' ' && expression.charAt(i + 1) != ' ')
106
107 words++;
108
109 }
110 if (count == 2 && words == 3 && expression.charAt(expression.length() - 1) == 'i')
111 return true;
112 else
113
114 return false;
115 }
116
117
118
119 }
120 //**********************************
121 public static String token(String s)
122 {
123 int space = s.indexOf(" ");
124 if(space != -1)
125 return s.substring(0,space);
126 else
127 return s;
128 }
129 //****************************************
130 public static String removeToken(String s)
131 {
132 int space = s.indexOf(" ");
133 if (space != -1)
134 return s.substring(0,space+1);
135 else
136
137 return s;
138 }
139 //***************************************
140 public static boolean validOp(int num)
141 {
142 //your code
143 return false;
144 }
145 //**************************************
146 public static boolean validOperation(String operation)
147 {
148 boolean result = false;
149 //your code
150 return result ;
151 }
152 //************************************
153 public static int calculate(int a, int b, String operation)
154 {
155 int result =0;
156 switch(operation)
157 {
158 case "+": return a + b;
159 case "-": return a - b;
160 case "*": return a * b;
161 case "/": if (b != 0)
162 {
163 return a/b;
164 }
165 else
166 {
167 return -1;
168 }
169 case "^": return Math.pow(a,b);
170 case "%": return a%b;
171 default: return -1;
172 }
173
174 }
175 //*************************************
176 public static String operandDescription(int op)
177 {
178 switch(op)
179 {
180 case 1: return "One";
181 case 2: return "Two";
182 case 3: return "Three";
183 case 4: return "Four";
184 case 5: return "Five";
185 case 6: return "Six";
186 case 7: return "Seven";
187 case 8: return "Eight";
188 case 9: return "Nine";
189
190
191 default: return "-1";
192 }
193 }
194
195 //****************************************
196 public static String operationDescription(String operator)
197 {
198 switch(operator)
199 {
200 case "+" : return "Plus";
201 case "-" : return "Minus";
202 case "*" : return "Multiplied by";
203 case "/" : return " Divided by";
204 case "^" : return "To the power of ";
205 case "%" : return "the remainder of ";
206
207
208 default: return "Not an operation";
209 }
210 }
211 //**********************************************
212 public static void output(int op1, int op2, String operation, int result)
213 {
214 //your code
215 operation = operationDecription(operation);
216 String opr1 = operandDescription(op1);
217 String opr2 = operandDescription(op2);
218 System.out.println(opr1 + " " + operation + " " + opr2 + "=" + result);
219
220 }
221 }

Grading policies: you will be graded based on 1. How well your program runs 2. Proper variable names, meaningful names 3. Proper indentation 4. Comments, you must provide a block comments for your program, explaining what the program does, also you must provide comments before each method explaining what the method does. 5. Implementing all the methods properly, you cannot add any more methods to your program. 6. Your program must work with any data 7. Test your program with the expressions provided at the bottom HW#1: Problem: Make a simple English calculator that does the following: 1. Takes one string from the keyboard, containing two single digits (0 to 9), a char representing one of the five operations and one The operations are +(addition), subtraction) /(division), *(multiplication), n(exponentiation) 2. The user will enter all the information on one line, you need to use the nextLine() method to read the input. Once you read the input, you need to use the methods form the String class to separate the tokens to two operands one operation as String data type. Awe need to convert the String operands to integer value. You can use the available Integer class in java to convert a string to its equivalent integer value. For example Integer.paresInt("3") will return the integer value 3. 3. Once you have the two operands and the operation, Output the description of the operation in plain English, as well as the numeric result.

Explanation / Answer

Hi, Your code is mostly correct but with minor glitches in few functions, i have edited the parts and commented wherever there were edits, i have tested and its fully functional.You can use this link to test- http://ideone.com/PXZQgu

import java.util.Scanner;

public class CaculatorShell

{

// the code for the main method is complete. once you implement all other methdos you can the program

public static void main(String[] args)

{

  

Scanner kb = new Scanner(System.in);

System.out.print("Welcome to my calculator website");

description();

for(int count =1; count <= 10; count++)

{

System.out.print("Enter your expression:");

String exp = kb.nextLine();

char semi = exp.charAt(exp.length() -1);

if(validFormat(exp) && semi != -1)

{

String op1 = token(exp);

//int operand1 = Integer.parseInt(op1);

  

exp = removeToken(exp);

  

String operation = token(exp);

  

exp = removeToken(exp);

String op2 = token(exp).substring(0,exp.length() -1);

  

//first we need to check if it is a valid character 0-9

  

if (validCharacter(op1) && validCharacter(op2))

{

int operand1 = Integer.parseInt(op1);

int operand2 = Integer.parseInt(op2);

//checking to see if it is a single digit number

  

if (validOp(operand1) && validOp(operand2) && validOperation(operation))

{

int result = calculate(operand1, operand2, operation);

output(operand1, operand2, operation, result);

}   

else

{

System.out.println("please check your expression");

description();

}

}

else

{

System.out.println("please check your expression");

description();

}   

  

}

else

{

System.out.println("please check your expression.");

description();

}

}

kb.close();

}

public static boolean validCharacter(String operand)

{

int zero = (int)'0';

int nine = (int)'9';

char op = operand.charAt(0);

if((int)op >= zero && (int)op <= nine)

return true;

else

return false;

}

public static void description()

{

System.out.println();

System.out.println(" your operands must be between 0-9");

System.out.println("The operations can only be +, -, *, /, ^, %");

System.out.println("Each token must be separated with exactly one space");

System.out.println("No space at the beginning of your expression");

System.out.println("You must end your expression with a ;");

System.out.println();

}

public static boolean validFormat(String expression)

{

// check if the expression is of the correct length without space at the front and wit ; at the end

if (expression.length() != 6 || expression.charAt(0)==' ' || expression.charAt(5)!=';' )

return false;

// this splits the string with delimiter as space, ex: 1 + 3; would be splitted as 1,+,3;

String[] splited = expression.split(" ");

if(splited.length==3)// if there are exactly 3 tokens

return true;

else

return false;

  

}

public static String token(String s)

{

String[] splited = s.split(" ");

return splited[0];// return the first element after splitting because we will be removing the tokens after splitting in another method

}

public static String removeToken(String s)

{

int space = s.indexOf(" ");

if (space != -1)

return s.substring(space+1);// remove the token after the first space i.e first token

else

  

return s;

}

public static boolean validOp(int num)

{

  

if(num>=0 && num<=9)// if number is a single digit

return true;

return false;

}

public static boolean validOperation(String operation)

{

switch(operation)

{

case "+" : return true;

case "-" : return true;

case "*" : return true;

case "/" : return true;

case "^" : return true;

case "%" : return true;

default: return false;

}

  

}

public static String operationDescription(String operator)

{

switch(operator)

{

case "+" : return "Plus";

case "-" : return "Minus";

case "*" : return "Multiplied by";

case "/" : return "Divided by";

case "^" : return "To the power of";

case "%" : return "Modulus"; // changed this as required in question

  

  

default: return "Not an operation";

}

}

public static void output(int op1, int op2, String operation, int result)

{

  

operation = operationDescription(operation);

String opr1 = operandDescription(op1);

String opr2 = operandDescription(op2);

if(result!=-1)

System.out.println(opr1 + " " + operation + " " + opr2 + " = " + result);

else

System.out.println(opr1 + " " + operation + " " + opr2 + "=Undefined" ); // added this to handle /0 case

}

  

public static String operandDescription(int op)

{

switch(op)

{

case 0: return "Zero"; // added this

case 1: return "One";

case 2: return "Two";

case 3: return "Three";

case 4: return "Four";

case 5: return "Five";

case 6: return "Six";

case 7: return "Seven";

case 8: return "Eight";

case 9: return "Nine";

  

default: return "-1";

}

}

public static int calculate(int a, int b, String operation)

{

  

switch(operation)

{

case "+": return a + b;

case "-": return a - b;

case "*": return a * b;

case "/": if (b != 0)

{

return a/b;

}

else

{

return -1;

}

case "^": return (int) Math.pow(a,b);

case "%": return a%b;

default: return -1;

}

}

  

}

Thumbs up if this was helpful, otherwise let me know in comments.
Good day.