
-----------------------------------
wtd
Wed Aug 25, 2004 6:17 pm

[Eiffel/O'Caml-tut] Translations from Java and C++
-----------------------------------
Comparisons of basic syntax.

Outputting a line to standard output

// in Java

System.out.println("Hello world");

// in C++

std::cout  0 in
   print_endline (match input with
      0 -> "What a tiny number!"
      | 1 -> "It's a start."
      | 2 -> "That's more like it."
      | _ -> "Show-off...")

Previous example, broken down into functions.

// in Java

import java.lang.*;
import java.io.*;

public class Test {
   private static BufferedReader in = new BufferedReader(
      new InputStreamReader(System.in));

   private static int getInput() {
      int output;

      try {
         output = Integer.parseInt(in.readLine());
      } catch (NumberFormatException e) {
         output = 0;
      }

      return 0;
   }

   private static String getResponse(int input) {
      switch (input) {
         case 0: return "What a tiny number!";
         case 1: return "It's a start.";
         case 2: return "That's more like it.";
         default: return "Show-off...";
      } 
   }

   public static void main(String[] args) {
      int input = getInput();
      String response = getResponse(input);
      System.out.println(response);
   }
}

// in C++

#include 
#include 

int get_input()
{
   int output = 0;
   std::cin >> output;
   return output;
}

std::string get_response(int input)
{
   switch (input) 
   {
      case 0: return "What a tiny number!";
      case 1: return "It's a start.";
      case 2: return "That's more like it.";
      default: return "Show-off...";
   } 
}

int main()
{
   int input = get_input();
   std::string response = get_response(input);
   std::cout 