Question

In: Computer Science

// **************************************************************** // Incrementarray.java // // Define a IncrementMatrix class with methods to create and read...

// ****************************************************************
// 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++;
            }

   }
}

Solutions

Expert Solution

// ****************************************************************
// 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++;
       }

   }
}

============================================================


Related Solutions

Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in Java. Circle Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii.
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in...
1: (Passing Objects to Methods) From the following UML Class Diagram, define the Circle class in Java. Circle Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii.
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another...
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another class named GenericCar with a parameter of the T type. This class manages a collection of object T (may be LinkedList) named a. Implementing some methods for GenericCar: Add: add new item of T to a Display: display all items of a getSize: return the number item of a checkEmpty: check and return whether a is empty or not delete(int pos): remove the item...
Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT