Java: switching between binary and decimal "modes" on a calculator -


i've created 2 separate convoluted calculators, 1 calculates decimal numbers , calculates binary numbers.

the user inputs equation want calculated on command line , result printed.

>1+1 >2 

or binary

>0b111 + 0b101 >0b1100 

what i'm having trouble being able switch between calculating binary , decimal numbers on fly. i'd make work this: (by default in decimal mode)

>1+1 >2 >bin >0b111 + 0b101 = 0b1100 >dec >5 * 10 >50 >exit 

(while typing exit @ point exit program)

is there way can pull off?

//decimal calculator import java.util.*; public class decarithmetic {     public static void main(string[] args)     {         scanner input = new scanner(system.in);         while(true)         {             string equation = input.nextline();             if (equation.equalsignorecase("exit"))             {                 system.exit(0);             }             equation = equation.replaceall("\\s+","");             int oplocation = equation.indexof("+");             if(oplocation == -1)             {                 oplocation = equation.indexof("-");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("*");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("/");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("|");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("&");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("^");             }             string number = equation.substring(0,oplocation);             int operandone = integer.parseint(number);              number = equation.substring(oplocation + 1);             int operandtwo = integer.parseint(number);              string op = equation.substring(oplocation, oplocation+1);              int result = calculate(op, operandone, operandtwo);             system.out.println(result);         }     }     public static int calculate(string operator, int operandone, int operandtwo)     {         if(operator.equals("+") == true)         {             return operandone + operandtwo;         }         else if(operator.equals("-") == true)         {             return operandone - operandtwo;         }         else if(operator.equals("*") == true)         {             return operandone * operandtwo;         }         else if(operator.equals("/") == true)         {             return operandone / operandtwo;         }         else if(operator.equals("^") == true)         {             return operandone ^ operandtwo;         }         else if (operator.equals("|") == true)         {             return operandone | operandtwo;         }         else if(operator.equals("&") == true)         {             return operandone & operandtwo;         }         else         {             scanner scan = new scanner(system.in);             system.out.println("please enter valid operator. (+, -, *, /, ^, |, &)");             return calculate(scan.nextline(), operandone, operandtwo);         }     } } 

//binary calculator import java.util.*; public class binaryarithmetic {     public static void main(string[] args)     {         scanner input = new scanner(system.in);         while(true)         {             string equation = input.nextline();             if (equation.equalsignorecase("exit"))             {                 system.exit(0);             }             equation = equation.replaceall("\\s+","");             equation = equation.replaceall("0b","");             int oplocation = equation.indexof("+");             if(oplocation == -1)             {                 oplocation = equation.indexof("-");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("*");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("/");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("|");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("&");             }             if(oplocation == -1)             {                 oplocation = equation.indexof("^");             }             string number = equation.substring(0,oplocation);             int bin1 = integer.parseint(number);             string b1 = integer.tostring(bin1);             int operandone = integer.parseint(b1, 2);              number = equation.substring(oplocation + 1);             int bin2 = integer.parseint(number);             string b2 = integer.tostring(bin2);             int operandtwo = integer.parseint(b2, 2);              string op = equation.substring(oplocation, oplocation+1);             string result = integer.tobinarystring(calculate(op, operandone, operandtwo));             system.out.println("0b"+result);          }     }     public static int calculate(string operator, int operandone, int operandtwo)     {         if(operator.equals("+") == true)         {             return operandone + operandtwo;         }         else if(operator.equals("-") == true)         {             return operandone - operandtwo;         }         else if(operator.equals("*") == true)         {             return operandone * operandtwo;         }         else if(operator.equals("/") == true)         {             return operandone / operandtwo;         }         else if(operator.equals("^") == true)         {             return operandone ^ operandtwo;         }         else if (operator.equals("|") == true)         {             return operandone | operandtwo;         }         else if(operator.equals("&") == true)         {             return operandone & operandtwo;         }         else         {             scanner scan = new scanner(system.in);             system.out.println("please enter valid operator. (+, -, *, /, ^, |, &)");             return calculate(scan.nextline(), operandone, operandtwo);         }     } } 

have main class called calculator take input user. class have main method

if input dec, read nextline , pass decimal calculator

if input bin, read nextline , pass binary calculator

if input exit, exit.

if input neither dec, nor bin nor exit, assume user has entered decimal equation , call decimal calculator.

try write code per have suggested. if struggle, can out.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -