Core Java Programming Examples Collection
Core Java Programming Examples
This document contains several fundamental Java code examples demonstrating various programming concepts.
1. Reverse an Integer
The ReverseNumber class demonstrates how to reverse an integer using a while loop.
public class ReverseNumber {
public static void main(String[] args) {
int number = 1234;
int reverse = 0;
int original = number;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
System.out.println("Original number: " + original);
System.out.println("Reversed number: " + reverse);
}
}2. Calculate Factorial
The Factorial class calculates the factorial of a given number using a for loop.
public class Factorial {
public static void main(String[] args) {
int num = 5;
int fact = 1;
for (int i = 1; i <= num; i++) {
fact = fact * i;
}
System.out.println("Factorial of " + num + " = " + fact);
}
}3. Static Members and Blocks
The Exp2Static class illustrates the use of static variables, static blocks, and static methods.
public class Exp2Static {
// Static variable
static int num = 10;
// Static block (executed when the class is loaded)
static {
System.out.println("Static Block: num = " + num);
}
// Static method
static void show() {
System.out.println("Static Method: num = " + num);
}
public static void main(String[] args) {
// Call static method
Exp2Static.show();
// Change static variable
num = 20;
// Call again
Exp2Static.show();
}
}4. Object-Oriented Programming: Employee Class
This section includes two classes demonstrating basic Object-Oriented Programming (OOP) concepts:
Employee Class Definition
The Employee class defines fields for name and salary, and includes a constructor with basic validation.
class Employee {
String firstName;
String lastName;
double monthlySalary;
Employee(String fName, String lName, double salary) {
firstName = fName;
lastName = lName;
if (salary > 0) {
monthlySalary = salary;
} else {
monthlySalary = 0.0;
}
}
void display() {
System.out.println("Employee Name: " + firstName + " " + lastName);
System.out.println("Monthly Salary: " + monthlySalary);
System.out.println("Yearly Salary: " + (monthlySalary * 12));
}
}EmployeeTest Class Usage
The EmployeeTest class creates instances and manipulates employee data, including applying a raise.
public class EmployeeTest {
public static void main(String[] args) {
Employee emp1 = new Employee("Pradeep", "Singh", 25000);
Employee emp2 = new Employee("Chirayu", "Maske", 30000);
System.out.println("---- Initial Salaries ----");
emp1.display();
System.out.println();
emp2.display();
// Applying a 10% raise
emp1.monthlySalary = emp1.monthlySalary * 1.10;
emp2.monthlySalary = emp2.monthlySalary * 1.10;
System.out.println("\n---- After 10% Raise ----");
emp1.display();
System.out.println();
emp2.display();
}
}5. Calculating Rectangle Area with Scanner Input
The AreaOfRectangle class uses the Scanner utility to take user input for length and breadth and calculates the area.
import java.util.Scanner;
class AreaOfRectangle {
int length;
int breadth;
void findArea() {
int area = length * breadth;
System.out.println("Area of Rectangle = " + area);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
AreaOfRectangle rect = new AreaOfRectangle();
System.out.print("Enter length of rectangle: ");
rect.length = sc.nextInt();
System.out.print("Enter breadth of rectangle: ");
rect.breadth = sc.nextInt();
rect.findArea();
}
}6. Basic Student Data Management
The Student class demonstrates simple data storage and retrieval using instance variables and a method.
class Student {
String name;
int rollNo;
float marks;
void showData() {
System.out.println("Student Name: " + name);
System.out.println("Roll Number: " + rollNo);
System.out.println("Marks: " + marks);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Chirayu Maske";
s1.rollNo = 11;
s1.marks = 85.5f;
s1.showData();
}
}