list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

Hello World Examples

As is customary when introducing a new language, here are some 'hello world' examples for Umple. Load these examples into UmpleOnline by clicking on the links. Then generate Java code by clicking on the 'Generate' button. Next click on the 'download zip file' link and run 'javac' on the result or else compile it in Eclipse.

First example below: This looks identical to how a Java 'hello world' example would look, illustrating a key feature of Umple: Umple adds features to existing languages: Code in the original language can and does remain the same. Umple just replaces and simplifies some (or a lot) of it.

Second example below: This shows some very simple features of Umple: An attribute, an association, a generalization, some Java methods and the mixin capability:

  • Attributes look very much like variables in Java or other similar programming languages; however they are more than that: Umple generates code for setting and getting each attribute. You can also add lots of other features to attributes, such as making them immutable (so they cannot change), adding constraints, or making them part of a 'key'.

  • An association such as the one shown here maintains links between objects at run-time. Here the student can get its mentors, and the mentor can find his or her optional student.
  • A generalization, as in other object-oriented language, sets up inheritance relationships. Umple uses the 'isA' keyword. Here both Student and Mentor inherit the name attribute.
  • The methods in the example are just normal Java methods. If you generate java from this, compile using javac, and then run the Person class using java, its main program will run.
  • You will notice that Person is apparently defined twice. This is a feature of Umple called using 'mixin' technology. All the elements are combined to create the resulting class.

Umple and UML: Here is the class diagram of the second example in UML. If you click on the 'open in UmpleOnline' link, you will see the UML diagram generated. You can then edit the UML diagram to change the code, or change the code to edit the UML diagram.

UML class diagram showing superclass Person (with attribute name), and with subclasses Student and Mentor. There is a 0..1 -- * association between the subclasses.

Example Showing a Simple Class with a Main Method

/*
 * Simple Hello World example for Umple.
 * Compile this with Umple and it will generate Java
 * that is essentially the same.
 * 
 * You could just as readily compile this code directly
 * with javac. However, this serves as the starting point:
 * Other examples in this manual show other things you
 * can do with Umple
 */
class HelloWorld {
  public static void main(String [ ] args) {
    System.out.println("Hello World");
  }
}

      

Load the above code into UmpleOnline

 

Example Showing Three Classes with an Association and Attributes (Diagram is Above)

/*
 * Introductory example of Umple showing classes,
 * attribute, association, generalization, methods
 * and the mixin capability. Generate java and run this.
 * 
 * The output will be:
 * The mentor of Tom The Student is Nick The Mentor
 * The students of Nick The Mentor are [Tom The Student]
 */
class Person {
  name; // Attribute, string by default
  String toString () {
    return(getName());
  }
}

class Student {
  isA Person;
}

class Mentor {
  isA Person;
}

association {
  0..1 Mentor -- * Student;
}

class Person {
  // Notice that we are defining more contents for Person
  // This uses Umple's mixin capability
  
  public static void main(String [ ] args) {
    Mentor m = new Mentor("Nick The Mentor");
    Student s = new Student("Tom The Student");
    s.setMentor(m);
    System.out.println("The mentor of "
      + s  + " is " +  s.getMentor());
    System.out.println("The students of "
      +  m  + " are " +  m.getStudents());
  }
}
      

Load the above code into UmpleOnline

 

Example Showing a Simple Class with a Main Method for Python Generation

/*
 * Simple Hello World example for Umple.
 * Compile this with Umple and it will generate Java
 * that is essentially the same.
 *
 * You could just as readily compile this code directly
 * with javac. However, this serves as the starting point:
 * Other examples in this manual show other things you
 * can do with Umple
 */
class HelloWorld {
  public static void main(String [ ] args) {
    print("Hello World")
  }
}

      

Load the above code into UmpleOnline

 

Example Showing Three Classes with an Association and Attributes (Diagram is Above) for Python Generation

/*
 * Introductory example of Umple showing classes,
 * attribute, association, generalization, methods
 * and the mixin capability. Generate java and run this.
 * 
 * The output will be:
 * The mentor of Tom The Student is Nick The Mentor
 * The students of Nick The Mentor are [Tom The Student]
 */
class Person {
  name; // Attribute, string by default
  String __str__() {
    return self.getName()
  }
}

class Student {
  isA Person;
}

class Mentor {
  isA Person;
}

association {
  0..1 Mentor -- * Student;
}

class Person {
  // Notice that we are defining more contents for Person
  // This uses Umple's mixin capability
  
  public static void main(String [ ] args) {
    import Mentor
    import Student
    m = Mentor.Mentor("Nick The Mentor")
    s = Student.Student("Tom The Student")
    s.setMentor(m)
    print("The mentor of " + str(s)  + " is " +  str(s.getMentor()))
    print("The students of " +  str(m)  + " are " + str(list(map(str, m.getStudents()))))
  }
}
      

Load the above code into UmpleOnline