Assignment

Solution

import java.util.Scanner;

public class Main {

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

int mode = mainMenu();

if (mode == 1) {

int operation = standardMenu();

System.out.println(“Please, enter 1st operand”);

int op1 = Integer.parseInt(scanner.nextLine());

System.out.println(“Please, enter 2nd operand”);

int op2 = Integer.parseInt(scanner.nextLine());

System.out.println(“The result is:”);

switch (operation) {

case 1:

System.out.println(op1 + op2);

break;

case 2:

System.out.println(op1 – op2);

break;

case 3:

System.out.println(op1 * op2);

break;

case 4:

System.out.println(op1 / (double)op2);

break;

case 5:

System.out.println(op1 % op2);

break;

}

}

else if (mode == 2) {

int methods = scientificMenu();

double op;

int operation;

switch (methods) {

case 1:

operation = trigonometricMenu();

System.out.println(“Please, enter operand”);

op = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

switch (operation) {

case 1:

System.out.println(Math.sin(op));

break;

case 2:

System.out.println(Math.cos(op));

break;

case 3:

System.out.println(Math.tan(op));

break;

case 4:

System.out.println(Math.acos(op));

break;

case 5:

System.out.println(Math.asin(op));

break;

case 6:

System.out.println(Math.atan(op));

break;

}

break;

case 2:

operation = exponentMenu();

double op1, op2;

switch (operation) {

case 1:

System.out.println(“Please, enter operand”);

op1 = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

System.out.println(Math.exp(op1));

break;

case 2:

System.out.println(“Please, enter operand”);

op1 = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

System.out.println(Math.log(op1));

break;

case 3:

System.out.println(“Please, enter operand”);

op1 = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

System.out.println(Math.log10(op1));

break;

case 4:

System.out.println(“Please, enter 1st operand”);

op1 = Double.parseDouble(scanner.nextLine());

System.out.println(“Please, enter 2nd operand”);

op2 = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

System.out.println(Math.pow(op1,op2));

break;

case 5:

System.out.println(“Please, enter operand”);

op1 = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

System.out.println(Math.sqrt(op1));

break;

}

break;

case 3:

operation = roundingMenu();

System.out.println(“Please, enter operand”);

op = Double.parseDouble(scanner.nextLine());

System.out.println(“The result is:”);

switch (operation) {

case 1:

System.out.println(Math.ceil(op));

break;

case 2:

System.out.println(Math.floor(op));

break;

case 3:

System.out.println(Math.rint(op));

break;

case 4:

System.out.println(Math.round(op));

break;

case 5:

System.out.println(Math.abs(op));

break;

}

break;

}

}

else if (mode == 3) {

int operation = conversionMenu();

System.out.println(“Please, enter operand”);

int op = Integer.parseInt(scanner.nextLine());

System.out.println(“The result is:”);

switch (operation) {

case 1:

System.out.println(convertToBin(op));

break;

case 2:

System.out.println(convertFromBin(op));

break;

}

}

}

private static int mainMenu() {

System.out.println(“Please, choose calculator mode:”);

while (true) {

System.out.println(“1) Standard mode”);

System.out.println(“2) Scientific mode”);

System.out.println(“3) Conversion mode”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

case “3”:

return 3;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int standardMenu() {

System.out.println(“Please, choose operation:”);

while (true) {

System.out.println(“1) Addition”);

System.out.println(“2) Subtraction”);

System.out.println(“3) Multiplication”);

System.out.println(“4) Division”);

System.out.println(“5) Modulus”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

case “3”:

return 3;

case “4”:

return 4;

case “5”:

return 5;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int scientificMenu() {

System.out.println(“Please, choose methods:”);

while (true) {

System.out.println(“1) Trigonometric Methods”);

System.out.println(“2) Exponent Methods”);

System.out.println(“3) Rounding Methods”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

case “3”:

return 3;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int trigonometricMenu() {

System.out.println(“Please, choose operation:”);

while (true) {

System.out.println(“1) sin”);

System.out.println(“2) cos”);

System.out.println(“3) tan”);

System.out.println(“4) acos”);

System.out.println(“5) asin”);

System.out.println(“6) atan”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

case “3”:

return 3;

case “4”:

return 4;

case “5”:

return 5;

case “6”:

return 6;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int exponentMenu() {

System.out.println(“Please, choose operation:”);

while (true) {

System.out.println(“1) exp”);

System.out.println(“2) log”);

System.out.println(“3) log10”);

System.out.println(“4) pow”);

System.out.println(“5) sqrt”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

case “3”:

return 3;

case “4”:

return 4;

case “5”:

return 5;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int roundingMenu() {

System.out.println(“Please, choose operation:”);

while (true) {

System.out.println(“1) ceil”);

System.out.println(“2) floor”);

System.out.println(“3) rint”);

System.out.println(“4) int round”);

System.out.println(“5) abs”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

case “3”:

return 3;

case “4”:

return 4;

case “5”:

return 5;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int conversionMenu() {

System.out.println(“Please, choose operation:”);

while (true) {

System.out.println(“1) Convert Decimal to Binary”);

System.out.println(“2) Convert Binary to Decimal”);

String input = scanner.nextLine();

switch (input) {

case “1”:

return 1;

case “2”:

return 2;

default:

System.out.println(“Invalid input. Please, try again\n”);

}

}

}

private static int convertToBin(int a) {

int coeff = 1;

int temp = a;

int result = 0;

while (temp > 0) {

result += (temp % 2) * coeff;

temp /= 2;

coeff *= 10;

}

return result;

}

private static int convertFromBin(int a) {

int coeff = 1;

int temp = a;

int result = 0;

while (temp > 0) {

result += (temp % 10) * coeff;

temp /= 10;

coeff *= 2;

}

return result;

}

}