3 Replies - 816 Views - Last Post: 22 April 2012 - 07:11 PM Rate Topic: -----

#1 maria775  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-April 12

2DArrayOperations

Posted 21 April 2012 - 11:17 PM

Hi very body, I'm writing this class named 2DArrayOperations with following the static methods:
(1) getTotal: This method should accept a two-dimensional array as its argument and return the total
of the values in the array. Write overloaded versions of this method that work with int, float, double, and long arrays.
(2) getAverage: This method should accept a two-dimensional array as its argument and return the average of the values in the array. Write overloaded versions of this method that work with int, float, double, and long arrays.
(3) getRowTotal: This method should accept a two-dimensional array as its first argument and and integer as its
second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row. Write overloaded versions of this method that work with
int,float, double, and long arrays.
(4) getColumnTota: This method should accept a two-dimensional array as its first argument and and integer as its second argument. The second argument should be the subscript of a column in the array. The method should return the total of the values in the specified row. Write overloaded versions of this method that work with int,float, double, and long arrays.
(5) getHighestInRow: This method should accept a two-dimensional
array as its first argument and and integer as its second argument.
The second argument should be the subscript of a row in the array.
The method should return the highest of the values in the specified
row. Write overloaded versions of this method that work with int,
float, double, and long arrays.
(6) getLowestInRow: This method should accept a two-dimensional array as its first argument and and integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row. Write overloaded versions of this method that work with int, float, double, and long arrays.
Demonstrate the class in a complete program with test data stored in two-dimensional array of various data types
I have done everything above

This my 2DArrayOperations class code
public class TwoDArrayOperations 
{

 	/*
	 The getTotal method accepts a two-dimensional array as its 
	 argument and returns the total of the values in the array.
	 In the form of an (int).
	 */

	public static int getTotal(int [][] testOne)
	{
		int total; // Accumulator.
		
		total = 0; // Set the accumulator to 0.
		
		// Sum the array elements.
		for (int row = 0; row < testOne.length; row++ )
		{
			for (int col = 0; col < testOne[row].length; col++ )
				total += testOne[row][col];
	
		}
		
		// Return the total. 
		return total;
	} 

	/*
	 The getTotal method accepts a one-dimensional array as its 
	 argument and returns the total of the values in the array.
	 In the form of an (float).
	 */

	public static float getTotal(float [][] testTwo)
	{
		float total; // Accumulator.

		total = 0; // Set the accumulator to 0. 
		
		// Sum the array elements.
		for (int row = 0; row < testTwo.length; row++ )
		{
			for (int col = 0; col < testTwo[row].length; col++ )
				total += testTwo[row][col];
	
		}
		
		// Return the total. 
		return total;
	}

 	/*
	 The getTotal method accepts a one-dimensional array as its 
	 argument and returns the total of the values in the array.
	 In the form of an (double).
	 */

	public static double getTotal(double [][] testThree)
	{
		double  total; // Accumulator.

		total = 0; // Set the accumulator to 0.
		
		// Sum the array elements.
		for (int row = 0; row < testThree.length; row++ )
		{
			for (int col = 0; col < testThree[row].length; col++ )
				total += testThree[row][col];
	
		}
		
		// Return the total. 
		return total;
	} 

	/*
	 The getTotal method accepts a one-dimensional array as its 
	 argument and returns the total of the values in the array.
	 In the form of an (long).
	 */

	public static long getTotal(long [][] testFour)
	{
		long total; // Accumulator.

		total = 0; // Set the accumulator to 0.
		
		// Sum the array elements.
		for (int row = 0; row < testFour.length; row++ )
		{
			for (int col = 0; col < testFour[row].length; col++ )
				total += testFour[row][col];
	
		}
		
		// Return the total. 
		return total;
	}

	// ============ End of the getToal method. ==========

	/*
	 The getAverage method should accept a two-dimensional array as 
	 its argument and return the average of the values in the array.
	 In the form of an (int).
	 */

