In: Computer Science
// **************************************************************** // Incrementarray.java // // Define a IncrementMatrix class with methods to create and read in // info for a matrix and to check whether the matrix (2d array) is increment, // in other words, all elements in each row are in increasing order and // all elements in each column are in increasing order. // **************************************************************** import java.util.Scanner; public class IncrementMatrix { int[][] matrix; //-------------------------------------- //create new array of given size //-------------------------------------- public IncrementMatrix(int row, int col) { matrix = new int[row][col]; } //----------------------------------------------- //check whether all elements are in increasing order //in the given row. If so, return true; otherwise, return false //----------------------------------------------- private boolean checkRow(int row) { //implement this method } //------------------------------------------------- //check whether all elements are in increasing order //in the given column. If so, return true; otherwise, return false //------------------------------------------------- private boolean checkCol(int col) { //implement this method } //------------------------------------------------------------------- //return true if the array is increment (all rows and cols have to be increment), //false otherwise; you need to use checkRow and checkCol as supporting //methods to implement increment; //------------------------------------------------------------------- public boolean increment() { //implement this method } //---------------------------------------------------- //read info into the matrix from the standard input. //---------------------------------------------------- public void readMatrix(Scanner scan) { for (int row = 0; row < matrix.length; row++) for (int col = 0; col < matrix[row].length; col ++) matrix[row][col] = scan.nextInt(); } //--------------------------------------------------- //print the contents of the matrix, neatly formatted //--------------------------------------------------- public void printMatrix() { //implement this method } }
///////////////////
// **************************************************************** // Driver.java // // Uses the IncrementMatrix class to read in matrix data and tell if // each matrix is increment. // // **************************************************************** import java.util.Scanner; import java.io.*; public class Driver { public static void main(String[] args) throws IOException { Scanner scan = new Scanner (new File("Data")); //put Data file in your program folder, if you use IDE like Eclipse to run, please put the file in your project folder. For giving absolute file path, if you use windows, you need to double backslash. For example: for reading file c:\f1\a.txt, you need to use file name "c:\\f1\\a.txt" in the program. int count = 1; //count which square we're on int row = scan.nextInt(); //size of next matrix, get the number of rows int col; //Expecting -1 at bottom of input file while (row != -1) { col= scan.nextInt(); //size of next matrix, get the number of columns //create a new matrix of the given size IncrementMatrix matrix = new IncrementMatrix(row, col); //call its read method to read the values of the matrix matrix.readMatrix(scan); System.out.println("\n******** Matrix " + count + " ********"); //print the matrix matrix.printMatrix(); //determine and print whether it is a increment square if (matrix.increment()) System.out.println ("It's an increment matrix!"); else System.out.println ("It's not an increment matrix!"); System.out.println(); //get size of next square row = scan.nextInt(); count++; } } }
//
****************************************************************
// Incrementarray.java
//
// Define a IncrementMatrix class with methods to create and read
in
// info for a matrix and to check whether the matrix (2d array) is
increment,
// in other words, all elements in each row are in increasing order
and
// all elements in each column are in increasing
order.
//
****************************************************************
import java.util.Scanner;
public class IncrementMatrix {
int[][] matrix;
// --------------------------------------
// create new array of given size
// --------------------------------------
public IncrementMatrix(int row, int col) {
matrix = new int[row][col];
}
//
-----------------------------------------------
// check whether all elements are in increasing
order
// in the given row. If so, return true; otherwise,
return false
//
-----------------------------------------------
private boolean checkRow(int row) {
int small = matrix[row][0];
for (int i = 1; i <
matrix[row].length; i++) {
if (small >
matrix[row][i]) {
return false;
}
}
return true;
}
//
-------------------------------------------------
// check whether all elements are in increasing
order
// in the given column. If so, return true; otherwise,
return false
//
-------------------------------------------------
private boolean checkCol(int col) {
// implement this method
int small = matrix[0][col];
for (int i = 1; i <
matrix.length; i++) {
if (small >
matrix[i][col]) {
return false;
}
}
return true;
}
//
-------------------------------------------------------------------
// return true if the array is increment (all rows and
cols have to be
// increment),
// false otherwise; you need to use checkRow and
checkCol as supporting
// methods to implement increment;
//
-------------------------------------------------------------------
public boolean increment() {
for (int i = 0; i <
matrix.length; i++) {
boolean flag =
checkRow(i);
if (flag ==
false) {
return false;
}
}
for (int i = 0; i <
matrix[0].length; i++) {
boolean flag =
checkCol(i);
if (flag ==
false) {
return false;
}
}
return true;
}
//
----------------------------------------------------
// read info into the matrix from the standard
input.
//
----------------------------------------------------
public void readMatrix(Scanner scan) {
for (int row = 0; row <
matrix.length; row++)
for (int col =
0; col < matrix[row].length; col++)
matrix[row][col] = scan.nextInt();
}
//
---------------------------------------------------
// print the contents of the matrix, neatly
formatted
//
---------------------------------------------------
public void printMatrix() {
// implement this method
for (int row = 0; row <
matrix.length; row++) {
for (int col =
0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + "\t");
}
System.out.println();
}
}
}
==================================================================
//
****************************************************************
// Driver.java
//
// Uses the IncrementMatrix class to read in matrix data and tell
if
// each matrix is increment.
//
//
****************************************************************
import java.util.Scanner;
import java.io.*;
public class Driver {
public static void main(String[] args) throws
IOException {
Scanner scan = new Scanner(new
File("Data.txt"));
//put Data file in your program folder, if you use IDE like Eclipse
to run, please put the file in your project folder. For giving
absolute file path, if you use windows, you need to double
backslash. For example: for reading file c:\f1\a.txt, you need to
use file name "c:\\f1\\a.txt" in the program.
int count = 1; // count which
square we're on
int row = scan.nextInt(); // size
of next matrix, get the number of rows
int col;
// Expecting -1 at bottom of
input file
while (row != -1) {
col =
scan.nextInt(); // size of next matrix, get the number of
columns
// create a new
matrix of the given size
IncrementMatrix
matrix = new IncrementMatrix(row, col);
// call its
read method to read the values of the matrix
matrix.readMatrix(scan);
System.out.println("\n******** Matrix " + count + "
********");
// print the
matrix
matrix.printMatrix();
// determine
and print whether it is a increment square
if
(matrix.increment())
System.out.println("It's an increment
matrix!");
else
System.out.println("It's not an increment
matrix!");
System.out.println();
// get size
of next square
row =
scan.nextInt();
count++;
}
}
}
============================================================