Colorado State University

Recitation R19 - Java Input/Output and Arrays
Spring 2014

CS160: Foundations in Programming


The purpose of this lab is to:

Interpret Java

What does the following code print out after the prompt(s)?

DO NOT RUN THIS CODE. Try and figure out what it does just by looking at it. You are not graded on correctness, this is purely to help you understand java.

//imports
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class InterpretJava {

     static int[] intArray = new int[100];
     static int openIntIndex = 0;
     
     static double[] doubleArray = new double[100];
     static int openDoubleIndex = 0;
     
     static String[] StringArray = new String[100];
     static int openStringIndex = 0;
     
     public static void main(String[] args) {
          readFile("inputFile.txt");
          writeFile("outputFile.txt");
     }
     
     public static void readFile( String inputFile){

          try{
               Scanner fileIn = new Scanner( new File(inputFile) );
          
               while( fileIn.hasNext() ){
                    
                    if( fileIn.hasNextDouble() ){
                         doubleArray[openDoubleIndex] = fileIn.nextDouble();
                         ++openDoubleIndex;
                         
                    } else if( fileIn.hasNextInt() ){
                         intArray[openIntIndex] =  fileIn.nextInt();
                         ++openIntIndex;
                         
                    } else {
                         StringArray[openStringIndex] = fileIn.next();
                         ++openStringIndex;
                         
                    }
               }
               
               fileIn.close();
          } catch( IOException e ){
               System.out.println("Error reading \""+inputFile+"\"\n"+e);
               System.exit(-1);
          }
          
     }
     
     public static void writeFile( String outputFile ){

          try{
               PrintWriter fileOut = new PrintWriter( new File(outputFile) );
               fileOut.print("[ ");
               for( int i = 0; i < openIntIndex; ++i ){
                    fileOut.print( intArray[i]+", " );
               }
               fileOut.println(" ]");
               
               fileOut.print("[ ");
               for( int i = 0; i < openDoubleIndex; ++i ){
                    fileOut.print( doubleArray[i]+", " );
               }
               fileOut.println(" ]");
               
               fileOut.print("[ ");
               for( int i = 0; i < openStringIndex; ++i ){
                    fileOut.print( StringArray[i]+", " );
               }
               fileOut.println(" ]");
               
               fileOut.close();
          } catch( IOException e ) {
               System.out.println("Error writing \""+outputFile+"\"\n"+e);
               System.exit(-1);
          }
     }
}
inputFile.txt:
--------------------------------
5 5.2 5g6.0 hello world! 100.0 7.5 11
3.1415962 pi  
1.6180339 phi 
2.71828 e
primeNumber( 0 )
--------------------------------
Assume that inputFile.txt is accessible and the folder is writable. On a separate sheet of paper write the contents of outputFile.txt. The expected output has three lines. There are no syntactical errors, but can you find any logical errors in this code?

Scope and Arrays

  1. Create a new R19 project in Eclipse and an associated class.
  2. Copy the following code into R19:
    	public static void squareInt( int number ) { 
    
    	} 
    
    	public static void squareArray( int[] array ) { 
    	
    	} 
    
    	public static void main(String[] args) {
    
    		int mainInt = 12;
    		int mainArray[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 };
    
    
    		System.out.println("Before: ");
    		System.out.println( mainInt );
    		System.out.println( "my array:"+ Arrays.toString( mainArray ) );
    
    		squareInt( mainInt );
    		squareArray( mainArray );
    
    		System.out.println("After: ");
    		System.out.println( mainInt );
    		System.out.println( Arrays.toString(mainArray) );
    
    	}
    
  3. Implement the squareInt function, which computes the square of input parameter and stores the result back into the parameter.
  4. Implement the squareArray function, which squares each element in the input array and stores the result back into the array.
  5. Write a comment in your code explaining how the two methods differ with respect to passing parameters.

Show your R19.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2014 CS160 Colorado State University. All Rights Reserved.