	public static int getAverage(int [][] testOne)
	{
		int total = 0; // Initialize accumulator.
		int average; // Will hold the average.

		// Add up all the values in the array.
		for (int row = 0; row < testOne.length; row++  )
			for (int col = 0; col < testOne[row].length; row++)
				total += testOne[row][col];

		// Calculate the Average.
		return average =  total / testOne.length;
	}

	/*
	 The getAverage method should accept a two-dimensional array as 
	 its argument and return the average of the values in the array.
	 In the form of an (float).
	 */

	public static float getAverage(float [][] testTwo)
	{
		float total = 0; // Initialize accumulator.
		float average; // Will hold the average.

		// Add up all the values in the array.
		for (int row = 0; row < testTwo.length; row++  )
			for (int col = 0; col < testTwo[row].length; row++)
				total += testTwo[row][col];

		// Calculate the Average.
		return average =  total / testTwo.length;
	}

	/*
	 The getAverage method should accept a two-dimensional array as 
	 its argument and return the average of the values in the array.
	 In the form of an (int).
	 */

	public static double getAverage(double [][] testThree)
	{
		double total = 0; // Initialize accumulator.
		double average; // Will hold the average.

		// Add up all the values in the array.
		for (int row = 0; row < testThree.length; row++  )
			for (int col = 0; col < testThree[row].length; row++)
				total += testThree[row][col];

		// Calculate the Average.
		return average = total / testThree.length;
	}

	/*
	 The getAverage method should accept a two-dimensional array as 
	 its argument and return the average of the values in the array.
	 In the form of an (int).
	 */

	public static long getAverage(long [][] testFour)
	{
		long total = 0; // Initialize accumulator.
		long average; // Will hold the average.

		// Add up all the values in the array.
		for (int row = 0; row < testFour.length; row++  )
			for (int col = 0; col < testFour[row].length; row++)
				total += testFour[row][col];

		// Calculate the Average.
		return average = total / testFour.length;
	}

	// ============ End of the getAverage method. ==========

	/*
	 The getRowTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the
     specified row. In the form of an (int).
	 */

	public static int getRowTotal(int [][] testOne)
	{
		int total ; // Accumulator.
		total = 0;	// Set the accumulator to 0.

		for (int col = 0; col < testOne.length; col++)

		//Sum a row.
		for (int row = 0; row < testOne[row].length; row++)
			total += testOne[row][col];
		
		// Return the total.
		return total;
	}

	/*
	 The getRowTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the
	 In the form of an (float).
	 */

	public static float getRowTotal(float [][] testTwo)
	{
		float total; // Accumulator.
		total = 0; // Set the accumulator to 0.
		
		// Add up all the values in the array.
		for (int row = 0; row < testTwo.length; row++  )

		//Sum a row.
		for (int col = 0; col < testTwo[row].length; col++)
			total += testTwo[row][col];
		
		// Return the total.
		return total;
	}

	/*
	 The getRowTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the
	 In the form of an (double).
	 */

	public static double getRowTotal(double [][] testThree)
	{
		double total; // Accumulator.
				total = 0; // Set the accumulator to 0.

		// Add up all the values in the array.
		for (int row = 0; row < testThree.length; row++  )

		//Sum a row.
		for (int col = 0; col < testThree[row].length; col++)
			total += testThree[row][col];
		
		// Return the total.
		return total;
	}

	/*
	 The getRowTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the.
	 In the form of an (long).
	 */

	public static long getRowTotal(long [][] testFour)
	{
		long total; // Accumulator.
		total = 0;	// Set the accumulator to 0.		

		// Add up all the values in the array.
		for (int row = 0; row < testFour.length; row++  )

		//Sum a row.
		for (int col = 0; col < testFour[row].length; col++)
			total += testFour[row][col];
		
		// Return the total.
		return total;
	}
	
	// ============ End of the getRowTotal method. ==========

	/*
	 The getColumnTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the
     specified row. In the form of an (int).
	 */

	public static int getColumnTotal(int [][] testOne)
	{
		int total ; // Accumulator.
		total = 0;	// Set the accumulator to 0.

		for (int col = 0; col < testOne[0].length; col++)

		//Sum a column.
		for (int row = 0; row < testOne.length; row++)
			total += testOne[row][col];
		
		// Return the total.
		return total;
	}

	/*
	 The getColumnTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the
	 In the form of an (float).
	 */

	public static float getColumnTotal(float [][] testTwo)
	{
		float total; // Accumulator.
		total = 0; // Set the accumulator to 0.
		
		// Add up all the values in the array.
		for (int col = 0; col < testTwo[0].length; col++)

		//Sum a column.
		for (int row = 0; row < testTwo.length; row++)
			total += testTwo[row][col];
		
		// Return the total.
		return total;
	}

	/*
	 The getColumnTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the
	 In the form of an (double).
	 */

	public static double getColumnTotal(double [][] testThree)
	{
		double total; // Accumulator.
		total = 0; // Set the accumulator to 0.

		// Add up all the values in the array.
		for (int col = 0; col < testThree[0].length; col++)

		//Sum a column.
		for (int row = 0; row < testThree.length; row++)
			total += testThree[row][col];
		
		// Return the total.
		return total;
	}

	/*
	 The getColumnTotal method should accept a two-dimensional array as 
	 its first argument and and integer as its second argument. 
	 The second argument should be the subscript of a row in the 
	 array. The method should return the total of the values in the.
	 In the form of an (long).
	 */

	public static long getColumnTotal(long [][] testFour)
	{
		long total; // Accumulator.
		total = 0;	// Set the accumulator to 0.		

		// Add up all the values in the array.
		for (int col = 0; col < testFour[0].length; col++ )

		//Sum a column.
		for (int row = 0; row < testFour.length; row++)
			total += testFour[row][col];
		
		// Return the total.
		return total;
	}
	
	// ============ End of the getColumnTotal method. ==========

	/*
	 The getHigestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (int).
	 */

	public static int getHigestInRow(int [][] testOne)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		int highest = testOne[0][0]; 
        highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testOne.length; row++  )
		{
			for (int col = 0; col < testOne.length ; col++)
			
			    if (testOne[row][col] > highest)
				   highest = testOne[row][col];
		}

		// Return the highest value.
		return highest;
	}

	/*
	 The getHigestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (float).
	 */

	public static float getHigestInRow(float [][] testTwo)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		float highest = testTwo[0][0]; 
        highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testTwo.length; row++  )
		{
			for (int col = 0; col < testTwo.length ; col++)
			
			    if (testTwo[row][col] > highest)
				   highest = testTwo[row][col];
		}

			

		// Return the highest value.
		return highest;
	}

	/*
	 The getHigestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (double).
	 */

	public static double getHigestInRow(double [][] testThree)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		double highest = testThree[0][0];
		highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testThree.length; row++  )
		{
			for (int col = 0; col < testThree.length ; col++)
			
			    if (testThree[row][col] > highest)
				   highest = testThree[row][col];
		}

			

		// Return the highest value.
		return highest;
	}

	/*
	 The getHigestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (long).
	 */

	public static long getHigestInRow(long [][] testFour)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		long highest = testFour[0][0]; 
		highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testFour.length; row++  )
		{
			for (int col = 0; col < testFour.length ; col++)
			
			    if (testFour[row][col] > highest)
				   highest = testFour[row][col];
		}
		// Return the highest value.
		return highest;
	}

	// ============ End of the getHighest method. ==========

	/*
	 The getlowestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (int).
	 */

	public static int getlowestInRow(int [][] testOne)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		int highest = testOne[0][0]; 
        highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testOne.length; row++  )
		{
			for (int col = 0; col < testOne.length ; col++)
			
			    if (testOne[row][col] > highest)
				   highest = testOne[row][col];
		}

		// Return the highest value.
		return highest;
	}

	/*
	 The getHigestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (float).
	 */

	public static float getlowestInRow(float [][] testTwo)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		float highest = testTwo[0][0]; 
        highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testTwo.length; row++  )
		{
			for (int col = 0; col < testTwo.length ; col++)
			
			    if (testTwo[row][col] > highest)
				   highest = testTwo[row][col];
		}

			

		// Return the highest value.
		return highest;
	}

	/*
	 The getlowestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (double).
	 */

	public static double getlowestInRow(double [][] testThree)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		double highest = testThree[0][0];
		highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testThree.length; row++  )
		{
			for (int col = 0; col < testThree.length ; col++)
			
			    if (testThree[row][col] > highest)
				   highest = testThree[row][col];
		}

			

		// Return the highest value.
		return highest;
	}

	/*
	 The getlowestInRow method should accept a two-dimensional array as its
	 first argument and and integer as its second argument. The second
	 argument should be the subscript of a row in the array.The method
	 should return the highest of the values in the specified row
	 In the form of an (long).
	 */

	public static long getlowestInRow(long [][] testFour)
	{
		/*
		 Store the first value in the testOne array in the
		 variable highest.
		*/
		long highest = testFour[0][0]; 
		highest = 0;	// Set the  highest to 0.

		for (int row = 0; row < testFour.length; row++  )
		{
			for (int col = 0; col < testFour.length ; col++)
			
			    if (testFour[row][col] > highest)
				   highest = testFour[row][col];
		}
		// Return the highest value.
		return highest;
	}

	// ============ End of the getHighest method. ==========
}


