Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please write this code with comments in java. My fraction class wasn

ID: 3740171 • Letter: C

Question

Can someone please write this code with comments in java. My fraction class wasn't good and I am just struggling with this. Please do everything asked and add detailed comments that way I understand what is going on.

Write a program to interactively quiz a student on fraction operations. Since this program is interactive, the workbench is not suitable for testing. You should develop the program using an IDE of your choice, I recomment jgrasp, but eclipse is also a good choice. When complete, you should create files on the workbench and copy/paste your code into those files on workbench for submission.

You should use the Fraction class that you developed in lab on 3/22, but you will almost certainly need to enhance it. You can also use the Dice class that was demonstrated in class to generate random numbers. You should find the Dice class in the book at the end of chapter 6. You will need this class (or something similar) to generate random numbers for quizzing the student.

Your program should allow the student to select the type of quiz and the number of questions. The type of quiz will be either (1) arithmetic or (2) relational. In an arithmetic quiz, the student will be given random fractions and asked to either add, subtract, multiply, or divide them. In a relational quiz, the student will be given two random fractions and need to identify which is largest or if they are equal. Your program will keep track of how many correct answers are given and report that at the end of the quiz.

Your program should also allow the student to enter a difficuly level before starting the quiz. The difficulty level will be an integer that represents the largest number that can be generated for the numerator or denominator of your fraction. So on a difficulty level of 9, all numerators and denominators will be 9 or less. The answer to quiz questions might have larger numbers however.

Hints: This should be obvious by now, but you need to spend some time thinking about the design. Note that the structure of the quiz logic is really unrelated to the type of question you are asking. If you find yourself getting a bit confused, think about writing a similar quiz that just uses integers and not fractions for the questions. Good modularity will help. A method that generates a questions, gets the answer, checks the answer, and returns true or false (which you interpret as correct or incorrect) would be good. It probably has helper methods like one to make a random fraction, one to choose a random math operation, etc. If you have a method to do one question, then a loop to call that method repeatedly should be simple. Start early. Ask questions. Have some fun with this.

Explanation / Answer

Hello. I have created the required quiz program for you. I have already defined a Fraction class for answering another question, so I’m using it here, which is perfect for this. Created Quiz.java class which contain different methods to display menu, set difficulty level, play arithmetic quiz, play relational quiz etc. Each of these methods are well explained using comments. Go through them, and understand how the things work. Thanks

EDIT: As the code is huge, Im getting troubles submitting the answer trying to keep the formatting. Shows character limit error. So I’m pasting as plain text, sacrificing the formatting. Sorry for the trouble.

// Fraction.java

