Created
September 19, 2011 20:48
-
-
Save wilkie/1227565 to your computer and use it in GitHub Desktop.
This is how pirates use Strings and void methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.Random; | |
class Piratey { | |
public static void main(String args[]) { | |
String name; | |
Scanner input = new Scanner(System.in); | |
System.out.print("Enter in your first name: "); | |
name = input.next(); | |
// Greet the pirate! | |
// | |
// The following is the same as: | |
// String pirateName = piratize(name); | |
// greet(pirateName); | |
// | |
// Notice how we can use the result of a function as | |
// input to another! | |
greet(piratize(name)); | |
// We can call functions with constants. | |
say("Me be a pirate. Ye be a landlubber."); | |
// Call upon another function (with no arguments) to | |
// use the shout function to exclaim a random pirate phrase | |
exclamation(); | |
} | |
// This is a function that takes a String as input | |
// It has no output, but it prints to the screen. | |
static void greet(String name) { | |
System.out.println("Ahoy, " + name + "!"); | |
} | |
// This is a function that also takes a String as input. | |
// It, however, returns a new String. | |
// | |
// Notice how it uses the parameter (name) and changes it | |
// This will _not_ change the value of the variable that is | |
// passed to it, because the value being assigned is a | |
// brand new value. | |
// | |
// It uses the return statement to end the function and | |
// present the output to the place that called the function | |
// originally. | |
static String piratize(String name) { | |
name = "Dread Pirate " + name; | |
// End the function and use the new value of name | |
// as the output. | |
return name; | |
} | |
// This function has a String as input, but no output. | |
// It simply prints to the screen. | |
static void say(String message) { | |
System.out.println(message + " Yarr!"); | |
} | |
// Likewise for this function, but it prints something | |
// different. | |
static void shout(String message) { | |
System.out.println("Arrr! " + message); | |
} | |
// This function takes no input nor any output. | |
// It relies on other functions to do real work. | |
static void exclamation() { | |
// Randomly pick a phrase to shout | |
Random rnd = new Random(); | |
int num = rnd.nextInt(3); | |
// Based upon the random number, we will determine what | |
// our pirate program will say. | |
String msg = ""; | |
if (num == 0) { | |
msg = "Shiver me timbers!"; | |
} | |
else if (num == 1) { | |
msg = "Dead men tell no tales."; | |
} | |
else if (num == 2) { | |
msg = "Weigh anchor and hoist the mizzen!"; | |
} | |
// Call the other function to shout this statement | |
// | |
// Notice how we can call a function using a variable | |
// as input. | |
shout(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment