Part One

Summary: Implement a program that will input a line of text from the user, use String operations to extract an operation to perform and a value for the operation, perform the operation (or determine the inputs are invalid), and print the result. For example if the user enters sqrt 4, then the program should print out 2.0. The set of operations should include sqrt, exp, log, and sin.

Example Input Example Output
sqrt 0 0.0
sqrt 2.25 1.5
sqrt -2.25 Invalid
exp 0 1.0
exp 2.25 9.487...
exp -2.25 0.105...
log 0 Invalid
log 2.25 0.810...
log -2.25 Invalid
sin 0 0.0
sin 30 0.5
sin -30 -0.5
foo 13.13 Invalid

For this program, it is invalid to perform the square root of a negative number, and it is invalid to perform the logarithm of a nonpositive number. Also, note that the value for the sine operation is given in degrees, so a conversion to radians will be needed. Finally, for full credit, the program should be able to input a line from the user, extract the operation and the value using string operations, while coping with any extra whitespace in the line.

Pseudocode:

1. Use Scanner to input the operation and value separately (e.g., input.next() followed by input.nextDouble()). It's ok if the program crashes if the value is not a number.

2. Use an if/else if/else statement to code separate blocks of code for the different operations, plus a block for invalid operations. Use the equals method to compare the operation to the Math operations you are looking for. Each block calculates a result (or determines that something is invalid).

3. Print the result.

Pseudocode for full credit (note similarities to the the inclass problem of separating the first, middle, and last names from a line of input):

1. Use a Scanner object to input a line from the user (e.g., input.nextLine()).

2. Use the trim method on the line to remove any whitespace on the left and the right (e.g., something like: line = line.trim();).

3. Use the indexOf method on the line to find the index of the first space on the line.

4. Use the substring method on the line to extract the operation.

5. Use the substring method on the line to extract the remainder of the line.

6. Use the trim method on the remainder of the line to remove any whitespace on the left and the right (e.g., something like: remainder = remainder.trim();).

7. Use something like Double.parseDouble(remainder) on the remainder to convert the string to a number. Your variable name might be different. It is ok if the program crashes if the input is not a number.

8. Use an if/else if/else statement to code separate blocks of code for the different operations, plus a block for invalid operations. Use the equals method to compare the operation extracted to the operation you are looking for. Each block calculates a result (or determines that something is invalid).

9. Print the result.

Ask for help if you get stuck on any of these steps. While debugging your program, you might it useful to print out what your strings look like as you process them. You could have a Boolean variable named debug (set it to false when you done debugging):

boolean debug = true;

and then use if statements to print out debugging information, for example:

if (debug) {
System.out.printf("line = \"%s\"\n", line);
}

Part Two

Answer the following questions and save your answers in a text file (.txt extension) in your Eclipse project. You can start editing a text file in Eclipse using:

File > New > Untitled Text File

Then do File > Save to start a dialog to save the file in your project.

1. Create a flowchart for the following code. Include a image (.jpg or .png) of the flowchart in your project.

Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt()
while (a < b) {
a += c;
b -= c;
}
System.out.printf("a = %s, b = %s\n", a, b);

2. Show the output of the above code for the following cases:

a = 1 and b = 3 and c = 1
a = 3 and b = 1 and c = 1
a = 0 and b = 10 and c = 1
a = 0 and b = 10 and c = 2
a = 0 and b = 10 and c = 3

3. Which of the above cases would print a different value if the while condition was changed to

a <= b?

4. Specify values for a, b, and c that would result in an infinite loop.

5. Look in the Java documentation (https://docs.oracle.com/javase/8/docs/api/) for the two addExact methods in the Math class. Write what is listed for their "Modifier and Type" and their Method and Description. Modify the two examples below so they will result in throwing an exception.

int a1 = 0; long b1 = 0;
int a2 = 0; long b2 = 0;
int a3 = Math.addExact(a1, a2); long b3 = Math.addExact(b1, b2);
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.