# 12.11: Exercises

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

The code for this chapter is in the ch04 directory of ThinkJavaCode. See [Section 0.4] for instructions on how to download the repository. Before you start the exercises, we recommend that you compile and run the examples.

If you have not already read Appendix A.4, now might be a good time. It describes an efficient way to test programs that take input from the user and display specific output.

Exercise $$\PageIndex{1}$$

The point of this exercise is to practice reading code and to make sure that you understand the flow of execution through a program with multiple methods.

1. Hint: Start by describing in words what ping and baffle do when they are invoked.
2. Draw a stack diagram that shows the state of the program the first time ping is invoked.
3. What happens if you invoke baffle(); at the end of the ping method? (We will see why in the next chapter.)
public static void zoop() {
baffle();
System.out.print("You wugga ");
baffle();
}

public static void main(String[] args) {
System.out.print("No, I ");
zoop();
System.out.print("I ");
baffle();
}

public static void baffle() {
System.out.print("wug");
ping();
}

public static void ping() {
System.out.println(".");
}


Exercise $$\PageIndex{2}$$

The point of this exercise is to make sure you understand how to write and invoke methods that take parameters.

1. Write the first line of a method named zool that takes three parameters: an int and two Strings.
2. Write a line of code that calls zool, passing as arguments the value 11, the name of your first pet, and the name of the street you grew up on.

Exercise $$\PageIndex{3}$$

The purpose of this exercise is to take code from a previous exercise and encapsulate it in a method that takes parameters. You should start with a working solution to Exercise 2.12.2.

1. Write a method called printAmerican that takes the day, date, month and year as parameters and that displays them in American format.
2. Test your method by invoking it from main and passing appropriate arguments. The output should look something like this (except that the date might be different):
1. Saturday, July 22, 2015

3. Once you have debugged printAmerican, write another method called printEuropean that displays the date in European format.

This page titled 12.11: Exercises is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .