Java – Exam Problem – Video Length

As I have already mentioned in this article, I have had a small fiasco at the Java Exam of the SoftUni. Anyway, I managed to pass the exam, so actually if I manage to graduate in my certificate Java would be present. 🙂 As far as I prefer to be able to actually solve the tasks, I have decided to take a look of the Java problems, given at the three exams and to try to solve them one by one.

Java_images

Tonight I tried to solve the first task of the first the exam – Video Length. I had really a good idea how to solve it, but I found out, that the Java syntax is really weird – it did not recognize my compare strings with “==”, so I simply cheated and I looked at the solution. Thus, I have decided to present you the solution line by line and to explain what it does.

So, here is the first part of the code:

So we do the following:

  1. We initialize int called totalMinutes;
  2. We initialize object named “input”, that we use to read from the console;
  3. We start a loop with while(true), and we read each line;
  4. If the line equals “End” (this is what I was not able to do for quite some time), then we break the while-loop;

The second part:

So, in the second part:

  1. We split the string “videoDuration” by the sign “:”. Thus, we receive tokens[0] and tokens[1];
  2. We parse the tokens[0] into int hours and tokens [1] into minutes;
  3. Then we calculate the minutes in total, multiplying hours by 60;
  4. At the end we calculate the total hours and the total minutes (for the minutes we use the % operator, which gives us the remainder only);
  5. At the end we print the result with System.out.printf. The “02” is needed, in order to obtain 100 points, otherwise it gives you about 70. The “\n” can be skipped.

If you skip the “02” in the last line and you may receive error in the following input:

About

VBA Developer

Tagged with: , , ,