Skip to content

Commit 727bd1a

Browse files
committed
Added some more practiceing file.
1 parent 660dd21 commit 727bd1a

8 files changed

Lines changed: 84 additions & 0 deletions

File tree

799 Bytes
Binary file not shown.
1.42 KB
Binary file not shown.
446 Bytes
Binary file not shown.
806 Bytes
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package programmingInJava;
2+
3+
4+
public class Binary {
5+
6+
public static void main(String[] args) {
7+
int n = Integer.parseInt(args[0]);
8+
9+
int power = 1;
10+
while (power <= n/2) {
11+
power *= 2;
12+
}
13+
14+
while (power > 0) {
15+
if(n < power) {
16+
System.out.print(0);
17+
}
18+
else {
19+
System.out.println(1);
20+
n -= power;
21+
}
22+
power /= 2;
23+
}
24+
System.out.println();
25+
}
26+
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package programmingInJava;
2+
3+
public class Gambler {
4+
5+
public static void main(String[] args){
6+
int stake = Integer.parseInt(args[0]);
7+
int goal = Integer.parseInt(args[1]);
8+
int trials = Integer.parseInt(args[2]);
9+
10+
int bets = 0;
11+
int wins = 0;
12+
13+
for (int t = 0; t< trials; t++) {
14+
int cash = stake;
15+
while (cash > 0 && cash < goal) {
16+
bets++;
17+
if (Math.random() < 0.5) cash++;
18+
else cash--;
19+
}
20+
if (cash == goal) wins++;
21+
}
22+
23+
System.out.println(wins + "wins of " + trials);
24+
System.out.println("Percent of games won = " + 100.0 * wins / trials);
25+
System.out.println("Avg # bets = " + 1.0 * bets / trials);
26+
}
27+
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package programmingInJava;
22

3+
34
public class HarmonicNumber {
5+
6+
public static void main(String[] args) {
7+
int n = Integer.parseInt(args[0]);
8+
9+
double sum = 0.0;
10+
for (int i= 1; i <= n; i++) {
11+
sum += 1.0 / i;
12+
13+
}
14+
15+
System.out.println(sum);
16+
}
417

518
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package programmingInJava;
2+
3+
4+
public class Squrt {
5+
6+
public static void main(String[] args) {
7+
double c = Double.parseDouble(args[0]);
8+
double epsilon = 1e-15;
9+
double t = c;
10+
11+
while (Math.abs(t - c/t) > epsilon*t) {
12+
t = (c/t + t) / 2.0;
13+
}
14+
System.out.println(t);
15+
}
16+
}

0 commit comments

Comments
 (0)