Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Calculator {
- public Calculator(){}
- private double r1 = 0;
- public double getR1() {
- return r1;
- }
- public void setR1(double r1) {
- this.r1 = r1;
- }
- private double r2 = 0;
- public double getR2() {
- return r2;
- }
- public void setR2(double r2) {
- this.r2 = r2;
- }
- public void setRegisters(double r1, double r2){
- this.r1 = r1;
- this.r2 = r2;
- }
- public void calculate(int operator){
- calculate(Operator.values()[operator]);
- }
- public void calculate(Operator operator){
- switch(operator){
- case ADD:
- r1 = r1 + r2;
- break;
- case SUBSTRACT:
- r1 = r1 - r2;
- break;
- case MULTIPlY:
- r1 = r1 * r2;
- break;
- case DIVIDE:
- r1 = r1 / r2;
- break;
- case SQRT:
- r1 = Math.sqrt(r1);
- break;
- case SQRT_N:
- r1 = Math.pow(r1, 1/r2);
- break;
- case POW:
- r1 = Math.pow(r1, r2);
- break;
- case PERCENT:
- r1 = r1 * (r2/100);
- break;
- case CLEAR:
- r1 = 0;
- r2 = 0;
- break;
- }
- }
- }
- public enum Operator{
- ADD(0),
- SUBSTRACT(1),
- MULTIPlY(2),
- DIVIDE(3),
- SQRT(4),
- SQRT_N(5),
- POW(6),
- PERCENT(7),
- CLEAR(8);
- private int value;
- private Operator(int value){
- this.value = value;
- }
- public int getValue(){
- return this.value;
- }
- }
- import java.io.*;
- class Keyboard{
- public static String readLine(){
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- return (in.readLine());
- } catch(Exception e) {
- return ("");
- }
- }
- public static double readDouble(){
- double enter = Double.parseDouble(readLine());
- return (enter);
- }
- public static int readInteger(){
- int enter = Integer.parseInt(readLine());
- return (enter);
- }
- public static char readChar(){
- char ch = readLine().charAt(0);
- return (ch);
- }
- public static void writeLine(String value){
- System.out.println(value);
- }
- public static void writeLine(Double value){
- System.out.println(value);
- }
- public static void writeLine(int value){
- System.out.println(value);
- }
- public static void writeLine(float value){
- System.out.println(value);
- }
- public static void writeLine(Object value){
- System.out.println(value);
- }
- public static void writeLine(char[] value){
- System.out.println(value);
- }
- public static void writeLine(boolean value){
- System.out.println(value);
- }
- public static void writeF(String format, Object args){
- System.out.printf(format,args);
- }
- }
- public class Main {
- public static void main(String[] args) {
- Calculator calc = new Calculator();
- boolean exit = false;
- while (!exit){
- Keyboard.writeLine("");
- Keyboard.writeLine("- OPERATION -");
- for (Operator op : Operator.values()) {
- Keyboard.writeLine(op.getValue() + ". " + op.name());
- }
- Keyboard.writeLine("");
- Keyboard.writeLine("- PARAMETERS -");
- Keyboard.writeLine(Operator.values().length + ". Change the 1st number (VALUE: " + calc.getR1() + ")");
- Keyboard.writeLine((Operator.values().length + 1) + ". Change the 2nd number (VALUE: " + calc.getR2() + ")");
- Keyboard.writeLine("");
- Keyboard.writeLine("- COMMANDS -");
- Keyboard.writeLine((Operator.values().length + 2) + ". Close program");
- Keyboard.writeLine("");
- Keyboard.writeLine("Please, enter the command you wish to execute: ");
- try{
- int selection = Keyboard.readInteger();
- if (selection < Operator.values().length && selection >= 0){
- calc.calculate(selection);
- }
- else{
- selection = selection - Operator.values().length;
- switch (selection){
- case 0:
- Keyboard.writeLine("Please, enter a new value for the 1st number:");
- double value1 = Keyboard.readDouble();
- calc.setR1(value1);
- break;
- case 1:
- Keyboard.writeLine("Please, enter a new value for the 2nd number:");
- double value2 = Keyboard.readDouble();
- calc.setR2(value2);
- break;
- case 2:
- Keyboard.writeLine("Bye! Bye!");
- exit = true;
- break;
- default:
- Keyboard.writeLine("Command not found");
- break;
- }
- }
- }
- catch (Exception e){
- Keyboard.writeLine("Error: " + e.toString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement