Fall 2006»Small Example

Small Example

Comparing Java and Python

Given the following task, write a program in the language of your choice (here we consider only Java and Python).

Things to consider include: How many times the user has to look up something up (e.g., search the language API), how many compiler errors; how many semantic errors; how many "headache" errors, i.e., errors that are hard to identify and/or fix (e.g., there is one such error remaining in the Java code below - see if you can find it).


Task: A meeting is scheduled to start at 3:15pm. It will end at 4pm, if the building where the meeting is held is J.C Long, or at 3:50pm otherwise.

Input: The building. (Assume error-free input.)

Output: The meeting's start and end time.


First, Python:

def meetingTime(building):
   start = "3:15pm"
   if (building == "J.C. Long"):
      end = "4:00pm"
   else:
      end = "3:50pm"

   return start, end

building = raw_input()
print meetingTime(building)
 

Now, Java:

import java.util.Scanner;

public class MeetingTime
{
    public static void main (String[] args)
    {
        MeetingTime obj = new MeetingTime();
        Scanner sc = new Scanner(System.in);
        String building = sc.next();
        String time[] = obj.getMeetingTime(building);
        System.out.println(time[0]+ "," + time[1]);
    }

    public String[] getMeetingTime(String building)
    {
       String start = "3:15pm";
       String end;
       if (building == "J.C. Long")
          end = "4:00pm";
       else
          end = "3:50pm";

       String answer[] = new String[2];
       answer[0] = start;
       answer[1] = end;
       return answer;
    }
}