Part 1: Writing Exercise:

The following are questions mostly from chapter 3 in your book.

a. Suppose x and y are variables of type double. Write a code fragment that sets y to x if x is positive and to 0 otherwise.

b. Write an expression to check whether an integer variable named num is even.

c. Write a statement that will assign a true value to a Boolean type if the variable named str is equal to "ASU, and false otherwise.

d. Consider the following code snippet.

if (!(!(a == 1) && (b == 0)))
System.out.println("TRUE");

Describe what the values for a and b must be for the program to output "TRUE". In other words, list up all cases when the expression will be true such as (a, b) = (0, 0), (1, 0), etc. Assume that a and b can be either 0 or 1.

e. Consider the following code, explain the difference. Show the value of s for the following set of input test cases:

1. if x =1 & y =0
2. if x = -1 & y =0
3. if x = 1 & y=-1
4. if x=1 & y =1
s = 0;
if (x > 0) { s++;}
if (y > 0) { s++;}
and
s = 0;
if (x > 0) { s++;}
else if (y >0) {s++;}

Note: The answers to the 5 questions (a through e) above should be typed in the block of comments in the java file such as;

/*
a) XXX != XXX …

d) (a, b) = (XXX, XXX) (XXX, XXX) …

*/

Part 2: Programming

Write a Java program called Assignment3.java. Your task is to implement a simple calculator. Your program will print a menu; ask the user for two numbers and computes the result based on user option. This program will follow a very simple process. see image.

Task: Prompt the user to enter two numbers as operands (type double), then print a menu to let user know what options are available (use n and t for formatting). Read the users selection to determine which operation the user has chosen. Use a switch statement with user input as the switch expression. Your switch has to provide two cases for each operation; for example ADD or +. For example, if the user enters ADD (in any combination of uppercase or lowercase letters) or a plus sign (+), your program will execute the addition and output the result.

Use only the Java statements that have been covered in class to date. DO NOT use any other statements (loop, array, break, etc.) or topic. If in doubt, ask your TA or instructor.

Example Execution:

The following is an example input and output. The input is shown in red.

Example 1

Welcome to the calculator
Enter the first operand: 23.4
Enter the second operand: 3

Operations are:
ADD or + for addition
SUBTRACT or – for subtraction
MULTIPLY or * for multiplication
DIVIDE or / for division
Enter your selection: multiply
The product is: 70.2
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.