Friday 29 September 2017

Java program that takes as input three integers, a, b, and c, from the Java console and determines if they can be used in a correct arithmetic formula.

Problem:

Write a short program that takes as input three integers, a, b, and c, from the Java
console and determines if they can be used in a correct arithmetic formula (in the
given order), like “a+b = c,” “a = b−c,” or “a ∗ b = c.”

Solution:

package com.basics;

import java.io.ObjectInputStream.GetField;
import java.util.Scanner;

public class FormulaFormation {

static int a;
static int b;
static int c;

public static void main(String[] args) {
FormulaFormation arithmeticFormula = new FormulaFormation();
arithmeticFormula.getTheNumber();
float value = 1;
int oper2Range;
for (int i = 0; i < 4; i++) {
if (i < 2)
oper2Range = 4;
else
oper2Range = 2;
for (int j = 0; j < oper2Range; j++)
if ((value = valueOf(a, b, c, i, j)) == 0) {
System.out.print("Your inputs can form the equation: ");
displayEquation(i, j);
break;
}
}
if (value != 0)
System.out.println("Your inputs can't form any equation.");
}

private static float valueOf(int a, int b, int c, int oper1, int oper2) {
if (oper2 == 2 || oper2 == 3)
return calculate(a, calculate(b, c, oper2), oper1);
else
return calculate(c, calculate(a, b, oper1), oper2);
}

private static float calculate(float x, float y, int oper) {
switch (oper) {
case 0:
return x + y;
case 1:
return x - y;
case 2:
return x * y;
default:
return (float) x / y;
}
}

private static void displayEquation(int oper1, int oper2) {
if (oper1 == 0 || oper1 == 1)
// a +- b */ c = 0 -> a = -+ b * c
if (oper2 == 2 || oper2 == 3)
System.out.printf("a = %cb %c c\n",
(toChar(oper1) == '+') ? '-' : '\u0000', toChar(oper2));
// a +- b +- c = 0 -> a +- b = -+ c
else
System.out.printf("a %c b = %cc\n", toChar(oper1),
(toChar(oper2) == '+') ? '-' : '\u0000');
// a */ b +- c = 0 -> a */b = -+ c
else
System.out.printf("a %c b = %cc\n", toChar(oper1),
(toChar(oper2) == '+') ? '-' : '\u0000');
}


private static char toChar(int operator) {
switch (operator) {
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
default:
return '/';
}
}

public void getTheNumber() {
Scanner scanner = new Scanner(System.in);
System.out.println("enter number a: ");
a = scanner.nextInt();
System.out.println("enter number b: ");
b = scanner.nextInt();
System.out.println("enter number c: ");
c = scanner.nextInt();
scanner.close();
}

}


Output:

enter number a:
1
enter number b:
1
enter number c:
1
Your inputs can form the equation: a = b * c
Your inputs can form the equation: a * b = c
Your inputs can form the equation: a / b = c

1 comment:

  1. These two statements "Your inputs can form the equation: a = b * c" and "Your inputs can form the equation: a / b = c" are same or not ???

    if a = b + c then b(or c) = a - c(or b),
    Similarly multiplication and division is correlated.


    Why can't we find maximum among them and utilize it to our advantage ...Let me know something like below will help us to resolve the problem statement or not :

    private static void arrange(int a, int b, int c) {
    int maxValue = max(max(a, b), c);
    int num1, num2;
    if(a == maxValue) {
    num1 = b;
    num2 = c;
    } else if(b == maxValue){
    num1 = a;
    num2 = c;
    } else {
    c = maxValue;
    num1 = a;
    num2 = b;
    }
    operation(maxValue, num1, num2);
    }

    private static void operation(int maxValue, int num1, int num2) {

    if(maxValue == (num1 * num2)) {
    System.out.println(maxValue +" * "+num2+" = "+num1);
    } else if(maxValue == (num1 + num2)) {
    System.out.println(maxValue +" = "+num2+" + "+num1);
    }
    }

    private static int max(int a, int b) {
    return a >= b ? a : b;
    }

    ReplyDelete

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...