Java – Exam – Sum of Cards

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:

Java_Tests

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:

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 :

 

 

Tagged with: , ,