You are on page 1of 78

JAVA

PRACTICAL
FILE

Submitted By :Unnati Srivastava


Indraprastha College for Women

M.Sc. Operational Research

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.
/*

Name :Unnati Srivastava

College : Indraprastha College for Women

Section : B

*/

//Program 1 : WAP to determine the roots of quadratic equations by taking inputs from the user.
import java.util.Scanner;

public class Java1

public static void main(String args[])

double D,r,x1,x2;

Scanner sc = new Scanner(System.in);

System.out.println("Enter value of a : ");

double a=sc.nextDouble();

System.out.println("Enter value of b : ");

double b=sc.nextDouble();

System.out.println("Enter value of c : ");

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 :

Roots of given equation are 3.0 and 3.0

*/

/* Name :Unnati Srivastava

College : Indraprastha College for Women

Section : B

*/

//Program 2 :Find roots of a quadratic equation through Command Line Arguments.


import java.util.Scanner;

public class java2

public static void main(String args[])

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

C:\Users\User\Desktop\Sem II>java java2 2 6 4

Roots of given equation are -2.0 and -4.0

*/

/*

Name :Unnati Srivastava

College : Indraprastha College for Women

Section : B */

//Program 3 : WAP for showing the concept of method overloading.


import java.util.*;

public class java3

int area(int a)

return a*a;

double area(double a)

return 3.14*a*a;

double area(double a, double b)

return a*b;

double area(double a, double b, double c)

double s=(a+b+c)/2;

return Math.sqrt(s*(s-a)*(s-b)*(s-c));

public static void main(String args[])

java3 obj = new java3();

Scanner sc = new Scanner(System.in);

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 :

System.out.print("\nEnter side of Square : ");

System.out.println("\nArea of Square is : " + obj.area(sc.nextInt()));

break;

case 2 :

System.out.print("\nEnter sides of Rectangle : ");

System.out.println("\nArea of Rectangle is : " + obj.area(sc.nextDouble(), sc.nextDouble()));

break;

case 3 :

System.out.print("\nEnter sides of Triangle : ");

System.out.println("\nArea of Triangle is : " + obj.area(sc.nextDouble(), sc.nextDouble(),


sc.nextDouble()));

break;

case 4 :

System.out.print("\nEnter Radius of Circle : ");

System.out.println("\nArea of Circle is : " + obj.area(sc.nextDouble()));

break;

default :

System.out.println("\nEnter only 1, 2, 3 or 4 ");


}

System.out.print("\nIf you want to continue then press 1 : ");

y=sc.nextInt();

/* Output :

Enter :

1 : Area of Square

2 : Area of Rectangle

3 : Area of Triangle

4 : Area of Circle

Enter your choice : 1

Enter side of Square : 6

Area of Square is : 36

If you want to continue then press 1 : 1

Enter :

1 : Area of Square

2 : Area of Rectangle

3 : Area of Triangle

4 : Area of Circle

Enter your choice : 2

Enter sides of Rectangle : 7 4

Area of Rectangle is : 28.0

If you want to continue then press 1 : 1

Enter :

1 : Area of Square
2 : Area of Rectangle

3 : Area of Triangle

4 : Area of Circle

Enter your choice : 3

Enter sides of Triangle : 3 4 5

Area of Triangle is : 6.0

If you want to continue then press 1 : 1

Enter :

1 : Area of Square

2 : Area of Rectangle

3 : Area of Triangle

4 : Area of Circle

Enter your choice : 4

Enter Radius of Circle : 8

Area of Circle is : 200.96

If you want to continue then press 1 : 0

*/

/*

Name :Unnati Srivastava

College : Indraprastha College for Women

Section : B */
//Program 4 : WAP for showing the concept of constructors overloading.

class StudentData

private int stuID;

private String stuName;

private int stuAge;

StudentData() //default constructor

stuID = 100;

stuName = "New Student";

stuAge = 18;

StudentData(int num1, String str, int num2) //parameterized constructor

stuID = num1;

stuName = str;

stuAge = num2;

public int getStuID() //getter and setter methods

return stuID;

public void setStuID(int stuID)

this.stuID = stuID;
}

public String getStuName()

return stuName;

public void setStuName(String stuName)

this.stuName = stuName;

public int getStuAge()

return stuAge;

public void setStuAge(int stuAge)

this.stuAge = stuAge;

public static void main(String args[]) //This object creation would call the default constructor

StudentDatamyobj = new StudentData();

System.out.println("Student Name is: "+myobj.getStuName());

System.out.println("Student Age is: "+myobj.getStuAge());

System.out.println("Student ID is: "+myobj.getStuID());

/*This object creation would call the parameterized constructor StudentData(int, String, int)*/

StudentData myobj2 = new StudentData(555, "Chaitanya", 25);

System.out.println("Student Name is: "+myobj2.getStuName());

