vicuna-33b· LMSYS
Arena Human Preference 55K
Write a simple Java program for a calculator that can understand and display fractions…
- User
Write a simple Java program for a calculator that can understand and display fractions and does simple calculation of plus, minus, divide, muluply.
- Assistant
Here's a simple Java program for a calculator that can understand and display fractions and perform basic calculations like addition, subtraction, multiplication, and division:
import java.util.Scanner; public class FractionCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the Fraction Calculator!"); System.out.println("Enter the first fraction: "); double[] firstFraction = getFraction(scanner); double[] secondFraction = getFraction(scanner); while (true) { System.out.println("Enter an operation (+, -, *, /) or type 'q' to quit: "); String operation = scanner.next(); if ("+".equals(operation)) { double[] result = addFractions(firstFraction, secondFraction); displayFraction(result); firstFraction = result; } else if ("-".equals(operation)) { double[] result = subtractFractions(firstFraction, secondFraction); displayFraction(result); firstFraction = result; } else if ("*".equals(operation)) { double[] result = multiplyFractions(firstFraction, secondFraction); displayFraction(result); firstFraction = result; } else if ("/".equals(operation)) { double[] result = divideFractions(firstFraction, secondFraction); displayFraction(result); firstFraction = result; } else if ("q".equals(operation)) { break; } else { System.out.println("Invalid operation. Please use +, -, *, / or q."); } System.out.println("Enter the second fraction or type 'q' to quit: "); secondFraction = getFraction(scanner); } scanner.close(); } private static double[] getFraction(Scanner scanner) { System.out.println("Enter the numerator: "); double numerator = scanner.nextDouble(); System.out.println("Enter the denominator: "); double denominator = scanner.nextDouble(); return new double[]{numerator, denominator}; } private static double[] addFractions(double[] f1, double[] f2) { double[] result = new double[2]; result[0] = f1[0] * f2[1] + f1[1] * f2[0]; result[1] = f1[1] * f2[1]; return result; } private static double[] subtractFractions(double[] f1, double[] f2) { double[] result = new double[2]; result[0] = f1[0] * f2[1] - f1[1] * f2[0]; result[1] = f1[1] * f2[1]; return result; } private static double[] multiplyFractions(double[] f1, double[] f2) { double[] result = new double[2]; result[0] = f1[0] * f2[0]; result[1] = f1[1] * f2[1]; return result; } private static double[] divideFractions(double[] f1, double[] f2) { double num = f1[0] * f2[0]; double den = f1[1] * f2[1]; double[] result = new double[2]; result[0] = num / den; result[1] = f1[1] / f2[1]; return result; } private static void displayFraction(double[] fraction) { System.out.println("Result: " + fraction[0] + "/" + fraction[1]); } }This program allows the user
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.