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

Armaan Java

The document contains a series of Java programming exercises authored by Shaikh Armaan for a SYBCA class. Each program demonstrates different programming concepts such as printing user information, calculating net salary, using multiple classes, command line arguments, data input, variable casting, pattern display, arithmetic and relational operators, and mathematical functions. The document includes code snippets and their respective outputs for each program.

Uploaded by

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

Armaan Java

The document contains a series of Java programming exercises authored by Shaikh Armaan for a SYBCA class. Each program demonstrates different programming concepts such as printing user information, calculating net salary, using multiple classes, command line arguments, data input, variable casting, pattern display, arithmetic and relational operators, and mathematical functions. The document includes code snippets and their respective outputs for each program.

Uploaded by

armaans0514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 84

SYBCA-4 406- Java Programming Language

PROGRAM:1 Write A Program For Print Your Name And Address.


/*********************************************************
NAME: Shaikh Armaan
PROGRAM:1 WRITE A PROGRAM FOR PRINT YOUR NAME AND
ADDRESS. CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Person
{
public static void main(String args[])
{
System.out.println("Name: "+args[0]);
System.out.println("Address: "+args[1]);
System.out.println("City: "+args[2]);
System.out.println("Pincode: "+args[3]);

}
}
/**************************OUTPUT***********************
Name:armaan
Address:town
City: Navsari
Pincode: 396445
******************************************************/

Shaikh Armaan 1

SYBCA-4 406- Java Programming Language

PROGRAM:2 W.A.P for calculate net salary from given basic salary.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:2 W.A.P FOR CALCULATE NET SALARY FROM
GIVEN BASIC SALARY.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class salary
{
int s,t,hi,pe,o,net,sub;
void input()
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter The Gross Salary:- ");
s=sc.nextInt();
t=(s*20)/100;
hi=(s*5)/100;
pe=(s*3)/100;
o=(s*1)/100;
}
void output ()
{
net=s-(t+hi+pe+o);
System.out.println("Your Net Salary is : "+ net);
}
}
public class prog2
{
public static void main(String args[])
{
salary obj= new salary();
obj.input();
obj.output();
}
}
/**************************OUTPUT***********************
Enter The Gross Salary:- 1500000
Your Net Salary is : 254000

*******************************************************/

Shaikh Armaan 2

SYBCA-4 406- Java Programming Language

PROGRAM:3 W.A.P for use multiple class.


/*********************************************************
NAME: Shaikh Armaan
PROGRAM:3 W.A.P FOR USE MULTIPLE CLASS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Demo
{
public void printMeth()
{
System.out.println("Hello");
}
}

public class Third


{
public static void main(String[] args)
{
Demo d=new
Demo();
d.printMeth();
}
}
/**************************OUTPUT***********************
Hello

*******************************************************/

Shaikh Armaan 3

SYBCA-4 406- Java Programming Language

PROGRAM:4 W.A.P for use of command line arguments.


/*********************************************************
NAME: Shaikh Armaan
PROGRAM:4 W.A.P FOR USE OF COMMAND LINE ARGUMENTS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Fourth
{
public static void main(String args[])
{
int count,i=0;
String str;
count=args.length;
System.out.println(“Command line argument program”);
System.out.println("No of arguments" + count);

while (i<count)
{
str=args[i];
i=i+1;
System.out.println(i + ":" + "java is " + str +":");
}
}
}
/**************************OUTPUT***********************
Command line argument program
No of arguments:3
1:java is secure:
2:java is simple:
3:java is robust:

*******************************************************/

Shaikh Armaan 4

SYBCA-4 406- Java Programming Language
PROGRAM:5 W.A.P for read data from keyboard using data input string.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:5 W.A.P FOR READ DATA FROM KEYBOARD USING DATA
INPUT STRING.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.io.DataInputStream;
class fifth
{
public static void main(String args[])
{
DataInputStream inp =new DataInputStream(System.in);
int a=0;
float b=0;
try
{
System.out.print("Enter the 1 Number:");
a=Integer.parseInt(inp.readLine());
System.out.print("Enter the 2 Number:");
b=Float.parseFloat(inp.readLine());
}
catch(Exception e)
{}
System.out.println("Your 1 Number is:" + a);
System.out.println("Your 2 Number is:" + b);
}
}

/**************************OUTPUT***********************
Enter the 1 Number:25
Enter the 2 Number:14
Your 1 Number is:25
Your 2 Number is:14
*******************************************************/

Shaikh Armaan 5

SYBCA-4 406- Java Programming Language
PROGRAM:6 W.A.P for casting of variables.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:6 W.A.P FOR CASTING OF VARIABLES.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class sixth1
{
public static void main(String args[])
{
double a=15.15;
int b=(int) a;
System.out.println("The value of a is:" + a);

System.out.println("Type casting");
System.out.print("The value of b is:" + b);
}
}
/**************************OUTPUT***********************
The value of a
is:15.15 Type casting
The value of b is:15

*******************************************************/

Shaikh Armaan 6

SYBCA-4 406- Java Programming Language
PROGRAM:7 W.A.P for display following patterns.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:7 W.A.P FOR DISPLAY FOLLOWING PATTERNS.

1). 2). 3).


1 1 $$$$$
23 11 $$$$
456 101 $$$
7 8 9 10 1001 $$
$

CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Seventh {

public static void main(String[] args) {

int i,j,l=1,m=1;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(" " +
m); m++;
}
System.out.println();
}

System.out.println("***********************************");

for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
if((i==3 && j==2) || (i==4 && (j==2 || j==3)))
{
System.out.print(" 0");
}
else if (i==1) {
System.out.print(" " +i);
}
else
{
System.out.print(" " +l);

Shaikh Armaan 7

SYBCA-4 406- Java Programming Language
}
}
System.out.println();
}

System.out.println("***************************************");

for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(" $");
}
System.out.println();
}
}
}
/**************************OUTPUT***********************
1
23
456
7 8 9 10
***********************************
1
11
101
1001
***************************************
$$$$$
$$$$
$$$
$$
$
*******************************************************/

Shaikh Armaan 8

SYBCA-4 406- Java Programming Language
PROGRAM:8 W.A.P sum of following series
1+1/2+1/3+1/4...................+1/n
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:6 W.A.P SUM OF FOLLOWING SERIES.
1+1/2+1/3+1/4...................+1/N
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Eight
{
public static void main(String args[])
{
int a;
double d=0.0,c;
a=Integer.parseInt(args[0]);
for(c=1;c<=a;c++)
{
System.out.println("1" + "/" + c);
d=d+(1/c);
}
System.out.println("The addiion is:"+d);
}
}
/**************************OUTPUT***********************
1/1.0
1/2.0
1/3.0
1/4.0
1/5.0
The addiion is:2.283333333333333
*******************************************************/

Shaikh Armaan 9

