5 Essential Java Programs for Beginners with Output Examples
21 days ago
5 Essential Java Programs for Beginners with Output Examples
Introduction to Practical Java Programming
Java remains one of the most popular programming languages for beginners due to its structured syntax and wide applicability. This post demonstrates five fundamental Java programs that every new programmer should understand, complete with working code and sample outputs. These examples cover basic mathematical operations, type conversions, and variable manipulation - essential building blocks for more complex programming tasks.
1. Kilometer to Meter Converter
This simple program demonstrates basic arithmetic operations and variable assignment in Java.
public class KilometerToMeter {
public static void main(String[] args) {
double kilometers = 5.5;
double meters = kilometers * 1000;
System.out.println(kilometers + " kilometers = " + meters + " meters");
}
}
Output:
5.5 kilometers = 5500.0 meters
2. Simple Interest Calculator
This program shows how to handle user input and perform financial calculations.
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter principal amount: ");
double principal = scanner.nextDouble();
System.out.print("Enter rate of interest: ");
double rate = scanner.nextDouble();
System.out.print("Enter time in years: ");
double time = scanner.nextDouble();
double interest = (principal * rate * time) / 100;
double amount = principal + interest;
System.out.println("Simple Interest: " + interest);
System.out.println("Total Amount: " + amount);
}
}
Sample Output:
Enter principal amount: 10000 Enter rate of interest: 5 Enter time in years: 2 Simple Interest: 1000.0 Total Amount: 11000.0
3. Rectangle Area Calculator
This example demonstrates basic geometric calculations with user input.
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter length of rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter width of rectangle: ");
double width = scanner.nextDouble();
double area = length * width;
System.out.println("Area of rectangle: " + area);
}
}
Sample Output:
Enter length of rectangle: 8.5 Enter width of rectangle: 4.2 Area of rectangle: 35.7
4. Temperature Converter (Celsius to Fahrenheit)
This program illustrates type conversion and formula implementation.
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
double celsius = scanner.nextDouble();
double fahrenheit = (9.0 / 5) * celsius + 32;
System.out.println(celsius + "°C = " + fahrenheit + "°F");
}
}
Sample Output:
Enter temperature in Celsius: 37 37.0°C = 98.6°F
5. Variable Swapping Program
This demonstrates a fundamental programming technique using a temporary variable.
public class VariableSwap {
public static void main(String[] args) {
int a = 5;
int b = 10;
System.out.println("Before swapping:");
System.out.println("a = " + a + ", b = " + b);
// Swapping logic
int temp = a;
a = b;
b = temp;
System.out.println("After swapping:");
System.out.println("a = " + a + ", b = " + b);
}
}
Output:
Before swapping: a = 5, b = 10 After swapping: a = 10, b = 5
Key Learning Points
- Basic I/O operations: Using Scanner class for user input
- Mathematical operations: Arithmetic calculations in Java
- Variable manipulation: Storing and changing values
- Type conversion: Handling different data types
- Algorithm implementation: Converting formulas to code
These five programs form a solid foundation for understanding Java's basic syntax and logic flow. Practice modifying these examples - try changing the data types, adding validation, or combining functionalities to deepen your understanding of Java programming.