[ Statistical_data.java ]
import java.io.*;
class Statistical_data
{
int data[];
Statistical_data()
{
data=new int[1];
}
void setdata(int value[])
{
data=new int[value.length];
for(int i=0;i<data.length;i++)
{
data[i]=value[i];
}
}
int add()
{
int sum=0,i;
for(i=0;i<data.length;i++)
{
sum=sum+data[i];
}
return sum;
}
void reset()
{
for(int i=0;i<data.length;i++)
{
data[i]=0;
}
data=new int[1];
}
double mean()
{
return (double)this.add()/data.length;
}
double median()
{
int size,n=0;
double median;
size=data.length;
this.sort();
if (size%2 == 0)
{
n=size/2 - 1 ;
median=((double)data[n] + data[n+1])/2;
}
else
{
System.out.println("ODD" + size);
n=(size)/2;
median=data[n];
}
return median;
}
double mode()
{
double mode;
mode= ( (double)3*median() ) - ( 2*mean() );
return mode;
}
double variance()
{
int size=data.length;
double mean=this.mean(),sum=0;
for(int i=0;i<size;i++)
{
sum=sum+Math.pow((data[i]-mean),2);
}
return sum/(size-1);
}
double standard_deviation()
{
return Math.sqrt(this.variance());
}
double percentile(int no)
{
double perc=0;
int size=data.length, temp;
if(no<0 && no>100)
{
System.out.println("Percentile should be more than 0 and less than 100..");
perc=-1;
}
else
{
temp=(no*size)/100;
if( temp == (double)(no*size)/100)
{
perc=(double)(data[temp]+data[temp-1])/2;
}
else
{
perc=(double)data[temp];
}
}
return perc;
}
double quartile(int no)
{
double quart=0;
int size=data.length, temp;
if(no<1 && no>3)
{
System.out.println("Quartile should be more than 1 and less than 3..");
quart=-1;
}
else
{
if(no==1)
no=25;
else if(no==2)
no=50;
else
no=75;
temp=(no*size)/100;
if( temp == (double)(no*size)/100)
{
quart=(double)(data[temp]+data[temp-1])/2;
}
else
{
quart=(double)data[temp];
}
}
return quart;
}
double IQR()
{
return this.quartile(3)-this.quartile(1);
}
void sort()
{
int temp,size;
size=data.length;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(data[i]<data[j])
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
}
}
class student{
int rollno,marks[];
String name;
student()
{
marks=new int[6];
}
void getdata() throws IOException
{
name=new String();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter RollNo");
rollno=Integer.parseInt(br.readLine());
System.out.println("Enter Name");
name=br.readLine();
System.out.println("Enter Marks");
for(int i=0;i<6;i++)
{
System.out.println("Subject " + (i+1) + " : ");
marks[i]=Integer.parseInt(br.readLine());
}
}
int gettotalmarks()
{
int total=0;
for(int i=0;i<6;i++)
{
total=total+marks[i];
}
return total;
}
}
class teststats{
public static void main(String args[]) throws IOException
{
int total[],no;
student stud[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number of Students:");
no=Integer.parseInt(br.readLine());
stud=new student[no];
total=new int[no];
for(int i=0;i<no;i++)
{
stud[i]=new student();
stud[i].getdata();
}
for(int i=0;i<no;i++)
{
total[i]=stud[i].gettotalmarks();
}
total=new int[args.length];
for(int i=0;i<total.length;i++)
{
total[i]=Integer.parseInt(args[i]);
}
Statistical_data stats=new Statistical_data();
stats.setdata(total);
System.out.println("MEAN:"+stats.mean());
System.out.println("MEDIAN:"+stats.median());
System.out.println("MODE:"+stats.mode()); System.out.println("PERCENTILE:"+stats.percentile(25));
System.out.println("VARIANCE:"+stats.variance());
System.out.println("STANDARD DEVIATION:"+stats.standard_deviation());
System.out.println("Q3:"+stats.quartile(3));
System.out.println("Q2:"+stats.quartile(2));
System.out.println("Q1:"+stats.quartile(1));
System.out.println("INTERQUARTILE DEVIATION:"+stats.IQR());
}
}
No comments:
Post a Comment