Saturday, January 4, 2014

10071: Back to High School Physics (Java)

I was able to solve this problem previously using C++. I tried to rewrite my code using Java. I have very little experience with Java and am somewhat learning slowly. So I tried to rewrite my previous code using the language Java that is steadily gaining popularity.

It took me a while to get the syntax right. First I realized that the class has to be named "Main"; otherwise, uVA will not accept the code and report compile error. I also somewhat learned how to get lines from standard input using Java. All of this is very technical in nature. All syntax concerns and not really algorithmic concerns. Here is my code after suffering with the syntax. Pain is associated with learning something new.

I noticed that Java somewhat runs slower than C++ for the same problem. Or maybe because I don't know how to optimize my Java code. I noticed that this code runs for about 1++ seconds, whereas my previous C++ code runs in few milliseconds.

import java.util.Scanner; 

public class Main {
  public static void main(String args[]) {
    int v, t, r;
    Scanner sc = new Scanner(;
         v = sc.nextInt();
         t = sc.nextInt();
         r = v * t;
         r = r << 1;

No comments:

Post a Comment