Java Programming Examples Document 2
Java Program 1: Hello World
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Explanation:
This program demonstrates the most basic Java application. The class name must match
the file name. The main method is the entry point of every Java application.
[Link] prints output to the console.
Java Program 2: Simple Calculator
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double a = [Link]();
[Link]("Enter second number: ");
double b = [Link]();
[Link]("Addition = " + (a + b));
[Link]("Subtraction = " + (a - b));
[Link]("Multiplication = " + (a * b));
[Link]("Division = " + (a / b));
[Link]();
}
}
Explanation:
Scanner is used for user input. Arithmetic operations are performed and results
are printed to the console.
Java Program 3: Factorial Using Loop
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
long factorial = 1;
for(int i = 1; i <= n; i++) {
factorial = factorial * i;
}
[Link]("Factorial = " + factorial);
[Link]();
}
}
Explanation:
A loop multiplies numbers from 1 to n. The result is stored in the factorial variable.
Java Program 4: Array Average
public class ArrayAverage {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50,60,70,80,90,100};
int sum = 0;
for(int i = 0; i < [Link]; i++) {
sum = sum + numbers[i];
}
double average = (double) sum / [Link];
[Link]("Average = " + average);
}
}
Explanation:
Arrays store multiple values of the same type. We loop through the array,
calculate the sum, and divide by the number of elements to get the average.
Java Program 5: Simple Class and Object Example
class Student {
String name;
int age;
void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
public class MainClass {
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Rahul";
[Link] = 18;
[Link]();
}
}
Explanation:
This demonstrates object■oriented programming. A class defines properties and
methods. Objects are instances of a class.
Java Program 1: Hello World
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Explanation:
This program demonstrates the most basic Java application. The class name must match
the file name. The main method is the entry point of every Java application.
[Link] prints output to the console.
Java Program 2: Simple Calculator
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double a = [Link]();
[Link]("Enter second number: ");
double b = [Link]();
[Link]("Addition = " + (a + b));
[Link]("Subtraction = " + (a - b));
[Link]("Multiplication = " + (a * b));
[Link]("Division = " + (a / b));
[Link]();
}
}
Explanation:
Scanner is used for user input. Arithmetic operations are performed and results
are printed to the console.
Java Program 3: Factorial Using Loop
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
long factorial = 1;
for(int i = 1; i <= n; i++) {
factorial = factorial * i;
}
[Link]("Factorial = " + factorial);
[Link]();
}
}
Explanation:
A loop multiplies numbers from 1 to n. The result is stored in the factorial variable.
Java Program 4: Array Average
public class ArrayAverage {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50,60,70,80,90,100};
int sum = 0;
for(int i = 0; i < [Link]; i++) {
sum = sum + numbers[i];
}
double average = (double) sum / [Link];
[Link]("Average = " + average);
}
}
Explanation:
Arrays store multiple values of the same type. We loop through the array,
calculate the sum, and divide by the number of elements to get the average.
Java Program 5: Simple Class and Object Example
class Student {
String name;
int age;
void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
public class MainClass {
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Rahul";
[Link] = 18;
[Link]();
}
}
Explanation:
This demonstrates object■oriented programming. A class defines properties and
methods. Objects are instances of a class.