SYBCA-4 406- Java Programming Language
PROGRAM:9 W.A.P for check arithmetic operators.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:9 W.A.P FOR CHECK ARITHMETIC OPERATORS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Arithmetic
{
int a=25,b=14,c;

public void Addition(int a,int b)


{
System.out.println("Additon:" +(a+b));
}

public void Subtraction(int a,int b)


{
System.out.println("Subraction:" +(a-b));
}

public void Multiply(int a,int b)


{
System.out.println("Multiplication:" +(a*b));
}

public void Division(int a,int b)


{
System.out.println("Division:" +(a/b));
}

public void Mod(int a,int b)


{
System.out.println("Mod:" +(a%b));
}
}

public class Ninth


{

public static void main(String args[])


{
int a,b;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
Arithmetic a1 = new
Arithmetic();
System.out.println("Arithmetic Opeartors:");

Shaikh Armaan 10

SYBCA-4 406- Java Programming Language
a1.Addition(a,b);
a1.Subtraction(a,b);
a1.Multiply(a,b);
a1.Division(a,b);
a1.Mod(a,b);
}

/**************************OUTPUT***********************
PS C:\Java> java Ninth 12 6
Arithmetic Opeartors:
Additon:39
Subraction:11
Multiplication:35
Division:1.78
Mod:0
*******************************************************/

Shaikh Armaan 11

SYBCA-4 406- Java Programming Language
PROGRAM:10 W.A.P for relational operators.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:10 W.A.P FOR RELATIONAL OPERATORS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Relational
{
void Equal_to(int a,int b)
{
if(a==b)
{
System.out.println("a and b are equal");
}
else
{
System.out.println("a and b are not equal");
}
}

void Greater(int a,int b)


{
if(a>b)
{
System.out.println("A is greater");
}
else
{
System.out.println("B is greater");
}
}

void Less(int a, int b)


{
if(a<b)
{
System.out.println("A is smaller");
}
else
{
System.out.println("B is smaller");
}
}

void Greater_Equal(int a,int b)


{

Shaikh Armaan 12

SYBCA-4 406- Java Programming Language
if(a>=b)
{
System.out.println("A is greater or equal to B");
}
else
{
System.out.println("B is greater or equal to A");
}
}

void Less_Equal(int a,int b)


{
if(a<=b)
{
System.out.println("A is less or equal to B");
}
else
{
System.out.println("B is less or equal to A");
}
}

void Not_Equal(int a,int b)


{
if(a!=b)
{
System.out.println("A is not equal to B");
}
else
{
System.out.println("A and B are equal");
}
}

}
public class Tenth
{
public static void main(String args[])
{
int c,d;
c=Integer.parseInt(args[0]);
d=Integer.parseInt(args[1]);

Relational R=new

Relational(); R.Equal_to(c, d);


R.Greater(c, d);
R.Less(c, d);

Shaikh Armaan 13

SYBCA-4 406- Java Programming Language
R.Greater_Equal(c, d);
R.Less_Equal(c, d);
R.Not_Equal(c, d);
}
}
/**************************OUTPUT***********************
a and b are not equal
B is greater
A is smaller
B is greater or equal to A
A is less or equal to B
A is not equal to B
*******************************************************/

Shaikh Armaan 14

SYBCA-4 406- Java Programming Language
PROGRAM:11 W.A.P for check maximum number from given three
number only use from conditional operator.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:11 W.A.P FOR CHECK MAXIMUM NUMBER FROM GIVEN
THREE NUMBER ONLY USE FROM CONDITIONAL OPERATOR.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/

import java.util.Scanner;
class Eleventh
{

public static void main(String args[])


{
int a,b,c,max;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number 1:");
a=sc.nextInt();

System.out.println("Enter number 2:");


b=sc.nextInt();

System.out.println("Enter number 3:");


c=sc.nextInt();

max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);

System.out.println("Maximum number among " + a


+ ", " + b + " and " + c + " is "
+ max);
}
}
/**************************OUTPUT***********************

Enter number 1:
25
Enter number 2:
14
Enter number 3:
05
Maximum number among 25, 14 and 05 is 25
\*******************************************************/

Shaikh Armaan 15

SYBCA-4 406- Java Programming Language

PROGRAM:12 W.A.P for use of ten mathematical function.


/*********************************************************
NAME: Shaikh Armaan
PROGRAM:12 W.A.P FOR USE OF TEN MATHEMATICAL FUNCTION.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;
public class Twelveth
{

public static void main(String[] args)


{
double num1,num2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no 1:");

num1 = sc.nextDouble();

System.out.println("Enter no 2:");

num2 = sc.nextDouble();

System.out.println( "Maths Functions ");

double sum = num1 + num2;


System.out.println("1. Addition: " +
sum);

double difference = num1 - num2;


System.out.println("2. Subtraction: " + difference);

double product = num1 * num2;


System.out.println("3. Multiplication: " + product);

double quotient = num1 / num2;


System.out.println("4. Division: " + quotient);

double powerResult = Math.pow(num1, num2);


System.out.println("5. Power: " +
powerResult);

Shaikh Armaan 16

SYBCA-4 406- Java Programming Language
double sqrtResult = Math.sqrt(num1);
System.out.println("6. Square Root: " + sqrtResult);

double absoluteValue = Math.abs(num2);


System.out.println("7. Absolute Value: " + absoluteValue);

double ceilingValue = Math.ceil(num1);


System.out.println("8. Ceiling Value: " + ceilingValue);

double floorValue = Math.floor(num2);


System.out.println("9. Floor Value: " + floorValue);

long roundValue = Math.round(num1);


System.out.println("10. Round Value: " + roundValue);
}
}

/**************************OUTPUT***********************

Enter no 1:
12.23
Enter no 2:
34.22

Maths Functions

1. Addition: 46.45
2. Subtraction: -21.99
3. Multiplication: 418.5106
4. Division: 0.35739333722969024
5. Power: 1.6282960141460089E37
6. Square Root: 3.4971416900091423
7. Absolute Value: 34.22
8. Ceiling Value: 13.0
9. Floor Value: 34.0
10. Round Value: 12

*******************************************************/

Shaikh Armaan 17

SYBCA-4 406- Java Programming Language
PROGRAM:13 W.A.P for find month name from given number using
switch case.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:11 W.A.P FOR FIND MONTH NAME FROM GIVEN NUMBER
USING SWITCH CASE.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;
public class Thirteen
{

public static void main(String args[])


{
int a;

Scanner sc = new Scanner(System.in);

System.out.println("Enter no: ");

a=sc.nextInt();

System.out.println("The entered number month is:");

switch (a)
{
case 1:
System.out.println("January");
break;
case 2:
System.out.println("Februay");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:

Shaikh Armaan 18

SYBCA-4 406- Java Programming Language
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("Septemeber");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("Novmeber");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Enter valid number");
break;
}
}

}
/**************************OUTPUT***********************
Enter no:
5
The entered number month is:
May

\*******************************************************/

