Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

More Methods: Array Parameters - Java is the code being used. Exercise 1: (2 points) A method for computing the average of an array of

More Methods: Array Parameters - Java is the code being used.

Exercise 1: (2 points) A method for computing the average of an array of integers

The first task will be to revise the Inequality.java code to have a method named arrayAverage that computes the average of an array of integers and returns it to the calling program. When you are finished the main method will look like : public static void main(String[] args) { // list of incomes in thousands int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21}; int average = arrayAverage(income); System.out.println(" Average of income array : " + average); } As you can see in the call to arrayAverage, it takes a single parameter which is a one dimensional array an returns and integer value. Paste your arrayAverage method into your solutions document. No output is required.

Exercise 2: (3 points) A check rows method for Magic squares

The next task is to begin the process of rewriting the magic squares code. The main routine has a large number of lines of code with loops, println's, ... This clutters up main and makes it hard to understand what is going on. You will be doing this in stages. The first step is to put the code for checking whether each each row adds up to the correct amount (targetSum) into a method named rowsEqTargetSum. main should call this method as follows : if (!rowsEqTargetSum(a, targetSum)) { return; } As you probably suspect rowsEqTargetSum is a method which returns a boolean value (true if all the rows equal the targetSum) and takes two parameters : a two dimensional array and an integer. To help you get started, the first line of the method should look like : public static boolean rowsEqTargetSum(int [][] a, int targetSum) Locate the code in the main method for checking whether the rows are equal to the target sum and put them in the method. You will need to return boolean values at certain points in the code to indicate whether a row does not equal the target sum (return false) and all rows equal the targetSum (return true). FYI : Determining which variables to compute in main and which to calculate in the method is not always obvious. We choose to send targetSum into the method because it will (eventually) be used by other methods so we do the computation once and send it to the methods. Other variables, like n, are straightforward enough that we let the method have a local variable that "calculates" the value of n. Copy your rowsEqTargetSum method into your solutions document. No output required.

Exercise 3: (2 points) A check diagonals method for Magic squares

The next task is to provide a method named diagonalEqTargetSum to check one of the diagonals to see if it equals the target sum. Main will call this routine as follows : if (!diagonalEqTargetSum(a, targetSum)) { return; } Again, the method returns a boolean value and has two parameters : a two dimensional array and an integer. Locate the code in main to check the diagonal and use it to construct the method.

Copy your diagonalEqTargetSum into your solutions document.

Exercise 4: (3 points) A check to make sure all numbers are represented.

The final cleanup is to create a method for checking that all numbers from 1 to n*n are used exactly once in the two dimensional array. This method, named allNumbersRepresented returns a boolean value (true if all numbers are represented, false otherwise) and takes a single parameter, the two dimensional array. The main routine will call the method as follows : if (!allNumbersRepresented(a)) { return; } Locate the code in main to check that all numbers are represented and use it to construct the method. Finally, remove any unused variables that are in your main routine. Copy allNumbersRepresented and the main method into your solutions document.

Here are the codes which need to be used.

Inequality code

public class Inequality { public static void main(String[] args) { // list of incomes in thousands int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21}; int sum = 0; int average; for (int i = 0;i< income.length;i++) { sum = sum + income[i]; } average = sum/income.length; System.out.println(" Average of income array : " + average); }

}

The Magic code

import java.util.Arrays;

public class Magic { public static void main(String []args) { int[][] a ={{4,9,2}, {3,5,7}, {8,1,6}}; final int n=a.length; final int nSquare=n*n; final int targetSum=n*(n*n+1)/2; boolean[] flag= new boolean[n*n]; int row, col, sum;

// Calculate the sum of each row ... if magic, then equal to targetSum for(row=0; row 0) System.out.print(" + "); // print plus before all except 1st System.out.print(value); } System.out.println(" = "+sum); if(sum != targetSum) { System.out.println("Row sum incorrect : Not a magic Square !"); return; } }

// TODO : Add a check for the sums of columns

// Calculate the sum of the diagonal from Lower Left to Upper Right sum=0; System.out.print("diagonal: "); for(int pos=0; pos 0) System.out.print(" + "); // print plus before all except 1st System.out.print(value); } System.out.println(" = "+sum); if(sum != targetSum) { System.out.println("Diagonal is incorrect : Not a magic Square !"); return; }

// TODO : Add check for the sum of primary diagonal (from upper left to bottom right)

// Lastly, we check that every number from 1 to n is represented for(row=0; row nSquare) { System.out.println("Number out of range : Not a magic Square !"); return; } if (flag[num-1]) { System.out.println("Duplicate number : Not a magic Square !"); return; } flag[num-1] = true; } }

System.out.println(" The following two dimensional array is Magic !"); for (int i = 0;i< a.length;i++) { System.out.println(Arrays.toString(a[i])); } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago