0% found this document useful (0 votes)
12 views10 pages

Java Long Document 2

The document provides several Java programming examples, including a basic 'Hello World' program, a simple calculator, a factorial calculator using loops, an array average calculator, and a class and object example. Each program is accompanied by explanations of its functionality and key concepts such as user input, arithmetic operations, loops, arrays, and object-oriented programming. These examples serve as foundational learning tools for understanding Java programming.

Uploaded by

manipratap69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Java Long Document 2

The document provides several Java programming examples, including a basic 'Hello World' program, a simple calculator, a factorial calculator using loops, an array average calculator, and a class and object example. Each program is accompanied by explanations of its functionality and key concepts such as user input, arithmetic operations, loops, arrays, and object-oriented programming. These examples serve as foundational learning tools for understanding Java programming.

Uploaded by

manipratap69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like