-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserInput.java
99 lines (93 loc) · 2.79 KB
/
userInput.java
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
95
96
97
98
99
import java.util.Scanner;
public class userInput {
public static final int START_VALUE = 0;
public static void main(String[] args) {
Rational firstRational;
Rational secondRational;
System.out.println("Please input your first Rational Number");
firstRational = initiateRational();
System.out.println("Please input your second Rational Number");
secondRational = initiateRational();
Rational additionRational = firstRational.add(secondRational);
Rational subRational = firstRational.subtract(secondRational);
Rational multiplyRational = firstRational.multiply(secondRational);
Rational divideRational = firstRational.divide(secondRational);
System.out.println("Your numbers simplify are: " +firstRational.toString()
+ " + " + secondRational.toString());
System.out.println("The result of the addition is: " + additionRational.toString());
System.out.println("The result of the subtraction is: " + subRational.toString());
System.out.println("The result of the multiplication is: " + multiplyRational.toString());
System.out.println("The result of the division is: " + divideRational.toString());
if(firstRational.equals(secondRational) )
{
System.out.println("The two rationals are equals");
}
else
{
System.out.println("The two rationals are not equals");
if(firstRational.isLessThan(secondRational) )
{
System.out.println("The first Rational is less than the second");
}
else
{
System.out.println("The first Rational is greather than the second");
}
}
}
public static Rational initiateRational()
{
boolean inputNum = true;
boolean inputDen = true;
int numerator = START_VALUE;
int denominator = START_VALUE ;
Scanner userInput = new Scanner(System.in);
while(inputNum)
{
System.out.print("Please enter the numerator:");
if( userInput.hasNextLine())
{
if( userInput.hasNextInt() )
{
numerator = userInput.nextInt();
inputNum = false;
}
else
{
System.out.println("Please input the number again."
+ " Remember enter them as intergers");
}
}
userInput = new Scanner(System.in);
}
while(inputDen)
{
System.out.print("Please enter the denominator:");
if( userInput.hasNextLine())
{
if( userInput.hasNextInt() )
{
denominator = userInput.nextInt();
if( denominator ==START_VALUE)
{
System.out.println("Please input the number again."
+ " Remember zero can't be on the bottom");
}
else
{
inputDen = false;
}
}
else
{
System.out.println("Please input the number again."
+ " Remember enter them as intergers");
}
}
userInput = new Scanner(System.in);
}
Rational newRational = new Rational (numerator, denominator);
newRational.simplify();
return newRational;
}
}