Shaikh Armaan 19

SYBCA-4 406- Java Programming Language
PROGRAM:14 W.A.P for print table of inputed number only use of
do…..while loop.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:14 W.A.P FOR PRINT TABLE OF INPUTED NUMBER ONLY
USE OF DO…..WHILE LOOP.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;
class table
{
int b,i=1;
void Table(int a)
{
do
{
b=a*i;
System.out.println(a + " x " + i + " = " +
b); i++;
}
while(i<=10);
}
}

public class Fourteen


{
public static void main(String[] args)
{
int no;
Scanner sc = new Scanner(System.in);

System.out.println("Enter No: ");


no=sc.nextInt();

table t=new table();


t.Table(no);
}
}

Shaikh Armaan 20

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

Enter No:
6

6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
*******************************************************/

Shaikh Armaan 21

SYBCA-4 406- Java Programming Language
PROGRAM:15 W.A.P for application of classes and objects.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:15 W.A.P FOR APPLICATION OF CLASSES AND OBJECTS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Demo
{
void Demo1()
{
System.out.println("This is First Class");
}
}

class Sdemo
{
void Demo2()
{
System.out.println("This is Second Class ");
}
}

public class Fifteen {


public static void main(String[] args) {
Demo d=new Demo();
Sdemo sd=new Sdemo();
d.Demo1();

System.out.println();

sd.Demo2();

}
}
/**************************OUTPUT***********************

This is First Class

This is Second Class

*******************************************************/

Shaikh Armaan 22

