-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemSet1X.java
160 lines (128 loc) · 4.21 KB
/
ProblemSet1X.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ProblemSets;
import java.lang.Math;
public class ProblemSet1X {
public static void main(String[] args){
// Question 1 test cases.
addTime(0, 20, 0, 30);
addTime(0, 40, 0, 30);
addTime(1, 30, 1, 31);
addTime(6, 30, 4, 45);
// Question 2a test cases.
sumUpTo(9);
// Question 2b test cases.
int a[] = {-12, 3, 20, 21, -15, 2};
int b[] = {};
int c[] = new int[10];
sumIntArrayAll(a);
sumIntArrayAll(b);
sumIntArrayAll(c);
// Question 2c test cases.
int d[] = {-12, 3, 20, 21, -15, 22};
int e[] = {};
sumIntArrayTwenty(d);
sumIntArrayTwenty(e);
// Question 2d test cases
int f[] = {-12, 3, 0, 21, -15, 2};
int g[] = {};
int h[] = {2, 3, 9, 5, 4, 1, 1};
countEvenNumbers(f);
countEvenNumbers(g);
countEvenNumbers(h);
// Question 3 test cases
termsRequired(0.9);
// Question 4 test cases
binaryToDecimal("1011");
binaryToDecimal("0");
binaryToDecimal("1");
binaryToDecimal("1101");
binaryToDecimal("1111");
}
// Question 1
public static void addTime(int hour1, int min1, int hour2, int min2){
int totalMin = min1 + min2;
int totalHour = hour1 + hour2;
// if condition if minutes are more than 60.
// % is used to find the remainder (e.g. 61 % 60 = 1). Therefore it is used to find the
// remaining minutes.
if (totalMin >= 60){
int remainHour = totalMin / 60;
int remainMin = totalMin % 60;
int finalHour = totalHour + remainHour;
System.out.println(finalHour + " hours " + remainMin + " minutes ");
}
else if (totalMin < 60) {
System.out.println(totalHour + " hours " + totalMin + " minutes ");
}
}
// Question 2a
public static void sumUpTo(int n){
int result = 0;
for (int i = 0; i <= n; i++){
result = result + i;
}
System.out.println("Question 1 answer: " + result);
}
// Question 2b
public static void sumIntArrayAll(int[] array){
int result = 0;
for (int i = 0; i < array.length; i++){
result = result + array[i];
}
System.out.println("Question 2b answer: " + result);
}
// Question 2c
public static void sumIntArrayTwenty(int[] array){
int result = 0;
for (int i = 0; i < array.length; i++){
if (array[i] > 20){
result = result + array[i];
}
}
System.out.println("Question 2c answer: " + result);
}
// Question 2d
public static void countEvenNumbers(int[] array){
int result = 0;
for (int i = 0; i < array.length; i++){
if (array[i] % 2 == 0) {
result += 1;
}
}
System.out.println("Question 2d answer: " + result);
}
// Question 3
public static void termsRequired(double p){
double fraction = (p * (Math.PI * Math.PI) / 6);
int n = 1;
int count = 0;
double sum = 0.0;
while (sum < fraction){
sum = sum + (1 / (Math.pow(n, 2)));
n++;
count++;
}
System.out.println("Question 3 Number of Terms Required: " + count);
}
// Question 4
public static char getCharFromString(String str, int index){
return str.toCharArray()[index];
}
public static void binaryToDecimal(String s){
int total = 0;
int sLength = s.length();
// loop through the string, determine the i position, then
// determine if its a 1 or 0, then
// calculate the equivalent value and add it to the total.
// binary = 1011
for (int i = 0; i <= sLength - 1; i++){
char digit = getCharFromString(s, i);
String binary = String.valueOf(digit);
if (binary.equals("1")){
int decimal = (int) Math.pow(2, sLength - 1 - i);
total = total + decimal;
}
}
// print result
System.out.println("Question 4 answer: " + total);
}
}