Formatting Output Practice
System.out vs. IO
There are two different ways to display output. You can use the object System.out or you can use the class IO, which stands for input and output. Both of these classes have print and println methods that display content to the console. The AP CSA exam tests output to the console using System.out. In the exercises below, we show both the use of System.out and IO to demonstrate they produce the same results. Feel free to use either, but be prepared that System.out will be on the AP CSA exam.
Output in Java Playground
The Java Playground is great for trying out segments of code, but if we write print statements to show that the output doesn't move to the next line unless you wrap the statements into a block of code using { at the start and } at the end. Without the statements being included in a block of code, it will treat each print statement distinctly and not behave as expected. Try the following in the Java Playground.
Prepare Your IDE
Another option is to move over to an IDE. If you need help, please reference the Getting Set-up page.
In this section, we will be using preview features in JDK 24. To use features that are still in preview, you will need to enable preview features in your IDE.
- Create a
.javafile. The file name should start with an uppercase letter. - Use the following
mainmethod template. Add your statements to the body of themainmethod. A comment is in the body of themain. A comment is ignored by the compiler. Comments are added to the program code to help programmers understand what the program code does. There are three types of comments:- single-line comments start with
// - multi-line comments start with
/*and end with*/ - document comments, which generate documentation, start with
/**and end with*/
- single-line comments start with
Practice Set 1 - Predict the Output of Code
For each of the following code segments:
- predict what will be generated as output
- test the program code in an IDE. (If you choose to use the Java Playground, remove
void main()as the Java Playground does not require you to use a main method. You can not use read or readln in the Java Playground.)
Using Escape Sequences
A. Given the following code, what is the output?
B. Given the following code, what is the output?
C. Given the following code, what is the output?
D. Given the following code, what is the output?
Practice Set 2 - Write Formatted Program Code for the Following
For each of the following:
- use escape sequences to write program code to produce the formatted output
- try it in the Java Playground (You could also try this in an IDE.)
- re-write the code to use textblocks
A - Displaying a Game Score
Player Score 1 150 2 100 3 95
B - File Path
C:\Name\JavaPrograms\OutputPractice.java