Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default I have trouble understanding this program!

    Im confuse with which method are those codes in green are calling.
    somebody please explain to me:confused::confused::confused:
    THANKS!:)

    the output is
    2.00,2.00,2.00
    2.00, 2.00, 2.00
    3.00,3.00,3.00
    4.00,4.00,4.00
    Java Code:
    public class ArrayTest
    {
    public static void main(String args[])
    {
    int arr[][] = {{1,3}, {1,2,3}};
    Array sub = new Array();
    
    for(int i=0;i<arr.length;i++)
    System.out.printf("%.2f\n", [COLOR="Green"]sub.avg(arr[i]));[/COLOR]
    System.out.printf("%.2f\n\n", [COLOR="green"]sub.avg(arr)[/COLOR]);
    
    for(int i=0;i<arr.length;i++)
    	for(int j=0;j<arr[i].length;j++)
    		[COLOR="green"]sub.change(arr[i][j]);[/COLOR]
    
    for(int i=0;i<arr.length;i++)
    System.out.printf("%.2f\n",[COLOR="green"] sub.avg(arr[i]));[/COLOR]
    System.out.printf("%.2f\n\n", [COLOR="green"]sub.avg(arr));[/COLOR]
    
    for(int i=0;i<arr.length;i++)
    	[COLOR="green"]sub.change(arr[i]);[/COLOR]
    
    for(int i=0;i<arr.length;i++)
    System.out.printf("%.2f\n", [COLOR="green"]sub.avg(arr[i]));[/COLOR]System.out.printf("%.2f\n\n", [COLOR="green"]sub.avg(arr));[/COLOR]
    
    sub.change(arr);
    
    for(int i=0;i<arr.length;i++)
    System.out.printf("%.2f\n", [COLOR="green"]sub.avg(arr[i]));[/COLOR]System.out.printf("%.2f\n\n",[COLOR="green"] sub.avg(arr));[/COLOR]
    }
    }
    Java Code:
    public class Array
    {
    public double avg(int arr[])
    {
    double total=0.0;
    for(int i=0;i<arr.length;i++)
    total += arr[i];
    return (total/arr.length);
    }
    
    public double avg(int arr[][])
    {
    		double total=0.0;
    		int cnt=0;
    
    	for(int i=0;i<arr.length;i++)
    		for(int j=0;j<arr[i].length;j++)
    		{
    	total += arr[i][j];
    	cnt++;
    		}
    	return (total/cnt);
    }
    
    public void change(int arr)
    {
    	arr++;
    }
    
    public void change(int arr[])
    {
    	for(int i=0;i<arr.length;i++)
    	arr[i]++;
    }
    
    public void change(int arr[][])
    {
    	for(int i=0;i<arr.length;i++)
    		for(int j=0;j<arr[i].length;j++)
    			arr[i][j]++;
    }
    }
    Last edited by PureAwesomeness; 03-16-2009 at 05:24 AM.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    two avg(int arr[]) and avg(int arr[][]) intake diff input

    sub.avg(arr[i]) call avg(int arr[])
    sub.avg(arr) call avg(int arr[][])

Similar Threads

  1. Having trouble understanding Class Graphic
    By Bernard Robitaille in forum JCreator
    Replies: 1
    Last Post: 04-18-2009, 03:55 AM
  2. Replies: 2
    Last Post: 02-23-2009, 03:18 AM
  3. [SOLVED] Trouble understanding or expressions
    By hungdukie in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 02:24 AM
  4. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 05:25 AM
  5. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •