SHARE
TWEET

Untitled

a guest Feb 17th, 2020 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lab10.cscd210methods;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * The methods for the Andromeda Problem.<br>
  7.  * All parameters will be passed as final<br>
  8.  * All preconditions and post conditions will be enforced
  9.  */
  10. public class CSCD210Lab10Methods
  11. {
  12.    /**
  13.     * This method fills the solutions array with the values
  14.     * from the file.
  15.     * @param fin Representing a Scanner object to the open file
  16.     * @return int [] Representing the filled array
  17.     * @throws IllegalArgumentException if fin is null
  18.     */
  19.    public static int [] fillSolution(final Scanner fin)
  20.    {
  21.       if(fin == null)
  22.       {
  23.          throw new IllegalArgumentException("This is not a valid scanner");
  24.       }
  25.       int [] solution = new int[16];
  26.       for(int x = 0; x<solution.length-1; x++)
  27.       {
  28.          solution[x] = fin.nextInt();
  29.       }
  30.      
  31.       return solution;
  32.    
  33.    }// end fillSolution
  34.    
  35.    
  36.    /**
  37.     * This method prints Solution and then the values from the solution array
  38.     * @param array Representing the filled solutions array
  39.     * @throws IllegalArgumentException if the array is null or the length less than 1
  40.     */
  41.    public static void print(final int [] array)
  42.    {
  43.       if(array == null || array.length-1<1)
  44.       {
  45.          throw new IllegalArgumentException("Bad Parameters");
  46.       }
  47.       for(int x = 0; x<array.length-1; x++)
  48.       {
  49.          System.out.print(array[x]+" ");
  50.       }
  51.       System.out.println();      
  52.    }// end print
  53.    
  54.    
  55.    /**
  56.     * This method creates the generation array which is a 10 x 10 2D array
  57.     * of integers. The array is then filled with the values
  58.     * from the file.
  59.     * @param fin Representing a Scanner object to the open file
  60.     * @return int[] [] Representing the filled generation array
  61.     * @throws IllegalArgumentException if fin is null
  62.     */
  63.    public static int [][] fillGeneration(final Scanner fin)
  64.    {
  65.       if(fin == null)
  66.       {
  67.          throw new IllegalArgumentException("This is not a valid Scanner");
  68.       }
  69.       int [][] generation = new int[10][10];
  70.       for (int x = 0; x<generation.length;x++)
  71.       {
  72.          for(int y = 0; y<generation.length; y++)
  73.          {
  74.             generation [x][y] = fin.nextInt();
  75.          }
  76.       }          
  77.       return generation;
  78.    
  79.    }// end fillGeneration
  80.    
  81.    
  82.    /**
  83.     * This method prints Generation and the day and then the generation array
  84.     * with the values converted to the proper characters
  85.     * @param array Representing the filled generation array
  86.     * @param day Representing which day the generation is on
  87.     * @throws IllegalArgumentException if the array is null or the length less than 1
  88.     * @throws IllegalArgumentException if day is less than 1
  89.     */
  90.    public static void print(final int [][] array, final int day)
  91.    {
  92.       if( array == null || array.length-1<0 || day<0)
  93.       {
  94.          throw new IllegalArgumentException("Bad parameters");
  95.       }
  96.       System.out.println("Generation: ");
  97.       System.out.print(day);
  98.       System.out.println();
  99.       for(int x = 0; x<array.length-1;x++)
  100.       {
  101.          for(int y = 0; y<array.length-1; y++)
  102.          {
  103.             if(-1<array[x][y] || array[x][y]<4)
  104.             {
  105.                if(array[x][y] == 0)
  106.                {
  107.                   System.out.print(".");
  108.                }
  109.                else if(array[x][y] == 1)
  110.                {
  111.                   System.out.print("!");
  112.                }
  113.                else if(array[x][y] == 2)
  114.                {
  115.                   System.out.print("X");
  116.                }
  117.                else
  118.                {
  119.                   System.out.print("#");
  120.                }
  121.            }
  122.        }
  123.        System.out.println();
  124.      }
  125.    }// end print
  126.    
  127.    
  128.    /**
  129.     * This method prints Generation 0 and then the generation array
  130.     * with the values converted to the proper characters
  131.     * @param array Representing the filled generation array
  132.     * @throws IllegalArgumentException if the array is null or the length less than 1
  133.     */
  134.    public static void print(final int [][] array)
  135.    {
  136.       if(array == null || array.length-1<1)
  137.       {
  138.          throw new IllegalArgumentException("Bad Parammeters");
  139.       }
  140.      
  141.       System.out.println("Generation: 0 ");
  142.       for(int x = 0; x<array.length-1;x++)
  143.       {
  144.          for(int y = 0; y<array.length-1; y++)
  145.          {
  146.             if(-1<array[x][y] || array[x][y]<4)
  147.             {
  148.                if(array[x][y] == 3)
  149.                {
  150.                   System.out.print("#");
  151.                }
  152.                else if(array[x][y] == 1)
  153.                {
  154.                   System.out.print("!");
  155.                }
  156.                else if(array[x][y] == 2)
  157.                {
  158.                   System.out.print("X");
  159.                }
  160.                else
  161.                {
  162.                   System.out.print(".");
  163.                }
  164.            }
  165.        }
  166.        System.out.println();
  167.      }
  168.  
  169.      
  170.    }// end print
  171.  
  172.    
  173.    
  174.    /**
  175.     * This method is the hardest part of the assignment.  It is passed the 1D solution array
  176.     * and the 2D generation array. The method makes a new temp 2D array that is 10 x 10. The idea
  177.     * here is each element in the generation array is examined. The solution is applied against the
  178.     * generation array and the results are stored in the new temp array for that element. Once all
  179.     * elements in the generation array have been processed the temp array is returned.
  180.     * @param array Representing the 2D generation array
  181.     * @param solution Representing the 1D solution array
  182.     * @return int [][] Representing a new 2D generation array
  183.     * @throws IllegalArgumentException if either array is null or the length is less than 1
  184.     */
  185.    public static int [][] runSolution(final int [][] array, final int [] solution)
  186.    {
  187.       if(array == null || array.length-1<1)
  188.       {
  189.          throw new IllegalArgumentException("Bad Parameters");
  190.       }
  191.       int[][] temp = new int [10][10];
  192.       for(int x = 0; x<array.length-1; x++)
  193.       {
  194.          for(int y = 0; y<array.length-1; y++)
  195.          {
  196.             int sum = 0;
  197.             if(x==0 && y == 0)
  198.             {
  199.                sum = sum + array[x+1][y];
  200.                sum = sum + array[x][y+1];
  201.             }
  202.             else if(x==0 && y == 9)
  203.             {
  204.                sum = sum + array[x][y-1];
  205.                sum = sum + array[x+1][y-1];
  206.             }
  207.             else if(x==9 && y == 0)
  208.             {
  209.                sum = sum + array[x-1][y];
  210.                sum = sum + array[x][y+1];
  211.             }
  212.             else if(x==9 && y == 9)
  213.             {
  214.                sum = sum + array[x-1][y];
  215.                sum = sum + array[x][y-1];
  216.             }
  217.             else if(x==0)
  218.             {
  219.                sum = sum + array[x][y-1];
  220.                sum = sum + array[x][y+1];
  221.                sum = sum + array[x+1][y];
  222.             }
  223.             else if(x==9)
  224.             {
  225.                sum = sum + array[x-1][y];
  226.                sum = sum + array[x][y-1];
  227.                sum = sum + array[x][y+1];
  228.             }
  229.             else if(y==0)
  230.             {
  231.                sum = sum + array[x][y+1];
  232.                sum = sum + array[x+1][y];
  233.                sum = sum + array[x-1][y];
  234.             }
  235.             else if(y==9)
  236.             {
  237.                sum = sum + array[x][y-1];
  238.                sum = sum + array[x+1][y];
  239.                sum = sum + array[x-1][y];
  240.             }
  241.             else
  242.             {
  243.             sum = sum + array[x][y];
  244.             sum = sum + array[x+1][y];
  245.             sum = sum + array[x-1][y];
  246.             sum = sum + array[x][y+1];
  247.             sum = sum + array[x][y-1];
  248.             }
  249.          /*while(sum>3)
  250.          {
  251.          sum = sum -3;
  252.          }*/
  253.          //int num = solution[sum];
  254.          //int total = num + array[x][y];
  255.          temp = solution[sum] + array [x][y];
  256.          }
  257.      
  258.       }
  259.      
  260.       return temp;
  261.    
  262.    }// end runSolution
  263.    
  264.    
  265.    /**
  266.     * The readNum methods reads the number of solutions and the number of days from the file
  267.     * @NOTE there is no prompt only just reading from the file
  268.     * @param fin Representing a valid Scanner object
  269.     * @return int Representing the number read from the file
  270.     * @throws IllegalArgumentException if fin is null
  271.     */
  272.    public static int readNum(final Scanner fin)
  273.    {
  274.       if (fin == null)
  275.       {
  276.          throw new IllegalArgumentException("This is not a valid scanner");
  277.       }
  278.       int num = Integer.parseInt(fin.nextLine());
  279.       return num;
  280.    
  281.    }
  282.    
  283.  
  284. }// end class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top