Tuesday, 5 March 2013

Sum/Addition Minijava Program

//By Michael Lashley 

//The following program states what the followiing code does for example, it sums all the numbers 
//in an array and returns the overall total. 

//Class is created
class AdditionTestMini
    public static void main(String[] a){ 
System.out.println(((new Sum()).init()).sumAdd());

//class is created for the sum of enteries 
class Sum
    int[] data;
    //below the line of code "getLenght" returns the length of the array (number of elements in the array).
    public int getLength()
        return data.length;

    //this method initials values to specific positions in the array.
    public Sum init()
int index;
        data = new int[5];
        data[0] = 4;
        data[1] = 6;
        data[2] = 8;
        data[3] = 12;
        data[4] = 14;
        return this;               

    //this method calculates/add together the value of all elements in the array stated above.
    public int sumAdd()
        int i;
        int total; 
        int current;
        current = 0;/
        total = 0;
        i = 0;

        //this while loop checks the condition, that the current position in the array is less than the length of the array.
        while (i < (data.length)){
                current = data[i];
                total = total + current;
                i = i + 1;
        return total;    

No comments:

Post a Comment