-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrak3.java
More file actions
147 lines (131 loc) · 4.42 KB
/
Prak3.java
File metadata and controls
147 lines (131 loc) · 4.42 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
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
import java.util.*;
public class Prak3 {
private Double roundTo(Double toRound, int n) {
return (double) Math.round(toRound / Math.pow(10, n)) * Math.pow(10, n);
}
//1
public void millionsRounding() {
Map<String, Integer> places = new HashMap<>();
places.put("Nice", 942208);
places.put("Abu Dhabi", 1482816);
places.put("Naples", 2186853);
places.put("Vatican City", 572);
printValues(places);
}
public void printValues(Map<String, Integer> map) {
for (Map.Entry<String, Integer> pair : map.entrySet()) {
double value = pair.getValue();
String key = pair.getKey();
value = Math.round(value / 1000000) * 1000000;
int x = (int) value;
System.out.println("[" + key + " " + x + "]");
}
}
//2
public ArrayList<Double> otherSides(Double len) {
return new ArrayList<Double>(Arrays.asList(roundTo(2 * len, -2), roundTo(Math.sqrt(3) * len, -2)));
}
//3
public String rps(String arg1, String arg2) {
Map<String, Integer> states = new HashMap<>();
states.put("rock", 1);
states.put("scissor", 2);
states.put("paper", 3);
int player1 = states.get(arg1);
int player2 = states.get(arg2);
int result = player1 - player2;
String gameResult = "Draw";
switch (result) {
case 2:
case -1:
gameResult = "Player 1 wins";
break;
case 1:
case -2:
gameResult = "Player 2 wins";
break;
}
return gameResult;
}
//4
public int warOfNumbers(int[] array) {
int even = 0;
int odd = 0;
for (int i : array) {
if (i % 2 == 0) {
even += i;
continue;
}
odd += i;
}
return Math.abs(even - odd);
}
//5
public String reverseCase(String text) {
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (Character.isUpperCase(c)) {
chars[i] = Character.toLowerCase(c);
} else if (Character.isLowerCase(c)) {
chars[i] = Character.toUpperCase(c);
}
}
return new String(chars);
}
//6
public String inatorInator(String arg) {
String vowels = "aeiouy";
String lastChar = arg.substring(arg.length() - 1);
String output = "";
if (vowels.contains(lastChar)) {
output = arg + "-inator" + " " + arg.length() + "000";
} else {
output = arg + "inator" + " " + arg.length() + "000";
}
return output;
}
//7
public boolean doesBrickFit(int height, int width, int thickness, int w, int h) {
ArrayList<Integer> brickDimensions = new ArrayList<Integer>(Arrays.asList(height, width, thickness));
brickDimensions.remove(Collections.max(brickDimensions));
return (brickDimensions.get(0) <= w && brickDimensions.get(1) <= h) || (brickDimensions.get(1) <= w && brickDimensions.get(0) <= h);
}
//8
public double totalDistance(double petrol, double petrolC, int passenger, boolean aC) {
double distance;
double petrolUsage;
if (aC) {
petrolUsage = petrolC * (1 + 0.05 * passenger) * (1 + 0.1);
} else {
petrolUsage = petrolC * (1 + 0.05 * passenger);
}
distance = petrol / petrolUsage * 100;
return distance;
}
//9
public double mean(double[] args) {
double sum = 0;
for (double i : args) {
sum += i;
}
return sum / args.length;
}
//10
public boolean parityAnalysisOld(String argStr) {
int arg = Integer.parseInt(argStr);
int argSum = 0;
String[] argSplit = argStr.split("");
for (int i = 0; i < argStr.length(); i++) {
argSum += Integer.parseInt(argSplit[i]);
}
return (arg % 2 - argSum % 2) == 0;
}
public boolean parityAnalysis(int arg) {
int argSum = 0;
for (int i = arg; i > 0; i /= 10) {
argSum += i % 10;
}
return (arg % 2 - argSum % 2) == 0;
}
}