0% found this document useful (0 votes)
6 views29 pages

Siddhant (JAVA)

The document contains a series of Java programs demonstrating various programming concepts such as printing messages, object-oriented programming, method overloading, inheritance, loops, conditionals, and interface usage. Each program is accompanied by a brief description of its purpose and the output it generates. The programs are attributed to Siddhant Gahlot.

Uploaded by

sg9407176
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)
6 views29 pages

Siddhant (JAVA)

The document contains a series of Java programs demonstrating various programming concepts such as printing messages, object-oriented programming, method overloading, inheritance, loops, conditionals, and interface usage. Each program is accompanied by a brief description of its purpose and the output it generates. The programs are attributed to Siddhant Gahlot.

Uploaded by

sg9407176
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
You are on page 1/ 29

PROGRAM – 01

01: Write a program to print “Welcome” in Java.

class Test {
public static void main (String args []) {
System.out.println ("Welcome");
System.out.println ("This output is produced by Siddhant Gahlot.");
}
}

Output:

1
PROGRAM – 02
02: Write a program to print “Hello” using object in Java.

class Test {
public void show () {
System.out.println ("Hello");
}
}

class Demo {
public static void main (String [] args) {
System.out.println ("This output is produced by Siddhant Gahlot.");
Test ob = new Test ();
ob.show ();
}
}

Output:

2
PROGRAM – 03
03: Write a program to add two numbers using two classes
in Java.
class Test {
int i, j;
public void getValue (int i, int j) {
this.i = i;
this.j = j;
}
public void setValue () {
int k = i + j;
System.out.println (k);
}
}
class MainClass {
public static void main (String [] args) {
System.out.println ("This output is produced by Siddhant Gahlot.");
Test ob = new Test ();
ob.getValue (10, 20);
ob.setValue ();
}
}
Output:

3
PROGRAM – 04
04: Write a program to add 2 numbers using multiple
functions in Java.
class TestMethod {
int i, j;
public int add () {
return i + j;
}
public void getValue (int i, int j) {
this.i = i;
this.j = j;
}
}
class MainClass {
public static void main (String [] args) {
System.out.println ("This output is produced by Siddhant Gahlot");
TestMethod ob = new TestMethod ();
ob.getValue (40, 20);
System.out.println (ob.add ());
}
}
Output:

4
PROGRAM – 05
05: Write a program for method overloading by number
of arguments in Java.
class TestMethod {
int i, j;
public int add (int i, int j) {
return i + j;
}
public int add (int i, int j, int k) {
return i + j + k;
}
}
class MainClass {
public static void main (String [] args) {
System.out.println ("This output is produced by Siddhant Gahlot.");
TestMethod ob = new TestMethod ();
System.out.println (ob.add (70, 20));
System.out.println (ob.add (10, 20, 30));
}
}
Output:

5
PROGRAM – 06
06: Write a program for method overloading by type of
arguments in Java.
class Test {
int i, j;
public int add (float i, float j, float k) {
return 90;
}
public int add (int i, int j, int k) {
return i + j + k;
}
}

class MainClass {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Test ob = new Test();
System.out.println (ob.add (1.2f, 3.2f, 4.6f));
System.out.println (ob.add (10, 20, 30));
}
}
Output:

6
PROGRAM – 07
07: Write a program to show single level inheritance in
Java.
class A {
public void show () {
System.out.println ("Welcome to class A");
}
}
class B extends A {
public void display() {
System.out.println ("Welcome to class B");
}
}
class MainClass {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
B ob = new B();
ob.show ();
ob.display ();
}
}
Output:

7
PROGRAM – 08
08: Write a program to show multilevel inheritance in
Java.
class A {
public void show () {
System.out.println ("Welcome to class A");
}
}
class B extends A {
public void display () {
System.out.println ("Welcome to class B");
}
}
class C extends B {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
C ob = new C ();
ob.show ();
ob.display ();
}
}
Output:

8
PROGRAM – 09
09: Write a program for method overloading by sequence
of arguments in Java.
class Test {
public float add (int i, float j) {
return i;
}
public float add (float i, int j) {
return i;
}
}
class MainClass {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Test ob = new Test ();
System.out.println (ob.add (10, 3.2f));
System.out.println (ob.add (3.2f, 10));
}
}
Output:

9
PROGRAM – 10
10: Write a program to show the use of protected access
specifier in Java.
class A {
protected void show () {
System.out.println ("I am form class A");
}
}
class B extends A {
public void display () {
System.out.println ("Welcome to B");
}
}
class C extends B {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
B ob = new B();
ob.show();
ob.display();
}
}
Output:

10
PROGRAM – 11
11: Write a program to show the use of private access
specifier on variable in Java.
class Test {
private int i;
public void show (int i) {
this.i = i;
System.out.println (this.i);
}
}
class Demo {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Test ob = new Test ();
ob.show (10);
}
}

Output:

11
PROGRAM – 12
12: Write a program to show the use of private access
specifier on method in Java.
class Test {
private void display () {
System.out.println ("Welcome");
}
public void call () {
display ();
System.out.println ("Hey");
}
}
class Demo {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Test ob = new Test ();
ob.call ();
}
}
Output:

12
PROGRAM – 13
13: Write a program to add 2 numbers taken by user using
scanner class in Java.
import java.util.*;
import java.io.*;
class Add {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Scanner sc = new Scanner (System.in);
System.out.println ("Enter first number: ");
int i = sc.nextInt ();
System.out.println ("Enter second number: ");
int j = sc.nextInt ();
int k = i + j;
System.out.println ("Sum is "+k);
}
}
Output:

13
PROGRAM – 14
14: Write a program to use interface in Java.
interface Ml {
void show ();
}
class Dl implements Ml {
public void show () {
System.out.println ("Welcome to Interface");
}
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Dl ob = new Dl ();
ob.show ();
}
}
Output:

14
PROGRAM – 15
15: Write a program to show the use of default method in
interface in Java.
interface Ml {
default void show () {
System.out.println ("Welcome to Default Method");
}
}
class Dl implements Ml {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
Dl ob = new Dl ();
ob.show ();
}
}

Output:

15
PROGRAM – 16
16: Write a program to show the use of multiple interfaces
in Java.
interface A {
void add ();
}
interface B extends A {
void sub ();
}
class C implements B {
public void add () {
int i = 54;
int j = 29;
int k = i + j;
System.out.println (k);
}
public void sub () {
int i = 45;
int j = 19;
int k = i - j;
System.out.println (k);
}
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
C ob = new C ();
ob.add ();
ob.sub ();
}
}

16
Output:

17
PROGRAM – 17
17: Write a program to implement multiple interfaces in
single class in Java.
interface A {
void add ();
}
interface B {
void sub ();
}
class C implements A, B {
public void add () {
int i = 41;
int j = 26;
int k = i + j;
System.out.println (k);
}
public void sub () {
int i = 59;
int j = 45;
int k = i - j;
System.out.println (k);
}
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
C ob = new C ();
ob.add ();
ob.sub ();
}
}

18
Output:

19
PROGRAM – 18
18: Write a program to show the use of for loop in Java.
public class LoopsDemo {
public static void main (String [] args) {
System.out.println ("This output is produced by Siddhant Gahlot");
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println("Sum of numbers from 1 to 5 is: " + sum);
}
}

Output:

20
PROGRAM – 19
19: Write a program to show the use of while loop in Java.
public class LoopsDemo {
public static void main (String [] args) {
System.out.println ("This output is produced by Siddhant Gahlot");
int number = 5;
int factorial = 1;
int j = 1;
while (j <= number) {
factorial *= j;
j++;
}
System.out.println ("Factorial of " + number + " is: " + factorial);
}
}
Output:

21
PROGRAM – 20
20: Write a program to show the use of Do while loop in
Java.
class Loop {
public static void main (String [] args) {
System.out.println ("This Output is produced by Siddhant Gahlot");
int i = 1;
do {
System.out.println (i);
i++;
} while (i < 11);
}
}
Output:

22
PROGRAM – 21
21: Write a program to show the use of for loop (New
Syntax) in Java.
public class EnhancedForLoopDemo {
public static void main (String [] args) {
int [] numbers = {10, 20, 30, 40, 50};
System.out.println ("This output is produced by Siddhant Gahlot");
System.out.println("Using enhanced for loop to print numbers:");
for (int num: numbers) {
System.out.println(num);
}
}
}

Output:

23
PROGRAM – 22
22: Write a program to show the use of if statement in
Java.
import java.util.Scanner;
public class IfDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println ("This output is produced by Siddhant Gahlot");
System.out.println("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
}
if (number % 2 != 0) {
System.out.println(number + " is odd.");
}
scanner.close();
}
}

Output:

24
PROGRAM – 23
23: Write a program to show the use of if-else statement in
Java.
import java.util.Scanner;
public class IfElseDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println ("This output is produced by Siddhant Gahlot");
System.out.println("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
scanner.close();
}
}
Output:

25
PROGRAM – 24
24: Write a program to show the use of elseif statement in
Java.
import java.util.Scanner;
class ElseIf {
public static void main (String [] args) {
Scanner scanner = new Scanner (System.in);
System.out.println ("This output is produced by Siddhant Gahlot");
System.out.println ("Enter a number: ");
int n = scanner.nextInt ();
if (n < 5) {
System.out.println ("Smaller than 5");
} else if ( n > 5) {
System.out.println ("Greater than 5");
} else {
System.out.println ("Equal to 5");
}
scanner.close ();
}
}
Output:

26
27
PROGRAM – 25
25: Write a program to show the use of switch statement
in Java.
import java.util.Scanner;
class ElseIf {
public static void main (String [] args) {
Scanner scanner = new Scanner (System.in);
System.out.println ("This output is produced by Siddhant Gahlot");
System.out.println ("Enter a number between 1 and 7: ");
int day = scanner.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:

28
System.out.println("Invalid input!");
}
scanner.close();
}
}
Output:

29

You might also like