0% found this document useful (0 votes)
29 views17 pages

Java Programming Basics and Examples

The document contains multiple Java programming examples covering basic to intermediate concepts, including addition, factorial calculation, simple calculator, Fibonacci series, prime number check, palindrome, Armstrong number, and various array and string manipulations. It also includes object-oriented programming principles such as class and object examples, constructors, method overloading, inheritance, encapsulation, and polymorphism. Each example is accompanied by code snippets and expected outputs.

Uploaded by

gurudilp456
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)
29 views17 pages

Java Programming Basics and Examples

The document contains multiple Java programming examples covering basic to intermediate concepts, including addition, factorial calculation, simple calculator, Fibonacci series, prime number check, palindrome, Armstrong number, and various array and string manipulations. It also includes object-oriented programming principles such as class and object examples, constructors, method overloading, inheritance, encapsulation, and polymorphism. Each example is accompanied by code snippets and expected outputs.

Uploaded by

gurudilp456
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

1 Add Two Numbers

public static void main(String[] args) {

int a = 5, b = 10;

int sum = a + b;

[Link]("Sum = " + sum);

2. Find Factorial of a Number

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;

[Link]("Factorial of " + num + " = " + fact);

Output:

Factorial of 5 = 120

3 .Simple Calculator (switch case)

public class Calculator {

public static void main(String[] args) {

int a = 20, b = 10;

char operator = '+'; // try -, *, /


switch(operator) {

case '+':

[Link]("Sum = " + (a + b));

break;

case '-':

[Link]("Difference = " + (a - b));

break;

case '*':

[Link]("Product = " + (a * b));

break;

case '/':

[Link]("Quotient = " + (a / b));

break;

default:

[Link]("Invalid Operator");

4. Print Multiplication Table

public class MultiplicationTable {

public static void main(String[] args) {

int num = 5;

for(int i = 1; i <= 10; i++) {

[Link](num + " x " + i + " = " + (num * i));

--------------------------------------------------------------------------------------------------------------

Intermediate Level
1. Fibonacci Series (first 10 numbers)
public class Fibonacci {

public static void main(String[] args) {

int n1 = 0, n2 = 1, n3, count = 10;

[Link]("Fibonacci Series: " + n1 + " " + n2);

for (int i = 2; i < count; i++) {

n3 = n1 + n2;

[Link](" " + n3);

n1 = n2;

n2 = n3;

Output:

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

2. Check Prime Number

public class PrimeNumber {

public static void main(String[] args) {

int num = 29;

boolean isPrime = true;

if (num <= 1) {

isPrime = false;

} else {

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) {

isPrime = false;

break;

}
}

if (isPrime)

[Link](num + " is a Prime Number");

else

[Link](num + " is NOT a Prime Number");

Output:

29 is a Prime Number

3. Palindrome Number

(A number that reads the same forward and backward, e.g., 121, 1221)

public class Palindrome {

public static void main(String[] args) {

int num = 121, original = num, rev = 0;

while (num != 0) {

int digit = num % 10;

rev = rev * 10 + digit;

num = num / 10;

if (original == rev)

[Link](original + " is a Palindrome");

else

[Link](original + " is NOT a Palindrome");

Output:
121 is a Palindrome

4. Armstrong Number

(A number where sum of cubes of digits = number itself, e.g., 153 → 1³ + 5³ + 3³ = 153)

public class Armstrong {

public static void main(String[] args) {

int num = 153, original = num, sum = 0;

while (num != 0) {

int digit = num % 10;

sum = sum + (digit * digit * digit);

num = num / 10;

if (sum == original)

[Link](original + " is an Armstrong Number");

else

[Link](original + " is NOT an Armstrong Number");

Output:

153 is an Armstrong Number

5. Find Largest of 3 Numbers

public class LargestNumber {

public static void main(String[] args) {

int a = 10, b = 25, c = 15;

if (a >= b && a >= c)

[Link](a + " is the largest");

else if (b >= a && b >= c)


[Link](b + " is the largest");

else

[Link](c + " is the largest");

Output:

25 is the largest

6. Sum of Digits of a Number

public class SumOfDigits {

public static void main(String[] args) {

int num = 987, sum = 0;

while (num != 0) {

int digit = num % 10;

sum = sum + digit;

num = num / 10;

[Link]("Sum of digits = " + sum);

Output:

Sum of digits = 24

7. Count Number of Digits

public class CountDigits {

public static void main(String[] args) {

int num = 12345, count = 0;

while (num != 0) {
num = num / 10;

count++;

[Link]("Number of digits = " + count);

Output:

Number of digits = 5

1. Reverse an Array

public class ReverseArray {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

[Link]("Original Array: ");

for(int num : arr) {

[Link](num + " ");

[Link]("\nReversed Array: ");

for(int i = [Link] - 1; i >= 0; i--) {

[Link](arr[i] + " ");

Output:

Original Array: 1 2 3 4 5

Reversed Array: 5 4 3 2 1

2. Find Maximum and Minimum in an Array

public class MaxMinArray {


public static void main(String[] args) {

int[] arr = {10, 25, 5, 30, 18};

int max = arr[0];

int min = arr[0];

for(int i = 1; i < [Link]; i++) {

if(arr[i] > max)

max = arr[i];

if(arr[i] < min)

min = arr[i];

[Link]("Maximum = " + max);

[Link]("Minimum = " + min);

Output:

Maximum = 30

Minimum = 5

3. String Palindrome

public class StringPalindrome {

public static void main(String[] args) {

String str = "madam";

String rev = "";

for(int i = [Link]() - 1; i >= 0; i--) {

rev = rev + [Link](i);

if([Link](rev))
[Link](str + " is a Palindrome");

else

[Link](str + " is NOT a Palindrome");

Output:

madam is a Palindrome

4. Count Vowels in a String

public class CountVowels {

public static void main(String[] args) {

String str = "Hello World";

int count = 0;

str = [Link](); // convert to lowercase

for(int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

count++;

[Link]("Number of vowels = " + count);

Output:

Number of vowels = 3
5. Sum of Elements in an Array

public class SumArray {

public static void main(String[] args) {

int[] arr = {2, 4, 6, 8, 10};

int sum = 0;

for(int num : arr) {

sum += num;

[Link]("Sum of array elements = " + sum);

Output:

Sum of array elements = 30

6. Find Duplicate Characters in a String

public class DuplicateChars {

public static void main(String[] args) {

String str = "programming";

str = [Link]();

[Link]("Duplicate characters: ");

for(int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if([Link](ch) != [Link](ch)) {

[Link](ch + " ");

}
Output (may repeat characters):

Duplicate characters: r g m

7. Count Words in a String

public class WordCount {

public static void main(String[] args) {

String sentence = "Java is easy to learn";

String[] words = [Link](" ");

[Link]("Number of words = " + [Link]);

Output:

Number of words = 5

1. Class and Object Example

class Student {

String name;

int age;

void display() {

[Link]("Name: " + name + ", Age: " + age);

public class Main {

public static void main(String[] args) {

Student s1 = new Student(); // creating object

[Link] = "Alice";

[Link] = 20;
[Link]();

Student s2 = new Student();

[Link] = "Bob";

[Link] = 22;

[Link]();

Output:

Name: Alice, Age: 20

Name: Bob, Age: 22

2. Constructor Example

class Car {

String brand;

int year;

// Constructor

Car(String b, int y) {

brand = b;

year = y;

void display() {

[Link]("Car: " + brand + ", Year: " + year);

public class Main {

public static void main(String[] args) {

Car c1 = new Car("Toyota", 2020);

Car c2 = new Car("Honda", 2022);


[Link]();

[Link]();

Output:

Car: Toyota, Year: 2020

Car: Honda, Year: 2022

3. Method Overloading

class MathOperation {

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

public class Main {

public static void main(String[] args) {

MathOperation m = new MathOperation();

[Link]("Sum (int): " + [Link](5, 10));

[Link]("Sum (double): " + [Link](3.5, 2.5));

Output:

Sum (int): 15

Sum (double): 6.0


4. Inheritance Example

class Animal {

void sound() {

[Link]("Animals make sounds");

class Dog extends Animal {

void sound() {

[Link]("Dog barks");

public class Main {

public static void main(String[] args) {

Animal a = new Animal();

[Link]();

Dog d = new Dog();

[Link]();

Output:

Animals make sounds

Dog barks

5. Super Keyword Example

class Person {

String name = "Parent Name";

void display() {

[Link]("This is Person class");

}
}

class Employee extends Person {

String name = "Child Name";

void display() {

[Link](); // call parent method

[Link]("Parent name: " + [Link]);

[Link]("Child name: " + name);

public class Main {

public static void main(String[] args) {

Employee e = new Employee();

[Link]();

Output:

This is Person class

Parent name: Parent Name

Child name: Child Name

6. Encapsulation Example (Getters & Setters)

class Account {

private double balance;

// Setter

public void setBalance(double amount) {

balance = amount;

// Getter

public double getBalance() {

return balance;
}

public class Main {

public static void main(String[] args) {

Account acc = new Account();

[Link](5000.75);

[Link]("Account Balance = " + [Link]());

Output:

Account Balance = 5000.75

7. Polymorphism (Method Overriding)

class Shape {

void draw() {

[Link]("Drawing a shape");

class Circle extends Shape {

void draw() {

[Link]("Drawing a circle");

class Square extends Shape {

void draw() {

[Link]("Drawing a square");

}
public class Main {

public static void main(String[] args) {

Shape s1 = new Circle();

Shape s2 = new Square();

[Link]();

[Link]();

Output:

Drawing a circle

Drawing a square

Summary of OOP Principles in Java

Summary of OOP Principles in Java


Concept Meaning Example

Class Blueprint for objects class Student {}

Object Instance of a class Student s = new Student();

Encapsulation Data hiding with getters & setters private balance; getBalance();

Inheritance Reusing code (child → parent) class Dog extends Animal

Polymorphism Many forms (overloading & overriding) add(2,3) & add(2.5,3.5)

Abstraction Hiding details, showing essential features abstract class Shape {}

You might also like