Java Projects For Beginners
Java is a powerful, widely used programming language. It has become the go-to choice for many developers and is considered one of the most popular programming languages. For beginners, learning Java can be intimidating, but with a little bit of guidance and some practice, anyone can master it. In this article, we will explore some Java projects for beginners step by step.
- Hello World The Hello World program is a classic project for beginners in any programming language. It is a simple program that displays the message “Hello, World!” on the screen. This program helps beginners understand the basic syntax and structure of the language.
Here’s how to create the Hello World program in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Calculator A calculator is another popular Java project for beginners. It allows you to perform basic mathematical operations such as addition, subtraction, multiplication, and division.
Here’s how to create a calculator program in Java:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers: ");
double num1 = sc.nextDouble();
double num2 = sc.nextDouble();
System.out.println("Enter an operator (+, -, *, /): ");
char operator = sc.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("Invalid operator");
return;
}
System.out.println(num1 + " " + operator + " " + num2 + " = " + result);
}
}
- Tic Tac Toe Tic Tac Toe is a popular game that is easy to program in Java. It is a good project for beginners who want to learn about user input and basic game logic.
Here’s how to create a Tic Tac Toe game in Java:
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] board = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
char currentPlayer = 'X';
while (true) {
printBoard(board);
System.out.println("Enter row (0-2) and column (0-2) separated by a space: ");
int row = sc.nextInt();
int col = sc.nextInt();
if (board[row][col] == '-') {
board[row][col] = currentPlayer;
if (checkWin(board, currentPlayer)) {
System.out.println("Player " + currentPlayer + " wins!");
break;
}
if (checkTie(board)) {
System.out.println("Tie game!");
break;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
} else {
System.out.println("That spot is taken, try again");
}
}
}
public static void printBoard(char[][] board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j
public static boolean checkWin(char[][] board, char player) {
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true;
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
public static boolean checkTie(char[][] board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
4. Guessing Game
A guessing game is another fun project for beginners. It is a simple program that generates a random number and asks the user to guess it. The program gives hints to the user, telling them whether their guess is too high or too low.
Here’s how to create a guessing game in Java:
import java.util.Scanner; import java.util.Random;
public class GuessingGame { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Random rand = new Random();
int number = rand.nextInt(100) + 1;
int tries = 0;
System.out.println("I'm thinking of a number between 1 and 100. Can you guess it?");
while (true) {
System.out.println("Enter your guess: ");
int guess = sc.nextInt();
tries++;
if (guess == number) {
System.out.println("Congratulations, you guessed it! It took you " + tries + " tries.");
break;
} else if (guess < number) {
System.out.println("Too low. Try again.");
} else {
System.out.println("Too high. Try again.");
}
}
}
}
5. Hangman
Hangman is a classic game that involves guessing a word by guessing its letters. It is a fun project for beginners who want to learn about strings and user input.
Here’s how to create a Hangman game in Java:
import java.util.Scanner; import java.util.Random;
public class Hangman { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Random rand = new Random();
String[] words = {"cat", "dog", "bird", "snake", "turtle"};
String word = words[rand.nextInt(words.length)];
int guesses = 6;
char[] guessesSoFar = new char[word.length()];
boolean guessedWord = false;
for (int i = 0; i < guessesSoFar.length; i++) {
guessesSoFar[i] = '_';
}
while (guesses > 0 && !guessedWord) {
System.out.println("Guess the word: " + new String(guessesSoFar));
System.out.println("Guesses left: " + guesses);
System.out.println("Enter a letter: ");
char letter = sc.next().charAt(0);
boolean guessedLetter = false;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letter) {
guessesSoFar[i] = letter;
guessedLetter = true;
}
}
if (!guessedLetter) {
guesses--;
System.out.println("Incorrect guess. Try again.");
}
if (new String(guessesSoFar).equals(word)) {
guessedWord = true;
System.out.println("Congratulations, you guessed the word!");
}
}
if (!guessedWord) {
System.out.println("Sorry, you ran out of guesses. The word was " + word);
}
}
}
6. Shopping List
A shopping list is a practical project for beginners who want to learn about arrays and loops. It is a program that allows the user to enter items they need to buy and displays them in a list.
Here’s how to create a shopping list program in Java:
import java.util.Scanner;
public class ShoppingList { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
String[] items = new String[10];
int itemCount = 0;
while (true) {
System.out.println("Enter an item or type 'quit' to exit: ");
String input = sc.nextLine();
if (input.equals("quit")) {
break;
}
items[itemCount] = input;
itemCount++;
if (itemCount == items.length) {
String[] newItems = new String[items.length * 2];
for (int i = 0; i < items.length; i++) {
newItems[i] = items[i];
}
items = newItems;
}
}
System.out.println("Your shopping list:");
for (int i = 0; i < itemCount; i++) {
System.out.println("- " + items[i]);
}
}
}
7. Weather App
A weather app is a good project for beginners who want to learn about APIs and JSON parsing. It is a program that allows the user to enter a location and displays the current weather information for that location.
Here’s how to create a weather app in Java:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject;
public class WeatherApp { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter a location: “); String location = reader.readLine();
String apiKey = "YOUR_API_KEY";
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + apiKey;
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject json = new JSONObject(response.toString());
JSONObject main = json.getJSONObject("main");
double temp = main.getDouble("temp");
JSONObject wind = json.getJSONObject("wind");
double windSpeed = wind.getDouble("speed");
System.out.println("Current temperature in " + location + " is " + temp + " K.");
System.out.println("Wind speed in " + location + " is " + windSpeed +