Today, I was participating in my first Java Exam at the Software University!
It was definitely NOT a success, as far as I had huge problems installing Eclipse and I was actually not able to run it properly before entering the contest. Anyway, I am not going to cry about it and I will simply show you what I did – I managed to solve one problem out of four, which ranked me in 101. place out of 226 participants. Not good, considering the fact that I actually make a living with coding, but quite OK if you take in mind that I have never touched Java before (and I am not quite sure I will ever do something with it again). The complete desciption of the problems and their original solutions can be found here (in zip).
So, with 130 points I think I really have huge room for improvement in Java. 130 points were actually one complete problem (100 points) and 30 points from other two problems:
So, what was the problem about, which I managed to solve?
The good old idea, of calculating the sum of cards. As an input, you are given a string of cards and as an output, you should give the total sum of the cards’ value. To make it a little more complicated, if two cards are given next to each other, their result is doubled.
E.g. Input: 2S 2H 3S 2C = 2*2 + 2*2 + 3 + 2 = 13
Finally, you should make the whole thing RUN in Eclipse, powered by Java, which is quite a task, if you have never actually written anything in Java.
So, what I did? At first, I was thinking to write the whole problem in C# and then to look for online C# to Java converter, but then I considered this a lame option. Then I simply started to look for solutions. After some time, I managed to understand what exactly a scanner is (I am ashamed to admit I tought that I should install hardware 🙂 ) and after some time I managed to create an array from the entry line! Not bad for a Java beginner:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class SumCards { public static void main(String[] args) { String myCards; Scanner in = new Scanner(System.in); myCards = in.nextLine(); String[] couple = myCards.split(" "); Integer Result = 0; Integer PreviousValue = 0; boolean firstDuplicate = true; |
Then I knew what I had to do… A simple for loop for all the members of the array, assigning corresponding values to each member. However, if we have two Queens, how do we know that we should multiply the first one, if we have not seen the second one? There I used a simple trick with boolean firstDuplicate which worked out successfully … after some time. So, pretty much the result is not the best one, but I was quite lazy to read how to make a method in Java (although I made one later). So, below you have a lot of code repetition, which is useless, but the program runs quite OK and gave me 100% of the points. So, enjoy the code and feel free to reuse it (you may want to format it before this – “Ctrl+Shift+F”) or submit it to http://govnokod.ru :
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
import java.util.*; public class SumCards { public static void main(String[] args) { String myCards; Scanner in = new Scanner(System.in); myCards = in.nextLine(); String[] couple = myCards.split(" "); Integer Result = 0; Integer PreviousValue = 0; boolean firstDuplicate = true; for(int i = 0; i < couple.length ; i++) { if(couple[i].charAt(0) == '1'){ if (PreviousValue == 10){ if(firstDuplicate){ Result += 10; firstDuplicate = false; } Result += 10*2; } else{ firstDuplicate = true; Result += 10; PreviousValue = 10; } } else if(couple[i].charAt(0) == '2'){ if (PreviousValue == 2){ if(firstDuplicate){ Result += 2; firstDuplicate = false; } Result += 2*2; } else{ firstDuplicate = true; Result += 2; PreviousValue = 2; } } else if(couple[i].charAt(0) == '3'){ if (PreviousValue == 3){ if(firstDuplicate){ Result += 3; firstDuplicate = false; } Result += 3*2; } else{ firstDuplicate = true; Result += 3; PreviousValue = 3; } } else if(couple[i].charAt(0) == '4'){ if (PreviousValue == 4){ if(firstDuplicate){ Result += 4; firstDuplicate = false; } Result += 4*2; } else{ firstDuplicate = true; Result += 4; PreviousValue = 4; } } else if(couple[i].charAt(0) == '5'){ if (PreviousValue == 5){ if(firstDuplicate){ Result += 5; firstDuplicate = false; } Result += 5*2; } else{ firstDuplicate = true; Result += 5; PreviousValue = 5; } } else if(couple[i].charAt(0) == '6'){ if (PreviousValue == 6){ if(firstDuplicate){ Result += 6; firstDuplicate = false; } Result += 6*2; } else{ firstDuplicate = true; Result += 6; PreviousValue = 6; } } else if(couple[i].charAt(0) == '7'){ if (PreviousValue == 7){ if(firstDuplicate){ Result += 7; firstDuplicate = false; } Result += 2*7; } else{ firstDuplicate = true; Result += 7; PreviousValue = 7; } } else if(couple[i].charAt(0) == '8'){ if (PreviousValue == 8){ if(firstDuplicate){ Result += 8; firstDuplicate = false; } Result += 2*8; } else{ firstDuplicate = true; Result += 8; PreviousValue = 8; } } else if(couple[i].charAt(0) == '9'){ if (PreviousValue == 9){ if(firstDuplicate){ Result += 9; firstDuplicate = false; } Result += 2*9; } else{ firstDuplicate = true; Result += 9; PreviousValue = 9; } } else if(couple[i].charAt(0) == 'J'){ if (PreviousValue == 12){ if(firstDuplicate){ Result += 12; firstDuplicate = false; } Result += 2*12; } else{ firstDuplicate = true; Result += 12; PreviousValue = 12; } } else if(couple[i].charAt(0) == 'Q'){ if (PreviousValue == 13){ if(firstDuplicate){ Result += 13; firstDuplicate = false; } Result += 2*13; } else{ firstDuplicate = true; Result += 13; PreviousValue = 13; } } else if(couple[i].charAt(0) == 'K'){ if (PreviousValue == 14){ if(firstDuplicate){ Result += 14; firstDuplicate = false; } Result += 2*14; } else{ firstDuplicate = true; Result += 14; PreviousValue = 14; } } else if(couple[i].charAt(0) == 'A'){ if (PreviousValue == 15){ if(firstDuplicate){ Result += 15; firstDuplicate = false; } Result += 2*15; } else{ firstDuplicate = true; Result += 15; PreviousValue = 15; } } } System.out.println(Result); } } |