All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.princeton.cs.introcs.StdArrayIO Maven / Gradle / Ivy

Go to download

Standard input and output libraries from Princeton's "Introduction to Programming in Java" textbook.

The newest version!
package edu.princeton.cs.introcs;


/*************************************************************************
 *  Compilation:  javac StdArrayIO.java
 *  Execution:    java StdArrayIO < input.txt
 *
 *  A library for reading in 1D and 2D arrays of integers, doubles,
 *  and booleans from standard input and printing them out to
 *  standard output.
 *
 *  % more tinyDouble1D.txt 
 *  4
 *    .000  .246  .222  -.032
 *
 *  % more tinyDouble2D.txt 
 *  4 3 
 *    .000  .270  .000 
 *    .246  .224 -.036 
 *    .222  .176  .0893 
 *   -.032  .739  .270 
 *
 *  % more tinyBoolean2D.txt 
 *  4 3 
 *    1 1 0
 *    0 0 0
 *    0 1 1
 *    1 1 1
 *
 *  % cat tinyDouble1D.txt tinyDouble2D.txt tinyBoolean2D.txt | java StdArrayIO
 *  4
 *    0.00000   0.24600   0.22200  -0.03200 
 *  
 *  4 3
 *    0.00000   0.27000   0.00000 
 *    0.24600   0.22400  -0.03600 
 *    0.22200   0.17600   0.08930 
 *    0.03200   0.73900   0.27000 
 *
 *  4 3
 *  1 1 0 
 *  0 0 0 
 *  0 1 1 
 *  1 1 1 
 *
 *************************************************************************/


/**
 *  Standard array IO. This class provides methods for reading
 *  in 1D and 2D arrays from standard input and printing out to 
 *  standard output.
 *  

* For additional documentation, see * Section 2.2 of * Introduction to Programming in Java: An Interdisciplinary Approach * by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class StdArrayIO { // it doesn't make sense to instantiate this class private StdArrayIO() { } /** * Read in and return an array of doubles from standard input. */ 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; } /** * Print an array of doubles to standard output. */ 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(); } /** * Read in and return an M-by-N array of doubles from standard input. */ 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; } /** * Print the M-by-N array of doubles to standard output. */ 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(); } } /** * Read in and return an array of ints from standard input. */ 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; } /** * Print an array of ints to standard output. */ 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(); } /** * Read in and return an M-by-N array of ints from standard input. */ 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; } /** * Print the M-by-N array of ints to standard output. */ 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(); } } /** * Read in and return an array of booleans from standard input. */ 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; } /** * Print an array of booleans to standard output. */ public static void print(boolean[] a) { int N = a.length; StdOut.println(N); for (int i = 0; i < N; i++) { if (a[i]) StdOut.print("1 "); else StdOut.print("0 "); } StdOut.println(); } /** * Read in and return an M-by-N array of booleans from standard input. */ 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; } /** * Print the M-by-N array of booleans to standard output. */ 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(); } } /** * Test client. */ public static void main(String[] args) { // read and print an array of doubles double[] a = StdArrayIO.readDouble1D(); StdArrayIO.print(a); StdOut.println(); // read and print a matrix of doubles double[][] b = StdArrayIO.readDouble2D(); StdArrayIO.print(b); StdOut.println(); // read and print a matrix of doubles boolean[][] d = StdArrayIO.readBoolean2D(); StdArrayIO.print(d); StdOut.println(); } } /************************************************************************* * Copyright 2002-2012, Robert Sedgewick and Kevin Wayne. * * This file is part of stdlib-package.jar, which accompanies the textbook * * Introduction to Programming in Java: An Interdisciplinary Approach * by R. Sedgewick and K. Wayne, Addison-Wesley, 2007. ISBN 0-321-49805-4. * * http://introcs.cs.princeton.edu * * * stdlib-package.jar is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * stdlib-package.jar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with stdlib-package.jar. If not, see http://www.gnu.org/licenses. *************************************************************************/





© 2015 - 2024 Weber Informatics LLC | Privacy Policy