public class Fraction {

private int numerator;

private int denominator;

public Fraction(int numerator, int denominator) {

if (denominator == 0) {

/**

* making sure denominator never be 0 , if an invalid input is

* given, setting denominator to 1

*/

denominator = 1;

}

/**

* if denom is less than 0, making it positive and changing the sign of

* numerator

*/

if (denominator < 0) {

denominator = Math.abs(denominator);

numerator = 0 - numerator;

}

/**

* setting validated values to the variables

*/

this.numerator = numerator;

this.denominator = denominator;

}

/**

* A private method to calculate the greatest common divisor of two numbers

*/

private int gcd(int x, int y) {

if (y == 0)

return x;

return gcd(y, x % y);

}

/**

* method to add a rational number to the current number and returns it

*/

public Fraction add(Fraction b) {

int num = (this.numerator * b.denominator)

+ (this.denominator * b.numerator);

int den = this.denominator * b.denominator;

Fraction rational = new Fraction(num, den);

return rational;

}

/**

* method to subtract a rational number from the current number and returns

* it

*/

public Fraction sub(Fraction b) {

int num = (this.numerator * b.denominator)

- (this.denominator * b.numerator);

int den = this.denominator * b.denominator;

Fraction rational = new Fraction(num, den);

return rational;

}

/**

* method to multiply a rational number to the current number and returns it

*/

public Fraction mult(Fraction b) {

int num = this.numerator * b.numerator;

int den = this.denominator * b.denominator;

Fraction rational = new Fraction(num, den);

return rational;

}

/**

* method to divide a rational number from the current number and returns it

*/

public Fraction divide(Fraction b) {

if (b.numerator == 0) {

System.out

.println("Unexpected Error: The denominator cannot be zero");

return null;

}

/**

* Finding the recipocal of second number and multiplying it

*/

b = b.invert();

return mult(b);

}

/**

* method to find and return the reciprocal of a rational number

*/

public Fraction invert() {

if (numerator == 0) {

System.out.println("No inverse for zero");

return null;

}

int newNumerator = this.denominator;

int newDenominator = Math.abs(this.numerator);

if (this.numerator < 0) {

newNumerator = 0 - newNumerator;

}

return new Fraction(newNumerator, newDenominator);

}

/**

* method to simplify a rational number and returns it. This method will

* find the gcd and divides it from bothy numerator and denominator

*/

public Fraction simplify() {

/**

* Finding the GCD

*/

int gcd = gcd(numerator, denominator);

/**

* removing the GCD from numer..and denom..

*/

int newNumerator = numerator / gcd;

int newDenominator = denominator / gcd;

Fraction simplified = new Fraction(newNumerator, newDenominator);

return simplified;

}

/**

* checks if a Fraction number is less than this number

*/

public boolean lessThan(Fraction f) {

double value1 = numerator / denominator;

double value2 = f.numerator / f.denominator;

if (value1 < value2) {

return true;

} else {

return false;

}

}

/**

* checks if a Fraction number is greater than this number

*/

public boolean greaterThan(Fraction f) {

double value1 = numerator / denominator;

double value2 = f.numerator / f.denominator;

if (value1 > value2) {

return true;

} else {

return false;

}

}

public boolean equals(Fraction f) {

Fraction simplified1 = simplify();

Fraction simplified2 = f.simplify();

if (simplified1.numerator == simplified2.numerator

&& simplified2.denominator == simplified2.denominator) {

return true;

}

return false;

}

/**

* method to return a string representation of a number

*/

public String toString() {

return numerator + "/" + denominator;

}

}

// Quiz.java

import java.util.Random;

import java.util.Scanner;

