Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab10.cscd210methods;
- import java.util.Scanner;
- /**
- * The methods for the Andromeda Problem.<br>
- * All parameters will be passed as final<br>
- * All preconditions and post conditions will be enforced
- */
- public class CSCD210Lab10Methods
- {
- /**
- * This method fills the solutions array with the values
- * from the file.
- * @param fin Representing a Scanner object to the open file
- * @return int [] Representing the filled array
- * @throws IllegalArgumentException if fin is null
- */
- public static int [] fillSolution(final Scanner fin)
- {
- if(fin == null)
- {
- throw new IllegalArgumentException("This is not a valid scanner");
- }
- int [] solution = new int[16];
- for(int x = 0; x<solution.length-1; x++)
- {
- solution[x] = fin.nextInt();
- }
- return solution;
- }// end fillSolution
- /**
- * This method prints Solution and then the values from the solution array
- * @param array Representing the filled solutions array
- * @throws IllegalArgumentException if the array is null or the length less than 1
- */
- public static void print(final int [] array)
- {
- if(array == null || array.length-1<1)
- {
- throw new IllegalArgumentException("Bad Parameters");
- }
- for(int x = 0; x<array.length-1; x++)
- {
- System.out.print(array[x]+" ");
- }
- System.out.println();
- }// end print
- /**
- * This method creates the generation array which is a 10 x 10 2D array
- * of integers. The array is then filled with the values
- * from the file.
- * @param fin Representing a Scanner object to the open file
- * @return int[] [] Representing the filled generation array
- * @throws IllegalArgumentException if fin is null
- */
- public static int [][] fillGeneration(final Scanner fin)
- {
- if(fin == null)
- {
- throw new IllegalArgumentException("This is not a valid Scanner");
- }
- int [][] generation = new int[10][10];
- for (int x = 0; x<generation.length;x++)
- {
- for(int y = 0; y<generation.length; y++)
- {
- generation [x][y] = fin.nextInt();
- }
- }
- return generation;
- }// end fillGeneration
- /**
- * This method prints Generation and the day and then the generation array
- * with the values converted to the proper characters
- * @param array Representing the filled generation array
- * @param day Representing which day the generation is on
- * @throws IllegalArgumentException if the array is null or the length less than 1
- * @throws IllegalArgumentException if day is less than 1
- */
- public static void print(final int [][] array, final int day)
- {
- if( array == null || array.length-1<0 || day<0)
- {
- throw new IllegalArgumentException("Bad parameters");
- }
- System.out.println("Generation: ");
- System.out.print(day);
- System.out.println();
- for(int x = 0; x<array.length-1;x++)
- {
- for(int y = 0; y<array.length-1; y++)
- {
- if(-1<array[x][y] || array[x][y]<4)
- {
- if(array[x][y] == 0)
- {
- System.out.print(".");
- }
- else if(array[x][y] == 1)
- {
- System.out.print("!");
- }
- else if(array[x][y] == 2)
- {
- System.out.print("X");
- }
- else
- {
- System.out.print("#");
- }
- }
- }
- System.out.println();
- }
- }// end print
- /**
- * This method prints Generation 0 and then the generation array
- * with the values converted to the proper characters
- * @param array Representing the filled generation array
- * @throws IllegalArgumentException if the array is null or the length less than 1
- */
- public static void print(final int [][] array)
- {
- if(array == null || array.length-1<1)
- {
- throw new IllegalArgumentException("Bad Parammeters");
- }
- System.out.println("Generation: 0 ");
- for(int x = 0; x<array.length-1;x++)
- {
- for(int y = 0; y<array.length-1; y++)
- {
- if(-1<array[x][y] || array[x][y]<4)
- {
- if(array[x][y] == 3)
- {
- System.out.print("#");
- }
- else if(array[x][y] == 1)
- {
- System.out.print("!");
- }
- else if(array[x][y] == 2)
- {
- System.out.print("X");
- }
- else
- {
- System.out.print(".");
- }
- }
- }
- System.out.println();
- }
- }// end print
- /**
- * This method is the hardest part of the assignment. It is passed the 1D solution array
- * and the 2D generation array. The method makes a new temp 2D array that is 10 x 10. The idea
- * here is each element in the generation array is examined. The solution is applied against the
- * generation array and the results are stored in the new temp array for that element. Once all
- * elements in the generation array have been processed the temp array is returned.
- * @param array Representing the 2D generation array
- * @param solution Representing the 1D solution array
- * @return int [][] Representing a new 2D generation array
- * @throws IllegalArgumentException if either array is null or the length is less than 1
- */
- public static int [][] runSolution(final int [][] array, final int [] solution)
- {
- if(array == null || array.length-1<1)
- {
- throw new IllegalArgumentException("Bad Parameters");
- }
- int[][] temp = new int [10][10];
- for(int x = 0; x<array.length-1; x++)
- {
- for(int y = 0; y<array.length-1; y++)
- {
- int sum = 0;
- if(x==0 && y == 0)
- {
- sum = sum + array[x+1][y];
- sum = sum + array[x][y+1];
- }
- else if(x==0 && y == 9)
- {
- sum = sum + array[x][y-1];
- sum = sum + array[x+1][y-1];
- }
- else if(x==9 && y == 0)
- {
- sum = sum + array[x-1][y];
- sum = sum + array[x][y+1];
- }
- else if(x==9 && y == 9)
- {
- sum = sum + array[x-1][y];
- sum = sum + array[x][y-1];
- }
- else if(x==0)
- {
- sum = sum + array[x][y-1];
- sum = sum + array[x][y+1];
- sum = sum + array[x+1][y];
- }
- else if(x==9)
- {
- sum = sum + array[x-1][y];
- sum = sum + array[x][y-1];
- sum = sum + array[x][y+1];
- }
- else if(y==0)
- {
- sum = sum + array[x][y+1];
- sum = sum + array[x+1][y];
- sum = sum + array[x-1][y];
- }
- else if(y==9)
- {
- sum = sum + array[x][y-1];
- sum = sum + array[x+1][y];
- sum = sum + array[x-1][y];
- }
- else
- {
- sum = sum + array[x][y];
- sum = sum + array[x+1][y];
- sum = sum + array[x-1][y];
- sum = sum + array[x][y+1];
- sum = sum + array[x][y-1];
- }
- /*while(sum>3)
- {
- sum = sum -3;
- }*/
- //int num = solution[sum];
- //int total = num + array[x][y];
- temp = solution[sum] + array [x][y];
- }
- }
- return temp;
- }// end runSolution
- /**
- * The readNum methods reads the number of solutions and the number of days from the file
- * @NOTE there is no prompt only just reading from the file
- * @param fin Representing a valid Scanner object
- * @return int Representing the number read from the file
- * @throws IllegalArgumentException if fin is null
- */
- public static int readNum(final Scanner fin)
- {
- if (fin == null)
- {
- throw new IllegalArgumentException("This is not a valid scanner");
- }
- int num = Integer.parseInt(fin.nextLine());
- return num;
- }
- }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement