Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 110

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE

JAVA (MCA – III)


Practical Assignment

Enrolment No: 095020693167

ASSIGNMENT – 1

1).write a program to find AVERAGE of 3 byte type variable .

package ass1;
class ass1
{
public static void main(String arg[])
{
Byte b1=10,b2=20,b3=30,avg;
avg=(byte)((b1+b2+b3)/100);

System.out.println("AVERAGE OF THREE BYTE NO


IS :- " + avg);
}
}

2).write a program to find sum of 5 short type variable .

package ass1;

class ass2
{
public static void main(String arg[])
{
Short a=10,b=20,c=30,d=40,e=50,sum=0;
sum=(short)(a+b+c+d+e);

System.out.println("SUM OF FIVE SHORT TYPE NO


IS :- " + sum);
}
}

Prepared By: Piyush C. Patel Page 1


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

3).Write a program to perform different bit wise operations

package ass1;

import static java.lang.Integer.*;

class ass3
{
public static void main(String arg[])
{
int a=10,b=20;
int and,or,exor,rightshift,leftshift;
and=a & b;
or= a | b;
exor= a^b;
rightshift= a>>2;
leftshift= b<<2;

System.out.println("BITWISE (AND) & OF :- " +


(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = " +and);
System.out.println("BITWISE (OR) | OF :- " +
(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = " +or);
System.out.println("BITWISE (EXOR) ^ OF :- " +
(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = " +exor);
System.out.println("LEFTSHIFT << OF :- " +
(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = " +leftshift);
System.out.println("RIGHTSHIFT >> OF :- " +
(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = " +rightshift);
System.out.println("ROTATELEFT :- " +
(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = "
+rotateLeft(b,2));
System.out.println("ROTATERIGHT :- " +
(toBinaryString(a))+ " & " +(toBinaryString(b)) + " = "
+rotateRight(a,2));
Prepared By: Piyush C. Patel Page 2
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
}

4.Write a java program that will compute the amount of interest


that is earned on an investment over a period of 5 years. The value
of the investment at the end of each year is output.

package ass1;
class Interest
{
public void printdata()
{
byte n=5;
float p=1200.50f,r=12.5f;

System.out.println("The interest is:=" + (p*r*n)/100);


}
}

class ass4
{
public static void main(String args[])
{
Interest inte=new Interest();
inte.printdata();
}
}

Prepared By: Piyush C. Patel Page 3


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

5). Write a program to convert Fahrenheit to Celsius. The formula


for the conversion is as follows: Celsius =( 5 / 9 ) * ( Fahrenheit
– 32 )

package ass1;
class ConFerCel
{
public void printdata(String s[])
{
int fahren=0;
float cels=0.0f;

fahren=Integer.parseInt(s[0]);

cels=((5/9f)*(fahren-32));

System.out.println("Celsius is:=" + cels);


}
}

class ass5
{
public static void main(String args[])
{
ConFerCel con=new ConFerCel();
con.printdata(args);
}
}

Prepared By: Piyush C. Patel Page 4


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

6). Print out the alphabet and ascii value on one line of output.
e.g. A 65 B 66 C 67….Z 90

A 65
B 66
. .
. .
. .
Z 90

package ass1;

class CharAscii
{
public void printdata()
{
char ch=65;

System.out.println("WELL COME\n\t..CAPITAL..");
for(int i=ch;i<=90;i++)
{
System.out.println(ch + "--->" + i);
ch++;
}
System.out.println(" ");

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

ch=97;
for(int i=ch;i<=122;i++)
{
System.out.println(ch + "--->" + i);
Prepared By: Piyush C. Patel Page 5
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

ch++;
}
}
}

class ass6
{
public static void main(String args[])
{
CharAscii carA=new CharAscii();
carA.printdata();
}
}

7).Write a program that displays the divisors of number, and


then it prints the result.

package ass1;

class Divisor
{
public void printdata()
{
int i=30;
float res=0.0f;

res=i/7f;

System.out.print("Result is:=" + res);


}
}

Prepared By: Piyush C. Patel Page 6


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

class ass7
{
public static void main(String args[])
{
Divisor div=new Divisor();
div.printdata();
}
}

8). Write a program to find primes nos. from 2 to 100.

package ass1;
class ass8
{
public static void main(String arg[])
{
int n=Integer.parseInt(arg[0]);
int j;
System.out.print("1 ");
for(int i=1;i<=n;i++)
{
for(j=2;j<=i-1;j++)
{
if(i%j==0)
break;
}
if(i==j)
System.out.print(j+" ");
}

}
}

Prepared By: Piyush C. Patel Page 7


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

9). Write program in java to generate following output.


Use Command line argument for this. (Hint : Accept N
number from user on command line)
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
1234512345
1234 2345
123 345
12 45 *
1 5 * * *
12 45 *
123 345
1234 2345 (Input : 3)
1234512345

*
* *
* * *
* * * *
* * *
* *
*

Prepared By: Piyush C. Patel Page 8


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

package ass1;

class tri1
{
public void printdata(String args[])
{
int n=Integer.parseInt(args[0]);
int i,j,k,l=n;

for(i=0;i<n;i++)
{
for(k=l;k>0;k--)
{
System.out.print(" ");
}
for(j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println(" ");
l--;
}

for(i=n-2;i>0;i--)
{
for(k=l+2;k>0;k--)
{
System.out.print(" ");
}
for(j=0;j<i;j++)
{
System.out.print("* ");
}

Prepared By: Piyush C. Patel Page 9


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println(" ");
l++;
}
}
}

class ass9
{
public static void main(String args[])
{
tri1 tri=new tri1();
tri.printdata(args);
}
}

/*

*
* *
* * *
* * * *
* * *
* *
*

*/

Prepared By: Piyush C. Patel Page 10


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

package ass1;

class tri
{
public void printdata(String args[])
{
int n=Integer.parseInt(args[0]);
int i,j;

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

for(j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println(" ");

for(i=n-2;i>0;i--)
{
for(j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println(" ");
}
}
}

Prepared By: Piyush C. Patel Page 11


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

class ass10
{
public static void main(String args[])
{
tri t=new tri();
t.printdata(args);
}
}

10(A). Write a simple java application to print a pyramid. The first


line has one character, 2nd line has two characters and so on. The
character to be used in the pyramid is taken as a command line
argument.

A) Input : PARAG
Output :

P
P A
P A R
P A R A
P A R A G

package ass1;

class Pyramid
{
public void printdata(String args[])
{
String ch=args[0];
//System.out.println(ch);

int n=args[0].length();

Prepared By: Piyush C. Patel Page 12


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

//System.out.print(n);

for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
System.out.print(args[0].charAt(j));

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

class ass11
{
public static void main(String args[])
{
Pyramid pri=new Pyramid();
pri.printdata(args);
}
}

10(B) Input : PARAG

P
A A
R R
A A
G G
A A
R R
A A
P

Prepared By: Piyush C. Patel Page 13


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

package ass1;
import java.io.*;
class ass12
{
public static void main(String args[])
{
System.out.println("TRINGLE");
int n=Integer.parseInt(args[0]);
int sp=0;
for(int i=0; i<n; i++)
{

for(int j=n; j>i; j--)


System.out.print("*");
for(int m=0; m<sp; m++)
{
System.out.print(" ");

}
for(int k=n; k>i; k--)
System.out.print("*");
sp=sp+2;
System.out.println(" ");
}
for(int p=0; p<n; p++)
{
for(int q=0; q<=p; q++)
System.out.print("*");

for(int m=sp-2; m>0; m--)


{
System.out.print(" ");

Prepared By: Piyush C. Patel Page 14


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
for(int l=0; l<=p; l++)
System.out.print("*");
sp=sp-2;
System.out.println(" ");
}
}
}

Prepared By: Piyush C. Patel Page 15


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

ASSIGNMENT – 2

1). Write a Java application which takes several command line


arguments, which are supposed to be names of students and prints
output as given below:
(Suppose we enter 3 names then output should be as follows):
Number of arguments = 3
1: First Student Name is =Tom
2: Second Student Name is =Dick
3: Third Student Name is =Harry
(Hint: An array may be used for converting from numeric values
from 1 to 20 into String.)

Package ass2;
class ass1
{
public static void main(String arg[]) throws Exception
{
if(arg.length==0)
{
System.out.println("Please pass data from command
line argument");
return;
}
String
str[]={"First","Second","Third","Forth","Fifth","Sixth","Seventh",
"Eigth","Nineth","Tenth","Eleventh","Tweleth"
,"Thirteenth","Fourteenth",
"Fifteenth","Fixteenth","Seventeenth","Eigthtte
enth","Nineteenth","Twenteeth"};

Prepared By: Piyush C. Patel Page 16


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

for(int i=0;i<arg.length;i++)
{
System.out.println(str[i] + " student Name is = " +
arg[i]);
}

}
}

2). Create an array that stores 20 prices, such as $2.34, $7.89, $1.34,
and so on. Display sum of All the prices. Display all values less than
$5. Calculate the average of the prices and display all the values
that are higher than calculated average value.

package ass2;
class ass2
{
public static void main(String arg[])
{
float prices[]={
2.34f,7.89f,1.34f,6.12f,5.14f,
8.12f,7.12f,14.10f,8.56f,9.14f,
10.5f,11.10f,20.0f,14.20f,2.26f,
3.45f,7.48f,6.23f,1.20f,5.5f
};

float sum=0;
for(int i=0;i<20;i++)
{
sum+=prices[i];
}
Prepared By: Piyush C. Patel Page 17
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println("Sum of all prices are: "+ sum);


System.out.println("Average of all prices: "+ (sum/=20));

System.out.println("Display all the values that are higther


than average value:\n ");
for(int i=0;i<20;i++)
{
if(prices[i]>sum)
System.out.println(prices[i]);
}

3). A set of 5 words is given. Write a program to


reverse each word and arrange the resulting words in
alphabetical order.

package ass2;

class ass3
{
public static void main(String arg[])
{
if(arg.length<5)
{
System.out.println("Please Enter data from
commandline \n (Enter min 5)");
return;
Prepared By: Piyush C. Patel Page 18
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

StringBuffer sb[]=new StringBuffer[arg.length];

System.out.println("\n\nActual Data\n");
for(int i=0;i<arg.length;i++)
{
System.out.println(arg[i]);
}

for(int i=0;i<arg.length;i++)
{
sb[i]=new StringBuffer(arg[i]);
sb[i].reverse();
arg[i]=sb[i].toString();
}

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

for(int i=0;i<arg.length;i++)
{
System.out.println(arg[i]);
}

for(int i=0;i<arg.length;i++)
{
for(int j=i+1;j<arg.length;j++)
{
if((arg[i].compareTo(arg[j]))>0)
{
String c=arg[i];
arg[i]=arg[j];

Prepared By: Piyush C. Patel Page 19


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

arg[j]=arg[i];
}
}
}

System.out.println("\n\nAfter Sorting");
for(int i=0;i<arg.length;i++)
{
System.out.println(arg[i]);
}

}
}

4). A set of 10 names are given. Write a program to


delete the first three characters of the names and
arrange the resulting names in alphabetical order and
print them out.

package ass2;
class ass4
{
public static void main(String arg[])
{

if(arg.length<10)
{
System.out.println("Please Enter 10 name from
commandline argument");
return;
Prepared By: Piyush C. Patel Page 20
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
try
{
String str[]=new String[arg.length];

System.out.println("\n\nActual Data\n");
for(int i=0;i<arg.length;i++)
{
System.out.println(arg[i]);
}

for(int i=0;i<arg.length;i++)
{
str[i]="";
for(int j=3;j<arg[i].length();j++)
{
str[i]=str[i]+arg[i].charAt(j);
}
}
System.out.println("\n\nAfter deleting first 3 letter\n");
for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
for(int i=0;i<str.length;i++)
{
for(int j=i+1;j<str.length;j++)
{
if((str[i].compareTo(str[j]))>0)
{
String c=str[i];
str[i]=str[j];
str[j]=str[i];

Prepared By: Piyush C. Patel Page 21


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
}
}
System.out.println("\n\nSorting data\n");

for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
}
catch(Exception e)
{
System.out.println("Caught: "+e);
}
}
}

5). Find the minimum and maximum values in an array.

package ass2;

class ass5
{
public static void main(String arg[])
{
float prices[]={
2.34f,7.89f,1.34f,6.12f,5.14f,
8.12f,7.12f,14.10f,8.56f,9.14f,
10.5f,11.10f,20.0f,14.20f,2.26f,
3.45f,7.48f,6.23f,1.20f,5.5f
};

Prepared By: Piyush C. Patel Page 22


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

float min=prices[0];
float max=0;
for(int i=0;i<20;i++)
{
if(max<prices[i])
max=prices[i];
if(min>prices[i])
min=prices[i];
}

System.out.println("Minimum value: "+min);


System.out.println("Maximum value: "+max);
}
}

6). Demonstrate the use of stack class for characters.


Hint(Use array of Characters)

package ass2;
class ass6
{

int top=0;

char st[]=new char[10];

void push()
{
if(top<10)
{
st[top]=(char)('A'+top);

Prepared By: Piyush C. Patel Page 23


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

top++;

}
else
System.out.println("Stack is Overflow");
}

char pop()
{
if(top<-1)
{
System.out.println("Stack is Underflow");
return 0;
}
return st[--top];
}
void show()
{
for(int i=0;i<10;i++)
System.out.println(st[i]);
}
public static void main(String arg[])
{

try
{
ass6 s=new ass6();

System.out.println("Push Operation\n");
for(int i=0;i<10;i++)
s.push();

Prepared By: Piyush C. Patel Page 24


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

s.show();

System.out.println("\n\nPop Operation\n");
for(int i=0;i<10;i++)
System.out.println(s.pop());
}
catch(Exception e)
{
System.out.println("Caught: "+ e);
}
}
}

7) Matrix Addition, Substraction, Multiplication of 3D


and 2D.

// USING USER INPUT

package ass2;
import java.io.*;
class ass7
{
public static void main(String arg[]) throws IOException
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];

DataInputStream ds=new DataInputStream(System.in);


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

Prepared By: Piyush C. Patel Page 25


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println("\n ENTER FIRST MATRIX \n");


for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print("Enter a[i][j]:- ");
String str=ds.readLine();
a[i][j]=Integer.parseInt(str);
}
}
System.out.println("\n ENTER SECOND MATRIX \n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print("Enter a[i][j]:- ");
String str=ds.readLine();
b[i][j]=Integer.parseInt(str);
}
}
System.out.println("\n DISPLAY FIRST MATRIX \n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println(" ");
}

System.out.println("\n DISPLAY SECOND MATRIX \n");


for(int i=0; i<3; i++)
{

Prepared By: Piyush C. Patel Page 26


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

for(int j=0; j<3; j++)


{
System.out.print(b[i][j] + " ");
}
System.out.println(" ");
}

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


for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(a[i][j]+b[i][j]);
}
System.out.println(" ");
}

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


for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(a[i][j]-b[i][j]);
}
System.out.println(" ");
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
for(int k=0; k<3; k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];

Prepared By: Piyush C. Patel Page 27


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
}
System.out.println(" ");
}
System.out.println("\n MULTIPLICATION \n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println(" ");
}
}
}

10) Calculate MEAN,MEDIAN,MODE

package ass2;
import java.io.*;

class Data
{
float data[];
Data()
{

}
Data(int n)
{
data=new float[n];
}

void getdata()
Prepared By: Piyush C. Patel Page 28
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
try
{
DataInputStream ds = new
DataInputStream(System.in);

for(int i=0;i<data.length;i++)
{
System.out.print("\nEnter number:- ");
data[i] =Float.parseFloat(ds.readLine());
}
}
catch(Exception e)
{

}
}

float Mean()
{
float tot=0f;
for(int i=0;i<data.length;i++)
{
tot+=data[i];
}
return tot/data.length;
}

void sort()
{
for(int i=0;i<data.length;i++)
{

Prepared By: Piyush C. Patel Page 29


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

for(int j=0;j<data.length;j++)
{
if(data[i]<data[j])
{
float temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
}
float Median()
{
int mid=data.length;
float ans;
sort();

if(mid%2!=1)
{
mid=mid/2;
ans=(data[mid]+data[mid+1])/2;
}
else
{
mid/=2;
ans=data[mid];

}
return ans;
}

float Mode()
{

Prepared By: Piyush C. Patel Page 30


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

float ans=0;
int tot1=0,tot2=0;
for(int i=0;i<data.length;i++)
{
tot2=0;
for(int j=0;j<data.length;j++)
{
if(data[i]==data[j])
{
tot2++;
}
}
if(tot2>tot1)
{
tot1=tot2;
ans=data[i];
}
}

return ans;
}
}

class ass10
{
public static void main(String arg[]) throws Exception
{
int n=0;
Data m=new Data(n);
DataInputStream ds= new DataInputStream(System.in);

int ch;

Prepared By: Piyush C. Patel Page 31


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

do
{
System.out.print("1.Mean\n");
System.out.print("2.Median\n");
System.out.print("3.Mode\n");
System.out.print("4.Exit\n");
System.out.print("Enter your choice:- ");
ch=Integer.parseInt(ds.readLine());

switch(ch)
{
case 1:
System.out.print("Enter number
observation:- ");
n=Integer.parseInt(ds.readLine());
m=new Data(n);
m.getdata();
System.out.println("Mean:-
"+m.Mean());
break;
case 2:
System.out.print("Enter number
observation:- ");
n=Integer.parseInt(ds.readLine());
m=new Data(n);
m.getdata();
System.out.println("Median:-
"+m.Median());
break;
case 3:
System.out.print("Enter number
observation:- ");

Prepared By: Piyush C. Patel Page 32


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

n=Integer.parseInt(ds.readLine());
m=new Data(n);
m.getdata();
System.out.println("Mode:-
"+m.Mode());
break;

case 4:
break;
}
}while(ch!=4);

}
}

Prepared By: Piyush C. Patel Page 33


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

GTU Program

2.Write a simple java application to print a pyramid with 5 lines.


The first line has one character, 2nd line has two characters and so
on. The character to be used in the pyramid is taken as a command
line argument.

import java.lang.*;
class prog1
{
public static void main(String[] args)
{
System.out.println("\n\n\n");

int n;

n=Integer.parseInt(args[0]);

for(int i=0;i<n;i++)
{
System.out.println();
for(int k=0;k<n-i;k++)
{
System.out.print(" ");
}
for(int j=0;j<=i;j++)
{
System.out.print(args[1]);
}
for(int k=0;k<i;k++)
{

Prepared By: Piyush C. Patel Page 34


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.print(args[1]);
}
}

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

/** OUTPUT

Z:\java\assi>java prog1 8 "*"

*
***
*****
*******
*********
***********
*************
***************

*/

Prepared By: Piyush C. Patel Page 35


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

3) Write a Java application which takes several command line


arguments, which are supposed to be names of students and prints
output as given below:
(Suppose we enter 3 names then output should be as follows):
Number of arguments = 3
1: First Student Name is =Tom
2: Second Student Name is =Dick
3: Third Student Name is =Harry
Hint: An array may be used for converting from numeric values
from 1 to 20 into String.

package GTU;
class GTU3
{
public static void main(String s[])
{
String
str[]={"frist","second","three","fourth","fifth","sixth","seventh","eigth",
"nine","ten"};

for(int i=0;i<s.length;i++)
{
System.out.println( (i+1) + " : " + str[i] + "Student
Name is = " + s[i]);
}
}
}

Prepared By: Piyush C. Patel Page 36


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

4) Write a class, with main method, which declares floating point


variables and observe the output of dividing the floating point
values by a 0, also observe the effect of assigning a high integer
value (8 digits and above) to a float and casting it back to int and
printing.

package GTU;

import java.io.*;
class GTU4
{

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


{
float f=3.1415f;
int j=123;

System.out.println("\nFloat value divide by Zero: "+f/0);


j=123456789;

f=j;
System.out.println("\nHigh int convert into float: " +f);

j=((int)f);

System.out.println("\nAfter the con. of float to int : " +j);

}
}

Prepared By: Piyush C. Patel Page 37


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

5) Write a class called Statistics, which has a static method called


average, which takes a onedimensional array for double type, as
parameter, and prints the average for the values in the array.
Now write a class with the main method, which creates a two-
dimensional array for the four weeks of a month, containing
minimum temperatures for the days of the week(an array of 4 by
7), and uses the average method of the Statistics class to compute
and print the average temperatures for
the four weeks.

package GTU;

import java.io.*;

class Statistics
{
public static void average(double a[])
{
for(int i=0;i<4;i++)
{
System.out.println("Average : "+(a[i]/7d));
}
}
}

class GTU5
{
public static void main(String ar[]) throws IOException
{
double week[]=new double[4];
int temp[][]=new int[4][7];

DataInputStream ds=new DataInputStream(System.in);


for(int i=0;i<4;i++)
Prepared By: Piyush C. Patel Page 38
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
for(int j=0;j<7;j++)
{
System.out.println("Enter the temprature for 4
week & it's days.");
System.out.print("Temprature :["+i+"]
["+j+"]");
int n=Integer.parseInt(ds.readLine());
temp[i][j]=n;
week[i]+=temp[i][j];
}
}
Statistics.average(week);
}
}

6). Define a class called Product, each product has a name, a


product code and manufacturer name. Define variables, methods
and constructors, for the Product class. Write a class called Test
Product, with the main method to test the methods and
constructors of the Product class.

package GTU;
import java.io.*;

class product
{
String name;
int prod_code;
String manufacture;

Prepared By: Piyush C. Patel Page 39


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

product()
{

name=null;
manufacture=null;
prod_code=0;
}
product(String pname,String pmanu,int a)
{
name=pname;
manufacture=pmanu;
prod_code=a;
}
product(String pname)
{
name=pname;
manufacture="micro";
prod_code=001;
}
product(String pname,String pmanu)
{

name=pname;
manufacture=pmanu;
prod_code=002;
}
void display()
{
System.out.println("product is :="+name);
System.out.println("product code is :="+prod_code);
System.out.println("product manufacture is
:="+manufacture);
}

Prepared By: Piyush C. Patel Page 40


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

class GTU6
{
public static void main(String args[])
{
int ch;
product p1=new product("nokia");
product p2=new product("model","N97");
product p3=new product("N8","Nokia",28000);
p1.display();
System.out.println();
p2.display();
System.out.println();
p3.display();
}
}

Prepared By: Piyush C. Patel Page 41


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

7). Define a class called Cartesian Point, which has two instance
variables, x and y. Provide the methods get X() and get Y() to
return the values of the x and y values respectively, a method called
move() which would take two integers as parameters and change
the values of x and y respectively, a method called display() which
would display the current values of x and y. Now overload the
method move() to work with single parameter, which would set
both x and y to the same values, . Provide constructors with two
parameters and overload to work with one parameter as well. Now
define a class called Test Cartesian Point, with the main method to
test the various methods in the Cartesian Point class.

package GTU;
import java.io.*;

class Cartesian_Point
{
int x,y;

Cartesian_Point()
{
x=0;
y=0;
}
Cartesian_Point(int a)
{
x=a;
}
Cartesian_Point(int a,int b)
{
x=a;
y=b;

Prepared By: Piyush C. Patel Page 42


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
int get_x()
{
return x;
}

int get_y()
{
return y;
}
void move()
{
int temp=x;
x=y;
y=temp;

}
void move(int a)
{
y=x;
}

void display()
{
System.out.print("Values of X is :- " + x);
System.out.print("Values of Y is :- " + y);
System.out.println(" ");
}
void display(int a,int b)
{
System.out.println("VALUES AFTER INTERCHANGE");
System.out.print("Values of X is :- " + x);
System.out.print("Values of Y is :- " + y);

Prepared By: Piyush C. Patel Page 43


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

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

class GTU7
{
public static void main(String s[])
{
Cartesian_Point c=new Cartesian_Point(10,20);
System.out.print("PROGRAM NO 7 OF GTU LIST\n\n");
c.get_x();
c.get_y();
c.display();
c.move();
c.display(10,20);
c.move(10);
c.display();
}
}

Prepared By: Piyush C. Patel Page 44


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

8 & 9) Define a class called Triangle, which has constructor with


three parameters, which are of type Cartesian Point, defined in the
exercise 7. Provide methods to find the area and the perimeter of
the Triangle, a method display() to display the three Cartesian
Points separated by ':' character, a method move() to move the first
Cartesian Point to the specified x, y location, the move should
take care of relatively moving the other points as well, a method
called rotate, which takes two arguments, one is the Cartesian
Point and other is the angle in clockwise direction. Overload the
move method to work with Cartesian Point as a parameter. Now
define a class called Test Triangle to test the various methods
defined in the Triangle class. Similarly also define a class called
Rectangle which has four Cartesian Point.

package GTU;

class Triangle
{
CartesianPoint P1;
CartesianPoint P2;
CartesianPoint P3;

Triangle()
{

}
Triangle(CartesianPoint p1, CartesianPoint p2, CartesianPoint
p3)
{
P1=p1;
P2=p2;
P3=p3;
}

Prepared By: Piyush C. Patel Page 45


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

void display()
{
System.out.println("Cartesian Point P1.X: "+P1.x);
System.out.println("Cartesian Point P1.Y: "+P1.y);
System.out.println("Cartesian Point P2.X: "+P2.x);
System.out.println("Cartesian Point P2.Y: "+P2.y);
System.out.println("Cartesian Point P3.X: "+P3.x);
System.out.println("Cartesian Point P3.Y: "+P3.y);
}
void move()
{
P1.x=P1.x+10;
P1.y=P1.y+10;

P2.x=P2.x+10;
P2.y=P2.y+10;

P3.x=P3.x+10;
P3.y=P3.y+10;
}

void rotate(CartesianPoint P,int degree)


{
int x,y;

x=(int)(P.x * Math.cos(degree) - P.y * Math.sin(degree));


y=(int)(P.x * Math.sin(degree) + P.y * Math.cos(degree));

P.x=x;
P.y=y;
}

public String toString()

Prepared By: Piyush C. Patel Page 46


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
System.out.println("Cartesian Point P1.X: "+P1.x);
System.out.println("Cartesian Point P1.Y: "+P1.y);
System.out.println("Cartesian Point P2.X: "+P2.x);
System.out.println("Cartesian Point P2.Y: "+P2.y);
System.out.println("Cartesian Point P3.X: "+P3.x);
System.out.println("Cartesian Point P3.Y: "+P3.y);
return "";
}

boolean equals(Triangle t)
{
boolean f;
if(P1.x==t.P1.x && P1.y==t.P1.y && P2.x==t.P2.x &&
P2.y==t.P2.y && P3.x==t.P3.x && P3.y==t.P3.y )
f=true;
else
f=false;

return f;
}
}

class Rectangle
{
CartesianPoint P1;
CartesianPoint P2;
CartesianPoint P3;
CartesianPoint P4;

Rectangle()
{

Prepared By: Piyush C. Patel Page 47


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
Rectangle(CartesianPoint p1, CartesianPoint p2, CartesianPoint
p3,CartesianPoint p4)
{
P1=p1;
P2=p2;
P3=p3;
P4=p4;
}
void display()
{
System.out.println("Cartesian Point P1.X: "+P1.x);
System.out.println("Cartesian Point P1.Y: "+P1.y);
System.out.println("Cartesian Point P2.X: "+P2.x);
System.out.println("Cartesian Point P2.Y: "+P2.y);
System.out.println("Cartesian Point P3.X: "+P3.x);
System.out.println("Cartesian Point P3.Y: "+P3.y);
System.out.println("Cartesian Point P4.X: "+P4.x);
System.out.println("Cartesian Point P4.Y: "+P4.y);

void move()
{
P1.x=P1.x+15;
P1.y=P1.y+15;

P2.x=P2.x+15;
P2.y=P2.y+15;

P3.x=P3.x+15;
P3.y=P3.y+15;

Prepared By: Piyush C. Patel Page 48


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

P4.x=P4.x+15;
P4.y=P4.y+15;
}

void rotate(CartesianPoint P,int degree)


{
int x,y;

x=(int)(P.x * Math.cos(degree) - P.y * Math.sin(degree));


y=(int)(P.x * Math.sin(degree) + P.y * Math.cos(degree));

P.x=x;
P.y=y;
}

public String toString()


{
System.out.println("Cartesian Point P1.X: "+P1.x);
System.out.println("Cartesian Point P1.Y: "+P1.y);
System.out.println("Cartesian Point P2.X: "+P2.x);
System.out.println("Cartesian Point P2.Y: "+P2.y);
System.out.println("Cartesian Point P3.X: "+P3.x);
System.out.println("Cartesian Point P3.Y: "+P3.y);
System.out.println("Cartesian Point P4.X: "+P4.x);
System.out.println("Cartesian Point P4.Y: "+P4.y);
return "";
}

boolean equals(Rectangle t)
{
boolean f;

Prepared By: Piyush C. Patel Page 49


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

if(P1.x==t.P1.x && P1.y==t.P1.y && P2.x==t.P2.x &&


P2.y==t.P2.y && P3.x==t.P3.x && P3.y==t.P3.y && P4.x==t.P4.x
&& P4.y==t.P4.y )
f=true;
else
f=false;

return f;
}
}

class GTU8
{
public static void main(String arg[])
{
CartesianPoint P1=new CartesianPoint(10,20);
CartesianPoint P2=new CartesianPoint(20,20);
CartesianPoint P3=new CartesianPoint(30,20);

Triangle t=new Triangle(P1,P2,P3);

System.out.println("\n\n\t\tTriangle\n\n");
t.display();

System.out.println("\n\nOverload toString method\n\n");


System.out.println(t);

System.out.println("\nEach Cartesian Point is move 10


relatively\n");
t.move();
t.display();

Prepared By: Piyush C. Patel Page 50


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println("\nEach Cartesian Point is rotating 90


degree");
t.rotate(P1,10);
t.rotate(P2,90);
t.rotate(P3,90);
t.display();

P1=new CartesianPoint(10,20);
P2=new CartesianPoint(40,30);
P3=new CartesianPoint(50,90);

Triangle t2=new Triangle(P1,P2,P3);


System.out.println("\n\n\t\tTriangle 2\n\n");
System.out.println(t2);
System.out.println("Equlas T1 and T2 "+ t.equals(t2));

CartesianPoint P4=new CartesianPoint(25,55);

Rectangle r=new Rectangle(P1,P2,P3,P4);

System.out.println("\n\n\t\tRectangle\n\n");
r.display();

System.out.println("\n\nOverload toString method\n\n");


System.out.println(r);

System.out.println("\nEach Cartesian Point is move 15


relatively\n");
r.move();
r.display();

Prepared By: Piyush C. Patel Page 51


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println("\nEach Cartesian Point is rotating 90


degree");
r.rotate(P1,90);
r.rotate(P2,90);
r.rotate(P3,90);
r.rotate(P4,90);
r.display();

P1=new CartesianPoint(10,20);
P2=new CartesianPoint(20,20);
P3=new CartesianPoint(30,20);
P4=new CartesianPoint(25,55);
Rectangle r2=new Rectangle(P1,P2,P3,P4);

System.out.println("\n\n\t\tRectangle 2\n\n");
System.out.println(r2);
System.out.println("Equlas R1 and R2 "+ r.equals(r2));

}
}

Prepared By: Piyush C. Patel Page 52


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

10) .Define an abstract class called Polygon. Provide a constructor


which takes an array of Cartesian Point as parameter.Also provide
method called perimeter, which calculates and returns the
perimeter of the Polygon. Declare abstract method area for this
class. Also define a method called move, which takes two
parameters x and y to specify the destination for the first point of
the Polygon, and overload to make it work for Cartesian Point as a
parameter. Now update the classes Triangle and Rectangle in the
exercise 8 above, to be a subclass of the Polygon class.
Writeappropriate class with main method to test the polymorphism
in the area method.

Package gtu;
class cart
{

int x,y;

int getx()
{
return x;
}
int gety()
{
return y;
}
void display()
{
System.out.println(" X : " + x);
System.out.println(" Y : " + y);
}

cart(){}
cart(int a,int b)

Prepared By: Piyush C. Patel Page 53


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
x=a;
y=b;
}
}

abstract class polygon


{

/**double a,b,c,d;

polygon()
{
}
polygon(cart c[])
{

a=Math.sqrt((c[0].getx()-c[1].getx())^2+(c[0].gety()-
c[1].gety())^2);
b=Math.sqrt((c[1].getx()-c[2].getx())^2+(c[1].gety()-
c[2].gety())^2);
c=Math.sqrt((c[2].getx()-c[3].getx())^2+(c[2].gety()-
c[3].gety())^2);
d=Math.sqrt((c[3].getx()-c[0].getx())^2+(c[3].gety()-
c[0].gety())^2);

float perimeter()
{
return a+b+c+d;
}*/
abstract void area();

}
Prepared By: Piyush C. Patel Page 54
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

class triangle extends polygon


{
cart c1,c2,c3;

triangle()
{
c1=new cart();
c2=new cart();
c3=new cart();
}
triangle(cart c1,cart c2,cart c3)
{
System.out.println("\n\n\n" + " Constructor Is Called" );
this.c1=c1;
this.c2=c2;
this.c3=c3;
}
public String toString()
{
String s;
s=("\n\n\n\nFirst Cart_point : \n\n X: " + c1.x + " y : "+ c1.y
+" \n\nSecond Cart_point : \n\n X: " + c2.x + " y : "+ c2.y +" \n\nThird
Cart_point : \n\n X: " + c3.x + " y : "+ c3.y + "\n\n\n");

return s;
}

public void area()


{

System.out.println("\n\n\n" + " Area Method Is Called " );


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

Prepared By: Piyush C. Patel Page 55


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

double a=Math.sqrt((c1.getx()-c2.getx())^2+(c1.gety()-
c2.gety())^2);
double b=Math.sqrt((c2.getx()-c3.getx())^2+(c2.gety()-
c3.gety())^2);
double c=Math.sqrt((c3.getx()-c1.getx())^2+(c3.gety()-
c1.gety())^2);

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

System.out.println(" A : " + a);


System.out.println(" B : " + b);
System.out.println(" C : " + c);
System.out.println(" S : " + s);

double f;
f=Math.sqrt(s*(s-a)*(s-b)*(s-c));

System.out.println("\n\n");
System.out.println(" Area Of Triangle : " + f);
}
void perimeter()
{

System.out.println("\n\n\n" + " Perimeter Method Is Called


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

double a=Math.sqrt((c1.getx()-c2.getx())^2+(c1.gety()-
c2.gety())^2);
double b=Math.sqrt((c2.getx()-c3.getx())^2+(c2.gety()-
c3.gety())^2);
double c=Math.sqrt((c3.getx()-c1.getx())^2+(c3.gety()-
c1.gety())^2);

double f=(a+b+c);
Prepared By: Piyush C. Patel Page 56
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println(" Perimeter Of Triangle : " + f);


}
void display()
{
System.out.println("\n\n\n" + " Display Method Is Called " );
System.out.println("\n\n");

c1.display();
c2.display();
c3.display();
}
}
class Ractangle extends polygon
{

cart r1,r2,r3,r4;

Ractangle()
{
r1=new cart();
r2=new cart();
r3=new cart();
r4=new cart();
}
Ractangle(cart r1,cart r2,cart r3,cart r4)
{
System.out.println("\n\n\n" + " Constructor Is Called" );
this.r1=r1;
this.r2=r2;
this.r3=r3;
this.r4=r4;
}

Prepared By: Piyush C. Patel Page 57


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

public String toString()


{
String s;
s=("\n\n\n\nFirst Cart_point : \n\n X: " + r1.x + " y : "+ r1.y
+" \n\nSecond Cart_point : \n\n X: " + r2.x + " y : "+ r2.y +" \n\nThird
Cart_point : \n\n X: " + r3.x + " y : "+ r3.y + " \n\nFourth Cart_point :
\n\n X: " + r4.x + " y : "+r4.y +"\n\n\n");

return s;

public void area()


{

System.out.println("\n\n\n" + " Area Method Is Called " );


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

double a=Math.sqrt((r1.getx()-r2.getx())^2+(r1.gety()-
r2.gety())^2);
double b=Math.sqrt((r2.getx()-r3.getx())^2+(r2.gety()-
r3.gety())^2);
double c=Math.sqrt((r3.getx()-r4.getx())^2+(r3.gety()-
r4.gety())^2);
double d=Math.sqrt((r4.getx()-r1.getx())^2+(r4.gety()-
r1.gety())^2);

if(a==c && b==d)


{
System.out.println(" A : " + a);
System.out.println(" B : " + b);
System.out.println(" C : " + c);
System.out.println(" D : " + d);

Prepared By: Piyush C. Patel Page 58


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

double ans= a*c;

System.out.println("\n\n");
System.out.println(" Area Of Rectangle : " + ans);
}
else
{
System.out.println("\n\n");
System.out.println("The Value You Given Not make
Rectangle");
}

}
void perimeter()
{

System.out.println("\n\n\n" + " Perimeter Method Is Called


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

double a=Math.sqrt((r1.getx()-r2.getx())^2+(r1.gety()-
r2.gety())^2);
double b=Math.sqrt((r2.getx()-r3.getx())^2+(r2.gety()-
r3.gety())^2);
double c=Math.sqrt((r3.getx()-r4.getx())^2+(r3.gety()-
r4.gety())^2);
double d=Math.sqrt((r4.getx()-r1.getx())^2+(r4.gety()-
r1.gety())^2);

double ans=2*(a+c);

System.out.println(" Perimeter Of Rectangle : " + ans);


}

void display()
Prepared By: Piyush C. Patel Page 59
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
System.out.println("\n\n\n" + " Display Method Is Called " );
System.out.println("\n\n");

r1.display();
r2.display();
r3.display();
r4.display();
}
}

class prog9
{

public static void main(String [] args)


{

cart c[]=new cart[4];

c[0]=new cart(2,5);
c[1]=new cart(3,6);
c[2]=new cart(4,7);
c[3]=new cart(5,8);

polygon p[]=new polygon[2];

p[0]=new Rectangle(c1,c2,c3,c4);
p[1]=new Triangle(c1,c2,c3);

System.out.println(p[0].area());
System.out.println(p[0].perimeter());

System.out.println(p[1].area());
System.out.println(p[1].perimeter());
Prepared By: Piyush C. Patel Page 60
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
}

13. Define a class called Polygon Manager, which manages a


number of Polygon instances. Provide methods to add, remove and
list the Polygon instances managed by it. Test the methods of
Polygon Manager by writing appropriate class with main method.

Package gtu;

import java.io.*;
interface input
{
BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
}
abstract class polygon_manager implements input
{
int x[];
int i;
int size;
int y[];
polygon_manager()
{
}
polygon_manager(int xy)
{
x=new int[xy];
y=new int[xy];
size=xy;
i=0;
}
abstract void display();
}
Prepared By: Piyush C. Patel Page 61
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

class triangle extends polygon_manager


{
triangle()
{
}
triangle(int xy)
{
super(xy);
}
void add()throws IOException
{
System.out.println("\nEnter the Three Co-Ordinates :");
for(i=0;i<3;i++)
{
System.out.println("Enter the x"+(i+1)+" And Y"+(i+1)+"=->");
x[i]=Integer.parseInt(br.readLine());
y[i]=Integer.parseInt(br.readLine());
}
}
void display()
{
System.out.print("\n\tTriangle :- {");
for(i=0;i<3;i++)
{
System.out.print("("+x[i]+","+y[i]+")");
}
System.out.print("}");
}
}
class rectangle extends polygon_manager
{
rectangle()
{
}
rectangle(int xy)
Prepared By: Piyush C. Patel Page 62
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
super(xy);
}
void add()throws IOException
{
System.out.println("\nEnter the four Co-Ordinates :");
for(i=0;i<4;i++)
{
System.out.println("Enter the x"+(i+1)+" And Y"+(i+1)+"=->");
x[i]=Integer.parseInt(br.readLine());
y[i]=Integer.parseInt(br.readLine());
}
}
void display()
{
System.out.print("\n\tRectangle :- {");
for(i=0;i<4;i++)
{
System.out.print("("+x[i]+","+y[i]+")");
}
System.out.print("}");
}
}
class pantagon extends polygon_manager
{
pantagon()
{
}
pantagon(int xy)
{
super(xy);
}
void add()throws IOException
{
System.out.println("\nEnter the five Co-Ordinates :");
Prepared By: Piyush C. Patel Page 63
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

for(i=0;i<5;i++)
{
System.out.println("Enter the x"+(i+1)+" And Y"+(i+1)+"=->");
x[i]=Integer.parseInt(br.readLine());
y[i]=Integer.parseInt(br.readLine());
}
}
void display()
{
System.out.print("\n\tPantagon :- {");
for(i=0;i<5;i++)
{
System.out.print("("+x[i]+","+y[i]+")");
}
System.out.print("}");
}
}
class hexagon extends polygon_manager
{
hexagon()
{
}
hexagon(int xy)
{
super(xy);
}
void add()throws IOException
{
System.out.println("\nEnter the six Co-Ordinates :");
for(i=0;i<6;i++)
{
System.out.println("Enter the x"+(i+1)+" And Y"+(i+1)+"=->");
x[i]=Integer.parseInt(br.readLine());
y[i]=Integer.parseInt(br.readLine());
}
Prepared By: Piyush C. Patel Page 64
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
void display()
{
System.out.print("\n\tHexagon :- {");
for(i=0;i<6;i++)
{
System.out.print("("+x[i]+","+y[i]+")");
}
System.out.print("}");
}
}
class octagon extends polygon_manager
{
octagon()
{
}
octagon(int xy)
{
super(xy);
}
void add()throws IOException
{
System.out.println("\nEnter the Eight Co-Ordinates :");
for(i=0;i<8;i++)
{
System.out.println("Enter the x"+(i+1)+" And Y"+(i+1)+"=->");
x[i]=Integer.parseInt(br.readLine());
System.out.println();
y[i]=Integer.parseInt(br.readLine());
}
}
void display()
{
System.out.print("\n\tOctagon :- {");
for(i=0;i<8;i++)
Prepared By: Piyush C. Patel Page 65
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
System.out.print("("+x[i]+","+y[i]+")");
}
System.out.print("}");
}
}
class polygon implements input
{
public static void main(String arg[])throws IOException
{
int ch,len=0,j=0,pos;
polygon_manager p[]=new polygon_manager[100];
while(true)
{
System.out.println("\nFunction Of Polygon...........");
System.out.println("1.Add");
System.out.println("2.Remove");
System.out.println("3.Display");
System.out.println("4.Exit");
System.out.println("Enter Your Choice =->");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
menuformat();
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
triangle t=new triangle(3);
t.add();
p[len]=t;
len++;
break;
case 2:
Prepared By: Piyush C. Patel Page 66
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

rectangle r=new rectangle(4);


r.add();
p[len]=r;
len++;
break;
case 3:
pantagon pa=new pantagon(5);
pa.add();
p[len]=pa;
len++;
break;
case 4:
hexagon h=new hexagon(6);
h.add();
p[len]=h;
len++;
break;
case 5:
octagon o=new octagon(8);
o.add();
p[len]=o;
len++;
break;
case 6:
break;
default :
System.out.println("\nPlz Enter 1 To 6 .................");
break;
}
break;
case 2 :

if(len<1)
{

Prepared By: Piyush C. Patel Page 67


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println("\nPolygon does not


exists............");
break;
}
System.out.println("\nList Of Polygon
Is ..............");
for(j=0;j<len;j++)
{
p[j].display();
}
menuformat();
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("\nEnter The
Position =->");
pos=Integer.parseInt(br.readLine());
if(pos<len)
{
if(p[pos-1].size==3)
{
p[j]=null;
}
else
{
System.out.println("\nInvalid
Selection................");
}
}
else
{
System.out.println("\nInvalid
Position................");
}
Prepared By: Piyush C. Patel Page 68
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

break;
case 2:
System.out.println("\nEnter The
Position =->");
pos=Integer.parseInt(br.readLine());
if(pos<len)
{
if(p[pos-1].size==4)
{
p[j]=null;
}
else
{
System.out.println("\nInvalid
Selection................");
}
}
else
{
System.out.println("\nInvalid
Position................");
}
break;
case 3:
System.out.println("\nEnter The
Position =->");
pos=Integer.parseInt(br.readLine());
if(pos<len)
{
if(p[pos-1].size==5)
{
p[j]=null;
}
else
{
Prepared By: Piyush C. Patel Page 69
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println("\nInvalid
Selection................");
}
}
else
{
System.out.println("\nInvalid
Position................");
}
break;
case 4:
System.out.println("\nEnter The
Position =->");
pos=Integer.parseInt(br.readLine());
if(pos<len)
{
if(p[pos-1].size==6)
{
p[j]=null;
}
else
{
System.out.println("\nInvalid
Selection................");
}
}
else
{
System.out.println("\nInvalid
Position................");
}
break;
case 5:
System.out.println("\nEnter The
Position =->");
Prepared By: Piyush C. Patel Page 70
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

pos=Integer.parseInt(br.readLine());
if(pos<len)
{
if(p[pos-1].size==8)
{
p[j]=null;
}
else
{
System.out.println("\nInvalid
Selection................");
}
}
else
{
System.out.println("\nInvalid
Position................");
}
break;
case 6:
break;
default :
System.out.println("\nPlz Enter 1 To 6 .................");
break;

}
break;
case 3 :
System.out.println("\nList Of Polygon
Is ..............");
for(j=0;j<len;j++)
{
p[j].display();
}
break;
Prepared By: Piyush C. Patel Page 71
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

case 4 :
return ;
}
}
}
public static void menuformat()
{
System.out.println("\n\nSelect Any Polygon..................");
System.out.println("1.Tringle");
System.out.println("2.Rectangle");
System.out.println("3.Pantagon");
System.out.println("4.Hexagon");
System.out.println("5.Octagon");
System.out.println("6.Back To Main Menu...........");
System.out.println("Enter Your Choice =->");
}

14. Define a class called Statistical Data which manages a number


of readings of type int. Provide a
method to set Data available in the form of an array, a method add
to individually add data of type
int, a method to reset the entire data, and methods to return the
following statistics:
1. mean
2. median
3. mode
4. variance
5. standard deviation
6. specified percentile (between 0 – 100) the method should have a
parameter.
7. specified quartile (between 1 - 3) the method should have a
parameter.

Prepared By: Piyush C. Patel Page 72


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

8. interquartile range
Use this class to calculate the given statistics for the data regarding
the total of marks obtained by all the students in your class in the
previous exams. The data may be accepted one by one for each
student from the keyboard.

Package gtu;

import java.io.*;
class p14
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
StatisticalData s=new StatisticalData();
int ch=1;
while(ch!=0)
{
/*float f1=2234.34534427f;
System.out.format("%1$.2f",f1);*/
System.out.println("\n\n\n\n\n\nMain Menu");
System.out.println("1. Insert Mark");
System.out.println("2. Mean");
System.out.println("3. Median");
System.out.println("4. Mode");
System.out.println("5. Variance");
System.out.println("6. Standard Deviation");
System.out.println("7. Specified Percentile");
System.out.println("8. Specified Quarlite");
System.out.println("9. Interquartile Range");
System.out.println("0. Exit");
System.out.print("Enter your choice :");
try

Prepared By: Piyush C. Patel Page 73


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
ch=Integer.parseInt(br.readLine());
}
catch(NumberFormatException nfx)
{
System.out.println("You can not enter the string.... OR
can not leave it blank.....");
ch=13;
}
if(s.get_size()==0 && ch!=1)
{
if(ch>10)
{
System.out.println("System can't recognize your
choice, re-enter your selection....");
}
else if( ch != 0)
{
System.out.println("Enter the mark of student
first.....");
}
}
else
{
switch(ch)
{
case 1:
String ch1="Y";
while(ch1.compareToIgnoreCase("N")!=0)
{
if(ch1.equalsIgnoreCase("Y") ||
ch1.equalsIgnoreCase("N"))
{
s.add();
}
Prepared By: Piyush C. Patel Page 74
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

else
{
System.out.println("Enter Y (for
Yes) \nEnter N (for No)... Otherwise System can't recognize....");
}
System.out.print("Want to add more ?
(Y/N) :");
ch1=br.readLine();
}
break;
case 2:
System.out.println("Mean : "+s.mean());
break;
case 3:
System.out.println("Median : "+s.median());
break;
case 4:
System.out.println("Mode : "+s.mode());
break;
case 5:
System.out.println("Variance : "+s.variance());
break;
case 6:
System.out.println("Standard Deviation :
"+s.standard_deviation());
break;
case 7:
System.out.println("Enter value for Specified
Percentile (0-100) :");
int p=Integer.parseInt(br.readLine());
if( p > 100 || p < 0 )
{
System.out.println("Enter between 0 -
100 .....");
}
Prepared By: Piyush C. Patel Page 75
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

else
{
System.out.println("Specified Percentile
for intput " + p + " is : "+s.specified_percentile(p));
}
break;
case 8:
System.out.println("Enter Value for Specified
Quarlite (1-3) :");
int q=Integer.parseInt(br.readLine());
if( q > 3 || q < 1)
{
System.out.println("Enter between 1 -
3 ......");
}
else
{
System.out.println("Specified Quarlite
for input " + q + " is : "+s.specified_quertile(q));
}
break;
case 9:
System.out.println("Interquartile Range : "+
s.interquarlite_rang());
break;
case 10:
s.display();
break;
case 13:
break;
case 0:
break;
default:
System.out.println("System can't recognize
your choice, re-enter your selection....");
Prepared By: Piyush C. Patel Page 76
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

break;
}
}
}
}
}

class StatisticalData
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int marks[];
int n;
StatisticalData()
{
marks=new int[60];
n=0;
}
void add() throws IOException
{
if(n==60)
{
System.out.println("There is 60 student in the class...");
System.out.println("You can't enter any more marks.....");
}
else
{
System.out.print("Enter Mark of Student : "+(n+1)+" : ");
try
{
int temp=Integer.parseInt(br.readLine());
if(temp>100 || temp<0)
{
System.out.println("Marks should be in range
between 0 - 100...");
Prepared By: Piyush C. Patel Page 77
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

add();
}
else
{
marks[n]=temp;
n++;
}

}
catch(NumberFormatException nfe)
{
System.out.println("Marks can't be the String..... OR
can not leave it blank.....");
add();
}
}
}
float mean()
{
float sum=0;
for(int i=0;i<n;i++)
{
sum+=marks[i];
}
return (sum/(n*1.0f));
}
float median()
{
sort();
float med=0;
int tmp=(n+1)/2;
if(n%2==0)
{
med=(marks[tmp-1]+marks[tmp])/2.0f;
}
Prepared By: Piyush C. Patel Page 78
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

else
{
med=marks[tmp];
}
return med;
}
int mode()
{
int m=0;
int count[]=new int[n];
for(int i=0;i<n;i++)
{
count[i]=0;
for(int j=0;j<n;j++)
{
if(i!=j)
{
if(marks[i]==marks[j])
{
count[i]++;
}
}
}
}
int temp=count[0];
int temp1=-1;
for(int k=0;k<n;k++)
{
if(count[k]>temp)
{
temp=count[k];
temp1=k;
System.out.println(temp1);
}
}
Prepared By: Piyush C. Patel Page 79
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

if(temp1==-1)
{
//f m=(2*median())+(3*mean());
}
else
{
m=marks[temp1];
}
return m;
}
float variance()
{
float mean=mean();
float sum=0;
for(int i=0;i<n;i++)
{
System.out.println((marks[i]-mean)+" "+((marks[i]-
mean)*(marks[i]-mean)));
sum+=(marks[i]-mean)*(marks[i]-mean);
}
return (sum/((n-1)*1.0f));
}
float standard_deviation()
{
float variance = variance();
return ((float)(Math.sqrt(variance)));
}
int specified_percentile(int nth)
{
sort();
int i=(int)Math.ceil(((nth*n)/100.0f));
return marks[i-1];
}
int specified_quertile(int nth)
{
Prepared By: Piyush C. Patel Page 80
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

int i=(int)Math.ceil(((nth*(n+1))/4.0f));
return marks[i-1];
}
int interquarlite_rang()
{
return (specified_quertile(3)-specified_quertile(1));
}
void sort()
{

for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(marks[i]>marks[j])
{
int temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
}
}
}
}

void display()
{
for(int i=0;i<n;i++)
{
System.out.println("Student "+(i+1)+" : "+marks[i]);
}
}
int get_size()
{
return n;
}

Prepared By: Piyush C. Patel Page 81


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

Prepared By: Piyush C. Patel Page 82


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

15. Update the class Statistical Data, and define a method called
load From CSV, which takes as parameter an Input Stream, where
numeric data is available in an ASCII format, in a comma
separated form. Overload this method to take a File instance as
parameter. Test the new methods using appropriate data.

Package gtu;

import java.util.*;
import java.io.*;

class p15{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
StatisticalData s=new StatisticalData();
int ch=1;
while(ch!=0){
System.out.println("\n\n\n\n\n\nMain Menu");
System.out.println("01. Insert Mark");
System.out.println("02. Mean");
System.out.println("03. Median");
System.out.println("04. Mode");
System.out.println("05. Variance");
System.out.println("06. Standard Deviation");
System.out.println("07. Specified Percentile");
System.out.println("08. Specified Quarlite");
System.out.println("09. Interquartile Range");
System.out.println("10. Load Data from CSV file ( using Input
Stream )");
System.out.println("11. Load Data from CSV file ( using File )");
System.out.println("0. Exit");
System.out.print("Enter your choice :");
try{
ch=Integer.parseInt(br.readLine());
Prepared By: Piyush C. Patel Page 83
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
catch(NumberFormatException nfx) {
System.out.println("You can not enter the string.... OR can not leave it
blank.....");
ch=13;
}
if(s.get_size()==0 && ch!=1 && ch!=10 && ch!=11)
{
if(ch>11) {
System.out.println("System can't recognize
your choice,re-enter your selection....");
}
else if( ch != 0 ) {
System.out.println("Enter the mark of student
first.....");
}
}
else{
switch(ch) {
case 1:
String ch1="Y";

while(ch1.compareToIgnoreCase("N")!=0){
if(ch1.equalsIgnoreCase("Y") ||
ch1.equalsIgnoreCase("N")){
s.add();
}
else{
System.out.println("Enter
Y (for Yes) \nEnter N (for No)... Otherwise System can't recognize....");
}
System.out.print("Want to add
more ? (Y/N) :");
ch1=br.readLine();
}
Prepared By: Piyush C. Patel Page 84
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

break;
case 2:
System.out.println("Mean :
"+s.mean());
break;
case 3:
System.out.println("Median :
"+s.median());
break;
case 4:
s.mode();
break;
case 5:
System.out.println("Variance :
"+s.variance());
break;
case 6:
System.out.println("Standard
Deviation : "+s.standard_deviation());
break;
case 7:
int flag=0;
while(flag==0) {
try{
System.out.print("Enter value for
Specified Percentile (0-100) :");
int
p=Integer.parseInt(br.readLine());
if( p > 100 || p < 1 ) {
System.out.println("Enter
between 1 - 100 .....");
}
else{
System.out.println("Specified
Percentile for intput " + p + " is : "+s.specified_percentile(p));
Prepared By: Piyush C. Patel Page 85
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

flag=1;
}
}
catch(NumberFormatException e) {
System.out.println("Enter between 1 - 100 .....");
}
}
break;
case 8:
flag=0;
while(flag==0) {
try{
System.out.print("Enter Value for
Specified Quarlite (1-3) :");
int
q=Integer.parseInt(br.readLine());
if( q > 3 || q < 1) {

System.out.println("Enter between 1 - 3 ......");


}
else{

System.out.println("Specified Quarlite for input " + q + " is :


"+s.specified_quertile(q));
flag=1;
}
}
catch(NumberFormatException e){
System.out.println("Enter between 1 - 3 ......");
}
}
break;
case 9:
System.out.print("Interquartile Range :
"+ s.interquarlite_rang());

Prepared By: Piyush C. Patel Page 86


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

break;
case 10:
flag=0;
String path="";
while(flag==0) {
try{
System.out.print("Enter the Path for csv file :");
path=br.readLine();
String ext=path.substring(path.length()-4);
if(ext.equalsIgnoreCase(".csv")){
inputStream istr=new FileInputStream(path);
s.load_from_csv(istr);
istr.close();
flag=1;
}
else{
System.out.println("System can't recognize your
input....");
System.out.println("System can support only .CSV
file....");
}
}
catch(FileNotFoundException e) {
System.out.println("System can't find your inputed
path....");
}

catch(StringIndexOutOfBoundsException e){
System.out.println("Can't leave it
blank...");
}
}
break;
case 11:
flag=0;
Prepared By: Piyush C. Patel Page 87
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

path="";
while(flag==0) {
String ext="";
try{
System.out.print("Enter the Path for csv file :");
path=br.readLine();
ext=path.substring(path.length()-4);
}
catch(StringIndexOutOfBoundsException e){
System.out.println("Cannot be Blank");
}
if(ext.equalsIgnoreCase(".csv")) {
try{
File f=new File(path);
s.load_from_csv(f);
flag=1;
}
catch(FileNotFoundException e) {
System.out.println("System can't find your inputed
path....");
}
}
else{
System.out.println("System can't recognize your
input....");
System.out.println("System can support only .CSV
file....");
}
}
break;
case 12:
s.display();
break;
case 0:
break;
Prepared By: Piyush C. Patel Page 88
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

default:
System.out.println("System can't
recognize your choice, re-enter your selection....");
break;
}
}
}
}
}

class StatisticalData{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int marks[];
int n;
StatisticalData(){
marks=new int[60];
n=0;
}
void add(){
if(n==60) {
System.out.println("There is 60 student in the
class...");
System.out.println("You can't enter any more
marks.....");
}
else{
System.out.print("Enter Mark of Student : "+
(n+1)+" : ");
try{
int temp=Integer.parseInt(br.readLine());
if(temp>100 || temp<0) {
System.out.println("Marks should be in
range between 0 - 100...");
add();
Prepared By: Piyush C. Patel Page 89
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
else{
marks[n]=temp;
n++;
}

}
catch(NumberFormatException nfe) {
System.out.println("Marks can't be the String.....
OR can not leave it blank.....");
add();
}
}
}
float mean() {
display();
float sum=0;
for(int i=0;i<n;i++){
sum+=marks[i];
}
return (sum/(n*1.0f));
}
float median(){
sort();
display();
float med=0;
int tmp=(n+1)/2;
if(n%2==0) {
med=(marks[tmp-1]+marks[tmp])/2.0f;
}
else {
med=marks[tmp-1];
}
return med;
}
Prepared By: Piyush C. Patel Page 90
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

void mode(){
display();
float m=0.0f;
int count[]=new int[n];
for(int i=0;i<n;i++){
count[i]=1;
for(int j=i+1;j<n;j++){
if(marks[i]==marks[j]) {
count[i]++;
} }
}
int temp=1;
int temp1=-1;
for(int k=0;k<n;k++){
if(count[k]>temp) {
temp=count[k];
temp1=k;
}
}
System.out.println("Mode is/are .....");
if(temp1==-1) {
m=(3.0f*median())-(2.0f*mean());
System.out.println(m);
}
else{
for(int i=0;i<n;i++){
if(count[i]==temp) {
System.out.println(marks[i]);
}
} }
}
float variance(){
float mean=mean();
float sum=0;
System.out.println("\n\n (X-Xbar) | (X-Xbar)^2 \n");
Prepared By: Piyush C. Patel Page 91
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

for(int i=0;i<n;i++){
System.out.println((marks[i]-mean)+" | "+
((marks[i]-mean)*(marks[i]-mean)));
sum+=(marks[i]-mean)*(marks[i]-mean);
}
return (sum/((n-1)*1.0f));
}
float standard_deviation(){
float variance = variance();
return ((float)(Math.sqrt(variance)));
}
int specified_percentile(int nth) {
sort();
display();
int i=(int)Math.ceil(((nth*(n))/100.0f));
return (marks[i-1]);
}
float specified_quertile(int nth) {
sort();
display();
float sq=0.0f ;
float temp=((nth*(n+1))/4.0f);
int t=(int)(temp);
float k=temp%t;
if(k==0.0f) {
sq=marks[t-1];
}
else{
int i=(int)Math.ceil(temp);
sq=((marks[i-1]+marks[i-2])/2.0f);
}
return sq;
}
float interquarlite_rang(){
return (specified_quertile(3)-specified_quertile(1));
Prepared By: Piyush C. Patel Page 92
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
void load_from_csv(File f) throws IOException{
FileInputStream fis=new FileInputStream(f);
Vector v=new Vector();
v.add(fis);
loading_process(v,1);
}
void load_from_csv(InputStream istr) throws IOException{
Vector v1=new Vector();
v1.add(istr);
loading_process(v1,0);
}
void loading_process(Vector v,int ch1) throws IOException{
InputStream istr=null;
FileInputStream fis=null;
if(ch1==0){
istr=(InputStream)v.get(0);
}
else{
fis=(FileInputStream)v.get(0);
}
int asc=0,i=0;
int m[]=new int[60];
char[] str=new char[1];
String temp2="";
System.out.println("Your Data is as Bellow ....");
while(asc!=-1) {
if(ch1==0) {
asc=istr.read();
}
else{
asc=fis.read();
}
if(asc==44 || asc==13) {
int t=Integer.parseInt(temp2);
Prepared By: Piyush C. Patel Page 93
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

if(t>100 || t<0) {
System.out.println(t+" is not valid Mark...., is not Accepted");
}
else{
System.out.println(t+" is Accepted....");
m[i]=t;
i++;
}
temp2="";
}
else{
if(asc>=48 && asc<=57) {
str[0]=(char)asc;
String temp3=new String(str);
temp2=temp2.concat(temp3);
} }
}
System.out.println("Your meaningfull data from CSV file found as
follow....");
for (int j=0;j<i;j++){
System.out.println(m[j]);
}
int k=0;
while(k==0) {
System.out.print("\nSure you want to import this data ? (Y/N) :");
String ch=br.readLine();
if(ch.equalsIgnoreCase("Y") || ch.equalsIgnoreCase("N")){
if(ch.equalsIgnoreCase("Y")){
marks=new int[60];
n=i;
marks=m;
System.out.println("\n"+n+" Student's Mark imported....");
System.out.println("\nYour Data is Successfully
imported....");
}
Prepared By: Piyush C. Patel Page 94
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

else if(ch.equalsIgnoreCase("N")){
System.out.println("\nYour previous data is as it is......\n
Importation of data has been cancelled....");
}
k=1;
}
else{
System.out.println("Enter Y (for Yes) \nEnter N (for
No)... \nOtherwise System can't recognize....");
}
}
}
void sort(){
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(marks[i]>marks[j]) {
int temp=marks[i];
marks[i]=marks[j];
marks[j]=temp;
} }
}
}
void display(){
System.out.println("\n Your Data is .....\n");
for(int i=0;i<n;i++){
System.out.print(marks[i]+" ");
}
System.out.println("");
}
int get_size(){
return n;
}
}

Prepared By: Piyush C. Patel Page 95


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

24. Create a class called Statistical Data, which has capability of


maintaining data regarding multiple variables. It should have a
method to specify the variable names as String array and the
method to load values from a file regarding the variables. eg. We
consider two variables as percentage of marks in GCET exam and
percentage of marks in 1st year of MCA, Provide methods in the
class to compute the correlation coefficient between any two
variables, specified in the parameter. Test the class by computing
the correlation coefficient between the marks of GCET and marks
of 1st year MCA for students of your class.

Package gtu;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class StatisticalData
{
int v1[],v2[];
String v1name,v2name;
public StatisticalData()
{
v1name=new String("str1");
v2name=new String();
v1=new int[5];
v2=new int[5];
v2name="str2";
}
public void get()
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int i;
System.out.println("Enter First variable name and its values: ");

Prepared By: Piyush C. Patel Page 96


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

try{
v1name = br.readLine();
for(i=0;i<5;i++)
{
v1[i]=Integer.parseInt(br.readLine());
}
System.out.println("Enter Second variable name and its values: ");
v2name = br.readLine();
for(i=0;i<5;i++)
{
v2[i]=Integer.parseInt(br.readLine());
}
}
catch(IOException e)
{
System.out.println("Error while Reading from console");
}
}
public void correl_coeff()
{
double d,m1=0,m2=0,tatb=0,sab,ta2=0,tb2=0,sa,sb,cor_coef;
int i;
System.out.printf("%s: ",v1name);
for(i=0;i<5;i++)
{
System.out.printf("%d ",v1[i]);
}
System.out.println();
System.out.printf("%s: ",v2name);
for(i=0;i<5;i++)
{
System.out.printf("%d ",v2[i]);
m1=m1+v1[i];
m2=m2+v2[i];
}
Prepared By: Piyush C. Patel Page 97
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

System.out.println();
m1=m1/5;
m2=m2/5;
for(i=0;i<5;i++)
{
tatb=tatb+(v1[i]-m1)*(v2[i]-m2);
ta2=ta2+(v1[i]-m1)*(v1[i]-m1);
tb2=tb2+(v2[i]-m2)*(v2[i]-m2);
}
sab=tatb/4;
sa=ta2/5;
sb=tb2/5;
sa=Math.sqrt(sa);
sb=Math.sqrt(sb);
cor_coef=sab/sa*sb;
System.out.println("correlation co-efficient: " + cor_coef);
}
}

class P24
{
public static void main(String args[])
{
StatisticalData x=new StatisticalData();
x.get();
x.correl_coeff();
}
}

28. Write a class in Java to plot a Scatter diagram for the data
available from the instance of Statistical Data class defined in
exercise 24 earlier. Make a provision for selecting the variables for
which scatter diagram is to be plotted.

Package gtu;

Prepared By: Piyush C. Patel Page 98


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class StatisticalData
{
int v1[],v2[];
String v1name,v2name;
public StatisticalData()
{
v1=new int[5];
v2=new int[5];
v2name="str2";
}
public void get()
{

int i;

for(i=0;i<5;i++)
{
v1[i]=i*2;
}

for(i=0;i<5;i++)
{
v2[i]=i*5;
}

}
public int correl_coeff(int x)
{

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

Prepared By: Piyush C. Patel Page 99


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

{
return v1[x];
} return 0;
}
public double correl_ans()
{
double d,m1=0,m2=0,tatb=0,sab,ta2=0,tb2=0,sa,sb,cor_coef;
int i;
for(i=0;i<5;i++)
{
m1=m1+v1[i];
m2=m2+v2[i];
}
m1=m1/5;
m2=m2/5;
for(i=0;i<5;i++)
{
tatb=tatb+(v1[i]-m1)*(v2[i]-m2);
ta2=ta2+(v1[i]-m1)*(v1[i]-m1);
tb2=tb2+(v2[i]-m2)*(v2[i]-m2);
}
sab=tatb/4;
sa=ta2/5;
sb=tb2/5;
sa=Math.sqrt(sa);
sb=Math.sqrt(sb);
cor_coef=sab/sa*sb;
return cor_coef;
}
public int correl_coeff2(int x)
{
for(int i=0;i<5;i++)
{
return v2[x];
} return 0;
Prepared By: Piyush C. Patel Page 100
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
}

public class prog28{


public static void main(String[] args){
int i;
StatisticalData x=new StatisticalData();
x.get();
Integer temp;
Double ans;

JFrame jf = new JFrame("Inventory");


Container c = jf.getContentPane();

Object[] colnames = {"H1","H2"};


Object[][] data=new Object[10][10];
for(i=0;i<5;i++)
{
temp=x.correl_coeff(i);
data[i][0]=temp;
}
for(i=0;i<5;i++)
{
temp=x.correl_coeff2(i);
data[i][1]=temp;
}
ans=x.correl_ans();
data[i][0]="Correlation";
data[i][1]=ans;

jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}

Prepared By: Piyush C. Patel Page 101


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

});

JTable jt = new JTable(data, colnames);


JScrollPane scrollableTable = new JScrollPane(jt);
c.add(scrollableTable);
jf.setSize(500, 150);
jf.setVisible(true);

}
}

NOTEPAD

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class test extends JFrame implements ActionListener


{

MenuItem
newmenuitem,openmenuitem,exitmenuitem,savemenuitem,saveasmenu
item,pagesetupmenuitem,printmenuitem;
MenuItem
cutmenuitem,copymenuitem,pastemenuitem,selectallmenuitem;
MenuItem fontmenuitem;
MenuItem statusbarmenuitem;
MenuItem aboutmenuitem;

Prepared By: Piyush C. Patel Page 102


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

JTextArea jtext;
test()
{
super("Notepad");
//setLayout(null);
setBounds(0,0,500,500);

jtext=new JTextArea();

//to close the window

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_O
N_CLOSE);

MenuBar mbr=new MenuBar();

//FILEMENU

Menu filemenu=new Menu("File");

MenuShortcut new1=new
MenuShortcut(KeyEvent.VK_N);
MenuShortcut open=new MenuShortcut(KeyEvent.VK_O);
MenuShortcut save=new MenuShortcut(KeyEvent.VK_S);
MenuShortcut print=new MenuShortcut(KeyEvent.VK_P);
MenuShortcut exit=new MenuShortcut(KeyEvent.VK_Q);

newmenuitem=new MenuItem("New",new1);
openmenuitem=new MenuItem("Open",open);

Prepared By: Piyush C. Patel Page 103


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

savemenuitem=new MenuItem("Save",save);
saveasmenuitem=new MenuItem("Save As");
pagesetupmenuitem=new MenuItem("Page Setup");
printmenuitem=new MenuItem("Print",print);
exitmenuitem=new MenuItem("Exit",exit);

filemenu.add(newmenuitem);
filemenu.add(openmenuitem);
filemenu.add(savemenuitem);
filemenu.add(saveasmenuitem);
filemenu.addSeparator();
filemenu.add(pagesetupmenuitem);
filemenu.add(printmenuitem);
filemenu.addSeparator();
filemenu.add(exitmenuitem);

newmenuitem.addActionListener(this);
openmenuitem.addActionListener(this);
savemenuitem.addActionListener(this);
saveasmenuitem.addActionListener(this);
printmenuitem.addActionListener(this);
pagesetupmenuitem.addActionListener(this);
exitmenuitem.addActionListener(this);
mbr.add(filemenu);

//EDITMENU
Menu editmenu=new Menu("Edit");

MenuShortcut cut=new MenuShortcut(KeyEvent.VK_X);


MenuShortcut copy=new MenuShortcut(KeyEvent.VK_C);
MenuShortcut paste=new
MenuShortcut(KeyEvent.VK_V);

Prepared By: Piyush C. Patel Page 104


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

MenuShortcut selectall=new
MenuShortcut(KeyEvent.VK_A);

cutmenuitem=new MenuItem("Cut",cut);
copymenuitem=new MenuItem("Copy",copy);
pastemenuitem=new MenuItem("Paste",paste);
selectallmenuitem=new MenuItem("Select All",selectall);

editmenu.add(cutmenuitem);
editmenu.add(copymenuitem);
editmenu.add(pastemenuitem);
editmenu.add(selectallmenuitem);
mbr.add(editmenu);

//FORMATMENU

Menu formatmenu=new Menu("Format");

MenuShortcut font=new MenuShortcut(KeyEvent.VK_F);

fontmenuitem=new MenuItem("Font",font);
formatmenu.add(fontmenuitem);
mbr.add(formatmenu);

//VIEW
Menu viewmenu=new Menu("View");

MenuShortcut status=new
MenuShortcut(KeyEvent.VK_S,true);

statusbarmenuitem=new MenuItem("Status",status);
viewmenu.add(statusbarmenuitem);

Prepared By: Piyush C. Patel Page 105


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

mbr.add(viewmenu);

//HELP
Menu helpmenu=new Menu("Help");

MenuShortcut help=new
MenuShortcut(KeyEvent.VK_H,true);

aboutmenuitem=new MenuItem("About Notepad",help);


helpmenu.add(aboutmenuitem);
mbr.add(helpmenu);

setMenuBar(mbr);

//JScrollPane s=new
JScrollPane(this,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS
_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_
NEEDED);

add(jtext);
setVisible(true);

}
public void actionPerformed(ActionEvent e)
{

if(e.getActionCommand()=="Open")
{
try
{

Prepared By: Piyush C. Patel Page 106


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

FileDialog fd=new
FileDialog(this,"OPen",FileDialog.LOAD);
fd.setVisible(true);
jtext.setText("");
openFile(fd.getFile());
}
catch(Exception ae)
{}
}

if(e.getActionCommand()=="Save")
{
try
{
FileDialog fd=new
FileDialog(this,"Save",FileDialog.SAVE);
fd.setVisible(true);
File curfile=new
File(fd.getDirectory(),fd.getFile());

FileWriter fw=new FileWriter(curfile);

fw.write(jtext.getText());
fw.close();

}
catch(Exception ar)
{}
}
/*if(e.getActionCommand=="Exit")
{
try
{

Prepared By: Piyush C. Patel Page 107


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

setDefaultOperation(javax.swing.WindowConstants.DISPOSE_ON_C
LOSE);

}
catch(Exception er)
{}
}*/
if(e.getActionCommand()=="Help")
{
try
{
new HelpPage();
HelpPage hm=new HelpPage();
hm.setTitle("About Notepad");
hm.setBounds(100,100,300,300);
hm.setVisible(true);

}
catch(Exception ea){}
}

if(e.getActionCommand()=="New")
{
try
{
jtext.setText("");

}
catch(Exception em)
{}
}

Prepared By: Piyush C. Patel Page 108


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
public void openFile(String filename) throws Exception
{
RandomAccessFile rd=new
RandomAccessFile(filename,"r");

String str="";
do
{
str=rd.readLine();
if(str!=null)
jtext.append(str + "\n");
}while(str!=null);
rd.close();
}

}
class HelpPage extends JFrame
{
HelpPage()
{
super("About Notepad");
//setBounds(100,100,300,300);
// setVisible(true);
}
}

class notepad
{
public static void main(String s[])
{
test t=new test();

Prepared By: Piyush C. Patel Page 109


ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE
JAVA (MCA – III)
Practical Assignment

Enrolment No: 095020693167

}
}

Prepared By: Piyush C. Patel Page 110

You might also like