Repository navigation
Expand file tree
/
Copy pathMethodsExercises.java
More file actions
111 lines (87 loc) · 3.21 KB
/
Copy pathMethodsExercises.java
File metadata and controls
111 lines (87 loc) · 3.21 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.Random;
import java.util.Scanner;
public class MethodsExercises {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
rollDice(scan);
while(true){
System.out.println("factorial test");
factorial();
System.out.println("Do you wish to continue? y/n");
String answer = scan.nextLine();
if(answer.toLowerCase().equals("n")){
break;
}
}
System.out.println("getInteger test");
getInteger(1, 10);
System.out.println( 2 == sum(1,1) );
System.out.println( 0 == sum(-1,1) );
System.out.println( 0 != sum(-2,-5) );
// System.out.println(subtract(5,1));
System.out.println( 4 == subtract(5,1) );
System.out.println( -2 == subtract(-1,1) );
System.out.println( 5 == multiply(5,1) );
System.out.println( -5 == multiply(-1,5) );
System.out.println( 2 == divide(10,5) );
System.out.println( 2 == divide(30,15) );
System.out.println( 0 == divide(30,0) );
System.out.println( 1 == modulus(5,2) );
System.out.println( 0 == modulus(6,2) );
}
public static double sum(double arg1, double arg2){
return arg1 + arg2;
}
public static double subtract(double arg1, double arg2){
return arg1 - arg2;
}
public static double multiply(double arg1, double arg2){
return arg1 * arg2;
}
public static double divide(double arg1, double arg2){
if(arg2 == 0){
System.out.println("Division by zero is not possible, zero is returned");
return 0;
}
return arg1 / arg2;
}
public static double modulus(double arg1, double arg2){
return arg1 % arg2;
}
public static int getInteger(int min, int max) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number between 1 and 10: ");
int userInput = Integer.parseInt(scan.nextLine());
// how to proceed
if( userInput < min || userInput > max ){
System.out.println("Number is outside of the range");
return getInteger(min, max);
} else {
// how to stop
System.out.println("Number is within the range");
return userInput;
}
}
public static void factorial(){
int userInput = getInteger(1,10);
long sum = 1;
for (int fa = userInput; fa > 0; fa--){
sum *= fa;
}
System.out.println("factorial of " + userInput +" = " + sum);
}
public static void rollDice(Scanner scan){
while( true ){
System.out.println("rollDice test");
System.out.println("Give me the number of sides");
short n = Short.parseShort(scan.nextLine());
int range = n - 1 + 1;
System.out.println("roll dice 1 = " + ((int)(Math.random() * range) + 1) );
System.out.println("roll dice 2 = " + ((int)(Math.random() * range) + 1) );
System.out.println("Do you wish to continue? y/n");
if(scan.nextLine().toLowerCase().equals("n")){
break;
}
}
}
}