Java problem using 1/2 and 3/4 for the variables
Code with C  Programming: Projects & Source Codes › Forums › JAVA Programming › Java problem using 1/2 and 3/4 for the variables
This topic contains 1 reply, has 1 voice, and was last updated by Adelaid 2 years, 6 months ago.

AuthorPosts

November 3, 2016 at 12:11 pm #10923
Here is the code I was given
1234567891011121314151617181920212223242526272829import java.util.Scanner;class Fraction{public static void main (String[] args) {String div; //decalring a variable for the stringint a, b, c, d;Scanner scan = new Scanner(System.in); //creating an object to use in the program belowSystem.out.print("Type a rational number (a / b): ");a = scan.nextInt();div = scan.next("/");b = scan.nextInt();System.out.print("Type a another rational number (c / d): ");c = scan.nextInt();div = scan.next("/");d = scan.nextInt();System.out.println("The sum in fractions is ");System.out.println(The input here is suppose to hold the same values for the variables previously used above not sure how to do this);System.out.println("The sum in decimals is ");System.out.println(Again based on input from the user, this is suppose to be used with a float casting to provide a precise sum);}}The output of the code is suppose to look like this, when using 1/2 and 3/4 for the variables. Any help would be greatly appreciated.
12345678Enter a fraction (a / b): 1 / 2Enter another fraction (a / b): 3 / 4The sum in fractions is1/2 + 3/4 = 10/8The sum in decimals is0.5 + 0.75 = 1.25 
November 3, 2016 at 12:12 pm #10924
Where you want to print out the variables in the last println statements mixed with integers but mention you’re not sure how to do it, you use string concatenation. From the example i used below:
1System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + calcFraction(a, b, c, d));To calculate the sum in fractions you need to find the LCD (lowest common denominator) and the GCD is then used to calculate the simplified fraction. With some googling I found already made methods for these. The code still has exceptions to be handled but works with the example cases, if you need any clarification just let me know. (Second year CS student)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import java.text.DecimalFormat;import java.util.Scanner;class Fraction {static DecimalFormat df = new DecimalFormat("###,###,###.###");public static void main(String[] args) {String div; //decalring a variable for the stringint a, b, c, d;Scanner scan = new Scanner(System.in); //creating an object to use in the program belowSystem.out.print("Type a rational number (a / b): ");a = scan.nextInt();div = scan.next("/");b = scan.nextInt();System.out.print("Type a another rational number (c / d): ");c = scan.nextInt();div = scan.next("/");d = scan.nextInt();System.out.println("The sum in fractions is");System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + calcFraction(a, b, c, d));System.out.println("The sum in decimals is ");System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + df.format(calcDecimal(a, b, c, d)));}private static int calcGCD(int a, int b) {if (b == 0) {return a;}return calcGCD(b, a % b);}private static int calcLCM(int x1, int x2) {if (x1 < = 0  x2 <= 0) {throw new IllegalArgumentException("Cannot compute the least "+ "common multiple of two "+ "numbers if one, at least,"+ "is negative.");}int max, min;if (x1 > x2) {max = x1;min = x2;} else {max = x2;min = x1;}for (int i = 1; i < = min; i++) {if ((max * i) % min == 0) {return i * max;}}throw new Error("Cannot find the least common multiple of numbers "+ x1 + " and " + x2);}private static String calcFraction(int a, int b, int c, int d) {String ans = null;int lcm = calcLCM(b, d);int timesToMultiplyAB = lcm / b;a *= timesToMultiplyAB;int timesToMultiplyCD = lcm / d;c *= timesToMultiplyCD;int numerator = a + c;int denominator = lcm;int gcd = calcGCD(numerator, denominator);numerator /= gcd;denominator /= gcd;ans = numerator + "/" + denominator;return ans;}private static double calcDecimal(double a, double b, double c, double d) {double ans;double left = a / b, right = c / d;ans = left + right;return ans;}} 
AuthorPosts
You must be logged in to reply to this topic.