Java Programming

Updated Daily · Fundamental to Advanced

Java Fundamentals

Explore Java programming concepts through hands-on exercises, updated daily. From fundamental basics to advanced topics—each program demonstrates essential techniques.

01

Factorial

Calculate factorial of a number using command-line arguments and iterative loops.

02

Reverse Number

Reverse integer digits using method-based approach and modulo operations.

03

Swap Numbers

Exchange two integers using temporary variable and method parameters.

04

Student Info

Manage student data with object-oriented principles and encapsulation.

Factorial

Factorial.java

Description

Calculates the factorial of a number passed as a command-line argument. Demonstrates command-line argument parsing, integer conversion, and iterative loop control.

Usage: java Factorial 5

Factorial.java
// factorial using command line argument

public class Factorial {
    public static void main(String args[]){
        int n = Integer.parseInt(args[0]);
        int fact=1;
        System.out.println(" FACTORIAL OF A NUMBER ");
        for(int i=1; i<=n; i++){
            fact *= i; 
        }
        System.out.println("The Factorial is:"+fact);
    }
}

Reverse Number

Reverse.java

Description

Reverses the digits of an integer entered by the user. Uses a method-based approach to demonstrate OOP principles and the modulo operator for digit extraction.

Usage: java Reverse (then enter a number)

Reverse.java
import java.util.Scanner;

public class Reverse {
    int rev(int num){
        int sum=0;
        while(num!=0){
            int r=num%10;
            sum = sum*10+r;
            num = num/10;
        }
        System.out.println(sum);
        return sum;
    }
    public static void main(String[] args) {
        int num;
        System.out.println("REVERSE OF A NUMBER USING FACTORIAL");
        System.out.println("\nEnter the number");
        try (Scanner input = new Scanner(System.in)) {
            num = input.nextInt();
        }
        Reverse obj=new Reverse();
        int res=obj.rev(num);
        System.out.println("The reverse of a number is:" + res );
    }
}

Swap Numbers

Swap.java

Description

Swaps two integers using a temporary variable. Demonstrates method creation, object instantiation, and parameter passing in Java.

Usage: java Swap (then enter two numbers)

Swap.java
import java.util.Scanner;

public class Swap{

     void swap(int a,int b){
        int c;
        c=a;
        a=b;
        b=c;
        
        System.out.println(a);
        System.out.println(b);

    }
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int a=input.nextInt();
        int b=input.nextInt();
        System.out.println("Enter the number a and b");
        Swap obj = new Swap();
        obj.swap(a, b);
 // why can we call static method without method creation 
    }
}

Student Info

Student.java

Description

Demonstrates object-oriented programming with a Student class containing attributes (ID, name, address, marks) and methods for data management. Showcases encapsulation and class design principles.

Usage: java Student (then enter ID, name, address, and marks)

Student.java
import java.util.*;

public class Student {
    int ID;
    String name;
    String address;
    int marks;

    void setData(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter ID, Name, address, marks");
        ID= input.nextInt();
        name= input.next();
        address= input.next();
        marks= input.nextInt();
    }

    void getData(){
        System.out.println("Name: "+ name );
        System.out.println("ID: "+ ID);
        System.out.println("address" + address);
        System.out.println("Marks: "+marks);
    }
    public static void main(String[] args) {
        System.out.println("Hello, Student!");
        Student std = new Student();
        std.setData();   
        std.getData();     

    }
}