System.out.println("Student Age is: "+myobj2.getStuAge());


System.out.println("Student ID is: "+myobj2.getStuID());

/*

Output :

C:\Users\User\Desktop\Sem II>javac java4.java

C:\Users\User\Desktop\Sem II>java StudentData

Student Name is: New Student

Student Age is: 23

Student ID is: 1146

Student Name is: Unnati

Student Age is: 22

Student ID is: 557

*/

/*

Name :Unnati Srivastava

College : Indraprastha College for Women

Section : B */
//Program 5 :Print Fibonacci series recursively.

import java.util.Scanner;

public class java5

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("How many terms you want to see? ");

int n = sc.nextInt();

int a=0,b=1,c=1;

System.out.print(a + " " );

for(int i=1;i<n;i++)

System.out.print(c + " ");

c=a+b;

a=b;

b=c;

System.out.println("\n");

/*

Output :

How many terms you want to see?7

0 1 1 2 3 5 8 */

/*

Name :Unnati Srivastava

College : Indraprastha College for Women


Section : B */

//Program 6 :Program to display tower of Hanoi on the output screen.

import java.util.*;

public class java6

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the block number : ");

int n = sc.nextInt();

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

for(int j=0 ; j<n-i ; j++)

System.out.print(" ");

for(int k=i ; k<2*i+1 ; k++)

System.out.print("||");

System.out.println("");

/*

Output :

Enter the block number :

**
****

******

*********

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

Section : B */

//Program 7 :Program to reverse the digits of number and sum of digits using string functions.

import java.util.Scanner;

public class java7

public static void main(String[] args)

StringBuilder st = new StringBuilder();

String str = new String();

Scanner sc = new Scanner(System.in);

System.out.print("Enter any number:");

str = sc.next();

st.append(str);

int sum = 0;

for(int i = 0, j=1 ; i<str.length() ; i++,j++)

sum = sum + Integer.parseInt(str.substring(i,j));

System.out.println("Reverse : " + st.reverse() );

System.out.println("Sum of digits:"+(sum));

/*
Output :

Enter any number:2345678

Reverse : 8765432

Sum of digits:35

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

Section : B */

//Program 8 :To sort data of employees on employee name, code, age.

import java.io.*;

import java.util.*;

class Emp

static BufferedReader scanner1 = new BufferedReader(new InputStreamReader(System.in));

static Scanner scanner = new Scanner(System.in);

String name;

int age,code;

Emp (String name1, int age1, int code1)

name = name1;

code = age1;

age = code1;

public static void update (Emp emp1[], int n) throws IOException

int s = emp1.length;

for (int i = s-n ;i< s; i++)

System.out.println();

System.out.print ("Enter the Name ");

String name1 = scanner1.readLine();

System.out.print ("Enter the unique code ");

int code1 = scanner.nextInt();

System.out.print ("Enter the age ");

int age1 = scanner.nextInt();


System.out.println();

emp1[i] = new Emp(name1, age1, code1);

public static void display (Emp emp1[])

for (int i = 0; i< emp1.length; i++)

System.out.println ("\n" +"Name " + emp1[i].name);

System.out.println ("Code " + emp1[i].code);

System.out.println ("Age " + emp1[i].age);

System.out.println();

public static Comparator<Emp>SortName = new Comparator<Emp>()

public int compare(Emp emp1, Emp emp2)

String empN1 = emp1.name.toUpperCase();

String empN2 = emp2.name.toUpperCase();

return empN1.compareTo(empN2);

};

public static Comparator<Emp>SortAge = new Comparator<Emp>()

public int compare(Emp emp1, Emp emp2)

if (emp1.age < emp2.age)

return (-1);
else if (emp1.age > emp2.age)

return (1);

else

return (0);

};

public static Comparator<Emp>SortCode = new Comparator<Emp>()

public int compare(Emp emp1, Emp emp2)

if (emp1.code < emp2.code)

return (-1);

else if (emp1.code > emp2.code)

return (1);

else

return (0);

};

public static Emp[] sort (Emp emp1[]) throws IOException

System.out.println ();

System.out.println ("Welcome to the Sorting Wizard ");

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;

public static void main (String [] args) throws IOException

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

System.out.println ("\tWelcome to the fictitious company's Emp Database ");

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

System.out.println ();

int choice,choice1;

int n, m = 0;

Emp[] emp = new Emp[m];

do

System.out.println ("1.Update the Database");

System.out.println ("2.Diplay the Database");

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);

System.out.println ("Database has been sorted .. To view press 2");

else

System.out.println();

break;

default:

break;

while (choice != 3);

}}

