Java Lesson 11: Classes as parameters

From Erlands Wiki

Jump to: navigation, search

The main class looks as follows:

public class MyClass {
    public static void main(String[] args) {
        IntArgument argument1 = ArgumentHelper.getArgumentAsInt(0,args);
        StringArgument argument2 = ArgumentHelper.getArgumentAsString(1,args);
        printStuff(argument1);
        printStuff(argument2);
    }
}

The helper class still looks as follows:

public class ArgumentHelper {
    public static IntArgument getArgumentAsInt(int argumentNumber, String[] args) {
        int argument = Integer.valueOf(args[argumentNumber]);
        return new IntArgument(argument);
    }
 
    public static StringArgument getArgumentAsString(int argumentNumber, String[] args) {
        String argument = args[argumentNumber];
        StringArgument strArgument = new StringArgument();
        strArgument.setString(argument);
        return strArgument;
    }
}

And the StringArgument class looks as follows:

public class StringArgument {
    private String strArgument;
    public void setString(String arg) {
        this.strArgument = arg;
    }
    public String getString() {
        return strArgument;
    }
}

And the IntArgument class

public class IntArgument {
    private int intArgument;
    public IntArgument(int intArgument) {
        this.intArgument = intArgument;
    }
    public int getInt() {
        return intArgument;
    }
}

The program is executed with:

java MyClass 5 Hello

Your work is to implement two printStuff methods in MyClass so it gives an output like this, you are now allowed to change anything in the other classes or method just add two printStuff methods:

You specified int: 5
You specified String: Hello
Personal tools