Built-In Functions and System.out.println
Learn Java’s built-in functions and the System.out.println() function by building your first digital receipt.
The project
You’ve probably gone shopping more times than you can count. You walk into the store, choose what you need, bring everything to the counter, and watch as the cashier scans each item. The prices appear on the screen, and finally, the receipt prints. That receipt isn’t random; it’s produced by a program that tells the system exactly what to show and in what order. This type of program is known as a point-of-sale (POS) system.
In this lesson, we’ll create our own small version of that system. We won’t do calculations or data storage yet; we’ll just print the receipt text exactly as it should appear on the screen.
This is what the receipt looks like:
You’ve been hired to develop this POS system. Here’s the catch: for now, you’ll replicate the receipt exactly as it’s presented. Our only tool will be Java’s built-in System.out.println()
function.
The programming concept
The concept for today is built-in functions, which are special commands that Java already knows. You don’t have to invent them; you just use them.
We’ll start with System.out.println()
. Its job is simple: show text or numbers on the screen. You put the thing you want to display inside the parentheses, and Java will display it.
What do you think this Java statement does? Read it out loud, and guess what this single line of code will do when you press the “Run” button:
System.out.println("Hello, World!");
This Java statement tells the system to invoke its output mechanism for the screen, and print a line on it. That line should say “Hello World!”
Congratulations. You are officially a programmer. This is an age-old tradition in the programming community: start learning a new programming language by printing “Hello, World”
on the screen!
What happens when we use multiple println
statements? Take a look at the code below. Do you expect each statement to appear on a separate line? Consider your answer before clicking the “Run” button.
System.out.println("Hello, World!");System.out.println("Testing, testing..");System.out.println(1);System.out.println(2);System.out.println(3);
So, this means that println
will always display output on a new line.
The significance of the concept in the project
If you look closely at our receipt below, it’s just lines of text and numbers. Without System.out.println()
, nothing would appear on the screen. For this first version of our point-of-sale app, each line of the receipt will be produced by a separate System.out.println()
statement.
=== MART RECEIPT ===Cashier Name: AlexItems Purchased:Milk (1): 4Bread (1): 2Eggs (12): 0.5 x 12 = 6Total bill: 4 + 2 + 6 = 12
Concept at a glance:
System.out.println()
is a built-in function. It tells the system (computer) to print something on the screen on a new line.The thing you want to present must go inside parentheses, like this:
System.out.println(...)
.If it’s text, you must put it in quotes:
"Hello!"
.
Learn to code for the concept
We have tried printing the first two lines of our receipt in the code widget below. Run it first, and then your task is to complete the code. There’s also an AI Mentor there to guide you:
System.out.println("=== MART RECEIPT ===");System.out.println("Cashier Name: Alex");
Run this and you’ll see:
=== MART RECEIPT ===Cashier Name: Alex
To match the full receipt, we just keep adding more print()
statements for each line. Try to print the complete receipt by clicking the “Run” button and test it by clicking the “AI Mentor” button.
Learn to code with AI
If you’re working with an AI assistant, you can simply enter a prompt.
Prompt:
Write a Java program that prints this exact receipt using only System.out.println() statements:=== MART RECEIPT ===Cashier Name: AlexItems Purchased:Milk (1): 4Bread (1): 2Eggs (12): 0.5 x 12 = 6Total bill: 4 + 2 + 6 = 12
The AI will instantly provide a working code that’s ready to execute.
Generate code with AI
Here is the Copilot, where you can write your prompt and get it to generate code for you:
Test the code with AI
Once the AI has generated the code, copy-paste it into the following code widget, run it, ask the “AI Mentor” for feedback, and
You’ll notice that in actual code generated by your AI Copilot, all the print statements are wrapped within a specific structure:class Receipt {
public static void main( String args[] )
{
// Your print statements come here
}
}
That’s how real Java developers write Java code. Java is an object-oriented programming language, which means all the code has to be part of a class. As we begin learning the art of programming and the syntax of Java for the first time, our focus will be on understanding core concepts and problem-solving. We'll rely on AI to generate the boilerplate professional code as our Copilot. That’s our philosophy for learning how to code in the age of AI.
class Receipt {public static void main( String args[] ) {// Write your code for receipt generation}}
Food for thought:
What happens if you try to print some text without quotes, e.g.,
System.out.println(Hello);
?What happens if you try to print some text without parentheses, e.g.,
System.out.println "Hello";
?If it’s text, you must put it in quotes:
"Hello!"
Practice makes perfect
You’ve successfully executed a Java program that meets your client’s requirements. Although the code was generated by AI, let’s see if you’ve grasped the core concept well enough to solve a similar problem on your own.
Problem
Print the following receipt exactly as shown, using only System.out.println()
statements. Only the cashier and items have changed:
=== MART RECEIPT ===Cashier Name: SamItems Purchased:Pen (1): 20Notebook (1): 45Bag (1): 120Total bill: 20 + 45 + 120 = 185
Code its solution
The final output should match the sample output shown above:
class Receipt {public static void main( String args[] ) {// Write your code here}}
Perfect! Now your POS app has successfully launched and is ready to ship to customers.