COP 3338 - Programming III

Florida International University (FIU)

School of Computer Science

COP 3338 - Programming III


Classes

The driver program - driver.java

This class handles input from keyboard - readFromKB.java

This class actually reads the data - getData.java

A partial definition of the class Matrix.java

Print the output


he driver program - driver.java

// A sketal outline of main
public class driver
{
	public static void main(String[] arg)
	{
		readFromKB kb = new readFromKB();

		getData get = new getData(kb);
		// Alternately, getData get = new getData(new 
readFromKB());

		Print p = new Print();

		int row = get.RowOrColumn("rows");
		int column = get.RowOrColumn("columns");
		p.print(row, column);

		double [][] matrixA = get.values(row, column);

		Matrix m1 = new Matrix(matrixA);
		m1.Display();

		//	:::::::::::::::
		// ::::::::::::::::
	}
}
Back to the top

Read data from keyboard - readFromKB.java

// Use the following class to assist you in reading the data from the 
keyboard
//readFromKB.java defines a class that can be used to read data from the 
keyboard.

import java.io.*;

public class readFromKB
{

  	private BufferedReader  bf;
  	private String          string;

  	public readFromKB()
  	{
	  	bf = new BufferedReader(new InputStreamReader(System.in));
	  	string = "";
  	}

  	// getChar retrieves the next char, white space or not.
  	public char getChar()
  	{
    	int myValue = 0;
    	try
		{
		  myValue = bf.read();
		}
		catch (IOException e)
		{
		  System.out.println(e);
		}
    	return (char) myValue;
  	}

  	// peekChar looks at the next char without reading it
	 public char peekChar()
	 {
		int myValue = 0;
		try
		{
		  bf.mark(1);            // mark current read-position
		  myValue = bf.read();   // read a char
		  bf.reset();            // reset read-position to mark
		}
		catch (IOException e)
		{
		  System.out.println(e);
		}
		return (char) myValue;
	  }

  	// skipWhiteSpace skips white space chars
   	public  void skipWhiteSpace()
  	{
		int ch;

		try
		{
		  for (;;)
		  {
			bf.mark(1);          // set mark in buffer
			ch = bf.read();      // read a char
			if (ch < 1)          // if EOF, quit
			  break;
			if (!Character.isWhitespace((char)ch))
			{
			  bf.reset();        // if non-WS, move back to 
mark & quit
			  break;
			}
		  }
		}
		catch (IOException e)
		{
		  System.out.println(e);
		}
	 }

  // readChar reads the next non-white-space char.
	public char readChar()
	{
		skipWhiteSpace();               // eat leading white space
		return getChar();              // read the next char
	}

   // readWord reads the next 'word' (delimited by white space).
  	public String readWord()
  	{
		int ch;                        // input variable
		String myValue = "";           // myValue is initially 
empty

		skipWhiteSpace();          // eat leading white space

		for (;;)
		{
		  ch = peekChar();        // check out next char
		  if (ch < 1 || Character.isWhitespace((char)ch))
			break;
		  ch = getChar();
		  myValue += (char) ch;        // append it to myValue
		}

		return myValue;
	}

  // readInt  reads the next word as an integer value.
  public int readInt()
  {
    string = readWord();
    return Integer.parseInt(string);
  }

  //readFloat tries to read the next word as a float value.
  public double readFloat()
  {
    string = readWord();
    return Float.parseFloat(string);
  }

  //readDouble tries to read the next word as a double value.
  public double readDouble()
  {
    string = readWord();
    return Double.parseDouble(string);
  }
}

Back to the top

Use this class to load each array

// Read the data
public class getData
{
	readFromKB kb;

	public getData(readFromKB kb)
	{
		this.kb = kb;
	}

	int RowOrColumn(String str)
	{

		System.out.println("Enter the number of " + str);
		int row = kb.readInt();
		return row;

	}

	double[][] values(int rows, int columns)
	{
		double arr[][] = new double[rows][columns];

		// Read the data and return it

		return arr;
	}
}
  • Back to the top

    The class Matrix.java

    // Partial definition
    public class Matrix
    {
    
    	 //--- Attributes ---
    		private int         myRows;
    		private int         myColumns;
    		private double [][] myArray;
    
    	 //--- Possible forms of Constructors ---
    	 // Default constructor
    	 public Matrix()
    	 {
    	  myArray = null;
    	  myRows = 0;
    	  myColumns = 0;
    	 }
    
    	 //Explicit-Value Constructor
    	 public Matrix(int rows, int columns)
    	 {
    		 myArray = new double[rows][columns];
    		 myRows = rows;
    		 myColumns = columns;
    	 }
    
    	public Matrix(double[][] m)
    	{
    		myArray = m;
    		myRows = m.length;
    		myColumns = m[0].length;
    	}
    
    	public void Display()
    	{
    		for (int i = 0; i < myRows; i++)
    			{
    				for(int j = 0 ; j < myArray[i].length; 
    j++)
    					System.out.print(myArray[i][j] + "  
    " );
    				System.out.println();
    			}
    	}
    
    	// Complete the definition for this class
    	//:::::::::::::::::::::::
    }
    
    
    Back to the top

    Print.java

    public class Print
    {
    	Print(){}
    
    	void print(int row, int col)
    	{
    		System.out.println("Matrix 1 is a " + row + " by " + col+ 
    " matrix" );
    	}
    
    	//::::::::::::::
    }
    

    Back to the top