SYBCA-4 406- Java Programming Language
PROGRAM:16 W.A.P for use of different types of constructor.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:16 W.A.P FOR USE OF DIFFERENT TYPES
OF CONSTRUCTOR.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class sixteen
{

public sixteen()
{
System.out.println("No-argument constructor called");
}

public sixteen(String message)


{
System.out.println("Parameterized constructor called with message: " + message);
}

public sixteen(sixteen other)


{
System.out.println("Copy constructor called. Copying message from another
object.");
}

public static void main(String[] args)


{

sixteen obj1 = new sixteen();


sixteen obj2 = new sixteen("Hello, World!");

sixteen obj3 = new sixteen(obj1);


}
}

Shaikh Armaan 23

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

No-argument constructor called


Parameterized constructor called with message: Hello, World!
Copy constructor called. Copying message from another object.

*******************************************************/

Shaikh Armaan 24

SYBCA-4 406- Java Programming Language
PROGRAM:17 W.A.P for method overloading.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:17 W.A.P FOR METHOD OVERLOADING.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;
public class Seventeen
{
public int add(int a, int b)
{
return a + b;
}

public int add(int a, int b, int c)


{
return a + b + c;
}

public double add(double a, double b)


{
return a + b;
}

public static void main(String[] args)


{
int a,b,c;

Scanner sc = new Scanner(System.in);


System.out.println("Enter no 1: ");
a=sc.nextInt();
System.out.println("Enter no 2: ");
b=sc.nextInt();
System.out.println("Enter no 3: ");
c=sc.nextInt();
Seventeen example = new Seventeen();

Syste.out.println("Method Overloading");
System.out.println("Sum (int): " + example.add(a,b));
System.out.println("Sum (int): " + example.add(a, b, c));
System.out.println("Sum (double): " +
example.add(a,b));
}

Shaikh Armaan 25

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************
Enter no 1:
12
Enter no 2:
13
Enter no 3:
14

Method Overloading

Sum (int): 25
Sum (int): 39
Sum (double): 25

*******************************************************/

Shaikh Armaan 26

SYBCA-4 406- Java Programming Language
PROGRAM:18 W.A.P for use of static data and static method.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:18 W.A.P FOR USE OF STATIC DATA AND
STATIC METHOD.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class eighteen
{

static int staticVariable = 10;

int instanceVariable;

public static void staticMethod()


{
System.out.println("This is a static method.");
}

public void instanceMethod()


{
System.out.println("This is an instance method.");
}

public static void main(String[] args)


{

System.out.println("Static Variable: " + eighteen.staticVariable);

eighteen.staticMethod();

eighteen instance = new eighteen();

instance.instanceVariable = 20;
System.out.println("Instance Variable: " + instance.instanceVariable);

instance.instanceMethod();
}

Shaikh Armaan 27

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************
Static Variable: 10
This is a static method.
Instance Variable: 20
This is an instance method.

\*******************************************************/

Shaikh Armaan 28

SYBCA-4 406- Java Programming Language
PROGRAM:19 W.A.P for use of nesting method.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:19 W.A.P FOR USE OF NESTING METHOD.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class nineteen
{

public void outerMethod()


{
System.out.println("Outer method is called.");

innerMethod();
}

private void innerMethod()


{
System.out.println("Inner method is called.");
}

public static void main(String[] args)


{
nineteen example = new nineteen();
System.out.println("Nested Method:");

example.outerMethod();
}
}
/**************************OUTPUT***********************

Nested Method:
Outer method is
called. Inner method is
called.

\*******************************************************/

Shaikh Armaan 29

SYBCA-4 406- Java Programming Language
PROGRAM:20 W.A.P for example of single inheritance.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:20 W.A.P FOR EXAMPLE OF SINGLE INHERITANCE.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Parent
{
void displayParent()
{
System.out.println("This is the parent class.");
}
}

public class Twenty extends Parent


{
void displayChild()
{
System.out.println("This is the child class.");
}

public static void main(String[] args)


{
Twenty twentyObj = new Twenty();

System.out.println("Single Inheritance:");
twentyObj.displayParent();
twentyObj.displayChild();
}
}
/**************************OUTPUT***********************

Single Inheritance:

This is the parent class.


This is the child class.

*******************************************************/

Shaikh Armaan 30

SYBCA-4 406- Java Programming Language
PROGRAM:21 W.A.P for example of multilevel inheritance.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:21 W.A.P FOR EXAMPLE OF MULTILEVEL INHERITANCE.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/

class Grandparent
{

void displayGrandparent()
{
System.out.println("This is the grandparent class.");
}
}

class Parent extends Grandparent


{

void displayParent()
{
System.out.println("This is the parent class.");
}
}

class Child extends Parent


{

void displayChild()
{
System.out.println("This is the child class.");
}
}

public class twentyone


{
public static void main(String[] args)
{

Child childObj = new Child();

System.out.println("Multilevel Inheritance:");
childObj.displayGrandparent();

Shaikh Armaan 31

SYBCA-4 406- Java Programming Language

childObj.displayParent();

childObj.displayChild();
}
}
/**************************OUTPUT***********************

Multilevel Inheritance:

This is the grandparent


class. This is the parent
class.
This is the child class.

*******************************************************/

Shaikh Armaan 32

SYBCA-4 406- Java Programming Language
PROGRAM:22 W.A.P for method overriding.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:22 W.A.P FOR METHOD OVERRIDING.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/

class Animal
{
void makeSound()
{
System.out.println("Animal makes a sound.");
}
}

class Dog extends Animal


{
void makeSound()
{
super.makeSound();
System.out.println("Dog barks.");
}
}

public class Twentytwo


{
public static void main(String[] args)
{
System.out.println("Method Overloading:");
Dog myDog = new Dog();
myDog.makeSound();
}
}

/**************************OUTPUT***********************

Method Overloading:
Animal makes a
sound. Dog barks.

*******************************************************/

Shaikh Armaan 33

SYBCA-4 406- Java Programming Language

PROGRAM:23 W.A.P for example of abstract classed and methods.


/*********************************************************
NAME: Shaikh Armaan
PROGRAM:23 W.A.P FOR EXAMPLE OF ABSTRACT CLASSED AND
METHODS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/

import java.util.Scanner;

abstract class Shape


{

abstract double calculateArea();

void display()
{
System.out.println("This is a shape.");
}
}

class Circle extends Shape


{
double radius;

Circle(double radius)
{
this.radius = radius;
}

double calculateArea()
{
return Math.PI * radius * radius;
}
}

class Rectangle extends Shape


{
double length;

Shaikh Armaan 34

SYBCA-4 406- Java Programming Language
double width;

Rectangle(double length, double width)


{
this.length = length;
this.width = width;
}

double calculateArea()
{
return length * width;
}
}

public class Twentythree


{
public static void main(String[] args)
{

double a,b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no: ");
a=sc.nextDouble();

System.out.println("Enter no: ");


b=sc.nextDouble();
Circle circle = new Circle(a);
Rectangle rectangle = new Rectangle(a,b);

System.out.println("Abstract method is called:");


System.out.println("Area of Circle: " + circle.calculateArea());
circle.display();

System.out.println("Area of Rectangle: " + rectangle.calculateArea());


rectangle.display();
}
}

Shaikh Armaan 35

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

Enter no:
12.3
Enter no:
13.45

Abstract method is called:


Area of Circle: 475.2915525615999
This is a shape.
Area of Rectangle: 165.435
This is a shape.
*******************************************************/

Shaikh Armaan 36

SYBCA-4 406- Java Programming Language
PROGRAM:24 W.A.P for use of final variable and final methods.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:24 W.A.P FOR USE OF FINAL VARIABLE AND FINAL
METHODS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Twentyfour
{

final static int MY_CONSTANT = 42;

public Twentyfour()
{

final void finalMethod()


{
System.out.println("This is a final method.");
}

public static void main(String[] args)


{

MY_CONSTANT = 33;
Twentyfour example = new Twentyfour();

System.out.println("Final Variable: " + example.MY_CONSTANT);

example.finalMethod();
}
}
/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 37

SYBCA-4 406- Java Programming Language
PROGRAM:25 W.A.P for create and use of interface.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:25 W.A.P FOR CREATE AND USE OF INTERFACE.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
interface Drawable
{
void draw();
}

class Rectangle implements Drawable


{
public void draw()
{
System.out.println("drawing rectangle");
}
}

class Circle implements Drawable


{
public void draw()
{
System.out.println("drawing circle");
}
}

class Twentyfive
{
public static void main(String args[])
{
Drawable d=new Circle();
d.draw();
}
}
/**************************OUTPUT***********************

drawing circle
*******************************************************/

Shaikh Armaan 38

SYBCA-4 406- Java Programming Language
PROGRAM:26 W.A.P for implementing multiple interface.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:26 W.A.P FOR IMPLEMENTING MULTIPLE INTERFACE
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/

interface AnimalEat
{
void eat();
}

interface AnimalTravel
{
void travel();
}

class Animal implements AnimalEat, AnimalTravel


{
public void eat()
{
System.out.println("Animal is eating");
}
public void travel()
{
System.out.println("Animal is travelling");
}
}

public class Twentysix


{
public static void main(String args[])
{
Animal a = new Animal();
System.out.println("Multiple Inheritance:");
a.eat();
a.travel();
}
}
/**************************OUTPUT***********************

Multiple Inheritance:
Animal is eating
Animal is travelling

*******************************************************/

Shaikh Armaan 39

SYBCA-4 406- Java Programming Language
PROGRAM:27 W.A.P for create single dimensional array with 10 element and
print that data in sorted list.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:27 W.A.P FOR CREATE SINGLE DIMENSIONAL ARRAY
WITH 10 ELEMENT AND PRINT THAT DATA IN SORTED LIST.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/

import java.util.*;
class Twentyseven
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();

int[] arr = new int[size];

System.out.println("Enter the elements of the array:");

for (int i = 0; i < size; i++)


{
System.out.print("Enter element at index " + i + ": ");
arr[i] = scanner.nextInt();
}

System.out.println("Entered array:");
for (int i = 0; i < size; i++)
{
System.out.print(arr[i] + " ");
}

Arrays.sort(arr);
System.out.println("\nThe sorted array is: ");
for (int num : arr)
{
System.out.print(num + " ");
}
}

Shaikh Armaan 40

SYBCA-4 406- Java Programming Language
}

/**************************OUTPUT***********************

Enter the size of the array: 3


Enter the elements of the array:
Enter element at index 0: 23
Enter element at index 1: 2
Enter element at index 2: 1
Entered array:
23 2 1
The sorted array is:
1 2 23

*******************************************************/

Shaikh Armaan 41

SYBCA-4 406- Java Programming Language
PROGRAM:28 W.A.P for to create two dimensional matrix and multiply that
matrix.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:28 W.A.P FOR TO CREATE TWO DIMENSIONAL MATRIX
AND MULTIPLY THAT MATRIX.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

public class Matrix


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of rows for matrix A:");


int rowsA = scanner.nextInt();
System.out.println("Enter the number of columns for matrix A:");
int colsA = scanner.nextInt();

System.out.println("Enter the number of rows for matrix B:");


int rowsB = scanner.nextInt();
System.out.println("Enter the number of columns for matrix B:");
int colsB = scanner.nextInt();

if (colsA != rowsB)
{
System.out.println("Matrix multiplication is not possible.");
return;
}

System.out.println("Enter elements for matrix A:");


int[][] matrixA = new int[rowsA][colsA];
for (int i = 0; i < rowsA; i++)
{
for (int j = 0; j < colsA; j++)
{
matrixA[i][j] = scanner.nextInt();
}
}

Shaikh Armaan 42

SYBCA-4 406- Java Programming Language
System.out.println("Enter elements for matrix B:");
int[][] matrixB = new int[rowsB][colsB];
for (int i = 0; i < rowsB; i++)
{
for (int j = 0; j < colsB; j++)
{
matrixB[i][j] = scanner.nextInt();
}
}

int[][] resultMatrix = multiplyMatrices(matrixA, matrixB);

System.out.println("Resultant Matrix:");
for (int i = 0; i < rowsA; i++)
{
for (int j = 0; j < colsB; j++)
{
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}
}

public static int[][] multiplyMatrices(int[][] a, int[][] b)


{
int rowsA = a.length;
int colsA = a[0].length;
int colsB = b[0].length;

int[][] result = new int[rowsA][colsB];

for (int i = 0; i < rowsA; i++)


{
for (int j = 0; j < colsB; j++)
{
for (int k = 0; k < colsA; k++)
{
result[i][j] += a[i][k] * b[k][j];
}
}
}

return result;
}

Shaikh Armaan 43

SYBCA-4 406- Java Programming Language

/**************************OUTPUT*********************** Enter the


number of rows for matrix A:
2
Enter the number of columns for matrix A:
2
Enter the number of rows for matrix B:
2
Enter the number of columns for matrix B:
2
Enter elements for matrix A:
12
13
14
15
Enter elements for matrix B:
16
17
18
19
Resultant Matrix:
426 451
494 523
*******************************************************/

Shaikh Armaan 44

SYBCA-4 406- Java Programming Language
PROGRAM:29 W.A.P TO USE STRING METHOD IN JAVA.[MINIMUM 10
METHOD]
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:28 W.A.P TO USE STRING METHOD IN JAVA.[MINIMUM 10
METHOD]
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

public class Twentynine


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");
String str1 = scanner.nextLine();
System.out.println("Enter another
string:"); String str2 = scanner.nextLine();

System.out.println("Length of first string: " + str1.length());

String concatenatedString = str1.concat(str2);


System.out.println("Concatenated string: " + concatenatedString);

System.out.println("Uppercase of first string: " + str1.toUpperCase());

System.out.println("Lowercase of second string: " + str2.toLowerCase());

System.out.println("Index of 'a' in first string: " + str1.indexOf('a'));

System.out.println("Character at index 2 in second string: " + str2.charAt(2));

System.out.println("Substring of first string from index 2: " + str1.substring(2));

System.out.println("Replacing 'o' with 'x' in second string: " + str2.replace('o', 'x'));

Shaikh Armaan 45

SYBCA-4 406- Java Programming Language

String str3 = " Hello World ";


System.out.println("Trimmed version of string 3: \"" + str3.trim() + "\"");

System.out.println("Does first string start with 'Enter'? " + str1.startsWith("Enter"));

System.out.println("Does second string end with 'string'? " +


str2.endsWith("string"));
}
}
/**************************OUTPUT***********************
Enter a string:
yash
Enter another string:
Varsale

Length of first string: 4

Concatenated string: armaanshaikh

Uppercase of firststring:ARMAAN

Lowercase of second string: shaikh

Index of 'a' in first string: 1

Character at index 2 in second string: r

Substring of first string from index 2: sh

Replacing 'o' with 'x' in second string: shaikh

Trimmed version of string 3: "Hello World"

Does first string start with 'Enter'? false

Does second string end with 'string'? false

*******************************************************/

Shaikh Armaan 46

SYBCA-4 406- Java Programming Language
PROGRAM:30 W.A.P to use of string bufferclass method.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:24 W.A.P TO USE OF STRING BUFFERCLASS METHOD.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Thirty
{
public static void main(String[] args)
{

StringBuffer stringBuffer = new StringBuffer("Hello");

System.out.println("Append Method");
stringBuffer.append(" World");
System.out.println("After appending: " + stringBuffer);
System.out.println();

System.out.println("Insert method");
stringBuffer.insert(5, " Java");
System.out.println("After inserting: " +
stringBuffer);
System.out.println();

System.out.println("Delete method");
stringBuffer.delete(5, 10);
System.out.println("After deleting: " + stringBuffer);
System.out.println();

System.out.println("Reverse method");
stringBuffer.reverse();
System.out.println("After reversing: " +
stringBuffer); System.out.println();

System.out.println("SetCharAt method");
stringBuffer.setCharAt(0, 'h');
System.out.println("After setting character: " + stringBuffer);
System.out.println();

System.out.println("Length method");
System.out.println("Length of StringBuffer: " + stringBuffer.length());
System.out.println();

System.out.println("Capacity method");
System.out.println("Capacity of StringBuffer: " + stringBuffer.capacity());
System.out.println();

Shaikh Armaan 47

SYBCA-4 406- Java Programming Language
System.out.println("IndexOf method");
int index = stringBuffer.indexOf("World");
System.out.println("Index of 'World': " + index);
System.out.println();

System.out.println("Substring method");
String subString = stringBuffer.substring(6);
System.out.println("Substring from index 6: " + subString);
System.out.println();

System.out.println("Replace method");
stringBuffer.replace(0, 1, "H");
System.out.println("After replacing: " + stringBuffer);
System.out.println();
}
}
/**************************OUTPUT***********************

Append Method
After appending: Hello World

Insert method
After inserting: Hello Java World

Delete method
After deleting: Hello World

Reverse method
After reversing: dlroW olleH

SetCharAt method
After setting character: hlroW olleH

Length method
Length of StringBuffer: 11

Capacity method
Capacity of StringBuffer: 21

IndexOf method
Index of 'World': -1

Substring method
Substring from index 6: olleH

Replace method
After replacing: HlroW olleH
*******************************************************/

Shaikh Armaan 48

SYBCA-4 406- Java Programming Language
PROGRAM:31 W.A.P to use of vectors method.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:25 W.A.P TO USE OF VECTORS METHOD.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.*;

public class Thirtyone


{
public static void main(String args[]) {
Scanner scanner = new
Scanner(System.in);

Vector<String> vec = new Vector<>();

System.out.println("Enter animal names (enter 'done' to finish):");


String input = scanner.nextLine();
while (!input.equals("done")) {
vec.add(input);
input = scanner.nextLine();
}

System.out.println("Size and capacity of vector");


System.out.println("Size is: " + vec.size());
System.out.println("Default capacity is: " + vec.capacity());

System.out.println("Vector elements are: " +


vec); System.out.println();

System.out.println("Adding element to
vector"); vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");

System.out.println("Size after addition: " + vec.size());


System.out.println("Capacity after addition is: " + vec.capacity());

System.out.println("Elements are: " +


vec); System.out.println();

System.out.println("Checking element in vector");


if (vec.contains("Tiger"))

Shaikh Armaan 49

SYBCA-4 406- Java Programming Language
{
System.out.println("Tiger is present at the index " + vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}

System.out.println();
System.out.println("The first animal of the vector is = " + vec.firstElement());

System.out.println("The last animal of the vector is = " + vec.lastElement());

}
}

/**************************OUTPUT***********************
Enter animal names (enter 'done' to finish):
lion
done
Size and capacity of vector
Size is: 1
Default capacity is: 10
Vector elements are: [lion]

Adding element to vector


Size after addition: 4
Capacity after addition is: 10
Elements are: [lion, Rat, Cat, Deer]

Checking element in vector


Tiger is not present in the list.

The first animal of the vector is = lion


The last animal of the vector is =
Deer
*******************************************************/

Shaikh Armaan 50

SYBCA-4 406- Java Programming Language
PROGRAM:32 W.A.P to use of wrapper class method.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:32 W.A.P TO USE OF WRAPPER CLASS METHOD.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

public class Thirtytwo


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Integer Wrapper class


Method"); System.out.print("Enter an integer: ");
int numInt = scanner.nextInt();
Integer integerWrapper = Integer.valueOf(numInt); // Autoboxing
System.out.println("Integer Wrapper: " + integerWrapper);
System.out.println("Integer MAX_VALUE: " +
Integer.MAX_VALUE); System.out.println("Integer MIN_VALUE: " +
Integer.MIN_VALUE); System.out.print("Enter another integer for
comparison: ");
int compareInt = scanner.nextInt();
System.out.println("Integer compareTo: "
+
integerWrapper.compareTo(compareInt));
System.out.println();

System.out.println("Double Wrapper class Method");


System.out.print("\nEnter a double: ");
double numDouble = scanner.nextDouble();
Double doubleWrapper = Double.valueOf(numDouble); // Autoboxing
System.out.println("\nDouble Wrapper: " + doubleWrapper);
System.out.println("Double MAX_VALUE: " +
Double.MAX_VALUE); System.out.println("Double MIN_VALUE: " +
Double.MIN_VALUE); System.out.print("Enter another double for
comparison: ");
double compareDouble = scanner.nextDouble();
System.out.println("Double compareTo: " +
doubleWrapper.compareTo(compareDouble));
System.out.println();

System.out.println("Character wrapper class


methods"); System.out.print("\nEnter a character: ");
char charValue = scanner.next().charAt(0);
Character charWrapper = Character.valueOf(charValue); // Autoboxing
System.out.println("\nCharacter Wrapper: " + charWrapper);

Shaikh Armaan 51

SYBCA-4 406- Java Programming Language
System.out.println("Character isLetter: " + Character.isLetter(charWrapper));

Shaikh Armaan 52

SYBCA-4 406- Java Programming Language
System.out.println("Character isDigit: " + Character.isDigit(charWrapper));

System.out.println("Boolean wrapper class methods");


System.out.print("\nEnter a boolean (true/false): ");
boolean boolValue = scanner.nextBoolean();
Boolean boolWrapper = Boolean.valueOf(boolValue); // Autoboxing System.out.println("\
nBoolean Wrapper: " + boolWrapper);

}
}

/**************************OUTPUT***********************
Integer Wrapper class Method
Enter an integer: 2
Integer Wrapper: 2
Integer MAX_VALUE: 2147483647
Integer MIN_VALUE: -2147483648
Enter another integer for comparison:
4 Integer compareTo: -1

Double Wrapper class Method

Enter a double: 23.3

Double Wrapper: 23.3


Double MAX_VALUE: 1.7976931348623157E308
Double MIN_VALUE: 4.9E-324
Enter another double for comparison: 22.2
Double compareTo: 1

Character wrapper class methods

Enter a character: e

Character Wrapper: e
Character isLetter: true
Character isDigit: false
Boolean wrapper class methods

Enter a boolean (true/false): true

Boolean Wrapper: true

*******************************************************/

Shaikh Armaan 53

SYBCA-4 406- Java Programming Language
PROGRAM:33 W.A.P to implement enum data.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:33 W.A.P TO IMPLEMENT ENUM DATA.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

enum WeekDay
{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY, SUNDAY;
}

public class Thirtythree


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a day of the week:");


String userInput = scanner.nextLine().toUpperCase();
try
{
WeekDay day = WeekDay.valueOf(userInput);
System.out.println("You entered: " + day);
}
catch (IllegalArgumentException e)
{
System.out.println("Invalid input! Please enter a valid day of the week.");
}

}
}
/**************************OUTPUT***********************
Enter a day of the week:
tuesday
You entered: TUESDAY
*******************************************************/

Shaikh Armaan 54

SYBCA-4 406- Java Programming Language
PROGRAM:34 W.A.P to use of exception handling.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:34 W.A.P TO USE OF EXCEPTION HANDLING.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

class Thirtyfour
{
public static void main(String args[])
{
int a, b, c;
Scanner sc = new Scanner(System.in);

try
{
System.out.println("Enter the numerator:");
a = sc.nextInt();
System.out.println("Enter the
denominator:"); b = sc.nextInt();

c = a / b;
System.out.println("Division is: " + c);
}
finally
{
sc.close();
System.out.println("Finally block executed.");
}
}
}
/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 55

SYBCA-4 406- Java Programming Language
PROGRAM:35 W.A.P to input command line argument and find out how
many integer arguments there[use try..catch block].
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:35 W.A.P TO INPUT COMMAND LINE ARGUMENT AND
FIND OUT HOW MANY INTEGER ARGUMENTS THERE[USE
TRY..CATCH BLOCK].
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Thirtyfive
{
public static void main(String[] args)
{
try
{
int integerCount = 0;

for (String arg : args)


{

try
{
int num = Integer.parseInt(arg);

integerCount++;
}
catch (NumberFormatException e)
{

System.out.println(e);
}
}

System.out.println("Number of integer arguments: " +


integerCount);
}
catch (Exception e)
{
System.out.println("An error occurred: " + e.getMessage());
}
}
}

Shaikh Armaan 56

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

C:\java>java Thirtyfive 1 2 3 4
Number of integer arguments: 4

*******************************************************/

Shaikh Armaan 57

SYBCA-4 406- Java Programming Language
PROGRAM:36 W.A.P to implement nested try statement.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:36 W.A.P TO IMPLEMENT NESTED TRY STATEMENT.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

public class Thirtysix


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

try
{

System.out.print("Enter the divisor: ");


int divisor = scanner.nextInt();

try
{

System.out.print("Enter the index of the number to divide: ");


int index = scanner.nextInt();

int[] numbers = {1, 2, 3};


System.out.println(numbers[index] / divisor);

}
catch (ArrayIndexOutOfBoundsException e)
{

System.out.println("Array index out of bounds.");


}

}
catch (ArithmeticException e)
{

System.out.println("Attempted to divide by zero.");


}

}
}

Shaikh Armaan 58

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 59

SYBCA-4 406- Java Programming Language
PROGRAM:37 W.A.P to implement multiple catch statements.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:37 W.A.P TO IMPLEMENT MULTIPLE CATCH
STATEMENTS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

public class Thirtyseven


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

try
{
System.out.print("Enter the numerator: ");
int numerator = scanner.nextInt();

System.out.print("Enter the denominator: ");


int denominator = scanner.nextInt();

int result = numerator / denominator;


System.out.println("Result of division: " + result);

int[] numbers = {1, 2, 3};


System.out.print("Enter the index to retrieve from array: ");
int index = scanner.nextInt();
System.out.println("Value at index " + index + ": " +
numbers[index]);
}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException: Attempted to divide by
zero.");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException: Index is out
of bounds.");
}
catch (Exception e)
{
System.out.println("Exception occurred: " + e.getMessage());

Shaikh Armaan 60

SYBCA-4 406- Java Programming Language
}

}
}

/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 61

SYBCA-4 406- Java Programming Language
PROGRAM:38 W.A.P to implement finally block.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:38 W.A.P TO IMPLEMENT FINALLY BLOCK
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

public class Thirtyseven


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

try
{
System.out.print("Enter the numerator: ");
int numerator = scanner.nextInt();

System.out.print("Enter the denominator: ");


int denominator = scanner.nextInt();

int result = numerator / denominator;


System.out.println("Result of division: " + result);

int[] numbers = {1, 2, 3};


System.out.print("Enter the index to retrieve from array: ");
int index = scanner.nextInt();
System.out.println("Value at index " + index + ": " + numbers[index]);
}
catch (Exception e)
{
if (e instanceof ArithmeticException)
{
System.out.println("ArithmeticException: Attempted to divide by zero.");
}
else if (e instanceof ArrayIndexOutOfBoundsException)
{
System.out.println("ArrayIndexOutOfBoundsException: Index is out of bounds.");
}
else
{
System.out.println("Exception occurred: " + e.getMessage());
}
}
finally

