Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

calculate values of array

import java.util.Scanner;

public class Sum {
   private int [] arr;
   private int size ;
   private int arrSum ;
   private int n;
   private int sigma = 0 ;

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

    public Sum(){

    }
    public Sum(int size) {
        this.size = size;
        this.arr = new int [size];
    }

    public void setArrAndSize(int size){
        this.arr = new int[this.size = size];
        System.out.println("	Array has been initialized
");
    }

    public void enterValues(){
        if(this.arr == null){
            System.out.println("	Enter size of the array
");
            int size = scanner.nextInt();
            setArrAndSize(size);
        }
        System.out.println("	(Enter "+this.arr.length+" numbers)
");
        for(int i = 0; i < this.arr.length ; ++i){
            this.arr[i] = scanner.nextInt();
        }
    }

    public void calculateValuesOfArr(){
        this.arrSum = recursiveSigma(this.size-1);
    }



    private int recursiveSigma( int l){
        if(l == 0 )
            return arr[l];

        return this.arr[l] + recursiveSigma(l-1);

    }

    public  void sigma(int n){
        this.n = n ;
        calculatSigma(n);

    }
    private int  calculatSigma(int n){
        if(n == 0)
             return this.sigma ;
        this.sigma = n + calculatSigma(n-1);
        return this.sigma;
    }

    public void printSumOfValues(){
        System.out.println("Array values = "+this.arrSum+"
");
    }

    public void printSigma(){
        System.out.println("Sigma "+ this.n+" = "+this.sigma+"
");
    }
}
Comment

calculate values of array

import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {

        Sum arr = new Sum();
        setArraysize(arr,5);
        enterValues(arr);
        System.out.println("Sigma ==> ");
        int sigma = 0;
        sigma = scanner.nextInt();
        calculateSigma(sigma);
        arr.calculateValuesOfArr();
        arr.printSumOfValues();


    }
    public static void setArraysize(Sum arr,int size){
        arr.setArrAndSize(size);
    }

    public static void  calculateSigma(int n){
        Sum sigma = new Sum();
        sigma.sigma(n);
        sigma.printSigma();
    }

    public static void enterValues(Sum arr){
        arr.enterValues();
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to save rich text format in database using java 
Java :: Java Insert Elements to EnumSet 
Java :: geometric primitive 
Java :: javafx choicebox get index 
Java :: conky cpu temperature 
Java :: java resultset.absolute 
Java :: change focus of edittext android when click outside 
Java :: add element to queue java 
Java :: how to read space separated characters in java 
Java :: login in java with 3 attepmtps 
Java :: can you automate mouseclicks with java 
Java :: open application programelly android studio 
Java :: The Unit Circle Codehs 
Java :: gif to blob java 
Java :: findby(xpath selenium java) 
Java :: for each loop summation 
Java :: string join in java 
Java :: como detener un void java 
Java :: disable listview readonly 
Java :: jagermeister price in bangalore 
Java :: javafx add vm options 
Java :: using handler runnable for refresh android 
Java :: java code to compare csv file against a table 
Java :: what is assignment in java with example 
Java :: convert zipfile to bytebuffer 
Java :: Create all possible substrings of a string java 
Java :: Java Enum toString() 
Java :: 4 byte timestamp java 
Java :: TestNG Data Provider example 
Java :: java difrence betwen x++ and ++x 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =