Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Eiffel/O'Caml-tut] Translations from Java and C++
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Wed Aug 25, 2004 6:17 pm   Post subject: [Eiffel/O'Caml-tut] Translations from Java and C++

Comparisons of basic syntax.

Outputting a line to standard output

code:
// in Java

System.out.println("Hello world");


code:
// in C++

std::cout << "Hello world" << std::endl;


code:
-- in Eiffel

std_output.put_string("Hello world")
std_output.put_new_line


code:
(* in O'Caml *)

print_endline "Hello world"


Outputting a line to a file

code:
// in Java

PrintStream outputFile = new PrintStream(
   new FileOutputStream("output.txt"));
outputFile.println("Hello world");



code:
// in C++

std::ofstream output_file("output.txt");
output_file << "Hello world" << std::endl;


code:
-- in Eiffel

local
   output_file: TEXT_FILE_WRITE
do
   create output_file.connect_to("output.txt")
   output_file.put_string("Hello world")
   output_file.put_new_line
end


code:
(* in O'Caml *)

let output_file = open_out "output.txt" in
   output_string output_file "Hello world";
   output_char output_file '\n'


Assignment and a simple conditional

code:
// in Java

String input = "foo";

if (input.equals("bar")) {
   System.out.println(input + " equals bar");
} else {
   System.out.println(input + " does not equal bar");
}


code:
// in C++
std::string input = "foo";

if (input == "bar")
{
   std::cout << input << " equals bar" << std::endl;
}
else
{
   std::cout << input << " does not equal bar" << std::endl;
}


code:
-- in Eiffel

local
   input: STRING is "foo"
do
   if input = "bar" then
      std_output.put_string(input + " equals bar")
   else
      std_output.put_string(input + " does not equal bar")
   end
   std_output.put_new_line
end


code:
(* in O'Caml *)

let input = "foo" in
   if input = "bar" then
      print_endline (input ^ " equals bar")
   else
      print_endline (input ^ " does not equal bar")


Different responses based on number input

code:
// in Java

BufferedReader in = new BufferedReader(
   new InputStreamReader(System.in));

int input;

try {
   input = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
   input = 0;
}

switch (input) {
   case 0:
      System.out.println("What a tiny number!");
      break;
   case 1:
      System.out.println("It's a start.");
      break;
   case 2:
      System.out.println("That's more like it.");
      break;
   default:
      System.out.println("Show-off...");
      break;
}


code:
// in C++

int input = 0;

std::cin >> input;

switch (input)
{
   case 0:
      std::cout << "What a tiny number!" << std::endl;
      break;
   case 1:
      std::cout << "It's a start." << std::endl;
      break;
   case 2:
      std::cout << "That's more like it." << std::endl;
      break;
   default:
      std::cout << "Show-off..." << std::endl;
      break;
}


code:
-- in Eiffel

local
   input: INTEGER
do
   std_input.read_integer
   input := std_input.last_integer

   inspect input
      when 0 then
         std_output.put_string("What a tiny number!")
      when 1 then
         std_output.put_string("It's a start.")
      when 2 then
         std_output.put_string("That's more like it.")
      else
         std_output.put_string("Show-off...")
   end
   std_output.put_new_line
end


code:
(* in O'Caml *)

let input = try read_int () with Failure "into_of_string" -> 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.

code:
// 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);
   }
}


code:
// in C++

#include <iostream>
#include <string>

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 << response << std::endl;
   return 0;
}


code:
-- in Eiffel
class
   TEST
creation { ANY }
   make
feature { NONE }
   get_input: INTEGER is
      do
         std_input.read_integer
         Result := std_input.last_integer
      end

   get_response(input: INTEGER): STRING is
      do
         inspect input
            when 0 then Result := "What a tiny number!"
            when 1 then Result := "It's a start."
            when 2 then Result := "That's more like it."
            else Result := "Show-off..."
         end
      end
feature { ANY }
   make is
      local
         input: INTEGER
         response: STRING
      do
         input := get_input
         response := get_response(input)
         std_output.put_string(response)
         std_output.put_new_line
      end
end


code:
(* in O'Caml *)

let get_input = read_int

let get_response = function
   0 -> "What a tiny number!"
   | 1 -> "It's a start."
   | 2 -> "That's more like it."
   | _ -> "Show-off..."

let _ =
   let input = get_input () in
      let response = get_response input in
         print_endline response
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Sep 01, 2004 9:24 pm   Post subject: (No subject)

Simple Class

code:
// in Java

import java.lang.*;

public class Name {
   private final String first;
   private final String last;

   public Name(String first, String last) {
      this.first = first;
      this.last = last;
   }

   public String toString() {
      return first + " " + last;
   }
}


code:
// in C++

#include <string>

class name
{
   private:
      const std::string first;
      const std::string last;
   public:
      name(std::string init_first, std::string init_last) : first(init_first), last(init_last) { }
      std::string str() const
      {
         return first + " " + last;
      }
};


code:
-- in Eiffel

class
   NAME
creation { ANY }
   make
feature { NONE }
   first, last: STRING
feature { ANY }
   make(init_first, init_last: STRING) is
      do
         first := init_first
         last := init_last
      end
   to_string: STRING is
      do
         Result := first + " " + last
      end
end


code:
(* in O'Caml *)

class name first last =
object(self)
   method to_string = first ^ " " ^ last
end
wtd




PostPosted: Sat Sep 04, 2004 5:06 pm   Post subject: (No subject)

Instantiating those Classes

code:
// in Java

Name bobsName = new Name("Bob", "Smith");


code:
// in C++

name * bobs_name = new name("Bob", "Smith");


code:
-- in Eiffel

local
   bobs_name: NAME
do
   create bobs_name.make("Bob", "Smith")
end


code:
(* in O'Caml *)

let bobs_name = new name "Bob", "Smith"
wtd




PostPosted: Sat Sep 04, 2004 11:23 pm   Post subject: (No subject)

Printing the name to standard output

code:
// in Java

System.out.println(bobsName);


code:
// in C++

// First override the << operator

class name
{
#include <string>
#include <iostream>

class name
{
   private:
      const std::string first;
      const std::string last;
   public:
      name(std::string init_first, std::string init_last) : first(init_first), last(init_last) { }
      std::string str() const
      {
         return first + " " + last;
      }
      friend std::ostream& operator<<(std::ostream& out, const name& n);
};

std::ostream& operator<<(std::ostream& out, const name& n)
{
   return out << n.str();
}

int main()
{
   name * bobs_name = new name("Bob", "Smith");
   std::cout << bobs_name << std::endl;
}


code:
-- in Eiffel

std_output.put_string(bobs_name.to_string)
std_output.put_new_line


code:
(* in O'Caml *)

print_endline bobs_name#to_string
wtd




PostPosted: Wed Sep 08, 2004 3:32 pm   Post subject: Single Inheritance

Single Inheritance

code:
// in Java

public class TitledName extends Name {
   private String title;

   public TitledName(String title, String first, String last) {
      super(first, last);
      this.title = title;
   }

   public String toString() {
      return title + " " + super.toString();
   }
}


code:
// in C++

class titled_name : public name
{
        private:
                std::string title;
        public:
                titled_name(std::string init_title, std::string init_first, std::string init_last) : title(init_title), name(init_first, init_last) { }
                                                                                                   
                std::string str() const
                {
                        return title + " " + name::str();
                }
};


code:
-- in Eiffel

class
   TITLED_NAME
inherit
   NAME
      rename to_string as name_to_string
      rename make as name_make
   end
creation { ANY }
   make
feature { NONE }
   title: STRING
feature { ANY }
   make(i_title, i_first, i_last: STRING) is
      do
         title := i_title
         name_make(first, last)
      end

   to_string: STRING is
      do
         Result := title + " " + name_to_string
      end
end


code:
(* in O'Caml *)

class titled_name init_title first last =
object(self)
   inherit name as super
   val mutable title = init_title
   method to_string = title ^ " " ^ super#to_string
end
wtd




PostPosted: Mon Sep 20, 2004 9:26 pm   Post subject: (No subject)

Interfaces

code:
// in Java

import java.lang.*;
import java.io.*;

interface Barker {
   public String bark();
}

class Dog implements Barker {
   public String bark() {
      return "Ruff!";
   }
}

class Tree implements Barker {
   public String bark() {
      return "Rough";
   }
}

class Seal implements Barker {
   public String bark() {
      return "Arfff!";
   }
}

public class Test {
   public static void main(String[] args) {
      Tree t = new Tree();
      Dog d = new Dog();
      Seal s = new Seal();

      printBark(t);
      printBark(d);
      printBark(s);
   }

   private static void printBark(Barker b) {
      System.out.println(b.bark());
   }
}


code:
// in C++

#include <iostream>
#include <string>
                                                                                           
class Barker
{
   public:
      virtual std::string bark() = 0;
};
                                                                                           
class Dog : public Barker
{
   public:
      std::string bark()
      {
         return "Ruff!";
      }
};

class Tree : public Barker
{
   public:
      std::string bark()
      {
         return "Rough";
      }
};

class Seal : public Barker
{
   public:
      std::string bark()
      {
         return "Arff!";
      }
};
                                                                                           
void print_bark(Barker& b)
{
   std::cout << b.bark() << std::endl;;
}
                                                                                           
int main()
{
   Dog d;
   Tree t;
   Seal s;
                                                                                           
   print_bark(d);
   print_bark(t);
   print_bark(s);
}


code:
-- in Eiffel

deferred class
   BARKER
feature { ANY }
   bark : STRING is
      deferred
      end
end

class
   DOG
inherit
   BARKER
feature { ANY }
   bark : STRING is
      do
         Result := "Ruff!"
      end
end

class
   TREE
inherit
   BARKER
feature { ANY }
   bark : STRING is
      do
         Result := "Rough"
      end
end

class
   SEAL
inherit
   BARKER
feature { ANY }
   bark : STRING is
      do
         Result := "Arff!"
      end
end

class
   MAIN
creation { ANY }
   make
feature { ANY }
   make is
      local
         d : DOG
         t : TREE
         s : SEAL
      do
         create d
         create t
         create s
         
         print_bark(d)
         print_bark(t)
         print_bark(s)
      end
feature { NONE }
   print_bark(b : BARKER) is
      do
         std_output.put_string(b.bark)
         std_output.put_new_line
      end
end


code:
(* in O'Caml *)

class dog =
object
   method bark = "Ruff!"
end

class tree =
object
   method bark = "Rough"
end

class seal =
object
   method bark = "Arff!"
end

let print_bark b = print_endline b#bark

let d = new dog and t = new tree and s = new seal in
   print_bark d;
   print_bark t;
   print_bark s


And just for fun... Smile

code:
// in Objective-C

#import <objc/Object.h>
#include <stdio.h>

@protocol Barker
- (char *) bark;
@end

@interface Dog : Object <Barker>
- (char *) bark;
@end

@interface Tree : Object <Barker>
- (char *) bark;
@end

@interface Seal : Object <Barker>
- (char *) bark;
@end

void printBark(id <Barker> b);

int main()
{
   Dog * d = [Dog new];
   Tree * t = [Tree new];
   Seal * s = [Seal new];

   printBark(d);
   printBark(t);
   printBark(s);
}

@implementation Dog
- (char *) bark
{
   return "Ruff!";
}
@end

@implementation Tree
- (char *) bark
{
   return "Rough";
}
@end

@implementation Seal
- (char *) bark
{
   return "Arff!";
}
@end

void printBark(id <Barker> b)
{
   puts([b bark]);
}
Martin




PostPosted: Thu Sep 23, 2004 11:24 am   Post subject: (No subject)

Awesome! Strangely enough, this helped me with the transition from C++ to Java.

When we reorganize the site, and we get a more organized tutorial section (devoted to moderator's choice tutorials) we'll put this there.

Great work!
wtd




PostPosted: Thu Sep 23, 2004 11:33 am   Post subject: (No subject)

Martin wrote:
Awesome! Strangely enough, this helped me with the transition from C++ to Java.


Not quite what I had in mind, but I'll take it. Smile
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: