0% found this document useful (0 votes)
3 views43 pages

Java Programming Experiments Overview

The document contains a series of Java programming experiments conducted by Daksh Mathur, each with a specific aim and corresponding code. The experiments cover various programming concepts such as printing output, calculating travel time, working with arrays, object-oriented programming, method overloading, and thread creation. Each experiment is followed by the output and includes the student's name and roll number.

Uploaded by

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

Java Programming Experiments Overview

The document contains a series of Java programming experiments conducted by Daksh Mathur, each with a specific aim and corresponding code. The experiments cover various programming concepts such as printing output, calculating travel time, working with arrays, object-oriented programming, method overloading, and thread creation. Each experiment is followed by the output and includes the student's name and roll number.

Uploaded by

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

Experiment-1

Aim:WAP to print Hello World.

Code:-

public class HelloWorld{


public static void main(String[] args) {
[Link]("Helllo World");
}
}

output-

Name: Daksh Mathur


Roll no: 05614802713
1
Experiment-2
Aim: WAP to calculate the time to travel from New York to Los Angeles at 75miles an hour
and distance between the two cities is 3000 miles.

Code:-

public class Nytola{


public static int distance(){
return 3000/75;
}
public static void main(String[] args) {
[Link]("The time for travel between
NY to LA is "+ distance() +"hrs");
}
}

output-

Name: Daksh Mathur


Roll no: 05614802713
2
Experiment-3

Aim: WAP to initialize a four element array and display the average of its values.
Code:-

import [Link].*;
public class ArrayAvg{
public static int init(int input[]){
Scanner s=new Scanner([Link]);
[Link]("Enter the four numbers");
for(int i=0;i<4;i++)
input[i]=[Link]();
[Link]();
return (input[0]+input[1]+input[2]+input[3])/4;
}
public static void main(String[] args) {
int input[]=new int[4];
int avg=init(input);
[Link]("The elements are");
for(int i=0;i<4;i++)
[Link](input[i]+" ");
[Link]();
[Link]("The average is:"+avg);

}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
3
Experiment-4

Aim: WAP to create a 2d array with int values containing first element as array containing
[Link] second element as array containing 500 and [Link] third element containing 39,45
and [Link] this array and its length.
Code:-
import [Link].*;
public class TwoDimensional{
public static void main(String[] args) {
int input[][]={
{32},{500,300},{39,45,600}
};
[Link]("The length is"+[Link]);
for (int i=0;i<[Link];i++ ){
for(int j=0;j<input[i].length;j++)
[Link](input[i][j]+" ");

}
}
}

output-

Name: Daksh Mathur


Roll no: 05614802713
4
Experiment-5

Aim:WAP to show swapping of two values in java use object reference.


Code:- public class Swapor {
int x,y,z;
static Swapor a,b;
public static void swap()
{ Swapor temp = new Swapor();
temp = a;
a=b;
b = temp;
}
public static void main(String[] args) {
a = new Swapor();
a.x=10; a.y=11; a.z=12;
b = new Swapor();
b.x=20; b.y=21; b.z=22;
[Link]("Values of object a :" + a.x + "\t" + a.y + "\t" + a.z);
[Link]("The values in object b :" + b.x + "\t" + b.y + "\t" + b.z);

swap();
[Link]("Values of object a :" + a.x + "\t" + a.y + "\t" + a.z);
[Link]("The values in object b :" + b.x + "\t" + b.y + "\t" + b.z);
}
}
Output-

Name: Daksh Mathur


Roll no: 05614802713
5
Experiment-6

Aim:WAP to show multiplication of double values received as command line arguments.


Code:-
import [Link];
import [Link];
import [Link];
import [Link].*;
public class DoubleProduct{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
String s1 = [Link]();
String s2 = [Link]();
double d1=[Link](s1);
double d2=[Link](s2);
[Link](d1*d2);

}
}output-

Name: Daksh Mathur


Roll no: 05614802713
6
Experiment-8

Aim:WAP to calculate area of circle from radius received as command line arguments.
Code:- import [Link].*;
import [Link];
import [Link];
import [Link];

public class AreaCircle{


private static final double pi=3.14;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
String s = [Link]();
double radius=[Link](s);
[Link](pi*radius*radius);
}
}
output-

