Java the Easy Way

First Java Program

In this lesson, we'll write our first Java program by imagining our daily routine and translating it into code, helping us understand how real-life activities can be represented using programming concepts.

Nov. 4, 2024, 6:32 a.m.

Let's start by writing a simple program that simulates our morning routine. This will be an easy and fun way to see how Java works in action. In this section, you'll come across concepts like classes and the main method. Don't worry about understanding everything right away—just focus on the logic and how the code flows. We'll explore the other details step by step as we go.

Java

Morning Routine Algorithm
1. Sleep Mode - If the time is before 6:00 AM, we stay asleep.
2. Wake Up - When the time reaches 6:00 AM, the alarm rings and says, "Good morning!"
3. Brushing Teeth - We go to the bathroom and brush our teeth. Brushing goes up and down for 2 minutes.
4. Choosing Clothes - We go to the wardrobe. Look through different clothes in a loop. Pick one outfit to wear for the day.
5. Having Breakfast - Go downstairs. Eat breakfast to get energy for the day.

Create a new Java file named MorningRoutine.java. Inside that file, add the code which is shown below step by step.

class

In Java, a class is like a notebook or instruction sheet where you write step-by-step tasks for the computer to follow.

For example, your class MorningRoutine is like a page titled "My Morning Steps", and inside it, you’ve written clear instructions like:
- Sleep until 6 AM
- Brush teeth
- Pick clothes
- Eat breakfast
When the program runs, the computer reads this page and does exactly what you wrote—just like following a list of tasks.

Now, add main method.

main method

When you see public static void main, it means this is the main method (or main function) of the class. Think of it like starting a car or bike—nothing works until you turn the key. Similarly, in Java, the program doesn’t run until the main method is called. This method is the starting point of your application. It's where the computer begins executing your code.

Now and interger and strings.

data type

If you look inside the main method, you’ll see this line:
int time = 5;

Here’s what it means:
1. int is a data type that tells the computer we are storing a whole number (no decimal point).
2. time is a variable, which means it's like a box or placeholder where we store data.
3. 5 is the value being stored in the variable.

Think back to math class in school. You might remember solving something like:
x + 5 = 6
x = 1

In this case:
1. x is a variable, just like time above.
2. 1 is the value stored in x, just like we stored 5 in time.

Other common data types include:
1. String – used for text (words, letters, or sentences)
2. int – used for whole numbers

We’ll explore data types more deeply in another module.

Now add while loop.

while

A while loop is used to repeat a block of code as long as a given condition is true.
Think of it like a fridge.
The fridge keeps cooling the items inside only when electricity is available.
So the condition is:
"If electricity is on → keep cooling."
As long as this condition is true, the fridge works.
But if the electricity goes off (the condition becomes false), the fridge stops cooling.

Similarly, in programming:
1. A while loop keeps running while the condition is true.
2. Once the condition becomes false, the loop stops.

In our code, the variable time starts with the value 5.
- When the code reaches the while loop, it checks the condition: while (time < 6)
- Since 5 < 6 is true, it goes inside the loop and runs this line: System.out.println(sleepingTime + time + " AM");
This prints the message to the terminal.
- Then, the line: time++;
increases the value of time from 5 to 6.
- Now, the loop checks the condition again: 6 < 6
- This is false, so the loop stops running, and the program moves to the next step.

Now, add if condition.

if condition

The if statement is used to check a condition, and it only runs its block of code if the condition is true.

Think of it like this:
- You're a vegetarian and you go to a restaurant.
- You’ll only order food if the menu has a vegetarian burger.
So, you check the menu:
- If there’s a veg burger → you place your order.
- If not → you leave the restaurant without ordering.

In our code, if time == 6 , than only it will excute: System.out.println("⏰ Alarm: Good Morning! It's " + time + " AM");
than it will print in the terminal.

Now add below code.

additioonal code

In this code, String[] means an array of Strings—it can hold multiple values instead of just one.
So, the line: String[] clothes = {"T-shirt", "Shirt", "Hoodie", "Sweater"};
defines an array named clothes that contains four clothing items.

Arrays are useful when you want to store and manage a group of similar data—in this case, different clothes.

Next, we use a for-each loop, which is a simplified version of a regular for loop, to go through each item in the array:

for (String cloth : clothes) {
System.out.println("Looking at: " + cloth);
}

This loop goes through each cloth inside the clothes array and prints it one by one.
If you want to access a specific item from the array, you can use its index.
For example: String selectedCloth = clothes[1];

This selects the second item in the array ("Shirt"), since array indexing starts from 0.

Now, below is the whole code. Add all the things and run the code.

one

If you have any confusion. You can mail me in the email address. " [email protected] "