This my 2DArrayOperationsDemo code
public class TwoDArrayOperationsDemo 
{
	public static void main(String[] args) 
	{
        /*
		 Four different kinds of arrays with four different 
		 data types.
		*/

		// First array named testOne.
		int[][] testOne = {{1, 2, 3, 4, 5, 6,
		                    7, 8, 9 , 10}};

		// Second array named testTwo.
		float[][] testTwo = {{11, 12, 13, 14, 15, 
			                 16, 17, 18, 19, 20 }};

		// Third array named testThree.
		double[][] testThree = {{2.1, 2.2, 2.3, 2.4,
							     2.5, 2.6, 2.7, 2.9,
							     3.0, 3.1, 3.2, 3.3 }};

		long[][] testFour = {{34, 35, 36, 37, 38,
					         39, 40, 41, 42, 43,
						     44, 45, 46, 47, 48,
						     49, 50, 51, 52, 53}};


		// Display the first array information:

		System.out.print("The following information is " +
							"for the testOne array:" + "\n" );
		
		System.out.println("The total is " +
						  TwoDArrayOperations.getTotal(testOne));
		System.out.println("The average is " +
						  TwoDArrayOperations.getAverage(testOne));
		System.out.println("The row total is " +
						  TwoDArrayOperations.getRowTotal(testOne));
		System.out.println("The column total is " +
						  TwoDArrayOperations.getColumnTotal(testOne));
		System.out.println("The heightest is " +
						  TwoDArrayOperations.getHigestInRow(testOne));
				System.out.println("The lowest is " +
						  TwoDArrayOperations.getlowestInRow(testOne));
		System.out.println();
		
		// Display the second array information:

		System.out.print("The following information is " +
							"for the testTwo array:" + "\n" );

		System.out.println("The total is " +
						  TwoDArrayOperations.getTotal(testTwo ));
		System.out.println("The average is " +
						  TwoDArrayOperations.getAverage(testTwo ));
		System.out.println("The row total is " +
						  TwoDArrayOperations.getRowTotal(testTwo ));
		System.out.println("The column total is " +
						  TwoDArrayOperations.getColumnTotal(testTwo ));
		System.out.println("The heightest is " +
						  TwoDArrayOperations.getHigestInRow(testTwo ));
				System.out.println("The lowest is " +
						  TwoDArrayOperations.getlowestInRow(testTwo ));
		System.out.println();
		
		// Display the third array information:
		
		System.out.print("The following information is " +
							"for the testThree array:" + "\n" );

		System.out.println("The total is " +
						  TwoDArrayOperations.getTotal(testThree));
		System.out.println("The average is " +
						  TwoDArrayOperations.getAverage(testThree));
		System.out.println("The row total is " +
						  TwoDArrayOperations.getRowTotal(testThree));
		System.out.println("The column total is " +
						  TwoDArrayOperations.getColumnTotal(testThree));
		System.out.println("The heightest is "+
						  TwoDArrayOperations.getHigestInRow(testThree));
				System.out.println("The lowest is " +
						  TwoDArrayOperations.getlowestInRow(testThree));
		System.out.println();
		
		// Display the fourth array information:
		
		System.out.print("The following information is " +
							"for the testFour array:" + "\n" );

		System.out.println("The total is " +
						  TwoDArrayOperations.getTotal(testFour));
		System.out.println("The average is " +
						  TwoDArrayOperations.getAverage(testFour));
		System.out.println("The row total is " +
						  TwoDArrayOperations.getRowTotal(testFour));
		System.out.println("The column total is " +
						  TwoDArrayOperations.getColumnTotal(testFour));
		System.out.println("The heightest is "+
						  TwoDArrayOperations.getHigestInRow(testFour));
		System.out.println("The lowest is " +
						  TwoDArrayOperations.getlowestInRow(testFour));
		System.out.println();
    }
}



