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 )--------------------------------
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) ); }