Create a console calculator application that:
-Takes one command-line argument; your name and surname. When the program starts, display the date with a welcome message for the user
-Your calculator must include the arithmetic operations, as well as at least 5 scientific operations of the Math class. It's must also have the ability to round a number and truncate it. When you multiply by 2, Don't use the * operator to perform the operation (use shift operators). It must also be able to reverse the sign of a number.
-Includes sufficient error checking to ensure that the user only enters valid input. Make use of the String, Character and other wrapper classes
-Is able to do conversions between decimal, octal and hexadecimal numbers.
-Makes use of a menu. You should give the user the option to end the program when enters a certain option.
-Displays a message for the user, stating the current time, calculates and displays how long the user used your program, when the program exits.
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.now();
System.out.println("Welcome " + (args.length == 0 ? "" : args[0] + (args.length == 2 ? args[1] : "")));
System.out.println("Current date: " + start.toLocalDate());
Scanner in = new Scanner(System.in);
double a;
double b;
while (true) {
System.out.println("\n1. +\n2. - \n3. *\n4. /\n5. mod\n6. abs\n7. sin\n8. cos\n" +
"9. ceil\n10. floor\n11. truncate\n12. sqrt\n13. Hex to Dec\n14. Oct to Dec\n15. Bin to Dec\n0. Exit");
try {
switch (in.nextLine()) {
case "1":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("b:");
b = Double.parseDouble(in.nextLine());
System.out.println(a + " + " + b + " = " + (a + b));
break;
case "2":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("b:");
b = Double.parseDouble(in.nextLine());
System.out.println(a + " - " + b + " = " + (a - b));
break;
case "3":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("b:");
b = Double.parseDouble(in.nextLine());
System.out.println(a + " * " + b + " = " + (b == 2 ? ((int) a << 1) : (a * b)));
break;
case "4":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("b:");
b = Double.parseDouble(in.nextLine());
if (b == 0) {
throw new NumberFormatException();
}
System.out.println(a + " / " + b + " = " + (a / b));
break;
case "5":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("b:");
b = Double.parseDouble(in.nextLine());
if (b == 0) {
throw new NumberFormatException();
}
System.out.println(a + " mod " + b + " = " + (a % b));
break;
case "6":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("|" + a + "| = " + Math.abs(a));
break;
case "7":
System.out.println("Rad a:");
a = Double.parseDouble(in.nextLine());
System.out.println("sin(" + a + ") = " + Math.sin(a));
break;
case "8":
System.out.println("Rad a:");
a = Double.parseDouble(in.nextLine());
System.out.println("cos(" + a + ") = " + Math.cos(a));
break;
case "9":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("ceil(" + a + ") = " + Math.ceil(a));
break;
case "10":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("floor(" + a + ") = " + Math.floor(a));
break;
case "11":
System.out.println("a:");
a = Double.parseDouble(in.nextLine());
System.out.println("(int)" + a + " = " + ((int) a));
break;
case "12":
System.out.println("a");
a = Double.parseDouble(in.nextLine());
if (a < 0) {
throw new NumberFormatException();
}
System.out.println("sqrt(" + a + ") = " + Math.sqrt(a));
break;
case "13":
System.out.println("a");
String hex = in.nextLine();
System.out.println(hex + " => " + Integer.parseInt(hex, 16));
break;
case "14":
System.out.println("a");
String octal = in.nextLine();
System.out.println(octal + " => " + Integer.parseInt(octal, 8));
break;
case "15":
System.out.println("a");
String binary = in.nextLine();
System.out.println(binary + " => " + Integer.parseInt(binary, 2));
break;
case "0":
System.out.println("Current time: " + LocalTime.now());
System.out.println("Program used: " + Duration.between(start, LocalDateTime.now()));
System.exit(0);
}
} catch (NumberFormatException e) {
System.out.println("Invalid");
}
}
}
}
Comments
Leave a comment