Shaikh Armaan 62

SYBCA-4 406- Java Programming Language
{
System.out.println("Finally block executed.");
}
}
}

/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 63

SYBCA-4 406- Java Programming Language
PROGRAM:39 W.A.P to implement throwing your own exception.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:39 W.A.P TO IMPLEMENT THROWING YOUR OWN EXCEPTION.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

class NegativeNumberException extends Exception


{
public NegativeNumberException(String message)
{
super(message);
}
}

public class Thirtyeight


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a positive number: ");


int number = scanner.nextInt();

try
{
if (number < 0)
{
throw new NegativeNumberException("Negative numbers are not allowed!");
}
else
{
System.out.println("You entered: " + number);
}
}
catch (NegativeNumberException e)
{
System.out.println("Error: " + e.getMessage());
}
}
}

Shaikh Armaan 64

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

Enter a positive number:


25
You entered: 25

Enter a positive number:


-14
Error: Negative numbers are not allowed!

*******************************************************/

Shaikh Armaan 65

SYBCA-4 406- Java Programming Language
PROGRAM:40 W.A.P to implement throws keyword.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:40 W.A.P TO IMPLEMENT THROWS KEYWORD.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
import java.util.Scanner;

class NegativeNumberException extends Exception


{
public NegativeNumberException(String message)
{
super(message);
}
}