/*
Output :

C:\Users\User\Desktop\Sem II>javac java8.java

C:\Users\User\Desktop\Sem II>java Emp

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

Welcome to the fictitious company's Emp Database

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

1.Update the Database

2.Diplay the Database

3.Exit

Do you wish to sort the data as well? (yes-1 or no-0) 1

Welcome to the Sorting Wizard

How would you like to Sort the data

1 for Name

2 for Age

3 for Code

Database has been sorted .. To view press 2

1.Update the Database

2.Diplay the Database

3.Exit

Do you wish to sort the data as well? (yes-1 or no-0) 0

1.Update the Database

2.Diplay the Database

3.Exit

2
Do you wish to sort the data as well? (yes-1 or no-0) 1

Welcome to the Sorting Wizard

How would you like to Sort the data (

1 for Name

2 for Age

3 for Code)

Database has been sorted .. To view press 2

1.Update the Database

2.Diplay the Database

3.Exit

Do you wish to sort the data as well? (yes-1 or no-0) 1

Welcome to the Sorting Wizard

How would you like to Sort the data (

1 for Name

2 for Age

3 for Code)

Database has been sorted .. To view press 2

1.Update the Database

2.Diplay the Database

3.Exit

Enter the number of Emps you you wish to enter the data for: 2

Enter the Name Amit Kumar


Enter the unique code 557

Enter the age 22

Enter the Name Amit

Enter the unique code 558

Enter the age 21

1.Update the Database

2.Diplay the Database

3.Exit

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

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

public double mean(double x[], int n) throws IOException

double sum=0.0;

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

sum=sum+x[i];

double mean=sum/n;

return (mean);

public double mean(int x[], int n) throws IOException

double sum=0.0;

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

sum=sum+x[i];

double mean=sum/n;

return (mean);

public double var(int x[], int n) throws IOException

double var=0.0, z=0.0, sum=0.0;

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

sum=sum+(x[i]*x[i]);
var=((sum/n)-(mean(x,n)*mean(x,n)));

return (var);

public double cov(int x[], double y[],int n) throws IOException

double var=0.0, sum=0.0;

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);

double slope=0.0, intercept=0.0;

public void line_eqn(int x[], double y[], int n) throws IOException

slope=cov(x,y,n)/var(x,n);

intercept=mean(y,n)-slope*mean(x,n);

System.out.println(" Equation of the line is \n"+"Y = "+intercept+"+"+slope+"x");

public void mse(int x[], double y[], double y_est[], int n) throws IOException

double mse, sum=0.0;

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");

System.out.println("\n X\tY \tY^");

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

public static void main (String args[]) throws IOException

Linear obj=new Linear();

int n,per,x[];

double y[],y_est[];

InputStreamReaderisr= new InputStreamReader(System.in);

BufferedReaderbr=new BufferedReader(isr);

String str,ch;

do

System.out.println(" Enter the number of periods: ");

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);

System.out.println("Enter the interval between the two periods :");

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;

System.out.println("Enter the observed values of time series data Y:");

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

str=br.readLine();

y[i]=Double.parseDouble(str);

System.out.println("Equation of the line to fit: y=ax+b, where x gives trend


values and \n y the respective forecasted values");

obj.line_eqn(x,y,n);

obj.mse(x,y,y_est,n);

System.out.println("Do you wish to continue (Y/N)");

ch = br.readLine();

while(ch.equals("Y")||ch.equals("y"));

System.out.println("See you soon");

/*
Output :

C:\Users\User\Desktop\Sem II>javac java9.java

C:\Users\User\Desktop\Sem II>java Line

Enter the number of periods:

Enter the value of the first period:

Enter the interval between the two periods :

Enter the observed values of time series data Y:

10

Equation of the line to fit: y=ax+b, where x gives trend values and

y the respective forecasted values

Equation of the line is

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

Do you wish to continue (Y/N)

See you soon

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

Section : B */

//Program 10 :Implement matrix class, Use methods for addition, subtraction, multiplication and
transpose.

import java.util.*;

public class java10

public static void main(String args[])

int r1=10,r2=10,c1=10,c2=10, i=0, j=0;

Scanner sc = new Scanner(System.in);

int y = 1;

do

