Reading and Writing Console Input and Output Practice
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.)
A - Taco Stand Order
B - Number of Total Customers
Practice Set 2 - Write Program Code for Input and Output
Ask yourself these guiding questions prior to writing the program code:
- What data is the program using?
- What data is being produced?
- What is the type of this data?
- What are good variables names for this data?
- What are the steps to write the program?
A - Displaying a Game Score
In an IDE or using the Java Playground:
- create a variable called
score - set the value of
scoreto150. - write an output statement that uses the variable
scoreand produces the following output:
Player 1 score is 150
B - Input and Format an Address
In an IDE:
- create variables for the following:
- street address
- city
- state
- postal code
- write statements to prompt users to input these values
- print the address in the following format:
street address
city, state postal code
For example:
123 Hello World Dr.
San Fransico, CA 12345
C - Hotel Staffing
For a hotel, print out how many rooms can be cleaned given a number of workers for the day. A worker will have 5 hours a day when they can clean rooms. Each room takes 2 workers, 15 minutes to clean. You will need to obtain the number for the day from the user. Only an even number of workers will be scheduled.