**ASSIGNMENT NO 6**

**Title:** Develop a calculator for arithmetic operators like +,-,*,/,% (use switch statement).

**Objectives:** To learn Constants, Variables, and Data Types, Operators and Expressions, Decision making statements in Java.

**Problem Statement:** Write a program in Java to implement a Calculator with simple arithmetic operations such as add, subtract, multiply, divide, factorial etc. using switch case and other simple java statements. ** **

**Outcomes:** Students will be able to demonstrate use of variables, data types, operators and expressions and switch statement in Java.

**Hardware requirements:** Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

**Software requirements:** 64 bit Linux/Windows Operating System, JDK 1.4 or later

**Theory: **Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. There are two data types available in Java −

- Primitive Data Types
- Reference/Object Data Types

**Primitive Data Types : **Java supports eight basic data types known as *primitive types*. In addition, it supports classes and arrays as composite data types, or reference types. Classes and arrays are documented later in this chapter. The primitive types are: a boolean type, a character type, four integer types, and two floating-point types. The four integer types and the two floating-point types differ in the number of bits that represent them, and therefore in the range of numbers they can represent

Type |
Size |
Range |

Boolean | 1 bit | NA |

Char | 16 bits | \u0000 to \uFFFF |

byte | 8 bits | -128 to 127 |

short | 16 bits | -32768 to 32767 |

int | 32 bits | -2147483648 to 2147483647 |

long | 64 bits | -9223372036854775808 to 9223372036854775807 |

float | 32 bits | ±1.4E-45 to ±3.4028235E+38 |

double | 64 bits | ±4.9E-324 to ±1.7976931348623157E+308 |

**Reference Types:** In addition to its eight primitive types, Java defines two additional categories of data types: classes and arrays. Java programs consist of class definitions; each class defines a new data type that can be manipulated by Java programs. An array type represents a list of values of some other type. char is a data type, and an array of char values is another data type, written char[].

**Operators : **Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups −

- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Misc Operators

**The Arithmetic Operators:** Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −

Assume integer variable A holds 10 and variable B holds 20, then −

Operator |
Description |
Example |

+ (Addition) | Adds values on either side of the operator. | A + B will give 30 |

– (Subtraction) | Subtracts right-hand operand from left-hand operand. | A – B will give -10 |

* (Multiplication) | Multiplies values on either side of the operator. | A * B will give 200 |

/ (Division) | Divides left-hand operand by right-hand operand. | B / A will give 2 |

% (Modulus) | Divides left-hand operand by right-hand operand and returns remainder. | B % A will give 0 |

++ (Increment) | Increases the value of operand by 1. | B++ gives 21 |

— (Decrement) | Decreases the value of operand by 1. | B– gives 19 |

**The Relational Operators :** There are following relational operators supported by Java language.

Assume variable A holds 10 and variable B holds 20, then −

Operator |
Description |
Example |

== (equal to) | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |

!= (not equal to) | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |

> (greater than) | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |

< (less than) | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |

>= (greater than or equal to) | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |

<= (less than or equal to) | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |

**The Bitwise Operators:** Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.

Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101

—————–

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

The following table lists the bitwise operators −

Assume integer variable A holds 60 and variable B holds 13 then −

Operator |
Description |
Example |

& (bitwise and) | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12 which is 0000 1100 |

| (bitwise or) | Binary OR Operator copies a bit if it exists in either operand. | (A | B) will give 61 which is 0011 1101 |

^ (bitwise XOR) | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) will give 49 which is 0011 0001 |

~ (bitwise compliment) | Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. | (~A ) will give -61 which is 1100 0011 in 2’s complement form due to a signed binary number. |

<< (left shift) | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |

>> (right shift) | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15 which is 1111 |

>>> (zero fill right shift) | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. | A >>>2 will give 15 which is 0000 1111 |

**The Logical Operators:** The following table lists the logical operators −

Assume Boolean variables A holds true and variable B holds false, then −

Operator |
Description |
Example |

&& (logical and) | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false |

|| (logical or) | Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. | (A || B) is true |

! (logical not) | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true |

**The Assignment Operators:** Following are the assignment operators supported by Java language –

Operator |
Description |
Example |

= | Simple assignment operator. Assigns values from right side operands to left side operand. | C = A + B will assign value of A + B into C |

+= | Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. | C += A is equivalent to C = C + A |

-= | Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. | C -= A is equivalent to C = C – A |

*= | Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. | C *= A is equivalent to C = C * A |

/= | Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. | C /= A is equivalent to C = C / A |

%= | Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand. | C %= A is equivalent to C = C % A |

<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |

>>= | Bitwise AND assignment operator. | C >>= 2 is same as C = C >> 2 |

&= | Right shift AND assignment operator. | C &= 2 is same as C = C & 2 |

^= | bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |

|= | bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |

**Miscellaneous Operators :** There are few other operators supported by Java Language.

**Conditional Operator ( ? : ):** Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −

variable x = (expression) ? value if true : value if false

**instanceof Operator :** This operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as −

( Object reference variable ) instanceof (class/interface type)

If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true.

**Switch Statement :** A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

**Syntax:**The syntax for a switch statement in Java is as follows:

switch(expression){ case constant-expression : statement(s); break; //optional case constant-expression : statement(s); break; //optional // you can have any number of case statements. default : //Optional statement(s); }

** ****The following rules apply to a switch statement:**

- You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
- The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- Not every case needs to contain a break. If no break appears, the flow of control will
*fall through*to subsequent cases until a break is reached. - A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

**Conclusion : **In this assignment, we have studied data types, operators and switch stamen in Java and demonstrated their use in program.

Roll No. |
Name of Student |
Date of Performance |
Date of Assessment |
Grade |
Sign of Student |
Sign of Faculty |

## Program

class Calci { public static void main(String args[]) { //Display Menu to enter your choice System.out.println("****************MENU******************") ; System.out.println("1. Add\n2. Sub\n3. Mul\n4. Div\n5. Mod"); System.out.println("Enter you choice"); int choice=Integer.parseInt(System.console().readLine()); //Read two number from user and convert numeric string in to integer System.out.println("Enter number 1 and number 2"); int num1=Integer.parseInt(System.console().readLine()); int num2=Integer.parseInt(System.console().readLine()); double result=0; //check choice entered by user and perform operation switch(choice) { case 1: result=num1+num2; break; case 2: result=num1-num2; break; case 3: result=num1*num2; break; case 4: result=num1/(double)num2; break; case 5: result=num1%num2; break; default: System.out.println("Wrong choice"); System.exit(0); } //Display result System.out.println("Result is "+result); } }

**OUTPUT:**