Prepare Your IDE
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, it won't behave as we expect. That is because it will treat each print
statement distinctly. At this time, to test out some code, we will want 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
.java
file. The file name should start with an uppercase letter.
- Use the following
main
method template. Add your statements to the body of the main
method. A comment is in the body of the main
. 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 */
void main() {
//add your statements here
}
Practice Set 1 - Predict the Output of Code
A - Taco Stand Order
Given the following code, what is the output?
void main() {
int numTacos = 5;
int numSodas = 3;
println("Order Summary");
println(numTacos + " Tacos");
println(numSodas + " Sodas");
}
B - Number of Total Customers
Given the following code, what is the output?
void main() {
int totalCustomers = 255;
int todaysCustomers = readln("Enter how mnay customers were served today");
totalCustomers = totalCustomers + todaysCustomers;
println("Today we served : " + todaysCustomers + " customers");
println("The total number of customers served is " + totalCustomers);
}
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
Create a variable called score
and set its value to 150
.
Write program code that will produce the following output:
Player 1 score is 150
Be sure to use the variable score
in the output.
Create variables for the following:
- street address
- city
- state
- postal code
Write statements to prompt users to input these values and then 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.