The code complies just find. the problem I'm having is that when I interpreter it gives the following
---------- java interpreter ----------
The following information is for the testOne array:
The total is 55
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at TwoDArrayOperations.getAverage(TwoDArrayOperations.java:151)
at TwoDArrayOperationsDemo.main(TwoDArrayOperationsDemo.java:38)

Output completed (0 sec consumed) - Normal Termination

What does this mean and how can fix it?

Thank you in advanced!!!! :smile2:

Is This A Good Question/Topic? 0
  • +

Replies To: 2DArrayOperations

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: 2DArrayOperations

Posted 22 April 2012 - 01:24 AM

What you're getting is a runtime error. Your code is syntactically correct, so it compiles and runs, but during the course of its execution, an error occurs that causes the Java Runtime Environment (or JRE) to halt. The error is reported as an ArrayIndexOutOfBoundsException, and the index that is out of bounds is 1.

The next part of the error message is the stack trace, and it's read from the bottom up. The path to the error began in main() at line 38 and the error occurred in the getAverage() method of TwoDArrayOperations at line 151. Though the line numbers in the error message don't match the code you've posted, it's easy enough to find the area causing the trouble from the other hints given. The offending code is:
	public static double getAverage(double [][] testThree)
	{
		double total = 0; // Initialize accumulator.
		double average; // Will hold the average.

		// Add up all the values in the array.
		for (int row = 0; row < testThree.length; row++  )
			for (int col = 0; col < testThree[row].length; row++)
				total += testThree[row][col];

		// Calculate the Average.
		return average = total / testThree.length;
	}

So somewhere in that method an array index is being set to 1, and that's greater than the array allows. Why would that happen? Hint: Look at the second for statement.
Was This Post Helpful? 0
  • +
  • -

#3 maria775  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-April 12

Re: 2DArrayOperations

Posted 22 April 2012 - 06:30 PM

I have looked at the for statement over and over again, and I still don't understand what is the problem. I'm also looking in book and the way I have it is the way its in book.

can you show me any example.
thank you :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: 2DArrayOperations

Posted 22 April 2012 - 07:11 PM

for (int col = 0; col < testThree[row].length; row++) :)

also you average() methods can call the total methods no need to duplicate code
And the average is not total / number of rows but
total / total number of cells in the array

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1