Java Programming Guide with Exercises & Projects
1. Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now
owned by Oracle).
It is platform-independent, meaning Java programs can run on any device that has a Java Virtual
Machine (JVM). Java is widely
used in web development, Android apps, enterprise applications, and more.
Features of Java:
- Simple and easy to learn
- Platform-independent (Write Once, Run Anywhere)
- Object-oriented
- Secure and robust
- Multithreading support
2. Basic Syntax & Data Types
In Java, every application starts with a class definition. The main method is the entry point of any
Java program.
Example:
```java
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
```
Data Types in Java:
- int (integer)
- double (decimal numbers)
- char (single character)
- boolean (true/false)
- String (text)
3. Practice Questions
Try solving these:
1. Write a program to check if a number is even or odd.
2. Write a program to find the sum of digits of a number.
3. Write a program to reverse a string.
4. Implement a Java program to find the largest number in an array.
5. Write a program to check if a string is a palindrome.
Example Solution (Even or Odd):
```java
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if(num % 2 == 0) {
[Link](num + " is Even.");
} else {
[Link](num + " is Odd.");
}
[Link]();
}
}
```
4. Mini Projects
Here are some beginner-friendly Java projects:
1. **Simple Calculator** - Build a calculator that performs addition, subtraction, multiplication, and
division.
2. **To-Do List App** - Create a basic console-based to-do list where users can add and remove
tasks.
3. **Number Guessing Game** - The program generates a random number, and the user has to
guess it.
4. **Student Grade Calculator** - A program that takes subject marks and calculates the final
percentage and grade.
Example Project: Number Guessing Game
```java
import [Link];
import [Link];
public class GuessGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = [Link](100) + 1;
Scanner sc = new Scanner([Link]);
int guess = 0;
[Link]("Guess a number between 1 and 100:");
while (guess != numberToGuess) {
guess = [Link]();
if (guess < numberToGuess) {
[Link]("Too low! Try again.");
} else if (guess > numberToGuess) {
[Link]("Too high! Try again.");
} else {
[Link]("Congratulations! You guessed the number.");
}
}
[Link]();
}
}
```