Name: Daksh Mathur


Roll no: 05614802713
7
Experiment-9

Aim:WAP to make a class person having instance variables to record name,age and salary. Set
and display these instance variables.

Code:-
public class Person{
private string name;
private int age;
private int salary;
public person(String name , int age , int salary){
[Link]=name;
[Link]=age;
[Link]=salary;
}
public void showData( ){
[Link](“ name = “+ name+”\n Age = ”+ age +” salary = “+ salary );
}
public static void main(String []args){
Person Daksh=new Person(“Daksh”,21,1000);
[Link]();
}
}
output-

Name: Daksh Mathur


Roll no: 05614802713
8
Experiment-10

Aim: WAP to use length property of strings to display any number of command line arguments

Code:-
public class Length {

public static void lengthofstring(String[] s){


for(int i=0;i<[Link];i++)
{
[Link]("args [" + i + "] :" + s[i]);
}

public static void main(String[] args) {

lengthofstring(args);

output-

Name: Daksh Mathur


Roll no: 05614802713
9
Experiment-11

Aim:WAP to create a circle having radius and center as the data members, initialize the data
member and display them.
Code:-
public class Circle {
int x;
int y;
int r;

public static void main(String[] args) {


Circle c = new Circle();
c.x =3;
c.y = 4;
c.r = 7;

[Link]("The given circle C has the following properties : \n" + "The center is at :
["+c.x+","+c.y+"]"+" "+"The radius is of length :"+c.r);

output-

Name: Daksh Mathur


Roll no: 05614802713
10
Experiment-12

Aim:Modify experiment-11 to have constructor to initialize the class variables.


Code:-

public class Circle2 {


static int x;
static int y;
static int r;

public static void main(String[] args) {


Circle2 c = new Circle2(3,4,5);

[Link]("The given circle C has the following properties : \n" + "The center is at :
["+c.x+","+c.y+"]"+" "+"The radius is of length :"+c.r);

private Circle2(int i, int j, int k) {


x=i; y=j; r=k;

}
}
output-

Name: Daksh Mathur


Roll no: 05614802713
11
Experiment-13

Aim:Modify experiment-12 to show constructor overloading.


Code:-

public class Circle3 {


static int x;
static int y;
static int r;

public static void main(String[] args) {


Circle3 c = new Circle3(3);

[Link]("The given circle C has the following properties : \n" + "The center is at :
["+c.x+","+c.y+"]"+" "+"The radius is of length :"+c.r);

private Circle3(int x, int y, int r) {


this.x=x; this.y=y; this.r=r;

private Circle3(int r){ //Circle having coordinates (r,r) with radius r.


this.x=r; this.y= r; this.r = r;
}

}output-

Name: Daksh Mathur


Roll no: 05614802713
12
Experiment-14

Aim:WAP to show the use of this keyword.


Code:-

public class Circle3 {


static int x;
static int y;
static int r;

public static void main(String[] args) {


Circle3 c = new Circle3(3);

[Link]("The given circle C has the following properties : \n" + "The center is at :
["+c.x+","+c.y+"]"+" "+"The radius is of length :"+c.r);
}
private Circle3(int x, int y, int r) { //this keyword has been used.
this.x=x; this.y=y; this.r=r;
}
private Circle3(int r){ //Circle having coordinates (r,r) with radius r.
this.x=r; this.y= r; this.r = r;
}
}
output-

Name: Daksh Mathur


Roll no: 05614802713
13
Experiment-15

Aim:WAP to count number of instances of a class.


Code:-
public class Instance {
static int count=0;
public Instance() { //Constructor for the class Instance.
count++;
[Link]("The no. of instances created :"+ count);
}
public static void main(String[] args) {
Instance i1 = new Instance();
Instance i2 = new Instance();
Instance i3 = new Instance();
Instance i4 = new Instance();
}
}

output:-

Name: Daksh Mathur


Roll no: 05614802713
14
Experiment-16

Aim:WAP to show method overloading .


Code:-
public class MethodOverloading {
final static double pi = 3.14;
public static int area(int x) //square
{
return(x*x);
}
public static int area(int a,int b) //rectangle
{
return(a*b);

}
public static double area(double r) //Circle
{
return(r*pi*r);
}
public static void main(String[] args) {

int square = area(5);


int rectangle = area(6,8);
double circle = area(4.5);

[Link]("Area of square of length 5 units is :"+ square + "\nArea of rectangle of


length 8 units and width of 6 units is :" + rectangle + "\nArea of circle of radius 4.5 units is :" +
circle);
}

}output-

Name: Daksh Mathur


Roll no: 05614802713
15
Experiment-17

Aim:WAP to show passing object as parameter .


Code:-
public class Objectasaparam {
class Complex{
public int real;
public int img;
Complex(int a , int b){
real = a;
img = b;
}
}
public static Complex multComplex(Complex c1 , Complex c2){
return new Objectasaparam().new Complex(([Link]*[Link])-([Link]*[Link]) ,
([Link]*[Link])+([Link]*[Link]));
}
public static void main(String[] args) {
Complex c1 = new Objectasaparam().new Complex(5,4);
Complex c2 = new Objectasaparam().new Complex(6,7);
Complex c3 = multComplex(c1,c2);
[Link]("The mult. of 5+ 4i and 6+ 7i = "+ [Link]+" + "+[Link]+"i");
}
}output-

Name: Daksh Mathur


Roll no: 05614802713
16
Experiment-18

Aim:WAP to show method overriding .


Code:- public class Bank{
void showIdentity(){
[Link]("This is a Bank");
}
public static void main(String[] args)
{
Bank b = new Bank();
[Link]();
Person p = new Person();
[Link]();
}
}
/*
Person is the class inheriting bank class.
*/
class Person extends Bank{
@Override
public void showIdentity(){
[Link]("I am a Person");
}

}
output-

Name: Daksh Mathur


Roll no: 05614802713
17
Experiment-19

Aim:WAP to show that the value of non-static variable is not visible to all the instances and
therefore can't be used to count the number of instances of a class.
Code:-

public class InstanceNS {


int count=0;
public InstanceNS() { //Constructor for the class Instance.
count++;
[Link]("The no. of instances created :"+ count);
}
public static void main(String[] args) {
InstanceNS i1 = new InstanceNS();
InstanceNS i2 = new InstanceNS();
InstanceNS i3 = new InstanceNS();
InstanceNS i4 = new InstanceNS();
}
}

output-

Name: Daksh Mathur


Roll no: 05614802713
18
Experiment-20

Aim:WAP to show simple inheritance.


Code:-
public class Bank1{
public String bank_name="State Bank";
void showIdentity(){
[Link]("This is a Bank");
}
public static void main(String[] args) {

Person p = new Person("Daksh");


[Link]();
[Link]("The Account Balance of "+ [Link]+" =
"+[Link]+" in bank "+p.bank_name);
}
}
class Person extends Bank1{
public int accountBalance=1000;
public String name;
public Person(String name){
[Link]=name;
}
@Override
public void showIdentity(){
[Link]("I am a Person");
}

}
output-

Name: Daksh Mathur


Roll no: 05614802713
19
Experiment 21

Aim : WAP to demonstrate a nested try catch.

Code:
import [Link];
public class Nestedtrycatch {
public static void main(String[] args) {
int x,y,r;
[Link]("insert two numbers x and y");
Scanner a = new Scanner([Link]);
x = [Link]();
y = [Link]();
try{
try{
r= x/y;
[Link]("the vaule of r is :" + r);
}
catch(ArithmeticException ex)
{ [Link]("The value of r is not defined, reinput values of x and y :");
[Link]("insert two numbers x and y");
Scanner b = new Scanner([Link]);
x = [Link]();
y = [Link]();
r=x/y;
[Link]("the vaule of r is :" + r);
}
}
catch(ArithmeticException ex)
{
[Link]("The value of r is not defined, reinput values of x and y :");
}
}
}

Name: Daksh Mathur


Roll no: 05614802713
20
Name: Daksh Mathur
Roll no: 05614802713
21
Experiment 22
Aim: WAP to create a thread using a subclass and interface.
import [Link];
import [Link];
import [Link];
class t1 extends Thread
{
@Override
public void run()
{
[Link]("This is thread t1");
}
}

class t2 implements Runnable


{
@Override
public void run()
{
[Link]("this is thread 2");
}
}
public class Threads {
public static void main(String[] args)
{
t1 T1 = new t1();
[Link]();

try {
[Link](1000);
for(int i=1;i<6;i++)
{
[Link](i + "sec");
[Link](1000);
}
} catch (InterruptedException ex){

}
t2 T2 = new t2();
Thread t = new Thread(T2);
[Link]();
}
}

Name: Daksh Mathur


Roll no: 05614802713
22
Name: Daksh Mathur
Roll no: 05614802713
23
EXPERIMENT 23

AIM: WAP in Java to implement Super keyboard by showing all its uses.

Code:
packagelabjava;
class Vehicle{
int speed;
Vehicle(int x){
speed = x;}
voiddispSpeed() {
[Link]("Speed of the Vehicle is "+speed);}
}
class Car extends Vehicle{
int speed;
Car(int x) {
super(x); //invokes parent's constructor
speed = [Link] + 20;
}
voiddispSpeed() {
[Link]("Speed of the Car is "+speed);
}
void display() {
[Link](); //invokes parent class function
dispSpeed(); //invokes derived class function
}
}
publicclass Practical29 {
publicstaticvoid main(String args[]) {
Car a = newCar(60);
[Link]();
}
}
OUTPUT:

Name: Daksh Mathur


Roll no: 05614802713
24
EXPERIMENT 24
AIM: WAP in Java to create a custom exception class and throw the exception.
CODE:
[Link];
[Link];

classCustomException extends Exception{


CustomException(String s){
super(s);
}
}

public class ThrowAndThrows {

static void method(int x) throws CustomException{


if(x<0)
throw new CustomException("Negative Number Entered");
else
[Link]("Positive Number Entered");

public static void main(String[] args){


Scanner scanner = new Scanner([Link]);
[Link]("Enter Number");
int x = [Link]();
try {
method(x);
} catch (CustomException e) {
// TODO Auto-generated catch block
[Link]();
}
}
}

Name: Daksh Mathur


Roll no: 05614802713
25
OUTPUT:

Experiment 25

AIM: Write a program to calculate factorial of the number entered by the user.
Source Code:
import [Link];
class Exp11
{
public static void main(String[] args)
{
int n,i,fac;
Scanner scn = new Scanner([Link]);
[Link]("Calculate Factorial of a number\n");
[Link]("Enter the number");
n = [Link]();
if(n<0)
{
[Link]("Invalid Input!");
}
else
{
fac = 1;
for(i=n;i>1;i--)
{
fac = fac * i;

Name: Daksh Mathur


Roll no: 05614802713
26
}
[Link]("Factorial of "+n+" is "+fac);
}
}
}

Output:

Experiment 26
AIM: Write a program to compute the decimal of a binary number.
Source Code:
import [Link];
class Exp12
{
public static void main(String[] args)
{
int dec = 0,i,val,n;
String binary;
[Link]("\nBinary to Decimal Conversion");
[Link]("Enter the binary number:");
Scanner scn = new Scanner([Link]);
binary = [Link]();
n= [Link]();
for(i=n-1;i>=0;i--)
{
val = [Link]([Link](i)) - 48;
if(val == 0)
{
dec = dec;
}

Name: Daksh Mathur


Roll no: 05614802713
27
else if(val == 1)
{
dec = dec + (int)[Link](2,n-i-1);
}
else
{
[Link]("Invalid Input!");
[Link](0);
}
}
[Link]("Decimal value of "+binary+" is "+dec);
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
28
Experiment 27
AIM: Write a program to implement stack.
Source Code:
import [Link];
class Exp13
{
public static void main(String[] args)
{
Stack s = new Stack();
int choice;
do
{
[Link]("1. PUSH");
[Link]("2. POP");
[Link]("3. PRINT STACK");
[Link]("4. EXIT");
Scanner scn = new Scanner([Link]);
choice = [Link]();
switch(choice)
{
case 1: [Link]("Enter the number: ");
int i = [Link]();
[Link](i);
break;
case 2: [Link]();
break;
case 3: [Link]();
break;
case 4: [Link](0);
break;
default: [Link]("Invalid Input");
}
}while(choice != 4);
}
}

class Stack
{
int[] arr = new int[3];
int top=-1;

void push(int i)

Name: Daksh Mathur


Roll no: 05614802713
29
{
if(top==2)
{
[Link]("Stack Overflow");
}
else
{
top++;
arr[top] = i;
}
}
void pop()
{
int i;
if(top == -1)
{
[Link]("Stack Underflow");
}
else
{
[Link]("POPed item "+arr[top]);
top--;
}
}
void printStack()
{
[Link]("Stack is: ");
for(int i=0;i<=top;i++)
{
[Link](arr[i]+" ");
}
[Link]("");
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
30
Name: Daksh Mathur
Roll no: 05614802713
31
Experiment 28
AIM: Write a program to implement Queue.
Source Code:
import [Link];
class Exp14
{
public static void main(String[] args)
{
Queue q = new Queue();
int choice;
do
{
[Link]("1. PUSH");
[Link]("2. POP");
[Link]("3. PRINT QUEUE");
[Link]("4. EXIT");
Scanner scn = new Scanner([Link]);
choice = [Link]();
switch(choice)
{
case 1: [Link]("Enter the number: ");
int i = [Link]();
[Link](i);
break;
case 2: [Link]();
break;
case 3: [Link]();
break;
case 4: [Link](0);
break;
default: [Link]("Invalid Input");
}
}while(choice != 4);
}
}

class Queue
{
int[] arr = new int[3];
int front=-1,rear=-1;

void push(int i)

Name: Daksh Mathur


Roll no: 05614802713
32
{
if(rear==2)
{
[Link]("Overflow");
}
else if(front == -1)
{
rear++;
front++;
arr[rear] = i;
}
else
{
rear++;
arr[rear] = i;
}
}
void pop()
{
int i;
if(front == -1 || front > rear)
{
[Link]("Underflow");
}
else
{
[Link]("POPed item "+arr[front]);
front++;
}
}
void printQueue()
{
[Link]("Queue is: ");
for(int i=front;i<=rear;i++)
{
[Link](arr[i]+" ");
}
[Link]("");
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
33
Name: Daksh Mathur
Roll no: 05614802713
34
Experiment 29
AIM: Write a program to print same character on each line and getting the number of character
incremented with each line.
Source Code:
import [Link];
class Exp16
{
public static void main(String[] args)
{
String a;
int n;
Scanner scn = new Scanner([Link]);
[Link]("Enter a character:");
a = [Link]();
[Link]("Enter the length of pattern:");
n = [Link]();
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=i;j++)
{
[Link](a);
}
[Link]("\n");
}
}
}
Ouput:

Name: Daksh Mathur


Roll no: 05614802713
35
Experiment 30
AIM: Write a program to print same character in each line and having the characters printed in ‘V’
shape.
Source Code:
import [Link];
class Exp17
{public static void main(String[] args)
{
int n,i,j,k,var;
Scanner scn = new Scanner([Link]);
[Link]("Enter height of V:");
n = [Link]()-1;
var = (2*n)-1;
for(i=0;i<=n;i++)
{
for(j=0;j<i;j++)
{
[Link](" ");
}
[Link]("*");
for(k=var;k>0;k--)
{
[Link](" ");
}
if(i!=n)
{
[Link]("*");
}
var=var-2;
}
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
36
Experiment 31
AIM: Write a program to print the sum of series 2-4+6-8+10-.......-100.
Source Code:
class Exp18
{
public static void main(String[] args)
{
int sum=0;
for(int i=1;i<=50;i++)
{
if(i%2 == 0)
{
sum = sum - (2*i);
}
else
{
sum = sum + (2*i);
}
}
[Link]("Sum of series 2-4+6-8+....-100 is: "+sum);
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
37
Experiment 32
AIM: Write a program to accept a number from the user and check if it is a palindrome or not.
CODE:
package palindromes;

import [Link];
import [Link];
import [Link];
public class Palindromes {
public static String reverse(String s)
{ int i,len= [Link]();
String rev = "";
for(i=len-1;i>=0;i--)
{
rev = rev + [Link](i);
}
return rev;
}
public static void main(String[] args) throws IOException {
boolean y;
[Link]("Enter a string");
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
String s = [Link]();
String s1 = reverse(s);
y = [Link](s1);
if (y)
{
[Link]("The string is a palindrome");
}
else{
[Link]("The entered string is not a palindrome");
}
}

Name: Daksh Mathur


Roll no: 05614802713
38
}

Experiment 33
AIM: Write a program to print the sum of series x/2+x/5+x/8+...+x/100.
Source Code:
import [Link];
class Exp20
{
public static void main(String[] args)
{
int x,i=2;
float sum=0;
Scanner scn = new Scanner([Link]);
[Link]("Series: x/2 + x/5 + x/8 +....+ x/100");
[Link]("Enter value of x: ");
x = [Link]();
while(i<=100)
{
sum = sum + (float)x/i;
i = i+3;
}
[Link]("Sum = "+sum);
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
39
Experiment 34
AIM: Write a program to remove duplicate numbers from the given list.
Source Code:
[Link].*;

class Exp21{
static Scanner ui=new Scanner([Link]);
public static void main(String[] args)
{
int[] a=new int[200];
intn,i,key,j,k;
[Link]("Enter the number of elements in list");
n=[Link]();
[Link]("Enter the elements of list");
for(i=0;i<n;i++)
a[i]=[Link]();
for(i=0;i<n;i++)
{
key=a[i];
for(j=i+1;j<n;j++)
{
if(a[j]==key)
{
for(k=j+1;k<n;k++)
{
a[k-1]=a[k];
}
j--;
n-=1;
}
}
}
[Link]("List is :");

Name: Daksh Mathur


Roll no: 05614802713
40
for(i=0;i<n;i++)
[Link](a[i]+" ");
}
}
Output:

Name: Daksh Mathur


Roll no: 05614802713
41
Experiment 36
AIM: Write a program to find the square of given matrix.
Source Code:
import [Link].*;
class Exp22{
static Scanner ui=new Scanner([Link]);
public static void main(String[] args)
{
int c,i,k,j;
int[][] a=new int[200][200];
int[][] b=new int[200][200];
[Link]("Enter no. of rows and columns in square matrix");
c=[Link]();
System .[Link]("Enter the elements of matrix rowwise");
for(i=0;i<c;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=[Link]();
}
}
for(i=0;i<c;i++)
{
for(j=0;j<c;j++)
{
b[i][j]=0;
for(k=0;k<c;k++)
{
b[i][j]+=a[i][k]*a[k][j];
}
}
}
[Link]("Square of array is :");

Name: Daksh Mathur


Roll no: 05614802713
42
for(i=0;i<c;i++)
{
for(j=0;j<c;j++)
{
[Link](b[i][j]);
}
}
}
}

Output:

Name: Daksh Mathur


Roll no: 05614802713
43

You might also like