Personal Blog : My OUM(Open University Malaysia) journey starting from Jan 2011 till graduate.
Everyone can copy the contents in this blog and please leave some credit or a backlink to me... -Thank You-

Friday, April 6, 2012

OBJECT ORIENTED PROGRAMMING

QUESTION 1

OBJECT ORIENTED PROGRAMMING

Write a program that contains y array and the z array. Both arrays have the same size. Assign {6,1,5,9,5} to y and {2,13,6,15,2} to z. The program also has a method called sumLargeNumber( ) which has the following header:

int sumLargeNumber(int a[],int b[], int size)

The method will compare the values in both arrays and sum up all the elements that are the larger value at each index position. This sum will be the returned value of the method. For example:

int[] y = {6,1,5,9,5}

int[] z = {2,13,6,15,2}

The sumLargeNumber(y, z, 5) will return 45 (6+13+6+15+5).

Write the complete and practical program for the above problem.

==============ANSWER===============================
/*
* Question 1
* JavaApplication1
*/
package javaapplication1;
/**
* @author Azah
*/
public class JavaApplication1
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//Code application logic
int[] y = {6,1,5,9,5};
int[] z = {2,13,6,15,2};
int total = sumLargeNumber(y, z, 5);
System.out.println(total);
}
public static int sumLargeNumber(int a[], int b[], int size)
{
int sum = 0;
if(a.length == size && b.length == size)
{
int i = 0;
while(i < size)
{
int value_a = a[i];
int value_b = b[i];
if(value_a > value_b)
sum += value_a;
else if(value_a < value_b)
sum += value_b;
else
sum += value_a;
i++;
}
}
return (int) sum ;
}
}

No comments:

Post a Comment

Table Grade