Created
September 13, 2021 17:50
-
-
Save jananpatel2002/5fd9fd49d9a32097443ab5f787166e9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 9/13/2020 | |
* Course Number: 220 | |
* Course Name: Data Structures | |
* Problem Number: 1 | |
* Email: jkpatel2001@student.stcc.edu | |
* Short Description of the Problem: Reducing fractions | |
*/ | |
import java.util.Scanner; | |
public class GcfProgram { | |
private final static String TITLE = "GCF Program V1.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
// ********************************************** | |
// Put as many methods you need here | |
private static int gcf(int n1, int n2) { | |
n1 = Math.abs(n1); | |
n2 = Math.abs(n2); | |
if (n2 == 0) { | |
return n1; | |
} | |
int rem = n1 % n2; | |
while (rem != 0) { | |
n1 = n2; | |
n2 = rem; | |
rem = n1 % n2; | |
} | |
return n2; | |
} | |
private static String outputFraction(int n1, int n2) { | |
String retval = ""; | |
if (n2 < 0) { | |
n1 = -n1; | |
n2 = -n2; | |
} | |
retval = retval + n1 + "/" + n2; | |
return retval; | |
} | |
private static String simplifiedFraction(int n1, int n2) { | |
int GCF = gcf(n1, n2); | |
n1 = n1 / GCF; | |
n2 = n2 / GCF; | |
if (n2 < 0) { | |
n1 = -n1; | |
n2 = -n2; | |
} | |
int prediv = Math.floorDiv(n1, n2); | |
if ((n1 < 0) && prediv != -1) { | |
n1 = -n1; | |
int div = Math.floorDiv(n1, n2); | |
int rem = n1 % n2; | |
return "-" + div + " " + rem + "/" + n2; | |
} | |
if (n1 > n2) { | |
int div = Math.floorDiv(n1, n2); | |
int rem = n1 % n2; | |
return "" + div + " " + rem + "/" + n2; | |
} | |
if (n2 == 1) { | |
return n1 + " "; | |
} | |
return outputFraction(n1, n2); | |
} | |
// ********************************************** | |
// Start your coding your logic in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
System.out.print("Enter Numerator: "); | |
int n1 = input.nextInt(); | |
System.out.print("Enter Denominator: "); | |
int n2 = input.nextInt(); | |
System.out.println("\n" + outputFraction(n1, n2) + " => " + simplifiedFraction(n1, n2) + "\n"); | |
input.nextLine(); // Clear Keyboard | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment