In: Computer Science
Use Java
Sample values from an experiment often need to be smoothed out. One simple approach is to replace each value in an array with the average of the value and its two neighboring values (or one neighboring value if it is at either end of the array). Given a class Data with instance fields
private double[] values;
private double valuesSize;
implement a method
public void smooth()
that carries out this operation. You should not create another array in your solution.
/*========================initial instruction begain===================
create package smoothning using any editor
crate a class by rigth clicking on package at tool bar and->new->select new class->and give name smooth
*if you are not good at create a new class than put in same class
================ instruction end=======================================*/
/*------if you have any query please text it again-------------------*/
//===============code begain===================================
/*==================== these is the class smooth=========================*/
package smoothning;
//package require to take input
import java.util.Scanner;
public class smooth {
//-----------Declaration start---------------------
private double value[]= new double[10];
private double valuesize;
//-------------Declaration end--------------------
//------when obj.take_input() function is call----
public void take_input(){
//-Create object sc to take input from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Element want for smoothing to be perform on that");
valuesize=sc.nextDouble(); //how many number of element you want
System.out.println("Please Enter the element for smoothing ");
for(int i=0;i<valuesize;i++){
value[i]=sc.nextDouble();//---taking each element
}
}
//------------take_input() function end-------------
//--------------when obj.smoothed_fun( is call---------
public void smoothed_fun(){
double temp,temp1; //temporary variable is declare
int i;
//no need for smoothing
temp=value[0];
value[0]=value[1];
for(i=1;i<valuesize-1;i++){
temp1=value[i];
value[i]=(temp+value[i+1])/2;
temp=temp1;
}
value[i]=temp;
System.out.println("----------------------------------");
for(i=0;i<valuesize;i++)
System.out.print(value[i]+" ");
System.out.println("\n----------------------------------");
}
}
/*==========================main function===============================
/*
from here the main programing is begain
for smoothoing process we declare class smooth
and call two function
*/
package smoothning;
public class Smoothning {
public static void main(String[] args) {
//Declare an object of class smooth
// where obj is the object
smooth obj=new smooth();
//calling function take_input() to make code user friendly
obj.take_input();
//here the smoothing is perform
obj.smoothed_fun();
}
}
o/p:===========================================================
run:
Enter the Element want for smoothing to be perform on that
5
Please Enter the element for smoothing
1.2
2.2
3.2
4.2
5.2
----------------------------------
2.2 2.2 3.2 4.2 4.2
----------------------------------
BUILD SUCCESSFUL (total time: 32 seconds)
//==================================================================