Loading...

Loading...

**Chapter 1: Hello World Program**

In this chapter, we start with the simplest Java program: a “Hello, World!” program. This program is often the first one taught in many programming languages, and it’s no different in Java. The purpose of this program is to get you familiar with the structure of a Java program and how to output text to the console.

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

}

}

**Chapter 2: Calculating the Area of a Circle**

In this chapter, we build on what we learned in the first chapter and write a program that does something a bit more complex: calculating the area of a circle given its radius. This introduces you to using variables and mathematical operations in Java.

Java

public class CircleArea {

public static void main(String[] args) {

double radius = 5.0;

double area = Math.PI * Math.pow(radius, 2);

System.out.println("The area of the circle is " + area);

}

}

**Chapter 3: Sorting an Array**

In this chapter, we take a big step forward and write a program that sorts an array of integers in ascending order. This introduces you to arrays, loops, and conditional statements in Java. We use the bubble sort algorithm for sorting, which is a simple but inefficient sorting algorithm suitable for small arrays.

Java

public class BubbleSort {

public static void main(String[] args) {

int[] array = {5, 3, 8, 1, 2};

for (int i = 0; i < array.length - 1; i++) {

for (int j = 0; j < array.length - i - 1; j++) {

if (array[j] > array[j + 1]) {

int temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

}

}

for (int i : array) {

System.out.print(i + " ");

}

}

}

**Conclusion**

In this book, we’ve taken a journey from writing the simplest Java program to creating more complex programs that perform tasks such as calculating the area of a circle and sorting an array.

We started with a “Hello, World!” program, which served as an introduction to the structure of a Java program and console output. Next, we calculated the area of a circle, which introduced the use of variables and mathematical operations. Finally, we sorted an array using the bubble sort algorithm, which introduced arrays, loops, and conditional statements.

Through these programs, you’ve learned not only how to write Java code, but also how to think like a programmer. You’ve seen how complex tasks can be broken down into smaller, more manageable problems that can be solved with code.

Remember, these are just the basics. There’s a whole world of programming concepts and techniques out there waiting for you to explore. Keep practicing and experimenting with code, and most importantly, have fun doing it! Happy coding! 🎉