Skip to content

Instantly share code, notes, and snippets.

@gisikw
Created April 18, 2012 00:59
Show Gist options
  • Save gisikw/2410232 to your computer and use it in GitHub Desktop.
Save gisikw/2410232 to your computer and use it in GitHub Desktop.
Dart: Java Example
/*
* Code Snippet for
* Dart: A Replacement for JavaScript
* A talk given at Twin Cities Code Camp 12
* http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
*/
/*
* The following is a very basic Java program, which
* represents a Person, and outputs the String representation
* of an instance.
*/
class Person {
String firstName;
String lastName;
String occupation;
public Person(String firstNameArg, String lastNameArg, String occupationArg) {
occupation = occupationArg;
firstName = firstNameArg;
lastName = lastNameArg;
}
public String toString(){
return firstName + " " + lastName + ", " + occupation;
}
}
class Application1 {
public static void main(String[] args) {
Person me = new Person("Kevin","Gisi","Senior Interactive Developer");
System.out.println(me);
}
}
// Output: "Kevin Gisi, Senior Interactive Developer"
# Code Snippet for
# Dart: A Replacement for JavaScript
# A talk given at Twin Cities Code Camp 12
# http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
# This is a Ruby implementation of Application1.java
# The program itself is shorter, because of some
# syntactic suger, but the functionality remains
# the same.
class Person
def initialize(first_name,last_name,occupation)
@first_name, @last_name, @occupation = first_name, last_name, occupation
end
def to_s
"#{@first_name} #{@last_name}, #{@occupation}"
end
end
# Application code
me = Person.new("Kevin","Gisi","Senior Interactive Developer")
puts me
# Output: "Kevin Gisi, Senior Interactive Developer"
/*
* Code Snippet for
* Dart: A Replacement for JavaScript
* A talk given at Twin Cities Code Camp 12
* http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
*/
/*
* This is an extension of Application1.java
* While the program has been updated to encapsulate
* an occupation in an object, the functionality remains
* the same.
*/
// Occupation class has been implemented with a very simple definition.
class Occupation {
String title;
public Occupation(String titleArg) {
title = titleArg;
}
public String toString() {
return title;
}
}
// The Person class has to be modified to accept the Occupation
class Person {
String firstName;
String lastName;
Occupation occupation; // This variable had to be changed
// The constructor needed to be modified
public Person(String firstNameArg, String lastNameArg, Occupation occupationArg) {
occupation = occupationArg;
firstName = firstNameArg;
lastName = lastNameArg;
}
public String toString(){
return firstName + " " + lastName + ", " + occupation;
}
}
class Application2 {
public static void main(String[] args) {
Occupation job = new Occupation("Senior Interactive Developer");
Person me = new Person("Kevin","Gisi",job);
System.out.println(me);
}
}
// Output: "Kevin Gisi, Senior Interactive Developer"
# Code Snippet for
# Dart: A Replacement for JavaScript
# A talk given at Twin Cities Code Camp 12
# http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
# This is a Ruby implementation of Application2.java
# While the program has been updated to encapsulate
# an occupation an object, the functionality remains
# the same.
# Occupation class has been implemented with a very simple definition
class Occupation
def initialize(title)
@title = title
end
def to_s
@title
end
end
# The Person class requires no modification, for two reasons
# 1. The instance variables do not have a defined type
# 2. The String interpretation of @occupation has not changed
class Person
def initialize(first_name,last_name,occupation)
@first_name, @last_name, @occupation = first_name, last_name, occupation
end
def to_s
"#{@first_name} #{@last_name}, #{@occupation}"
end
end
# Application code
job = Occupation.new("Senior Interactive Developer")
me = Person.new("Kevin","Gisi",job)
puts me
# Output: "Kevin Gisi, Senior Interactive Developer"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment