Andhika Andhika - 2 months ago 15
Java Question

Random fraction with random operator calculator in Java

I'm trying to create fraction calculator with Java and with random operation, there will be 10 question, my problem is the answerKey always give me answer null. I think the wrong method is in answerKey's method, because can't convert String into Fraction. i still don't know how to fix it.

here's my code:

public class TesterFractionTugas1 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Random rand = new Random();
final int jumlahSoal = 10;
int count = 0;
String output = "";

while(count < jumlahSoal){
int angka1 = (int) (Math.random() * 10 + 1);
int angka2 = (int) (Math.random() * 10 + 1);
int angka3 = (int) (Math.random() * 10 + 1);
int angka4 = (int) (Math.random() * 10 + 1);

char operator = genOperator(rand.nextInt(3));

Fraction pecahan1 = new Fraction(angka1, angka2);
Fraction pecahan2 = new Fraction(angka3, angka4);

System.out.println("Question :" + evaluateQuestion(pecahan1.getFraction(), pecahan2.getFraction(), operator));
System.out.println("AnswerKey: " + answerKey(pecahan1, pecahan2, operator)); // wrong code in here

System.out.println("your answer: ");
Fraction pecahanAnswer = new Fraction();
String answer = in.nextLine();
String[] sptext = new String[2];

sptext = answer.split("/");
pecahanAnswer.setNumerator(Integer.parseInt(sptext[0]));
pecahanAnswer.setDenominator(Integer.parseInt(sptext[1]));

if(answer == answerKey(pecahan1, pecahan2, operator)){
System.out.println("You right");
count++;
} else {
System.out.println("You Wrong");
count++;
}
}
}

// giving operator for math
public static char genOperator(int a){
switch(a){
case 0: return '+';
case 1: return '-';
case 2: return '*';
case 3: return '/';
default: return '+';
}
}

// the question here
public static String evaluateQuestion(String pecahan1, String pecahan2, char operator){
return pecahan1 + " " + operator + " " + pecahan2;
}

// answerkey
public static String answerKey(Fraction pecahan1, Fraction pecahan2, char operator){
switch (operator){
case '+': pecahan1.addFraction(pecahan2);
case '-': pecahan1.subtractFraction(pecahan2);
case '*': pecahan1.multiplyByFraction(pecahan2);
case '/': pecahan1.divideByFraction(pecahan2);
default: return null;
}

}
}


class Fraction {
private int numerator, denominator;

public Fraction(){
numerator = 0;
denominator = 1;
}

public Fraction(int numerator, int denominator){
this.numerator = numerator;
this.denominator = denominator;
}

public void setNumerator(int numerator){
this.numerator = numerator;
}

public int getNumerator(){
return this.numerator;
}

public void setDenominator(int denominator){
this.denominator = denominator;
}

public int getDenominator(){
return this.denominator;
}

public String getFraction() {
return numerator + "/" + denominator;
}

public String toString(){
return numerator + "/" + denominator;
}

public String addFraction(Fraction pecahan2) {
// Sum formula: a/b + c/d = (ad + cb)/bd
int a = this.numerator;
int b = this.denominator;
int c = pecahan2.numerator;
int d = pecahan2.denominator;
return ((a*d) + (c*b)) + "/" + (b*d);
}

public String subtractFraction(Fraction pecahan2) {
// Subtraction formula: a/b - c/d = (ad - cb)/bd
int a = this.numerator;
int b = this.denominator;
int c = pecahan2.numerator;
int d = pecahan2.denominator;
return ((a*d) - (c*b)) + "/" + (b*d);
}


public String multiplyByFraction(Fraction pecahan2) {
// Multiplication formula: a/b * c/d = ac/bd
int a = this.numerator;
int b = this.denominator;
int c = pecahan2.numerator;
int d = pecahan2.denominator;
return (a*c) + "/" + (b*d);
}

public String divideByFraction(Fraction pecahan2) {
// Division formula: (a/b) / (c/d) = ad/bc
int a = this.numerator;
int b = this.denominator;
int c = pecahan2.numerator;
int d = pecahan2.denominator;
return (a*d) + "/" + (b*c);
}
}


I mean how to fix answerKey that not giving me null value

Answer

you are missing the return statements in your answerKey method change it to this.

You should always look for breaking the switch cases else all of them will be executed.

public static String answerKey(Fraction pecahan1, Fraction pecahan2, char operator) {
        switch (operator) {
        case '+':
            return pecahan1.addFraction(pecahan2);
        case '-':
            return pecahan1.subtractFraction(pecahan2);
        case '*':
            return pecahan1.multiplyByFraction(pecahan2);
        case '/':
            return pecahan1.divideByFraction(pecahan2);
        default:
            return null;
        }