Essential Java Programming Examples for Beginners
Java Programming Examples
This collection provides fundamental Java code snippets demonstrating core concepts such as classes, arrays, interfaces, and data structures.
1. Employee Salary Management
This example demonstrates how to manage employee data using an array of objects and basic arithmetic calculations.
import java.util.Scanner;
class Emp {
private int id;
private String name;
private double sal, pf, hra;
void set(int i, String n, double s, double p, double h) {
id = i; name = n; sal = s; pf = p; hra = h;
}
void display() {
double total = sal + hra - pf;
System.out.println(id + " " + name + " Total: " + total);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Emp[] e = new Emp[n];
for (int i = 0; i < n; i++) {
e[i] = new Emp();
e[i].set(sc.nextInt(), sc.next(), sc.nextDouble(), sc.nextDouble(), sc.nextDouble());
}
for (int i = 0; i < n; i++)
e[i].display();
}
}2. Library Fine Calculation
This snippet illustrates the use of interfaces to define library book attributes and calculate late fees.
import java.util.*;
interface Lib {
String book_name="", book_title="", acctype="";
int date=0; double bal=0;
}
class L implements Lib {
String b, t, a; int d1, d2;
void get(Scanner sc) {
b=sc.next(); t=sc.next(); a=sc.next();
d1=sc.nextInt(); d2=sc.nextInt();
}
void show() {
int days=d2-d1;
double fine=(days>7)?(days-7)*5:0;
System.out.println(b+" "+t+" "+a+" Days:"+days+" Fine:"+fine);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
L x=new L();
x.get(sc);
x.show();
}
}3. Student Grading System
A practical application of conditional logic to determine student grades based on percentage.
import java.util.*;
class Student {
String name, gender;
int roll, age, s1, s2;
void get(Scanner sc) {
name=sc.next(); roll=sc.nextInt();
age=sc.nextInt(); gender=sc.next();
s1=sc.nextInt(); s2=sc.nextInt();
}
void show() {
int total=s1+s2;
double per=total/2.0;
String g=(per>=75)?"A":(per>=60)?"B":(per>=40)?"C":"Fail";
System.out.println(name+" "+roll+" Total:"+total+" %:"+per+" Grade:"+g);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Student s[]=new Student[n];
for(int i=0;i4. Stack Data Structure Implementation
A basic implementation of a Stack using an array, featuring push, pop, and display operations.
import java.util.*;
class Stack {
int a[] = new int[10], top = -1;
void push(int x) {
if (top == 9) System.out.println("Overflow");
else a[++top] = x;
}
void pop() {
if (top == -1) System.out.println("Underflow");
else System.out.println("Popped: " + a[top--]);
}
void display() {
for (int i = top; i >= 0; i--)
System.out.print(a[i] + " ");
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
for (int i = 0; i < 3; i++) s.push(sc.nextInt());
s.pop();
s.display();
}
}
with a size of 3.61 KB