public class Thirtynine


{

public static void takeInput() throws NegativeNumberException


{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a positive number: ");
int number = scanner.nextInt();

if (number < 0)
{
throw new NegativeNumberException("Negative numbers are not allowed!");
}
els
e
{ System.out.println("You entered: " + number);

}
}

public static void main(String[] args)


{
try
{
takeInput();
}
catch (NegativeNumberException e)
{
System.out.println("Error: " + e.getMessage());
}

Shaikh Armaan 66

SYBCA-4 406- Java Programming Language
}
}

/**************************OUTPUT***********************

Enter a positive number:


-14
Error: Negative numbers are not allowed!

Enter a positive number:


25
You entered: 25

*******************************************************/

Shaikh Armaan 67

SYBCA-4 406- Java Programming Language
PROGRAM:41 W.A.P to create your own package and use that package in other
class..
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:41 W.A.P TO CREATE YOUR OWN PACKAGE AND USE THAT
PACKAGE IN OTHER CLASS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
Package created:

package pak;
public class Fortyone
{
public void add(int a,int b)
{
System.out.println("Addition is:" +(a+b));
}
}

Package Implemented:
import pak.Fortyone;

public class Fortyone_pak


{
public static void main(String[] args)
{
Fortyone f=new Fortyone();
f.add(6,6);
}
}

/**************************OUTPUT***********************
Addition is:12

*******************************************************/

Shaikh Armaan 68

SYBCA-4 406- Java Programming Language
PROGRAM:42 W.A.P to create sub package..
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:42 W.A.P TO CREATE SUB PACKAGE.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
Created Sub Package:
package mypackage.operations;

public class Subtraction {


public int subtract(int a, int b) {
return a - b;
}
}

package mypackage.operations;

public class Addition {


public int add(int a, int b) {
return a + b;
}
}

Pacakge Implemented:
package mypackage;

// Import the classes from the subpackage


import mypackage.operations.Addition;
import mypackage.operations.Subtraction;

public class Calculator {


public static void main(String[] args) {
// Perform addition operation
Addition add = new Addition();
int sum = add.add(5, 3);
System.out.println("Sum: " + sum);

// Perform subtraction operation


Subtraction sub = new
Subtraction(); int difference =
sub.subtract(10, 11);
System.out.println("Difference: " + difference);
}
}

Shaikh Armaan 69

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

Sum: 2
Difference: 1

*******************************************************/

Shaikh Armaan 70

SYBCA-4 406- Java Programming Language
PROGRAM:43 W.A.P to create applet that print your detail.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:43 W.A.P TO CREATE APPLET THAT PRINT YOUR DETAIL.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
HTML FILE:
<!DOCTYPE html>
<html>
<head>
<title>My Details Applet</title>
</head>
<body>
<applet code="MyDetailsApplet.class" width="300" height="150">
<!-- This message is displayed if the applet cannot be loaded -->
<p>Your browser does not support Java applets.</p>
</applet>
</body>
</html>

CODE FILE:
import java.applet.Applet;
import java.awt.*;

