Methods 3 - creating your own methods

Level 1 - Basics

Copy this fragment to an exercise in IntelliJ. Remove the existing run() method first.
Try to predict what the program will do. Start to program to test it.

public void run() {

    showMessageOnScreen("Hello!");

}

public void showMessageOnScreen(String msg) {
    SaxionApp.print("The message is: ");
    SaxionApp.printLine(msg);
}

Change the program, so there will be two messages shown, for example:

The message is: Hello!
The message is: This is amazing!!!!
public void run() { showMessageOnScreen("Hello!"); showMessageOnScreen("This is amazing!!!!"); } public void showMessageOnScreen(String msg) { SaxionApp.print("The message is: "); SaxionApp.printLine(msg); }

Change the program, so it does not only show the message, but also shows how many characters the message has.

The message 'Hello!' has 6 characters
The message 'This is amazing!!!!' has 19 characters 
public void run() { showMessageOnScreen("Hello!"); showMessageOnScreen("This is amazing!!!!"); } public void showMessageOnScreen(String msg) { SaxionApp.print("The message '"); SaxionApp.print(msg); SaxionApp.print("' has "); SaxionApp.print(msg.length()); SaxionApp.printLine(" characters"); }

Now the name of the method doesn't fit the contents any longer. Change the name of the method to showMessageLength. Does your program still work? Note that you have to change the name on multiple locations.

