a Write a method createArithmeticSeq that prompts the user t

a. Write a method createArithmeticSeq that prompts the user to input two number, first and diff. The method then creates a one-dimensional array of 16 elements ordered in an arithmetic sequence. It also outputs the arithmetic sequence. For example, if first = 21 and diff = 5, the arithmetic sequence is 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96.

b. Write a method matricize that takes a one-dimensional array of 16 elements and a two-dimensional arra of 4 rows and 4 columns as parameters. This method puts the elements of the one-dimensional array into the two-dimensional array. For example, if A is the one-dimensional array created in part a and B is a two-dimensional array, then after putting the elements of A into B, the array B is:
21 26 31 36
41 46 51 56
61 66 71 76
81 86 91 96

c. Write a method reverseDiagonal that reverses both diagonals of a two-dimensional array. For example, if the two-dimensional array is as in part b, after reversing the diagonals, the two-dimensional array is:
96 26 31 81
41 71 66 56
61 51 46 76
36 86 91 21

d. Write a method magicCheck that takes a one-dimensional array of size 16, a two-dimensional array of 4 rows and 4 columns, and the sizes of the arrays as parameters. By adding all of the elements of the one-dimensional array and dividing by 4, this method determines the magicNumber. The method then adds each row, each column, and each diagonal of the two-dimensional array and compares each sum with the magic number. If the sum of each row, each column, and each diagonal is equal to the magicNumber, the method outputs \"It is a magic square\"; otherwise, it outputs \"It is not a magic number\". Do not print the sum of each row, each column, and the diagonals.

e. Write a method printMatrix that outputs the elements of a two-dimensional array, one row per line. This output should be as close to a square form as possible.

f. The methods written in parts a through e should be general enough to apply to an array of any size. (The work should be done by using loops.) The method magicCheck works better if it calls two separate methods: one to add the rows and columns, and one to add the diagonals. (In this case, the program compares the magc number with each sum(row,column, and diagonal) in the respective method.)

I need this to use methods

Solution

(a)

public class ArithmeticSeq

{


public static void main(String[] args)
{

int first = 21,diff =5;

int[] a2 = new int[16];


for (int i = 0; i < a2.length; i++)

{

a2[0] = first;

first += diff;



{

System.out.print(\" \" + a2[0]);

}

  

}

  }
}

(b)

public class creatArithmiticSequence

{

public static void main(String[] args)

{

int first = 21,diff = 5;

int[] array = new int[16];

array[0] = first;

for(int i = 1;i < array.length;i++){

first += diff;

array[i] = first;

System.out.println(\" \" + first);

}
}
}

---------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;
public class Ch9_PrExercise13
{
static final int rows = 4;
static final int columns = 4;
static final int listSize = 16;

public static void main (String[]args)
{
int[] list = new int[listSize];
int [][] matrix = new int[rows][columns];

createArithmeticSeq(list);
matricize(list, matrix);
printMatrix(matrix);
reverseDiagonal(matrix);
printMatrix(matrix);
magicCheck(list, matrix);
}
public static void createArithmeticSeq(int list[])
{int first,diff,i;
Scanner input=new Scanner(System.in);
System.out.println(\"Enter starting number \");
first=input.nextInt();
System.out.println(\"Enter difference between numbers \");
diff=input.nextInt();
System.out.println(\"The numbers are:\");
for(i=0;i<listSize;i++)
   { list[i]=first;
   System.out.print(list[i]+\" \");
first+=diff;
}
System.out.println();
}
public static void matricize(int list[], int matrix[][])
{int i,j,count=0;
for(i=0;i<rows;i++)
   for(j=0;j<columns;j++)
    matrix[i][j]=list[count++];
}
public static void printMatrix(int matrix[][])
{int i,j;
    System.out.println(\"\ The Matrix\");
for(i=0;i<rows;i++)
    {System.out.println();
   for(j=0;j<columns;j++)
     System.out.print(matrix[i][j]+\" \");
  }
System.out.println();
}
public static void reverseDiagonal(int matrix[][])
{int i,j,temp;
for(i=0; i<rows/2; i++)
   {temp=matrix[i][i];
    matrix[i][i]=matrix[columns-1-i][columns-1-i];
    matrix[columns-1-i][columns-1-i]=temp;
   }
for(i=0; i<rows/2; i++)
   {temp=matrix[i][columns-1-i];
    matrix[i][columns-1-i]=matrix[columns-1-i][i];
    matrix[columns-1-i][i]=temp;
   }

}
public static void magicCheck(int list[], int matrix[][])
{int i,j,number=0;
boolean magic=true;
int[] rowsum=new int[rows];
int[] colsum=new int[columns];
int[] diagsum=new int[2];
for(i=0;i<listSize;i++)
    number+=list[i];
number/=4;
System.out.println(\"\ The magic number is \"+number);
for(i=0;i<columns;i++)
   {colsum[i] =addcol(matrix,i);
if(colsum[i]!=number)
    magic=false;
}
for(i=0;i<rows;i++)
    {rowsum[i]=addrow(matrix,i);
   if(rowsum[i]!=number)
      magic=false;
}
for(i=0;i<2;i++)
   { diagsum[i]=adddiag(matrix,i);
   if(diagsum[i]!=number)
      magic=false;
}
if(magic)
     System.out.println(\"\ It is a magicsquare\");
else
     System.out.println(\"\ It is not a magicsquare\");
}
public static int addcol(int matrix[][],int i)
{int j,sum=0;
for(j=0;j<rows;j++)
     sum+=matrix[j][i];
return sum;
}
public static int addrow(int matrix[][],int i)
{int j,sum=0;
for(j=0;j<columns;j++)
     sum+=matrix[i][j];
return sum;
}
public static int adddiag(int matrix[][],int i)
{int j,sum=0;
if(i==0)
for(j=0;j<columns;j++)
     sum+=matrix[j][j];
else
for(j=0;j<columns;j++)
      sum+=matrix[j][columns-1-j];
return sum;
}
}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site