Thursday, October 9, 2008

Java - Arrays

Calling program:
public class runmyArray3
{
public static void main(String[] str)
{
myArray3 m1 = new myArray3();
m1.getData();
m1.findLargest();
}
}

Called program:
public class myArray3
{
int[] n = new int[3];
int largest;

public void getData()
{
Scanner input = new Scanner(System.in);
for (int k = 0; k < n.length; k++)
{
System.out.printf("Enter value for n : ");
n[k] = input.nextInt();
}
}

public void findLargest()
{
largest = n[0];
for (int k = 0; k < n.length ; k++)
{
if (largest < n[k])
{
largest = n[k];
}
}
System.out.printf("\nLargest is %d\n", largest);
}
}

No comments:

Post a Comment