Friday, January 30, 2015

Std Array IO.java in program



 StdArrayIO.java

public class StdArrayIO 
{
 private StdArrayIO() { }
              public static double[] readDouble1D() 
{
        int N = StdIn.readInt();
        double[] a = new double[N];
        for (int i = 0; i < N; i++) 
 {
            a[i] = StdIn.readDouble();
        }
        return a;
    }
    public static void print(double[] a) 
   {
        int N = a.length;
        StdOut.println(N);
        for (int i = 0; i < N; i++) 
       {
            StdOut.printf("%9.5f ", a[i]);
        }
        StdOut.println();
    }
                 public static double[][] readDouble2D()
   {
        int M = StdIn.readInt();
        int N = StdIn.readInt();
        double[][] a = new double[M][N];
        for (int i = 0; i < M; i++) 
        {
            for (int j = 0; j < N; j++) 
           {
                a[i][j] = StdIn.readDouble();
            }
        }
        return a;
    }
    public static void print(double[][] a) 
   {
        int M = a.length;
        int N = a[0].length;
        StdOut.println(M + " " + N);
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                StdOut.printf("%9.5f ", a[i][j]);
            }
            StdOut.println();
        }
    }
 public static int[] readInt1D() 
{
        int N = StdIn.readInt();
        int[] a = new int[N];
        for (int i = 0; i < N; i++) 
       {
            a[i] = StdIn.readInt();
        }
        return a;
    }
              public static void print(int[] a) 
{
        int N = a.length;
        StdOut.println(N);
        for (int i = 0; i < N; i++) 
        {
            StdOut.printf("%9d ", a[i]);
        }
        StdOut.println();
    }
               public static int[][] readInt2D() 
{
        int M = StdIn.readInt();
        int N = StdIn.readInt();
        int[][] a = new int[M][N];
        for (int i = 0; i < M; i++) 
        {
            for (int j = 0; j < N; j++) 
         {
                a[i][j] = StdIn.readInt();
            }
        }
        return a;
    }
   public static void print(int[][] a)
 {
        int M = a.length;
        int N = a[0].length;
        StdOut.println(M + " " + N);
        for (int i = 0; i < M; i++) 
        {
            for (int j = 0; j < N; j++) 
           {
                StdOut.printf("%9d ", a[i][j]);
            }
            StdOut.println();
        }
    }
                 public static boolean[] readBoolean1D() 
    {
        int N = StdIn.readInt();
        boolean[] a = new boolean[N];
        for (int i = 0; i < N; i++) 
       {
            a[i] = StdIn.readBoolean();
        }
        return a;
    }
 
    public static void print(boolean[] a) 
   {
        int N = a.length;
        StdOut.println(N);
        for (int i = 0; i < N; i++) 
       {
            if (a[i])  
            StdOut.println("1 ");
            else      
            StdOut.println("0 ");
        }
        StdOut.println();
    }
    public static boolean[][] readBoolean2D() 
   {
            int M = StdIn.readInt();
            int N = StdIn.readInt();
            boolean[][] a = new boolean[M][N];
            for (int i = 0; i < M; i++) 
    {
           for (int j = 0; j < N; j++) 
          {
                a[i][j] = StdIn.readBoolean();
            }
        }
        return a;
    }
                 public static void print(boolean[][] a) 
    {
        int M = a.length;
        int N = a[0].length;
        StdOut.println(M + " " + N);
        for (int i = 0; i < M; i++) 
            {
            for (int j = 0; j < N; j++) 
            {
            if (a[i][j]) 
            StdOut.print("1 ");
            else         
            StdOut.print("0 ");
            }
            StdOut.println();
            }
    }
                    public static void main(String[] args)
       {
                    double[] a = StdArrayIO.readDouble1D();
        StdArrayIO.print(a);
        StdOut.println();
        double[][] b = StdArrayIO.readDouble2D();
        StdArrayIO.print(b);
        StdOut.println();
        boolean[][] d = StdArrayIO.readBoolean2D();
        StdArrayIO.print(d);
        StdOut.println();
       }
}

No comments:

Post a Comment