Documente Academic
Documente Profesional
Documente Cultură
PRACTICAL
FILE
Semester II
Section B
INDEX
S.No Practical T. Signature
1 WAP to determine the roots of quadratic equations by taking inputs from the
user.
2 Find roots of a quadratic equation through Command Line Arguments.
3 WAP for showing the concept of method overloading.
4 WAP for showing the concept of constructors overloading.
5 Print Fibonacci series recursively.
6 Program to display tower of Hanoi on the output screen.
7 Program to reverse the digits of number and sum of digits using string
functions.
8 To sort data of employeeson employee name, code, age.
9 Program to fit a linear line on list of observations. Also compute the mean
spuare error of fitting in each case.
10 Implement matrix class, Use methods for addition, subtraction, multiplication
and transpose.
11 Implement a menu driven Statistical calculator for the following
Mean
Harmonic Mean
Geometric Mean
Variance
Covariance
Regression coefficient
12 Program to fit poisson distribution and find the mean square error of fitting.
13 Write a menu driven program for 4 EOQ models using inheritance. Model 1
should be the base class.
14 Write a menu driven program to implement M|M|1 , M|M|C and M|M|
infinity queuing models.
/*
Section : B
*/
//Program 1 : WAP to determine the roots of quadratic equations by taking inputs from the user.
import java.util.Scanner;
double D,r,x1,x2;
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=sc.nextDouble();
D=Math.pow(b,2)-4*a*c;
if (D>0)
r=Math.sqrt(D);
System.out.println("Roots of given equation are " + ((-b+r)/2) + " and " + ((-b-r)/2));
else if (D<0)
r=Math.sqrt(-D);
System.out.println("Roots of given equation are " + (-b/2)+ "+" +(r/2)+"i" + " and " + (-b/2) + (-
r/2)+"i");
else
System.out.println("Roots of given equation are " + (-b/2) + " and " + (-b/2));
}
}
//Output :
/*
Enter value of a :
Enter value of b :
-6
Enter value of c :
*/
Section : B
*/
double D,r,x1,x2;
double a = Integer.parseInt(args[0]);
double b = Integer.parseInt(args[1]);
double c = Integer.parseInt(args[2]);
D=Math.pow(b,2)-4*a*c;
if (D>0)
r=Math.sqrt(D);
System.out.println("Roots of given equation are " + ((-b+r)/2) + " and " + ((r)/2));
else if (D<0)
r=Math.sqrt(-D);
System.out.println("Roots of given equation are " + (-b/2)+ "+" +(r/2)+"i" + " and
" + (-b/2) + (-r/2)+"i");
else
System.out.println("Roots of given equation are " + (-b/2) + " and " + (-b/2));
//Output :
/*
C:\Users\User\Desktop\Sem II>javac java2.java
*/
/*
Section : B */
int area(int a)
return a*a;
double area(double a)
return 3.14*a*a;
return a*b;
double s=(a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
int y=1;
while(y==1)
{
System.out.print("\nEnter : \n1 : Area of Square\n2 : Area of Rectangle\n3 : Area of Triangle\n4 : Area of
Circle\nEnter your choice : ");
switch(sc.nextInt())
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
default :
y=sc.nextInt();
/* Output :
Enter :
1 : Area of Square
2 : Area of Rectangle
3 : Area of Triangle
4 : Area of Circle
Area of Square is : 36
Enter :
1 : Area of Square
2 : Area of Rectangle
3 : Area of Triangle
4 : Area of Circle
Enter :
1 : Area of Square
2 : Area of Rectangle
3 : Area of Triangle
4 : Area of Circle
Enter :
1 : Area of Square
2 : Area of Rectangle
3 : Area of Triangle
4 : Area of Circle
*/
/*
Section : B */
//Program 4 : WAP for showing the concept of constructors overloading.
class StudentData
stuID = 100;
stuAge = 18;
stuID = num1;
stuName = str;
stuAge = num2;
return stuID;
this.stuID = stuID;
}
return stuName;
this.stuName = stuName;
return stuAge;
this.stuAge = stuAge;
public static void main(String args[]) //This object creation would call the default constructor
/*This object creation would call the parameterized constructor StudentData(int, String, int)*/
/*
Output :
*/
/*
Section : B */
//Program 5 :Print Fibonacci series recursively.
import java.util.Scanner;
int n = sc.nextInt();
int a=0,b=1,c=1;
for(int i=1;i<n;i++)
c=a+b;
a=b;
b=c;
System.out.println("\n");
/*
Output :
0 1 1 2 3 5 8 */
/*
import java.util.*;
int n = sc.nextInt();
System.out.print(" ");
System.out.print("||");
System.out.println("");
/*
Output :
**
****
******
*********
*/
/*
Section : B */
//Program 7 :Program to reverse the digits of number and sum of digits using string functions.
import java.util.Scanner;
str = sc.next();
st.append(str);
int sum = 0;
System.out.println("Sum of digits:"+(sum));
/*
Output :
Reverse : 8765432
Sum of digits:35
*/
/*
Section : B */
import java.io.*;
import java.util.*;
class Emp
String name;
int age,code;
name = name1;
code = age1;
age = code1;
int s = emp1.length;
System.out.println();
System.out.println();
return empN1.compareTo(empN2);
};
return (-1);
else if (emp1.age > emp2.age)
return (1);
else
return (0);
};
return (-1);
return (1);
else
return (0);
};
System.out.println ();
System.out.println ("How would you like to Sort the data (\n1 for Name\n2 for Age\n3
for Code)");
int choice;
choice = scanner.nextInt();
if (choice == 1)
Arrays.sort(emp1, Emp.SortName);
}
else if (choice == 2)
Arrays.sort(emp1, Emp.SortAge);
else if (choice == 3)
Arrays.sort(emp1, Emp.SortCode);
return emp1;
System.out.println
("/**********************************************************************/");
System.out.println
("/**********************************************************************/");
System.out.println ();
int choice,choice1;
int n, m = 0;
do
System.out.println ("3.Exit");
choice = scanner.nextInt();
switch (choice)
{
case 1:
System.out.print ("Enter the number of Emps you you wish to enter the data for: ");
n = scanner.nextInt();
m = m + n;
emp = Arrays.copyOf(emp, m );
update(emp, n);
break;
case 2:
display (emp);
System.out.println();
System.out.print("Do you wish to sort the data as well? (yes-1 or no-0) ");
choice1 = scanner.nextInt();
if(choice1==1)
sort(emp);
else
System.out.println();
break;
default:
break;
}}
/*
Output :
/**********************************************************************/
/**********************************************************************/
3.Exit
1 for Name
2 for Age
3 for Code
3.Exit
3.Exit
2
Do you wish to sort the data as well? (yes-1 or no-0) 1
1 for Name
2 for Age
3 for Code)
3.Exit
1 for Name
2 for Age
3 for Code)
3.Exit
Enter the number of Emps you you wish to enter the data for: 2
3.Exit
*/
/*
Section : B */
//Program 9 :Program to fit a linear line on list of observations. Also compute the mean spuare error of
fitting in each case.
import java.io.*;
import java.math.*;
class Linear
double sum=0.0;
for(int i=0;i<n;i++)
sum=sum+x[i];
double mean=sum/n;
return (mean);
double sum=0.0;
for(int i=0;i<n;i++)
sum=sum+x[i];
double mean=sum/n;
return (mean);
for(int i=0;i<n;i++)
sum=sum+(x[i]*x[i]);
var=((sum/n)-(mean(x,n)*mean(x,n)));
return (var);
for(int i=0;i<n;i++)
sum=sum+x[i]*y[i];
double cov=(sum/n)-(mean(x,n)*mean(y,n));
return (cov);
slope=cov(x,y,n)/var(x,n);
intercept=mean(y,n)-slope*mean(x,n);
public void mse(int x[], double y[], double y_est[], int n) throws IOException
slope=cov(x,y,n)/var(x,n);
intercept=mean(y,n)-slope*mean(x,n);
for(int i=0;i<n;i++)
y_est[i]=intercept+slope*x[i];
System.out.println("\n");
for(int i=0;i<n;i++)
{
System.out.println();
System.out.print(x[i]+"\t"+y[i]+"\t"+y_est[i]);
for(int i=0;i<n;i++)
sum=sum+((y[i]-y_est[i])*(y[i]-y_est[i]));
mse=sum/(n-1);
System.out.println("\nmse="+mse);
System.out.println();
class Line
int n,per,x[];
double y[],y_est[];
BufferedReaderbr=new BufferedReader(isr);
String str,ch;
do
str=br.readLine();
n=Integer.parseInt(str);
x=new int[n];
y=new double[n];
y_est=new double[n];
System.out.println("Enter the value of the first period: ");
str=br.readLine();
x[0]=Integer.parseInt(str);
x[0]=Integer.parseInt(str);
str=br.readLine();
per=Integer.parseInt(str);
for(int i = 1;i<n;i++ )
x[i]=x[i-1]+per;
for(int i=0;i<n;i++ )
str=br.readLine();
y[i]=Double.parseDouble(str);
obj.line_eqn(x,y,n);
obj.mse(x,y,y_est,n);
ch = br.readLine();
while(ch.equals("Y")||ch.equals("y"));
/*
Output :
10
Equation of the line to fit: y=ax+b, where x gives trend values and
Y = 10.08+-0.18x
X Y Y^
6 10.0 9.0
11 7.0 8.1
16 8.0 7.2
21 4.0 6.300000000000001
26 7.0 5.4
mse=2.6750000000000003
*/
/*
Section : B */
//Program 10 :Implement matrix class, Use methods for addition, subtraction, multiplication and
transpose.
import java.util.*;
int y = 1;
do
int ch = sc.nextInt();
switch(ch)
case 1:
r1 = sc.nextInt();
System.out.print("\nEnter column size of matrix 1 :");
c1 = sc.nextInt();
r2 = sc.nextInt();
c2 = sc.nextInt();
m1[i][j] = sc.nextInt();
m2[i][j] = sc.nextInt();
System.out.println();
else
break;
case 2:
//SUBTRACTION
r1 = sc.nextInt();
c1 = sc.nextInt();
r2 = sc.nextInt();
c2 = sc.nextInt();
m1[i][j] = sc.nextInt();
m2[i][j] = sc.nextInt();
System.out.print(sub[i][j]+"\t");
System.out.println();
else
break;
case 3:
//MULTIPLICATION
r1 = sc.nextInt();
c1 = sc.nextInt();
System.out.print("\nEnter row size of matrix 2 :");
r2 = sc.nextInt();
c2 = sc.nextInt();
m1[i][j] = sc.nextInt();
m2[i][j] = sc.nextInt();
if ( c1==r2 )
{
for ( j = 0 ; j <Math.max(c1,c2) ; j++ )
System.out.print(mul[i][j]+"\t");
System.out.println();
else
break;
case 4:
r1 = sc.nextInt();
c1 = sc.nextInt();
m1[i][j] = sc.nextInt();
System.out.println("Transpose of matrix:-");
trans[j][i] = m1[i][j];
System.out.print(trans[i][j]+"\t");
System.out.println();
break;
default :
y=sc.nextInt();
while(y==1);
/*
Output :
Enter :
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Transpose
Enter :
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Transpose
234
526
481
121
552
344
3 5 5
10 7 8
7 12 5
Enter :
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Transpose
35
64
28
03
1 -3
6 1
Enter :
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Transpose
325
352
162
Enter the elements of second matrix
634
247
312
37 22 36
34 31 51
24 29 50
Enter :
1 : Addition
2 : Subtraction
3 : Multiplication
4 : Transpose
123
456
789
Transpose of matrix:-
1 4 7
2 5 8
3 6 9
*/
/*
Section : B */
/*
• Mean
• Harmonic Mean
• Geometric Mean
• Variance
• Covariance
• Regression coefficient
*/
import java.util.*;
class java11
int n ,i ;
double mul = 1;
int y = 1;
while(y == 1)
{
int ch = sc.nextInt();
switch (ch)
case 1 :
System.out.println("Enter : \n1 : For Individual Data Series\n2 : For Discrete Data Series\n3 : For
Continuous Data Series\nEnter your choice : ");
switch (ch1)
case 1 :
n = sc.nextInt();
series[i] = sc.nextInt();
break;
case 2 :
n = sc.nextInt();
{
series[i] = sc.nextInt();
freq[i] = sc.nextInt();
break;
case 3 :
n = sc.nextInt();
CI[i] = sc.next();
mp[i] = (0.5)*(Integer.parseInt(CI[i].substring(0,1)) +
Integer.parseInt(CI[i].substring(2)));
freq[i] = sc.nextInt();
}
for(i = 0 ; i< n ; i++)
break;
break;
case 2 :
n = sc.nextInt();
series[i] = sc.nextInt();
break;
case 3 :
n = sc.nextInt();
series[i] = sc.nextInt();
mul = mul*series[i];
case 4 :
n = sc.nextInt();
series[i] = sc.nextInt();
break;
case 5 :
n = sc.nextInt();
series[i] = sc.nextInt();
series1[i] = sc.nextInt();
sum1 = sum1 + series1[i];
break;
case 6 :
n = sc.nextInt();
series[i] = sc.nextInt();
series1[i] = sc.nextInt();
break;
default :
y = sc.nextInt();
/*
Output :
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
1
Enter :
23726
Mean = 4.0
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
Enter :
2691
8216
Mean = 15.75
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
Enter :
5
8
Mean = -10.0
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
2 5 7 9 11
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
2345
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
3687
Variance = 59.75
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
3456
6243
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
3456
6243
Regression Co-Efficient = NaN
Enter :
1 : Mean
2 : Harmonic Mean
3 : Geometrical Mean
4 : Variance
5 : Co-Variance
6 : Regression Coefficient
*/
/*
Section : B */
//Program 12 :Program to fit poisson distribution and find the mean square error of fitting.
import java.io.*;
import java.math.*;
class Calculate
double total;
total =0.0;
double mean;
for(int i=0;i<n;i++)
total = total+x[i];
mean = (sum/total);
return mean;
int temp;
double diff;
double sse= 0.0, error, mse = 0.0;
int p = 1;
p=1;
p = p*j;
poi[i] = (Math.exp(-mean)*(Math.pow(mean,i)))/p;
ef[i] = total*poi[i];
for(int i=0;i<n-1;i++)
temp = (int)(ef[i]);
diff = (ef[i]-temp);
ef[i] = temp+1;
else
ef[i] = temp;
System.out.println(i+"\t\t\t"+x[i]+"\t\t\t"+ef[i]);
System.out.println(n-1+"\t\t\t"+x[n-1]+"\t\t\t"+ef[n-1]);
mse = sse/n;
class Poi
String str,ch;
int n;
double mean;
int x[];
do
System.out.println("/*********************************************/");
System.out.println("/*********************************************/");
System.out.println(" Large no. of occurence of an event\n\t\t\t with
avery low probability of occurence");
str = br.readLine();
n = Integer.parseInt(str);
x = new int[n+1];
str = br.readLine();
x[i] = Integer.parseInt(str);
mean = calc.mean(x,n+1);
calc.fitting(x,n+1,mean);
ch = br.readLine();
while(ch.equals("Yes")||ch.equals("yes"));
System.out.println(" bbye");
/*
Output :
/*******************************************************/
/*******************************************************/
0 4 2.0
1 3 5.0
2 4 6.0
3 6 5.0
4 2 3.0
5 5 3.0
Type Yes or No
No
*/
/*
Section : B */
//Program 13 :Write a menu driven program for 4 EOQ models using inheritance. Model 1 should be the
base class.
import java.util.*;
import java.io.*;
import java.lang.*;
int choice;
String ch;
do
System.out.println("\n\t\t\tInventory System\n");
choice=scanner.nextInt();
switch(choice)
case 1:
o1.input();
o1.calq();
o1.display();
break;
case 2:
o2.input();
o2.input2();
o2.calepq();
o2.display();
break;
case 3:
o3.input();
o3.input3();
o3.caleoqs();
o3.display();
break;
case 4:
o4.input();
o4.input4();
o4.calepqs();
o4.display();
break;
default:
ch=scanner1.nextLine();
}
while(ch.equals("Yes")||ch.equals("yes"));
class Eoq
a=in.nextDouble();
ic=in.nextDouble();
l=in.nextDouble();
q=(2*a*l)/(ic);
q=Math.sqrt(q);
{
Scanner in2 = new Scanner(System.in);
double s;
s=in2.nextDouble();
q=((2*a*l/ic)*(s/(s-l)));
q=Math.sqrt(q);
double sc;
sc=in3.nextDouble();
q=((2*a*l/ic)*((ic+sc)/sc));
q=Math.sqrt(q);
double sc,s;
s=in4.nextDouble();
sc=in4.nextDouble();
q=((2*a*l/ic)*(s/(s-l))*((ic+sc)/sc));
q=Math.sqrt(q);
/*
Output :
Inventory System
Select a command:
1.EOQ
2.EPQ
1
Enter Ordering cost per order:
500
1000
Type Yes or No
yes
Inventory System
Select a command:
1.EOQ
2.EPQ
100
5000
Type Yes or No
yes
Inventory System
Select a command:
1.EOQ
2.EPQ
1000
10000
Type Yes or No
yes
Inventory System
Select a command:
1.EOQ
2.EPQ
90
780
Type Yes or No
No
*/
/*
Section : B */
//Program 14 :Write a menu driven program to implement M|M|1 , M|M|C and M|M|infinity queuing
models
import java.io.*;
import java.util.*;
class design
float po,pn,r,l,lq,w,wq,t,c,r1=0,r2=0;
int n;
design()
n=0;
void input()
n=scanner.nextInt();
float fact(int x)
if(x<=1)
return (1);
else
return (x*(fact(x-1)));
{
r=a/s;
System.out.println("r="+r);
po=1-r;
System.out.println("system is idle:"+po);
pn=(float) (po*Math.pow(r,n));
System.out.println("\nqueuelength:="+pn);
else
System.out.println("cannot compute:");
System.out.println("measure of effectivess:");
l= r/(1-r);
lq=(float) (Math.pow(r,2)/(1-r));
w=r/(a*(1-r));
wq=(float) (Math.pow(r,2)/(a*(1-r)));
System.out.println("\nl="+l+"\nlq="+lq+"\nw="+w+"\nwq="+wq);
c=g.nextInt();
r=a/s;
System.out.println("r="+r);
t=r/c;
System.out.println("cu="+t);
if(t<1)
for(int i=0;i<=c-1;i++)
r1=(float)(r1+Math.pow(r,i)/fact(i));
System.out.println("r1="+r1);
r2=(float)(Math.pow(r,c)/(fact((int) c)*(1-(r/c))));
System.out.println("r2="+r2);
po=1/(r1+r2);
System.out.println("System is idle="+po);
if(n<c)
else if(n>=c)
else
System.out.println("cannot compute:");
System.out.println("queue length="+pn);
System.out.println("measure of effectivness:");
System.out.println("\nl="+l+"\nlq="+lq+"\nw="+w+"\nwq="+wq);
r=a/s;
System.out.println("\nr="+r);
if(n>=0)
po=(float) (Math.exp(-r));
System.out.println("\nSystem is idle="+po);
System.out.println("\nqueue length="+pn);
System.out.println("measure of effectivness:");
l=a/s;
lq=l-r;
w=1/s;
wq=lq/a;
System.out.println("\nl="+l+"\nlq="+lq+"\nw="+w+"\nwq="+wq);
class Queue
design d;
d = new design();
int ch;
String check;
float a,s;
a=k.nextFloat();
s=k.nextFloat();
d.input();
do
System.out.println("menu::::::::::");
ch= in.nextInt();
switch(ch)
case 1:
d.MM1(a,s);
break;
case 2:
d.MMC(a,s);
break;
}
case 3:
d.MM(a,s);
break;
default:
check=ins.nextLine();
while(check.equals("yes")||check.equals("Yes"));
/*
Output :
.5
.6
menu
r=0.8333333
System is idle=0.4345982
queue length=2.0214728E-4
measure of effectivness:
l=0.8333333
lq=0.0
w=1.6666666
wq=0.0
do u want to continue(yes/no)
yes
menu
r=0.8333333
system is idle:0.16666669
queue length:=0.055816326
measure of effectivess:
l=4.9999995
lq=4.166666
w=9.999999
wq=8.333332
do u want to continue(yes/no)
yes
menu
r=0.8333333
cu=0.27777776
r1=2.1805553
r2=0.13354701
System is idle=0.432133
queue length=8.933361E-4
measure of effectivness:
l=0.8555295
lq=0.022196177
w=1.711059
wq=0.04439236
do u want to continue(yes/no)
no
*/