package bla; import java.util.*; class Person { String name; String surname; Person(String name, String surname) { this.name = name; this.surname = surname; } } role Student playedby Person{ int getNumber(); void receiveVote(int vote); double getAvg(); } role Professor playedby Person{ int assignVote(Student student); String getCourse(); } /* We declare a class Person that will be our main role. Methods inside role will be necessarly defined inside defineroles */ class School { String name; definerole Student { private double avg; private int numberExams; private int number; Student(int number) { this.number = number; avg = 0; numberExams = 0; } public double getAvg() { return avg; } public void receiveVote(int vote) { avg = (avg * numberExams + vote) / ((double) (numberExams + 1)); numberExams++; } public int getNumber() { return number; } } definerole Professor{ String course; Professor(String course){ this.course=course; } public String getCourse(){ return course; } public int assignVote(Student s){ int vote = (new Random()).nextInt(12)+19; s.receiveVote(vote); return vote; } } } /* We now declare a completly different role but still played by Person*/ role Emperor playedby Person{ void conquer(Empire impero); } role Tutor playedby Person{ void advice(Emperor emperor); } class Empire{ String name; Empire(String name){ this.name=name; } definerole Emperor{ public void conquer(){ ;//! } } definerole Tutor{ public void advice(Emperor emperor){ emperor.conquer();//!! } } /*Let's see PowerJava in action!*/ public class Main{ public static void main(String[] args){ School athene = new School(); Empire macedonia = new Empire("Macedonia"); Person plato = new Person("Plato", "Aristocle"); athene.new Professor(plato,"Phylosofy"); Person aristotele = new Person("Aristotele", "Nicomaco"); athene.new Student(aristotele, 328902); ((athene.Professor)plato).assignVote((athene.Student)aristotele); ((athene.Professor)plato).destroy(); /*Plato doesn't teach anymore*/ athene.new Professore(aristotele, "Phylosofy"); Person alexander = new Person("Alexander", "the Great"); athene.new Student(alexander, 440334);/*Alexander is a student*/ macedonia.new Emperor(alessandro);/*But also a worker!*/ macedonia.new Tutor(aristotele); ((macedonia.Tutor)aristotele).consiglia(alexander); } }