-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2310_myFractionScale.java
More file actions
95 lines (58 loc) · 2.84 KB
/
CSc2310_myFractionScale.java
File metadata and controls
95 lines (58 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class myFractionScale {
//Stephanie Wyckoff
//7/10/2016
//FractionScale
//prompts user for a fraction and a scale factor, scales the fraction up or down by the factor per the users input. Prompts user for another fraction to divide by and
//prints out the quotient. User may enter spaces before or after the integer parts if they wish.
public static int findNum(String input){
int dash = input.indexOf('/');
String temp = input.substring(0, dash);
return Integer.parseInt(temp); //Returns string converted to int
}
public static int findDen(String input){
int dash = input.indexOf('/');
String temp = input.substring(dash + 1, input.length());
return Integer.parseInt(temp); //Returns string converted to int
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int scale;
int factor;
System.out.println("Enter a fraction: ");
String frac = input.nextLine(); //works fine here but I copied it to the section below (with updated variable names) and it doesnt work?
frac = frac.replaceAll("\\s", ""); //This removes any spaces given by the user
Fraction frac1 = new Fraction(findNum(frac), findDen(frac)); //Creates new Fraction object
frac1.getNumerator();
frac1.getDenominator();
System.out.print("\nScale up or down (1: up, 0: down): ");
scale = input.nextInt();
while(scale != 1 && scale != 0){
System.out.println("Please enter either a 1 to scale up or 0 to scale down. ");
scale = input.nextInt();
}
System.out.print("\nEnter a scale factor: ");
factor = input.nextInt();
if(scale == 1){
while(factor == 0){
System.out.println("\n"+frac1.scale(factor, true));
factor = input.nextInt();
}
System.out.print("\nThe scaled fraction is: " + frac1.scale(factor, true));
}
else if(scale == 0){
while(factor == 0){
System.out.print("\n"+frac1.scale(factor, false));
factor = input.nextInt();
}
System.out.print("\nThe scaled fraction is: " + frac1.scale(factor, false));
}
System.out.println("\n\nEnter a fraction to divide by: ");
Scanner scan = new Scanner(System.in);
String newFrac = scan.nextLine(); //throws string index out of range: -1, if i use nextLine, but should be same as above right? Works with .next as long as no spaces
newFrac = newFrac.replaceAll("\\s", ""); //Only replacing first set of spaces because of the next vs nextLine above, throws out of bounds exception
Fraction frac2 = new Fraction(findNum(newFrac), findDen(newFrac)); //Creates new Fraction object
Fraction quotient = frac1.divide(frac2);
System.out.println("\nThe quotient of the fractions is: " + quotient.getNumerator() + "/" + quotient.getDenominator());
}
}