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.

The first two examples demonstrate executable Umple programs with embedded Java main programs. Load these examples into UmpleOnline by clicking on the links. The click 'Execute It' to see the output. Or generate Java code by clicking on the 'Generate It' button; then click on the 'download zip file' link and run 'javac' on the result, followed by 'java' on the resulting class file.

About the first example below: This contains just Java code and is the same as a plain Java program; it illustrates 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.

About the 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.  

About the third and fourth examples below: These are the same as the first two examples, except that they contain embedded main programs in Python and C++. After you load these into UmpleOnline, select either Python or C++ from the 'GENERATE' menu, then select 'Generate It'.

 

Example Showing a Simple Class with a Java 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) Java {
    System.out.println("Hello World");
  }

  public static void main(String [ ] args) Python {
    print("Hello World")
  }  
}

      

Load the above code into UmpleOnline

 

Example Showing Three Classes with an Association and Attributes (Diagram is Above) and a Java Main Method

/*
 * 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 () Java {
    return(getName());	
  }
  
  String __str__ () Python {
    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) Java {
    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());
  }

  public static void main(String [ ] args) Python {
    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(m.getStudents()))
  }
  
}
      

Load the above code into UmpleOnline

 

Example Showing a Simple Class with a Main Method for Python Generation or C++

/*
 * Simple Hello World example for Umple.
 * Compile this with Umple specifying either
 * -g Python or -g Cpp
 *
 * Or specify in UmpleOnline either Python or C++
 * in the generate menu
 */
class HelloWorld {
  public static void main(String [ ] args) Python{
    print("Hello World")
  }
  
  public static void main(String [ ] args) Cpp{
    cout<<"Hello World!";
  }
}


      

Load the above code into UmpleOnline

 

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

  /*
  * Introductory example of Umple showing classes,
  * attribute, association, generalization, methods
  * and the mixin capability.
  * Generate either Python or Cpp 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]
  *
  * If you try to execute this with Java, it will
  * indicate there is no Java main program. See
  * the separate Java example in the user manual.
  */
  class Person {
    name; // Attribute, string by default
    String __str__() Python {
      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) Python{
      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()))))
      
  }
    public static void main(String [ ] args)  Cpp {  
      // Creating a mentor and a student.   
      Mentor m("Nick The Mentor");
      Student s("Tom The Student");
      // Associating the student with the mentor
      s.setMentor(&m);
      m.addStudent(&s);
      // Retrieving and printing the mentor of the student.
      cout << "The mentor of " << s.getName() << " is " << s.getMentor()->getName() << endl; 
      // Retrieving the list of students of the mentor.
      vector<Student*>* students = m.getStudents();
      cout << "The students of " << m.getName() << " are: " << endl;
      // Iterating over the students and printing their names.
      for (Student* student : *students) {
        cout << student->getName() << endl;
      }
      return 0;
    }
  }

  
      

Load the above code into UmpleOnline