Last modified 3 years ago Last modified on 2015-02-13 11:09:52

Demot » Demo 12, 13.4.2015

Task 4-6. Command prompt

Write a program that would work in the following way (see to it that more commands can be easily added to the program).

Give command>Add 3 4 5 [RET]
Result is 12.
Give command>Capitals chicken [RET]
chicken in capitals is CHICKEN
Give command>IsPalindrome evitative [RET}
Word "evitative" is a palindrome!
Give command>Add 2 1[RET]
Result is 3.
Give command>Ad 2 1[RET]
Result is 3.
Give command>And 2 1[RET]
Command "AND" is unknown!
Give command>[RET]

The completed example java file looks a bit like:

  public interface CommandInterface{
    String execute(String parameters);

  public static class Add implements CommandInterface { ...
  public static class Help implements CommandInterface { ...
  public static class IsPalindrome implements CommandInterface { ...
  public static class Capitals implements CommandInterface { ...

... classes Command and Commands here(includes eg. method "find") ...

  public static void main(String[] args)  {
    Commands commands = new Commands();
    Help help = new Help("command.txt");

    commands.add(new Command("?",help));
    commands.add(new Command("apua",help));
    commands.add(new Command("+",new Add()));
    commands.add(new Command("add",new Add()));
    commands.add(new Command("capitals",new Capitals()));
    commands.add(new Command("ispalindrome",new IsPalindrome()));

    String s;

    while ( true ) {
      s = Syotto.kysy("Give command"); //Syotto.kysy is one of Vesas own algorithms that will ask for input and return it to the user
      if ( s.equals("") ) break;
      String result = commands.interpret(s);


Hint: take the newest Ali.jar. Then Help-class can print out a string by doing:

        private Help help = null;
        private ByteArrayOutputStream bos = new ByteArrayOutputStream();
                help = new Help(file);
                help.setOut(new PrintStream(bos));
       return bos.toString();

Task 7*. Help

Even if you wouldn't do task 4-6, add to it help using If you haven't done 6, write a main that prints help by pressing ? using methods from Also do handwritten basic help file.

Task 8. Validity check for Clock-class

Write class Clock that has the method check(String line) that will do a validity check on the parameter and see if it is in valid form for a clock. If there are some problems, the problem is returned as a String, otherwise null-reference is returned. Start by writing tests.

Task B1-2 Names in order

Write a program, that asks names until an empty line is inputted. After this the program prints out all the names in alphabetical order. The names are inputted in the form "Donald Duck", but they are sorted out according to surname so for example "Donald Duck" is before "Anonymous Mouse".

Task B3. Graphical command prompt

Make a graphical user interface for the tasks 4-7 command prompt.