CS141 Programming Assignment #10 Due Sunday, May 5th. 1) Write a class with the following methods: a) max( int [][] a) Returns the maximum integer in the array. b) min(int [][] a) Returns the minimum integer in the array. c) avergae(int [][] a) Returns the average of integers in the array. d) copy(int [][] a,int [][]b) Copy array a to array b. e) print(int [][] a) Prints the array. 2) Test your class methods you implemented in part 3 on the following array int [][] a = {{10, 5, -6, 0,{11, 8, -2, 7,{4, 6, 2, 1,{9, -5, -3, 50; Make sure to print your answer after each method call. Sol //Student name //Student ID //Assignment# 10 Question #1-2 //DueDate: May 5,2013 //141/51 Computer Programming //This Program containing 5 methods: // 1. Returns the maximum integer in the array // 2. Returns the minimum integer in the array. // 3. Returns the average of integers in the array. // 4. Copy array a to array b. // 5. Prints the array. public class Q1 { /* Method to find the maximum value in an array */ public static int max(int [][] a){ int large = a[0][0]; // stores the initial value to compare with if ( large < a[i][j]) large = a[i][j]; return large; /* Method to find the minimum value in an array */ public static int min(int [][] a){ int small = a[0][0]; // stores the initial value to compare with if ( small > a[i][j]) small = a[i][j]; return small; /* Method to find the average of the values in the array */ public static double average(int [][] a){ int sum = 0; // stores the sum of the values in the array sum += a[i][j]; return sum/(double)(a.length*a.length);
/* Method to copy the first array argument into * * the second array argument */ public static void copy(int [][]a, int [][]b){ for(int i = 0; i < a.length; i++){ b[i][j] = a[i][j]; /* Method to print the 2 dimensional array */ public static void print(int [][]a){ for(int i = 0; i < a.length; i++){ System.out.printf("%3d", a[i][j]); public static void main(string[] args) { int [][] a = {{10, 5, -6, 0, {11, 8, -2, 7, {4, 6, 2, 1, {9, -5, -3, 50; System.out.println("The values in the first array are: "); print(a); System.out.println("The maximum value in the array is " + max(a)); System.out.println("The minimum value in the array is " + min(a)); System.out.printf("The average of the values in the array is %.2f\n", average(a)); int [][]b = new int[a.length][a.length]; copy(a, b); System.out.println("The numbers in the second array are: "); print(b); //end main // end class 3) Write a class that reads values of a two-dimensional integer array A[4][4] from the keyboard, and then computes values of integer array B[4], such that B[i] is the smallest value of i-th row of array A. Sol //Student name //Student ID //Assignment# 10 Question #3 //DueDate: May 5,2013 //141/51 Computer Programming //This Program containing 5 methods: import java.util.scanner; public class Q3 {
public static void main(string[] args) { int [][]a = new int [4][4]; int []b = new int[4]; readarray(a); findsmall(a,b); printarrays(a, b); /* Method to read the values and store them into the 4x4 matrix */ public static void readarray(int[][]a){ Scanner input = new Scanner(System.in); System.out.println("Enter the values in the 4x4 matrix:"); for(int i = 0; i < a.length; i++) for(int j = 0; j < a[i].length; j++) a[i][j] = input.nextint(); /* Method to find and return the smallest number in an array */ public static int min(int [] a){ int small = a[0]; // Stores an initial value to compare with for (int i = 1; i < a.length; i++) if ( small > a[i]) small = a[i]; return small; /* Method calls method min()to find the smallest value in each * * row and stores it in a second one dimensional array */ public static void findsmall(int [][]a, int []b){ for(int i = 0; i < a.length; i++) b[i] = min(a[i]); /* Method to print the values in the two arrays */ public static void printarrays(int [][]a, int []b){ for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++) System.out.printf("%5d",a[i][j]); System.out.printf(" Smallest is %5d\n", b[i]); 4) Write a class Matrix with the following methods: -Matrix( int row, int col) -Matrix(int [][] m) -print() -fill () -add(matrix a) -sub(matrix a) -mul(matrix a) Sets the matrix array size to row and col Sets the matrix array to the values given by m Prints the matrix array Sets the matrix array values by the user Add the 2 matrices Subtract the two matrices Multiply the two matrices
5) Test your class methods you implemented in part 6 on the following Matrices [ ] [ ] Make sure to print your answer after each method call. Sol //Student name //Student ID //Assignment# 10 Question #4-5 //DueDate: May 5,2013 //141/51 Computer Programming //This Program containing 5 methods: //1. Prints the matrix array //2. Sets the matrix array values by the user //3. Add two matrices //4. Subtract two matrices //5. Multiply two matrices import java.util.scanner; class Matrix { private int a[][]; // instance variable to store the matrix /* constructor to set matrix to specific size */ public Matrix(int row, int col) { a = new int[row][col]; /* constructor to set matrix to given values */ public Matrix(int[][] m) { a = new int[m.length][m[0].length]; for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) a[i][j] = m[i][j]; /* method to print the matrix */ public void print() { { System.out.printf("%5d", a[i][j]); /* method to set matrix values by the user */ public void fill() { Scanner input = new Scanner(System.in); System.out.println("Enter the values in your matrix: "); a[i][j] = input.nextint();
/* method to add two matrices and return a third matrix containing the result */ public Matrix add(matrix a) { Matrix C = new Matrix(this.a.length, a.a.length); // stores the result if (this.a.length == a.a.length && this.a[0].length == a.a[0].length) { // check if arrays are compatible for (int i = 0; i < this.a.length; i++) for (int j = 0; j < this.a[i].length; j++) C.a[i][j] = this.a[i][j] + a.a[i][j]; else System.out.println("Array rows and columns are incompatible!!" + " Addition could not be performed"); return C; /* method to subtract two matrices and return a third matrix containing the result */ public Matrix sub(matrix a) { Matrix C = new Matrix(this.a.length, a.a.length); // stores the result if (this.a.length == a.a.length && this.a[0].length == a.a[0].length) { // check if array are compatible for (int i = 0; i < this.a.length; i++) for (int j = 0; j < this.a[i].length; j++) C.a[i][j] = this.a[i][j] - a.a[i][j]; else System.out.println("Array rows and columns are incompatible!!" + " Subtraction could not be performed"); return C; /* method to multiply two matrices and return a third matrix containing the result */ public Matrix mul(matrix a) { Matrix C = new Matrix(this.a.length, a.a[0].length); // stores the result if (this.a.length == a.a[0].length) { // check if arrays are compatible for (int i = 0; i < this.a.length; i++) for (int j = 0; j < this.a[i].length; j++) { int sum = 0; for (int k = 0; k < a.a.length; k++) sum += this.a[i][k] * a.a[k][j]; C.a[i][j] = sum; else System.out.println("Array rows and columns are incompatible!!" + " Multiplication could not be performed"); return C; public static void main(string[] args) { int[][] a = { { 3, 4, -4, { 9, 11, 0, { -2, 5, 5 ; Matrix A = new Matrix(a); // first matrix created thru given matrix Matrix B = new Matrix(3, 3);// second matrix created thru specific size B.fill(); System.out.println("The elements in matrix A: "); A.print(); System.out.println("The elements in matrix B:"); B.print();
Matrix C = A.add(B); // stores the result of addition System.out.println("A+B= "); C.print(); Matrix D = A.sub(B); // stores the result of subtraction System.out.println("A-B= "); D.print(); Matrix E = A.mul(B); // stores the result of multiplication System.out.println("A*B= "); E.print(); // end main // end class