The rest of the train ride passed by uneventfully, although Sunny could have sworn that she saw an error flying by once. Or at least, something with red scribbles. But soon enough, the skyscrapers of Method City came into view, and the train slowed to a stop.
"We're here!" a young Javalandian cheered. Sunny couldn't help but crack a smile at that.
"Let's go, Robert! We're almost to the climax of our quest!" Sunny grinned at the robot, who reluctantly floated up out of his seat.
"Oh, joy. Cities are too loud to get any decent rest," he grumbled.
Despite his complaints, however, Sunny and Robert were soon standing on the train platform, looking at a map of Method City. Sunny pointed to a cartoon depiction of a mountain range near the top of the map. "I'm assuming Spaghetti Code Mountain is there?"
Robert snorted. "No, Sunny, it's actually down there, in the middle of the ocean."
"Wow, thanks." Sunny rolled her eyes. "Well, it looks like we're not too far… My stomach is telling me that it's around lunchtime, though, so are there any good places to eat around here?"
"I don't eat."
"Noted. But in your travels as an ambassador, have you heard people talking about nice cafés or restaurants around here?" Sunny persisted.
Robert hummed. "Hmm… Perhaps the Cinnamon Synonym Café? It's on the way to Spaghetti Code mountain."
"The Cinna- Synon–" Sunny gave up. "Alright, that sounds good."
The pair left the train station and, around a twenty-minute walk later, they arrived at the café. Sunny was still bubbling with the new sights and sounds of the city.
"Did you see that person juggling numbers? And the variapet show! Plus the street performers… I had no idea so many words rhymed with array."
Robert nodded along as he entered the café. "Shall we sit there?" The place was already starting to fill up with patrons for the lunch rush, and the table Robert indicated was squeezed into the back, next to a fake plant.
"Sure. And then there were a couple of ambassadors too– Robert, how come you don't have any fancy colors on you?"
"Customization is optional." Robert took a seat at the small table.
"Okay. But– ooh, such a cute menu!" Sunny got distracted by the glossy images of food. "The ampersand sandwich looks good. But so do the carat soup, and the hashtag hashbrowns…"
Robert closed his eyes. "Let me know when you're done. I need to conserve battery if we're not going to stop for a charge before heading to Spaghetti Code Mountain."
"Oh." Sunny had kind of wanted to ask him more about the quirks of Method City, but as Robert went to sleep, she shrugged and dedicated her full attention to the menu.
Before Sunny could decide on an item, a waitress arrived at their table. "Welcome to the Cinnamon Synonym Café! What can I get for you today?" She adjusted her apron and took out a piece of paper.
"What's popular here?" Sunny asked.
"Popular… popular…" the waitress whispered, scanning the piece of paper in her hand. "Aha! listPopularItems()." She cleared her throat, then pointed to the top of Sunny's menu. "The bracket burgers paired with curly braces fries are always a good choice. And we have the soup of the day, which is carat soup today. But if you're a little more adventurous, you might like the pear and parentheses pancakes special." She winked.
"Hmm. That sounds good! I'll have the pancakes, then." Sunny beamed.
The waitress glanced at her paper again, whispered, "noteDownOrder()," then took out a notepad and jotted something down on it. "Perfect. It'll be ready in around fifteen minutes." As the waitress turned to leave, the backwash from a customer walking by whisked the paper out of her hand, and it fluttered to Sunny's side of the table.
Curious, she picked it up. The top of the paper had a main method and a while loop written on it:
public static void main(String[] args) {
while (true) {
if (customer hasn't decided yet) {
System.out.println("Do you need more time?);
} else if (customer asks for more information) {
listPopularItems();
} else if (customer knows that they want) {
noteDownOrder();
break;
} else {
System.out.println("Do you need help figuring out what to order?);
}
}
bringOrderToKitchen();
}
Below that, there was more text about listPopularItems() and noteDownOrder() and bringOrderToKitchen():
static String[] popularItems = {"brackets burgers with curly braces fries", "soup of the day", "pear and parentheses pancakes special"};
public static void listPopularItems() {
for (int i = 0; i < popularItems.length; i++) {
System.out.println(popularItems[i]);
}
}
public static void noteDownOrder() {
listenToCustomer();
writeOrderInNotebook();
}
public static void bringOrderToKitchen() {
walkToKitchen();
giveOrderToChefs();
}
Sunny stared at the paper for a moment, trying to make sense of the code. "Robert? Are these methods?"
The robot cracked open an eye, then glanced at the paper that Sunny was holding out to him. "Indeed. It looks like listPopularItems(), noteDownOrder(), and bringOrderToKitchen() are all methods. Methods are kind of like variables, but they store actions instead of data. So once you write the method listPopularItems(), you can call it as many times as you want to repeat the actions inside the method. In this case, those actions would be printing the contents of the array popularItems."
The waitress shifted nervously. "Would you mind giving me my instructions back? I'm new here, and I need them to–"
"Oh! Sorry." Sunny quickly handed the paper back. "Thank you so much."
The waitress nodded and hurried away, and Sunny turned back to Robert. "So. Methods?"
Robert let out a long-suffering sigh. "What do you want to know about them?"
"Well, the methods on the paper had all those words in front of them, like public and static and void?"
"You don't need to worry about public and static for now; those are more important when you get into objects and classes. But void means that the method doesn't return anything."
"Return? Like, return clothing that doesn't fit?"
"I'm afraid not. A return value is something that the method sends back to wherever it was called. Here's an example." Robert flipped open the computer screen in his head.
public static void addAndPrint(int a, int b) {
System.out.println(a + b);
}
public static int addAndReturn(int a, int b) {
return a + b;
}
"So, here we have two different methods for adding. One prints the result, while the other returns it. Now, let's try calling both of these in the main method– remember, that's the place you put code that you want to execute."
public static void main(String[] args) {
addAndPrint(1, 2);
}
Robert ran the program, and the number 3 printed onto the console. "So, addAndPrint directly prints the result. Let's see what addAndReturn does."
public static void main(String[] args) {
addAndReturn(1, 2);
}
This time, nothing printed. "See? addAndReturn returns the value to the main method, but if you want to see it, you have to print it in the main method."
Sunny shook her head in confusion. "Okay, but then why would you ever return instead of printing? Don't you want to know the result of the method?"
"Not always," Robert explained. "Sometimes you want to do something else with the result of the method."
public static void main(String[] args) {
System.out.println(addAndReturn(addAndReturn(1, 2), 5));
}
The program printed 8. "You can call methods multiple times. Here, if we had just printed 1+2, we wouldn't have been able to do anything with the sum afterwards. But by returning the value, we could call the addAndReturn method on it again, giving us the final answer of 8."
Sunny rubbed her head. "Oookay."
Robert summarized, "You should return values when you're going to do more with them later. Print them when all you want to do is know what those values are. But don't worry. Knowing which one to do will become natural as you gain more experience."
"I sure hope so." For the first time, Sunny felt a twinge of doubt in her abilities. Variables and loops and all had been easy enough to understand the first time they were explained to her, but methods? Even besides the print versus return thing, she wasn't really sure when or how to use them.
Hopefully Robert was right. She just needed more practice. Sunny comforted herself with rule #14, which was, "The bad guy strikes when you least expect it. Meaning, you'll never be a hundred percent ready." All she could do was learn as much as possible about the code this world ran on, and hope that was enough to be able to know how to defeat the enemy in the end...
Points: 4550
Reviews: 3800
Donate