public void run() { showMessageLength("Hello!"); // where you call the method showMessageLength("This is amazing!!!!"); // the name should also be changed } public void showMessageLength(String msg) { // this is where you change the name SaxionApp.print("The message "); SaxionApp.print(msg); SaxionApp.print(" has "); SaxionApp.print(msg.length()); SaxionApp.printLine(" characters"); }

Now add a new method to the program:

public void waitOneMoment() {
    SaxionApp.sleep(0.2);
}

Call this method after each call to showMessageLength.

public void run() { showMessageLength("Hello!"); waitOneMoment(); // the new call goes here showMessageLength("This is amazing!!!!"); waitOneMoment(); // and here } public void showMessageLength(String msg) { SaxionApp.print("The message "); SaxionApp.print(msg); SaxionApp.print(" has "); SaxionApp.print(msg.length()); SaxionApp.printLine(" characters"); // alternative solution would be to call the method here: // waitOneMoment(); } public void waitOneMoment() { SaxionApp.sleep(0.2); }

Level 2 - do it yourself

Say we want to limit the amount of characters that will be shown on screen. For this we need a method that will print only the first 15 characters of the message if the message is too long.

Copy this example into your project.

public void run() {
    truncateMessage("Hello!");
    truncateMessage("This is a line that is way too long.");
}

public void your_method_name_here(param_type parameter_name) {
    
    // your code here
    
}

Change your_method_name_here to the correct method name. Change param_type and parameter to the correct type and name.

After that make sure that a only the first 15 characters of a message are shown on screen. Use the String method substring(0, 15) to get the first 15 characters, but make sure to check the length of the string first.

public void run() { truncateMessage("Hello!"); truncateMessage("This is a line that is way too long."); } public void truncateMessage(String msg) { if (msg.length() > 15) { SaxionApp.printLine(msg.substring(0, 15)); } else { SaxionApp.printLine(msg); } }

We don't always want 15 characters. It would be better if that could be configured instead of that it is a fixed amount. Extend the code so that the maximum can be set by the user first.

Use the following snippet as the contents of the run() method and change the code of your truncateMessage method to use the length as parameter instead of the fixed 15.

SaxionApp.print("What is the max amount of characters? ");
int maxChars = SaxionApp.readInt();
truncateMessage("Hello!", maxChars);
truncateMessage("This is a line that is way too long.", maxChars);
public void run() { SaxionApp.print("What is the max amount of characters? "); int maxChars = SaxionApp.readInt(); truncateMessage("Hello!", maxChars); truncateMessage("This is a line that is way too long.", maxChars); } // added a second argument with name "max" and type int public void truncateMessage(String msg, int max) { if (msg.length() > max) { // use it here SaxionApp.printLine(msg.substring(0, max)); // and here } else { SaxionApp.printLine(msg); } }

Now add a new method with name showMessage. It should take a message as the first argument and a second with name scream that can be either true or false. If the scream argument is true, print the text as uppercase characters only. Use the string method .toUpperCase() to change the text.

Try out the method in your run method.

public void run() { showMessage("Now say this in a calm and peaceful manner", true); showMessage("what did I tell you!?!", true); showMessage("okay, sorry", false); } public void showMessage(String msg, boolean scream) { if (scream) { SaxionApp.printLine(msg.toUpperCase()); } else { SaxionApp.printLine(msg); } }

Level 3 - arguments

Copy the following code into your own project. Can you predict what will happen? Run it to see if your expectations will be met.

public void run() {
    int number = 10;
    SaxionApp.printLine("The original number is: " + number);
    
    enlarge(number, 5);
    SaxionApp.printLine("The number is now: " + number);
}

public void enlarge(int number, int factor) {
    number = number * factor;
    
    SaxionApp.printLine("The enlarged number is: " + number);
}

In the method enlarge, change the name of the argument number to value, without changing the variable number in the run method. Try to run the program. Does it still work?

public void run() { int number = 10; // this variablename stays the same SaxionApp.printLine("The original number is: " + number); enlarge(number, 5); // this only sends the *contents* of the // variable to the method. The name doesn't matter. SaxionApp.printLine("The number is now: " + number); } // "number" has been changed to "value" below on 3 lines: public void enlarge(int value, int factor) { value = value * factor; SaxionApp.printLine("The enlarged number is: " + value); }

Create a new method reduce that, reduces the value of a number by a given factor. For example reducing 10 by 5 should give 2 as outcome.

public void run() { int number = 10; SaxionApp.printLine("The original number is: " + number); enlarge(number, 5); reduce(number, 5); SaxionApp.printLine("The number is now: " + number); } public void reduce(int value, int factor) { value = value / factor; SaxionApp.printLine("The reduced number is: " + value); } // (enlarge method left out here)

Level 4 - return values

A method can also return something. You have already seen this quite a few times:

// the SaxionApp method readInt() returns an int, 
// and we can store it into a variable.
int number = SaxionApp.readInt();

Now you will create your own method that returns an int, and we will extend it to be of more use later. Copy the following snippet into an exercise file:

public void run() {
    // call our own method and store the result in a new var named someNumber.
    int someNumber = getNumberFromUser();
    SaxionApp.printLine("The number is: " + someNumber);
}

public int getNumberFromUser() {
    SaxionApp.print("Please enter a number: ");
    int number = SaxionApp.readInt();

    // this will send the contents of "number" back to the caller of the method.
    // Again, the name doesn't matter. 
    return number; 
}

Try to predict what the program does, and run it to check if you were correct.

Now add a new method getTextFromUser that asks "Please enter a text:" and returns a String. Call the method and print the result on screen in the run method.

public void run() { // call our own method and store the result in a new var named someNumber. int someNumber = getNumberFromUser(); SaxionApp.printLine("The number is: " + someNumber); String someText = getTextFromUser(); SaxionApp.printLine("The text is: " + someText); } // the method should return a String public String getTextFromUser() { SaxionApp.print("Please enter a text: "); String text = SaxionApp.readString(); return text; // return the text to the caller } // (left out method getNumberFromUser here)

If we really want a text from the user, we are not satisfied if the user just presses Enter without actually entering a text.

Extend the method getTextFromUser, so it asks for a text in an endless loop. It should only return the text if the length of the text is greater than 0.

public String getTextFromUser() { while (true) { // endless loop SaxionApp.print("Please enter a text: "); String text = SaxionApp.readString(); if (text.length() > 0) { return text; // this immediately returns the value // (and breaks the loop) } } }

And we also want a maximum amount of characters. Extend the method with a parameter for the maximum amount of characters, and truncate the text if the text is too long.

public String getTextFromUser(int max) { while (true) { SaxionApp.print("Please enter a text: "); String text = SaxionApp.readString(); if (text.length() > 0) { // same algorithm as used in the truncateMessage method earlier if (text.length() > max) { return text.substring(0, max); } else { return text; } } } }

Now extend the program so the getNumberFromUser method has two parameters: the min and max value. Show a message to the user that they need to enter a number between those values. Only return from the method if the user enters a correct value.

public int getNumberFromUser(int min, int max) { while (true) { SaxionApp.print("Please enter a number between " + min + " and " + max + ": "); int number = SaxionApp.readInt(); if (number >= min && number <= max) { return number; } } }