Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: i am new..hello^^

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    australia
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default i am new..hello^^

    Whats wrong with last part(bold font)??why i cant found the error ??


    public class ArrayTwoDimensionall {
     
    	public static void main(String[] args) {
    		int [][] hour = {{2,4,3,4,5,8,8}
    						,{7,3,4,3,3,4,4}
    						,{3,3,4,3,3,2,2}
    						,{9,3,4,7,3,4,1}
    						,{3,5,4,3,6,3,8}};
     
    		String [][] week = {{"Sun"},{"Mon"},{"Tue"},{"Wed"},
    							{"Thu"},{"Fri"},{"Sat"}};
     
    		System.out.print("		");
    		for(int i=0;i<week.length;i++)
    		{
     
    			for(int j=0;j<week[i].length;j++)
    			{
    				System.out.print(week[i][j]+"	");
    			}	
    		}
     
    		System.out.println();
     
    		displayAllArrayElements(hour);
     
    		int []total = calculateEmployeeHours(hour);
    		for(int i =0;i<total.length;i++){
    		System.out.println("The total of working hours employee "+(i+1)+" is "+ total[i]);
    		}
     
    		System.out.println();
     
    		int []payment = calculateMonthlyPayment(total);
    		for(int i=0;i<total.length;i++){
    		System.out.println("The monthly payment of employee "+(i+1)+" is RM "+ payment[i]);
    		}
     
    		double []average = calculateAverage(hour);
    		for (int i=0;i<average.length;i++)
    		{
    			for (int j=0;j<week[i].length;j++)
    			{
    			System.out.println("The average working hours for "+week[i][j]+" is "+average[i]);
    			}
    		}
     
    		}
    			public static void displayAllArrayElements(int [][]hour){
    				for(int i= 0;i<hour.length;i++)
    				{
    					System.out.print("Employee "+(i+1)+"|"+"	");
    					for(int j=0;j<hour[i].length;j++)
    					{
    						System.out.print(hour[i][j]+"	");	
    					}
    					System.out.println();			
    		}
    			System.out.println();
    	}
     
     
    			public static int [] calculateEmployeeHours(int[][]hour){
    				int[]total = new int[5];
    				for(int i=0;i<hour.length;i++)
    				{
    					total[i] = 0;
    					for(int j=0;j<hour[i].length;j++)
    					{
    						total[i] += hour[i][j];
     
    					}	
    				}
    				return total;	
    			}
     
     
     
    			public static int [] calculateMonthlyPayment(int[]total){
    				int []payment = new int [5];
    				for (int i=0;i<total.length;i++)
    				{
    					payment[i] = 0;
    					for(int j=0;j<total.length;j++)
    					{
    						payment[i] = ( total[i] * 50 * 4 );
    					}
    				}
    				return payment;
    			}		
     
    			[B]public static double []calculateAverage(int [][]hour){
    				double []average = new double[7];
    				for(int i=0;i<hour.length+2;i++)
    				{
    					int sum = 0;
    					for(int j=0;j<hour.length;j++)
    					{
    						sum += hour[j][i];
    						average[i] = (double)(sum) / 5;
    				}
    					return average;
    				}[/B]
    			}
    }
    Last edited by Freaky Chris; December 25th, 2011 at 05:55 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: i am new..hello^^

    Can you describe the problem?
    Do you get a compiler error? If so, Please copy and paste the full text here.
    If the results are wrong, please post the output and add comments to it saying what is wrong with it and show what the output should be.

    Is your problem with integer division? 4/5 = 0
    Change the divisor to a double: 5.0

  3. The Following User Says Thank You to Norm For This Useful Post:

    martinlim (December 27th, 2011)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: i am new..hello^^

    why i cant found the error ??
    What's the error? Paste the error/exception message here. What is int [] [] hour ?

  5. The Following User Says Thank You to Mr.777 For This Useful Post:

    martinlim (December 27th, 2011)

  6. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: i am new..hello^^

    You nearly have it. Look at how you iterated through a 2D array in the calculateEmployeeHours (which looks like it works correctly) and compare it to the erroneous calculateAverage. The first is adding and the second is multiplying but look at how the for loop's are structured.

  7. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    martinlim (December 27th, 2011)

  8. #5
    Junior Member
    Join Date
    Dec 2011
    Location
    australia
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: i am new..hello^^

    Oic i fix it ady ..thx ya

  9. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: i am new..hello^^

    Quote Originally Posted by martinlim View Post
    Oic i fix it ady ..thx ya
    Mark it SOLVED.