public class Quiz {

// defining all the required attributes

static int DIFFICULTY = 5;

static Random random = new Random();

static Scanner scanner = new Scanner(System.in);

static char[] arith_operations = { '+', '-', '/', '*' };

static char[] relational_operations = { '<', '>', '=' };

public static void main(String[] args) {

/**

* setting difficulty level

*/

setDifficulty();

/**

* getting choice

*/

int choice = printMenuAndGetChoice();

if (choice == 1) {

// arithmetic quiz

playArithmetic();

} else {

// relational quiz

playRelational();

}

}

/**

* method to display the menu and return the choice

*

* @return either 1 or 2

*/

static int printMenuAndGetChoice() {

System.out.println("1. Arithmetic Quiz");

System.out.println("2. Relational Quiz");

System.out.println("## Choose your quiz");

int choice = Integer.parseInt(scanner.nextLine());

if (choice == 1 || choice == 2) {

return choice;

} else {

System.out.println("Invalid choice, try again");

/**

* prompting again

*/

return printMenuAndGetChoice();

}

}

/**

* method to prompt and set difficulty level between 2-9

*/

static void setDifficulty() {

do {

System.out.println("Enter a valid difficulty level (2-9): ");

DIFFICULTY = Integer.parseInt(scanner.nextLine());

} while (DIFFICULTY < 2 || DIFFICULTY > 9);

}

/**

* method to generate a random number between 1 and DIFFICULTY

*/

static int generateRandomNumber() {

return random.nextInt(DIFFICULTY) + 1;

}

/**

* method to generate a random arithmetic operation

*

* @return either +,-,/ or *

*/

static char generateRandomArithmeticOperation() {

int randomIndex = random.nextInt(arith_operations.length);

return arith_operations[randomIndex];

}

/**

* method to generate a random relational operation

*

* @return either <,> or =

*/

static char generateRandomRelationalOperation() {

int randomIndex = random.nextInt(relational_operations.length);

return relational_operations[randomIndex];

}

/**

* method to play the arithmetic quiz

*/

static void playArithmetic() {

boolean gameOver = false;

int score = 0;

int question = 0;

/**

* loops until game is over (wrong input is given)

*/

while (!gameOver) {

question++;

/**

* generating two random fraction numbers

*/

Fraction num1 = new Fraction(generateRandomNumber(),

generateRandomNumber());

Fraction num2 = new Fraction(generateRandomNumber(),

generateRandomNumber());

Fraction result = null;

/**

* generating a random operation

*/

char c = generateRandomArithmeticOperation();

/**

* performing the required operation and storing the result

*/

switch (c) {

case '+':

result = num1.add(num2).simplify();

break;

case '-':

result = num1.sub(num2).simplify();

break;

case '*':

result = num1.mult(num2).simplify();

break;

case '/':

result = num1.divide(num2).simplify();

break;

}

/**

* Displaying the question

*/

System.out.printf("Question (%d): %s %c %s ", question,

num1.toString(), c, num2.toString());

System.out

.println("Enter your answer: (numerator <space> denominator)");

/**

* getting answer

*/

String input = scanner.nextLine();

String fields[] = input.split(" ");

int numerator = Integer.parseInt(fields[0]);

int denominator = Integer.parseInt(fields[1]);

/**

* defining a fraction instance and checking if the it is equal to

* the result

*/

Fraction userInput = new Fraction(numerator, denominator);

if (result.equals(userInput)) {

System.out.println("Right answer!");

score++;

} else {

System.out.println("Wrong answer! the answer was " + result);

System.out.println("Your score: " + score);

gameOver = true;

}

}

}

/**

* method to play relational quiz

*/

static void playRelational() {

boolean gameOver = false;

int score = 0;

int question = 0;

while (!gameOver) {

question++;

Fraction num1 = new Fraction(generateRandomNumber(),

generateRandomNumber());

Fraction num2 = new Fraction(generateRandomNumber(),

generateRandomNumber());

boolean result = false;

/**

* generating a random relational operation, and storing the boolean

* value in a variable

*/

char c = generateRandomRelationalOperation();

switch (c) {

case '<':

result = num1.lessThan(num2);

break;

case '>':

result = num1.greaterThan(num2);

break;

case '=':

result = num1.equals(num2);

break;

}

/**

* Asking the question and getting the boolean result

*/

System.out.printf("Question (%d): %s %c %s ", question,

num1.toString(), c, num2.toString());

System.out.println("Enter your answer: (true or false)");

String input = scanner.nextLine();

boolean userInput;

if (input.equalsIgnoreCase("true")) {

userInput = true;

} else {

userInput = false;

}

if (result == userInput) {

System.out.println("Right answer!");

score++;

} else {

System.out.println("Wrong answer! the answer was " + result);

System.out.println("Your score: " + score);

gameOver = true;

}

}

}

}

/*OUTPUT*/

Enter a valid difficulty level (2-9):

3

1. Arithmetic Quiz

2. Relational Quiz

## Choose your quiz

1

Question (1): 1/2 - 2/1

Enter your answer: (numerator <space> denominator)

-3 2

Right answer!

Question (2): 3/3 * 1/2

Enter your answer: (numerator <space> denominator)

3 6

Right answer!

Question (3): 2/1 + 3/3

Enter your answer: (numerator <space> denominator)

5 3

Wrong answer! the answer was 3/1

Your score: 2

Enter a valid difficulty level (2-9):

5

1. Arithmetic Quiz

2. Relational Quiz

## Choose your quiz

2

Question (1): 1/2 < 1/4

Enter your answer: (true or false)

false

Right answer!

Question (2): 5/4 = 4/2

Enter your answer: (true or false)

false

Right answer!

Question (3): 4/4 = 2/3

Enter your answer: (true or false)

false

Right answer!

Question (4): 2/4 < 4/1

Enter your answer: (true or false)

true

Right answer!

Question (5): 3/1 < 1/1

Enter your answer: (true or false)

true

Wrong answer! the answer was false

Your score: 4