public class MyDetailsApplet extends Applet {

public void paint(Graphics g) {


// Set font for the text
g.setFont(new Font("Arial", Font.BOLD, 16));

// Print your details


g.drawString("Name: Armaan Shaikh 50, 50);
g.drawString("Age: 20", 50, 70);
g.drawString("Location: Navsari", 50, 90);
g.drawString("Occupation: Software developer", 50, 110);
}
}

Shaikh Armaan 71

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 72

SYBCA-4 406- Java Programming Language
PROGRAM:44 W.A.P for create applet that uses any seven graphics class methods.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:44 W.A.P FOR CREATE APPLET THAT USES ANY SEVEN
GRAPHICS CLASS METHODS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Graphics Demo Applet</title>
</head>
<body>
<applet code="GraphicsDemo.class" width="600" height="400">
<!-- This message is displayed if the applet cannot be loaded -->
<p>Your browser does not support Java applets.</p>
</applet>
</body>
</html>

CODE:
import java.applet.Applet;
import java.awt.*;

public class GraphicsDemo extends Applet {

public void paint(Graphics g) {


// Set background color
setBackground(Color.WHITE);

// Draw a filled rectangle


g.setColor(Color.BLUE);
g.fillRect(50, 50, 100, 50);

// Draw a filled oval


g.setColor(Color.RED);
g.fillOval(200, 50, 100, 100);

// Draw a line
g.setColor(Color.BLACK);
g.drawLine(50, 200, 250, 200);

// Draw a polygon
int[] xPoints = {400, 450, 500};

Shaikh Armaan 73

SYBCA-4 406- Java Programming Language
int[] yPoints = {200, 150, 200};
g.setColor(Color.GREEN);
g.fillPolygon(xPoints, yPoints, 3);

// Draw a string
g.setColor(Color.BLACK);
g.drawString("Java Applet", 100, 300);

// Draw an arc
g.setColor(Color.ORANGE);
g.fillArc(300, 300, 100, 100, 45, 270);

// Draw a rounded rectangle


g.setColor(Color.MAGENTA);
g.fillRoundRect(450, 300, 100, 50, 20, 20);
}
}

/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 74

SYBCA-4 406- Java Programming Language
PROGRAM:45 W.A.P to create applet that uses param tag.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:46 W.A.P TO CREATE APPLET THAT USES PARAM TAG.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
Code File:
import java.applet.Applet;
import java.awt.Graphics;

public class ParamApplet extends Applet {


private String message;

public void init() {


// Get the parameter values from
HTML message =
getParameter("message");
if (message == null) {
message = "Hello, World!";
}
}

public void paint(Graphics g) {


// Display the message on the applet
window g.drawString(message, 20, 20);
}
}

Html File:
<!DOCTYPE html>
<html>
<head>
<title>Param Applet</title>
</head>
<body>
<applet code="ParamApplet.class" width="300" height="200">
<param name="message" value="Welcome to my applet!">
</applet>
</body>
</html>

Shaikh Armaan 75

SYBCA-4 406- Java Programming Language
/**************************OUTPUT***********************

*******************************************************/

Shaikh Armaan 76

SYBCA-4 406- Java Programming Language
PROGRAM:46 W.A.P to create a throw using thread class.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:46 W.A.P TO CREATE A THROW USING THREAD CLASS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Fortysix extends Thread
{
public void run()
{
System.out.println("Thread is running.");
}

public static void main(String[] args)


{

Fortysix myThread = new Fortysix();

myThread.start();
}
}

/**************************OUTPUT***********************

Thread is running.

*******************************************************/

Shaikh Armaan 77

SYBCA-4 406- Java Programming Language
PROGRAM:47 W.A.P to create a thread using runnable interface.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:47 W.A.P TO CREATE A THREAD USING RUNNABLE INTERFACE.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
public class Fortyseven implements Runnable {
public void run() {
System.out.println("Thread is
running.");
}

public static void main(String[] args) {

Fortyseven myRunnable = new

Fortyseven();

Thread thread = new Thread(myRunnable);

thread.start();
}
}

/**************************OUTPUT***********************

Thread is running.

*******************************************************/

Shaikh Armaan 78

SYBCA-4 406- Java Programming Language
PROGRAM:48 W.A.P to create singly link list with its functions.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:48 W.A.P TO CREATE SINGLY LINK LIST WITH ITS FUNCTIONS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}

class SinglyLinkedList {
private Node head;

SinglyLinkedList() {
this.head = null;
}

void insertAtBeginning(int data) {


Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}

void insertAtEnd(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}

void delete(int data) {

Shaikh Armaan 79

SYBCA-4 406- Java Programming Language
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
return;
}
Node current = head;
while (current.next != null) {
if (current.next.data == data) {
current.next = current.next.next;
return;
}
current = current.next;
}
}

void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}

public class Fortyeight


{
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();

list.insertAtBeginning(15);
list.insertAtEnd(12);
list.insertAtEnd(16);
list.insertAtBeginning(4);

System.out.println("Original list:");
list.display();

list.delete(12);

System.out.println("List after deleting 12:");


list.display();
}
}

Shaikh Armaan 80

SYBCA-4 406- Java Programming Language

/**************************OUTPUT***********************
Original list:
4 15 12 16
List after deleting 10:
4 15 16

*******************************************************/

Shaikh Armaan 81

SYBCA-4 406- Java Programming Language
PROGRAM:49 W.A.P to create circular singly link list with its functions.
/*********************************************************
NAME: Shaikh Armaan
PROGRAM:49 W.A.P TO CREATE CIRCULAR SINGLY LINK LIST WITH ITS
FUNCTIONS.
CLASS: SYBCA
DIV: 4
DATE:
*********************************************************/
class Node_1 {
int data;
Node next;

Node_1(int data) {
this.data = data;
this.next = null;
}
}

class CircularSinglyLinkedList {
private Node head;

CircularSinglyLinkedList() {
this.head = null;
}

void insertAtBeginning(int data) {


Node newNode = new Node(data);
if (head == null) {
newNode.next = newNode;
} else {
Node current = head;
while (current.next != head) {
current = current.next;
}
current.next = newNode;
newNode.next = head;
}
head = newNode;
}

void insertAtEnd(int data) {


Node newNode = new Node(data);
if (head == null) {
newNode.next = newNode;

Shaikh Armaan 82

SYBCA-4 406- Java Programming Language
head = newNode;
} else {
Node current = head;
while (current.next != head) {
current = current.next;
}
current.next = newNode;
newNode.next = head;
}
}

void delete(int data) {


if (head == null) {
return;
}

Node current = head, prev =

null; if (current.data == data) {


while (current.next != head) {
current = current.next;
}
head = head.next;
current.next = head;
return;
}

while (current.next != head && current.data != data) {


prev = current;
current = current.next;
}

if (current.data != data) {
return;
}

prev.next = current.next;
}

void display() {
if (head == null) {
System.out.println("List is
empty."); return;

Shaikh Armaan 83

SYBCA-4 406- Java Programming Language
}

Node current = head;


do {
System.out.print(current.data + " ");
current = current.next;
} while (current != head);
System.out.println();
}
}

public class FortyNine


{
public static void main(String[] args)
{
CircularSinglyLinkedList list = new CircularSinglyLinkedList();

list.insertAtEnd(7);
list.insertAtBeginning(6);
list.insertAtEnd(4);
list.insertAtBeginning(1);

System.out.println("Original list:");
list.display();

list.delete(6);

System.out.println("List after deleting 3:");


list.display();
}
}

/**************************OUTPUT***********************
Original list:
1674
List after deleting 3:
174

*******************************************************/

Shaikh Armaan 84

You might also like