System.out.println("Enter :\n1 : Addition\n2 : Subtraction\n3 : Multiplication\n4 :


Transpose\nPlease Enter your choice : ");

int ch = sc.nextInt();

int m1[][] = new int[r1][c1];

int m2[][] = new int[r2][c2];

int sum[][] = new int[Math.max(r1,r2)][Math.max(c1,c2)];

int sub[][] = new int[Math.max(r1,r2)][Math.max(c1,c2)];

int mul[][] = new int[Math.max(r1,r2)][Math.max(c1,c2)];

int trans[][] = new int[c1][r1];

switch(ch)

case 1:

System.out.print("\nEnter row size of matrix 1 :");

r1 = sc.nextInt();
System.out.print("\nEnter column size of matrix 1 :");

c1 = sc.nextInt();

System.out.print("\nEnter row size of matrix 2 :");

r2 = sc.nextInt();

System.out.print("\nEnter column size of matrix 2 :");

c2 = sc.nextInt();

//INPUT FIRST MATRIX

System.out.println("Enter the elements of first matrix");

for ( i = 0 ; i< r1 ; i++ )

for ( j = 0 ; j < c1 ; j++ )

m1[i][j] = sc.nextInt();

//INPUT SECOND MATRIX

System.out.println("Enter the elements of second matrix");

for ( i = 0 ; i< r2 ; i++ )

for ( j = 0 ; j < c2 ; j++ )

m2[i][j] = sc.nextInt();

if ( r1==r2 && c1==c2 )

for ( i = 0 ; i<Math.max(r1,r2) ; i++ )

for ( j = 0 ; j <Math.max(c1,c2) ; j++ )

sum[i][j] = m1[i][j] + m2[i][j];

//PRINT SUMMATION OF MATRICES

System.out.println("Sum of entered matrices:-");

for ( i = 0 ; i<Math.max(r1,r2) ; i++ )

for ( j = 0 ; j <Math.max(c1,c2) ; j++ )


System.out.print(sum[i][j]+"\t");

System.out.println();

else

System.out.println("Addition is not possible : ");

break;

case 2:

//SUBTRACTION

System.out.print("\nEnter row size of matrix 1 :");

r1 = sc.nextInt();

System.out.print("\nEnter column size of matrix 1 :");

c1 = sc.nextInt();

System.out.print("\nEnter row size of matrix 2 :");

r2 = sc.nextInt();

System.out.print("\nEnter column size of matrix 2 :");

c2 = sc.nextInt();

//INPUT FIRST MATRIX

System.out.println("Enter the elements of first matrix");

for ( i = 0 ; i< r1 ; i++ )

for ( j = 0 ; j < c1 ; j++ )

m1[i][j] = sc.nextInt();

//INPUT SECOND MATRIX

System.out.println("Enter the elements of second matrix");


for ( i = 0 ; i< r2 ; i++ )

for ( j = 0 ; j < c2 ; j++ )

m2[i][j] = sc.nextInt();

if ( r1==r2 && c1==c2 )

for ( i = 0 ; i<Math.max(r1,r2) ; i++ )

for ( j = 0 ; j <Math.max(c1,c2) ; j++ )

sub[i][j] = m1[i][j] - m2[i][j];

//PRINT SUBTRACTION OF MATRICES

System.out.println("Subtraction of entered matrices:-");

for ( i = 0 ; i<Math.max(r1,r2) ; i++ )

for ( j = 0 ; j <Math.max(c1,c2) ; j++ )

System.out.print(sub[i][j]+"\t");

System.out.println();

else

System.out.println("Subtraction is not possible : ");

break;

case 3:

//MULTIPLICATION

System.out.print("\nEnter row size of matrix 1 :");

r1 = sc.nextInt();

System.out.print("\nEnter column size of matrix 1 :");

c1 = sc.nextInt();
System.out.print("\nEnter row size of matrix 2 :");

r2 = sc.nextInt();

System.out.print("\nEnter column size of matrix 2 :");

c2 = sc.nextInt();

//INPUT FIRST MATRIX

System.out.println("Enter the elements of first matrix");

for ( i = 0 ; i< r1 ; i++ )

for ( j = 0 ; j < c1 ; j++ )

m1[i][j] = sc.nextInt();

//INPUT SECOND MATRIX

System.out.println("Enter the elements of second matrix");

for ( i = 0 ; i< r2 ; i++ )

for ( j = 0 ; j < c2 ; j++ )

m2[i][j] = sc.nextInt();

if ( c1==r2 )

for ( i = 0 ; i<Math.max(r1,r2) ; i++ )

for ( j = 0 ; j <Math.max(c1,c2) ; j++ )

for ( int k = 0 ; k < c2 ; k++ )

mul[i][j] = mul[i][j] + m1[i][k] *


m2[k][j];

//PRINT MULTIPLICATION OF MATRICES

System.out.println("Sum of entered matrices:-");

for ( i = 0 ; i<Math.max(r1,r2) ; i++ )

{
for ( j = 0 ; j <Math.max(c1,c2) ; j++ )

System.out.print(mul[i][j]+"\t");

System.out.println();

else

System.out.println("Multiplication is not possible : ");

break;

case 4:

System.out.print("\nEnter row size of matrix :");

r1 = sc.nextInt();

System.out.print("\nEnter column size of matrix :");

c1 = sc.nextInt();

System.out.println("\nEnter the elements of matrix");

for ( i = 0 ; i< r1 ; i++ )

for ( j = 0 ; j < c1 ; j++ )

m1[i][j] = sc.nextInt();

System.out.println("Transpose of matrix:-");

for ( i = 0 ; i< r1 ; i++ )

for ( j = 0 ; j < c1 ; j++ )

trans[j][i] = m1[i][j];

for ( i = 0 ; i< c1 ; i++ )

for ( j = 0 ; j < r1 ; j++ )

System.out.print(trans[i][j]+"\t");
System.out.println();

break;

default :

System.out.println("\nPlease enter a valid entry : ");

System.out.println("\nyou want to continue then press (1) : ");

y=sc.nextInt();

while(y==1);

/*

Output :

Enter :

1 : Addition

2 : Subtraction

3 : Multiplication

4 : Transpose

Please Enter your choice :

Enter row size of matrix 1 :3

Enter column size of matrix 1 :3


Enter row size of matrix 2 :3

Enter column size of matrix 2 :3

Enter the elements of first matrix

Exception in thread "main"

C:\Users\User\Desktop\Sem II>java java10

Enter :

1 : Addition

2 : Subtraction

3 : Multiplication

4 : Transpose

Please Enter your choice :

Enter row size of matrix 1 :3

Enter column size of matrix 1 :3

Enter row size of matrix 2 :3

Enter column size of matrix 2 :3

Enter the elements of first matrix

234

526
481

Enter the elements of second matrix

121

552

344

Sum of entered matrices:-

3 5 5

10 7 8

7 12 5

you want to continue then press (1) :

Enter :

1 : Addition

2 : Subtraction

3 : Multiplication

4 : Transpose

Please Enter your choice :

Enter row size of matrix 1 :2

Enter column size of matrix 1 :2

Enter row size of matrix 2 :2

Enter column size of matrix 2 :2

Enter the elements of first matrix

35
64

Enter the elements of second matrix

28

03

Subtraction of entered matrices:-

1 -3

6 1

you want to continue then press (1) :

Enter :

1 : Addition

2 : Subtraction

3 : Multiplication

4 : Transpose

Please Enter your choice :

Enter row size of matrix 1 :3

Enter column size of matrix 1 :3

Enter row size of matrix 2 :3

Enter column size of matrix 2 :3

Enter the elements of first matrix

325

352

162
Enter the elements of second matrix

634

247

312

Sum of entered matrices:-

37 22 36

34 31 51

24 29 50

you want to continue then press (1) :

Enter :

1 : Addition

2 : Subtraction

3 : Multiplication

4 : Transpose

Please Enter your choice :

Enter row size of matrix :3

Enter column size of matrix :3

Enter the elements of matrix

123

456

789

Transpose of matrix:-

1 4 7

2 5 8
3 6 9

you want to continue then press (1) :

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

Section : B */

/*

Program 11 :Implement a menu driven Statistical calculator for the following

• Mean

• Harmonic Mean

• Geometric Mean

• Variance

• Covariance

• Regression coefficient

*/

import java.util.*;

class java11

public static void main(String args[])

int n ,i ;

double sum = 0 , sum1 = 0 , sum2 = 0 , sqsum = 0 , sqsum1 = 0;

double mul = 1;

int series[] = new int[1000];

int series1[] = new int[1000];

int freq[] = new int[1000];

String CI[] = new String[1000];

double mp[] = new double[1000];

Scanner sc = new Scanner(System.in);

int y = 1;

while(y == 1)
{

System.out.println("Enter : \n1 : Mean\n2 : Harmonic Mean\n3 : Geometrical Mean\n4 : Variance\n5 :


Co-Variance\n6 : Regression Coefficient\nEnter your choice : ");

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 : ");

int ch1 = sc.nextInt();

switch (ch1)

case 1 :

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

n = sc.nextInt();

System.out.println("Enter the values : ");

for(i = 0 ; i< n ; i++)

series[i] = sc.nextInt();

sum = sum + series[i];

System.out.println("Mean = " + sum/n);

break;

case 2 :

System.out.println("How many values the series have : ");

n = sc.nextInt();

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

for(i = 0 ; i< n ; i++)

{
series[i] = sc.nextInt();

System.out.println("Enter the frequencies : ");

for(i = 0 ; i< n ; i++)

freq[i] = sc.nextInt();

for(i = 0 ; i< n ; i++)

sum = sum + freq[i]*series[i];

System.out.println("Mean = " + sum/n);

break;

case 3 :

System.out.println("How many values the series have : ");

n = sc.nextInt();

System.out.println("Enter the Class Intervals : ");

for(i = 0 ; i< n ; i++)

CI[i] = sc.next();

mp[i] = (0.5)*(Integer.parseInt(CI[i].substring(0,1)) +
Integer.parseInt(CI[i].substring(2)));

System.out.println("Enter the frequencies : ");

for(i = 0 ; i< n ; i++)

freq[i] = sc.nextInt();

}
for(i = 0 ; i< n ; i++)

sum = sum + freq[i]*mp[i];

System.out.println("Mean = " + sum/n);

break;

break;

case 2 :

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

n = sc.nextInt();

System.out.println("Enter the values : ");

for(i = 0 ; i< n ; i++)

series[i] = sc.nextInt();

sum = sum + 1/series[i];

System.out.println("Harmonic Mean = " + n/sum);

break;

case 3 :

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

n = sc.nextInt();

System.out.println("Enter the values : ");

for(i = 0 ; i< n ; i++)

series[i] = sc.nextInt();

mul = mul*series[i];

System.out.println("Geometric Mean = " + Math.pow(mul,1/n));


break;

case 4 :

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

n = sc.nextInt();

System.out.println("Enter the values : ");

for(i = 0 ; i< n ; i++)

series[i] = sc.nextInt();

sum = sum + series[i];

for(i = 0 ; i< n ; i++)

sqsum = sqsum + Math.pow(sum/n - series[i], 2);

System.out.println("Variance = " + sqsum/n);

break;

case 5 :

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

n = sc.nextInt();

System.out.println("Enter the values of X : ");

for(i = 0 ; i< n ; i++)

series[i] = sc.nextInt();

sum = sum + series[i];

System.out.println("Enter the values of Y : ");

for(i = 0 ; i< n ; i++)

series1[i] = sc.nextInt();
sum1 = sum1 + series1[i];

for(i = 0 ; i< n ; i++)

sqsum = sqsum + (sum/n - series[i])*(sum1/n - series1[i]);

System.out.println("Population Co-Variance = " + sqsum/n + "\nSample Co-Variance = " + sqsum/


(n-1) );

break;

case 6 :

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

n = sc.nextInt();

System.out.println("Enter the values of X : ");

for(i = 0 ; i< n ; i++)

series[i] = sc.nextInt();

sum = sum + series[i];

sqsum = sqsum + Math.pow(series[i],2);

System.out.println("Enter the values of Y : ");

for(i = 0 ; i< n ; i++)

series1[i] = sc.nextInt();

sum1 = sum1 + series1[i];

sqsum1 = sqsum1 + Math.pow(series1[i],2);

for(i = 0 ; i< n ; i++)

sum2 = sum2 + series[i]*series1[i];


}

for(i = 0 ; i< n ; i++)

sqsum = sqsum + (sum/n - series[i])*(sum1/n - series1[i]);

System.out.println("Regression Co-Efficient = " + (n*sum2 - sum*sum1)/(Math.pow((n*sqsum -


Math.pow(sum,2))*(n*sqsum1 - Math.pow(sum1,2)),0.5)) );

break;

default :

System.out.println("Nice Try! ");

System.out.println("Do you want to try again, press(y/n) : ");

y = sc.nextInt();

/*

Output :

C:\Users\User\Desktop\Sem II>javac java11.java

C:\Users\User\Desktop\Sem II>java java11

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

1
Enter :

1 : For Individual Data Series

2 : For Discrete Data Series

3 : For Continuous Data Series

Enter your choice :

Enter no. of elements :

Enter the values :

23726

Mean = 4.0

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

Enter :

1 : For Individual Data Series

2 : For Discrete Data Series

3 : For Continuous Data Series

Enter your choice :

How many values the series have :


4

Enter the elements of the series :

2691

Enter the frequencies :

8216

Mean = 15.75

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

Enter :

1 : For Individual Data Series

2 : For Discrete Data Series

3 : For Continuous Data Series

Enter your choice :

How many values the series have :

Enter the Class Intervals :

0-10 10-20 20-30

Enter the frequencies :

5
8

Mean = -10.0

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

Enter no. of elements :

Enter the values :

2 5 7 9 11

Harmonic Mean = -0.16666666666666666

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :


3

Enter no. of elements :

Enter the values :

2345

Geometric Mean = 1.0

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

Enter no. of elements :

Enter the values :

3687

Variance = 59.75

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance
5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

Enter no. of elements :

Enter the values of X :

3456

Enter the values of Y :

6243

Population Co-Variance = 58.875

Sample Co-Variance = 78.5

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

Enter no. of elements :

Enter the values of X :

3456

Enter the values of Y :

6243
Regression Co-Efficient = NaN

To try again, press(1) :

Enter :

1 : Mean

2 : Harmonic Mean

3 : Geometrical Mean

4 : Variance

5 : Co-Variance

6 : Regression Coefficient

Enter your choice :

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

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;

double mean(int x[], int n)

total =0.0;

double sum =0.0;

double mean;

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

sum = sum +(i*x[i]);

total = total+x[i];

System.out.println("The total is "+total);

mean = (sum/total);

return mean;

void fitting (int x[], int n, double mean)

double poi[] = new double[n];

double ef[] = new double[n];

double sum = 0.0;

int temp;

double diff;
double sse= 0.0, error, mse = 0.0;

int p = 1;

System.out.println("x\t\t"+"Observed Frequency \t\t"+"Expected Frequency");

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

p=1;

for(int j=1; j<i+1; j++)

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);

if(diff >= 0.5)

ef[i] = temp+1;

else

ef[i] = temp;

sum =sum + ef[i];

System.out.println(i+"\t\t\t"+x[i]+"\t\t\t"+ef[i]);

ef[n-1] = total - sum;

System.out.println(n-1+"\t\t\t"+x[n-1]+"\t\t\t"+ef[n-1]);

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

error = ef[i] - x[i];


sse = sse + Math.pow(error,2);

mse = sse/n;

System.out.println(" The MSE is"+mse);

class Poi

public static void main(String args[]) throws IOException

String str,ch;

int n;

double mean;

int x[];

InputStreamReaderisr = new InputStreamReader(System.in);

BufferedReaderbr = new BufferedReader(isr);

do

Calculate calc = new Calculate();

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

System.out.println("\t\tThe Poisson Distribution:");

System.out.println("/*********************************************/");
System.out.println(" Large no. of occurence of an event\n\t\t\t with
avery low probability of occurence");

System.out.println("\nEnter the total number of occurences of an event");

str = br.readLine();

n = Integer.parseInt(str);

x = new int[n+1];

System.out.println(" Enter the frequency of the respective occurences");


for(int i=0; i<n+1; i++)

str = br.readLine();

x[i] = Integer.parseInt(str);

mean = calc.mean(x,n+1);

System.out.println(" The mean of the given data is "+mean);

System.out.println(" Fitting of the poisson data");

calc.fitting(x,n+1,mean);

System.out.println(" do you wish to continue");

System.out.println(" Type Yes or No");

ch = br.readLine();

while(ch.equals("Yes")||ch.equals("yes"));

System.out.println(" bbye");

/*

Output :

C:\Users\User\Desktop\Sem II>javac java12.java

C:\Users\User\Desktop\Sem II>java Poi

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

The Poisson Distribution:

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

Large no. of occurence of an event

with avery low probability of occurence

Enter the total number of occurences of an event


5

Enter the frequency of the respective occurences

The total is 24.0

The mean of the given data is 2.5833333333333335

Fitting of the poisson data

x Observed Frequency Expected Frequency

0 4 2.0

1 3 5.0

2 4 6.0

3 6 5.0

4 2 3.0

5 5 3.0

The MSE is3.0

do you wish to continue

Type Yes or No

No

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

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.*;

public class Eoqinh

public static void main(String[] args)throws IOException

Scanner scanner = new Scanner(System.in);

Scanner scanner1 = new Scanner(System.in);

int choice;

String ch;

Eoq o1=new Eoq();

Epq o2=new Epq();

Eoqs o3=new Eoqs();

Epqs o4=new Epqs();

do

System.out.println("\n\t\t\tInventory System\n");

System.out.println("Select a command:\n1.EOQ\n2.EPQ\n3.EOQ with shortages


allowed");

System.out.println("4.Generalised Lot size model\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:

System.out.println("Enter valid key!");

System.out.println("\nDo you want to continue");

System.out.println("\nType Yes or No");

ch=scanner1.nextLine();

}
while(ch.equals("Yes")||ch.equals("yes"));

class Eoq

Scanner in = new Scanner(System.in);

public double a,ic,l,q;

public void input()

System.out.println("Enter Ordering cost per order:");

a=in.nextDouble();

System.out.println("Enter inventory carrying charge per unit per unit time:");

ic=in.nextDouble();

System.out.println("Enter annual demand:");

l=in.nextDouble();

public void calq()

q=(2*a*l)/(ic);

q=Math.sqrt(q);

public void display()

System.out.println("\nOptimum order/production quantity="+q);

class Epq extends Eoq

{
Scanner in2 = new Scanner(System.in);

double s;

public void input2()

System.out.print("\nEnter annual supply:");

s=in2.nextDouble();

public void calepq()

q=((2*a*l/ic)*(s/(s-l)));

q=Math.sqrt(q);

class Eoqs extends Eoq

Scanner in3 = new Scanner(System.in);

double sc;

public void input3()

System.out.print("\nEnter stock cost per unit per unit time:");

sc=in3.nextDouble();

public void caleoqs()

q=((2*a*l/ic)*((ic+sc)/sc));

q=Math.sqrt(q);

class Epqs extends Eoq


{

Scanner in4 = new Scanner(System.in);

double sc,s;

public void input4()

System.out.print("\nEnter annual supply:");

s=in4.nextDouble();

System.out.print("\nEnter stock cost per unit per unit time:");

sc=in4.nextDouble();

public void calepqs()

q=((2*a*l/ic)*(s/(s-l))*((ic+sc)/sc));

q=Math.sqrt(q);

/*

Output :

C:\Users\User\Desktop\Sem II>javac java13.java

C:\Users\User\Desktop\Sem II>java Eoqinh

Inventory System

Select a command:

1.EOQ

2.EPQ

3.EOQ with shortages allowed

4.Generalised Lot size model

1
Enter Ordering cost per order:

500

Enter inventory carrying charge per unit per unit time:

Enter annual demand:

1000

Optimum order/production quantity=707.1067811865476

Do you want to continue

Type Yes or No

yes

Inventory System

Select a command:

1.EOQ

2.EPQ

3.EOQ with shortages allowed

4.Generalised Lot size model

Enter Ordering cost per order:

100

Enter inventory carrying charge per unit per unit time:

Enter annual demand:

5000

Enter annual supply:6000

Optimum order/production quantity=1414.213562373095

Do you want to continue

Type Yes or No

yes
Inventory System

Select a command:

1.EOQ

2.EPQ

3.EOQ with shortages allowed

4.Generalised Lot size model

Enter Ordering cost per order:

1000

Enter inventory carrying charge per unit per unit time:

Enter annual demand:

10000

Enter stock cost per unit per unit time:10

Optimum order/production quantity=2449.489742783178

Do you want to continue

Type Yes or No

yes

Inventory System

Select a command:

1.EOQ

2.EPQ

3.EOQ with shortages allowed

4.Generalised Lot size model

Enter Ordering cost per order:

90

Enter inventory carrying charge per unit per unit time:


6

Enter annual demand:

780

Enter annual supply:1000

Enter stock cost per unit per unit time:4

Optimum order/production quantity=515.6637382142465

Do you want to continue

Type Yes or No

No

*/

/*

Name :Unnati Srivastava


College : Indraprastha College for Women

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()

Scanner scanner =new Scanner(System.in);

System.out.println("enter the number of states:");

n=scanner.nextInt();

float fact(int x)

if(x<=1)

return (1);

else

return (x*(fact(x-1)));

void MM1(float a,float s)

{
r=a/s;

System.out.println("r="+r);

po=1-r;

System.out.println("system is idle:"+po);

if(r<1 && n>=1)

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);

void MMC(float a,float s)

Scanner g=new Scanner(System.in);

System.out.println("enter the number of coustomers:");

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)

pn=(float) (Math.pow(r,n)*po/(fact((int) n)));

else if(n>=c)

pn=(float) (Math.pow(r,n)*po/(fact((int) c)*Math.pow(c,n-c)));

else

System.out.println("cannot compute:");

System.out.println("queue length="+pn);

System.out.println("measure of effectivness:");

l=(float) (r+(Math.pow(r,c)*t*po/(fact((int) c)*Math.pow((1-t),2))));

lq=(float) (Math.pow(r,c)*t*po/(fact((int) c)*Math.pow((1-t),2)));

w=(float) ((1/s)+Math.pow(r,c)*po/(fact((int) c)*Math.pow((1-t),2)*(c*s)));


wq=(float) (Math.pow(r,c)*po/(fact((int) c)*Math.pow((1-t),2)*(c*s)));

System.out.println("\nl="+l+"\nlq="+lq+"\nw="+w+"\nwq="+wq);

void MM(float a,float s)

r=a/s;

System.out.println("\nr="+r);

if(n>=0)

po=(float) (Math.exp(-r));

System.out.println("\nSystem is idle="+po);

pn=(float) (Math.pow(r,n)*po/fact((int) n));

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

public static void main(String [] args)

design d;

d = new design();

int ch;

String check;
float a,s;

Scanner k = new Scanner(System.in);

System.out.println("enter the arrival rate:");

a=k.nextFloat();

System.out.println("enter the service rate:");

s=k.nextFloat();

d.input();

do

Scanner in = new Scanner(System.in);

Scanner ins = new Scanner(System.in);

System.out.println("menu::::::::::");

System.out.println("enter 1.model MM1");

System.out.println("enter 2.model MMC");

System.out.println("enter 3.model MM0 ");

System.out.println("enter your choice");

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:

System.out.println("invalid choice :-) ");

System.out.println("do u want to continue(yes/no)");

check=ins.nextLine();

while(check.equals("yes")||check.equals("Yes"));

/*

Output :

C:\Users\User\Desktop\Sem II>javac java14.java

C:\Users\User\Desktop\Sem II>java Queue

enter the arrival rate:

.5

enter the service rate:

.6

enter the number of states:

menu

enter 1.model MM1

enter 2.model MMC

enter 3.model MM0

enter your choice:3

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

enter 1.model MM1

enter 2.model MMC

enter 3.model MM0

enter your choice

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

enter 1.model MM1

enter 2.model MMC

enter 3.model MM0


enter your choice

enter the number of coustomers:

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

*/