Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. // Name: Ian Mahern-Macias
  2. // Class: 1400-002
  3. // Program #: 17
  4. // Due Date: 11/25/14
  5. //
  6. // Honor Pledge: On my honor as a student of the University
  7. // of Nebraska at Omaha, I have neither given nor received
  8. // unauthorized help on this homework assignment.
  9. //
  10. // NAME: Ian Mahern-Macias
  11. // EMAIL: Ianmahern@gmail.com
  12. // NUID: 061   
  13. // Colleagues:
  14. //
  15. //  This program makes "magic boxes" that are 4x4 and determines
  16. //  if what the user entered is is a true magic box or not.  
  17. import java.util.Scanner;
  18.  
  19. public class imahernmacias_Magic
  20. {
  21. public static void main (String [] args)
  22. {
  23.     Scanner input = new Scanner (System.in);       
  24.     int [][] theSquare = new int [4][4];
  25.    
  26.    
  27.  
  28.     System.out.println("The magic value for your square is 34, which means that every row, \n column and diagonal of your square must add up to that number.");
  29.  
  30.  
  31.  
  32.    
  33.     System.out.print("Please enter the 4 values for row 0, separated by spaces:");
  34.     theSquare[0][0] = input.nextInt();
  35.     theSquare[0][1] = input.nextInt();
  36.     theSquare[0][2] = input.nextInt();
  37.     theSquare[0][3] = input.nextInt();
  38.    
  39.     //System.out.println("");  
  40.  
  41.     System.out.print("Please enter the 4 values for row 1, separated by spaces:");
  42.     theSquare[1][0] = input.nextInt();
  43.     theSquare[1][1] = input.nextInt();
  44.     theSquare[1][2] = input.nextInt();
  45.     theSquare[1][3] = input.nextInt();
  46.    
  47.     //System.out.println("");
  48.    
  49.     System.out.print("Please enter the 4 values for row 2, separated by spaces:");
  50.     theSquare[2][0] = input.nextInt();
  51.     theSquare[2][1] = input.nextInt();
  52.     theSquare[2][2] = input.nextInt();
  53.     theSquare[2][3] = input.nextInt();
  54.    
  55.     //System.out.println("");  
  56.  
  57.     System.out.print("Please enter the 4 values for row 3, separated by spaces:");
  58.     theSquare[3][0] = input.nextInt();
  59.     theSquare[3][1] = input.nextInt();
  60.     theSquare[3][2] = input.nextInt();
  61.     theSquare[3][3] = input.nextInt();
  62.    
  63.     System.out.println("Checking square for problems:");
  64.     boolean valid = true;
  65.  
  66.     valid = checkDiagonals(theSquare);
  67.     valid = checkRows(theSquare);
  68.     valid = checkColumns(theSquare);
  69.     valid = checkRange(theSquare);
  70.    
  71.     //fucking check magic here, asshole. also print
  72.  
  73.     System.out.print("  MAGIC:");
  74.  
  75.     if ( valid == true)
  76.     {
  77.         System.out.println("VALID");
  78.     }
  79.     if (valid == false)
  80.     {
  81.         System.out.println("NO");
  82.     }
  83. }
  84. public static boolean checkRows (int theSquare[][])
  85. {
  86. System.out.print("  ROWS: ");
  87. boolean valid = true;
  88.  
  89.  
  90.      if (theSquare[0][0] + theSquare[0][1] +    theSquare[0][2] + theSquare[0][3] != 34)
  91.     {
  92.         valid = false;
  93.         System.out.print("0 ");
  94.     }
  95.  
  96.     if (theSquare[1][0] + theSquare[1][1] + theSquare[1][2] + theSquare[1][3] != 34)
  97.     {
  98.         valid = false;
  99.         System.out.print("1 ");
  100.     }
  101.  
  102.     if (theSquare[2][0] + theSquare[2][1] + theSquare[2][2] + theSquare[2][3] != 34)
  103.     {
  104.         valid = false;
  105.         System.out.print("2 ");
  106.     }
  107.  
  108.  
  109.     if (theSquare[3][0] + theSquare[3][1] + theSquare[3][2] + theSquare[3][3] != 34)
  110.     {
  111.         valid = false;
  112.         System.out.print("3 ");
  113.     }
  114.  
  115.     if ( valid == true)
  116.     {
  117.         System.out.print("VALID");
  118.     }
  119.     System.out.println("");
  120.     return valid;
  121.        
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. public static boolean checkColumns (int theSquare[][])
  129. {  
  130. System.out.print("  COLS: ");
  131.  
  132. boolean valid = true;
  133.  
  134.         if (theSquare[0][0] + theSquare[1][0] + theSquare[2][0] + theSquare[3][0] != 34)
  135.     {
  136.         valid = false;
  137.         System.out.print("0 ");
  138.     }
  139.  
  140.     if (theSquare[0][1] + theSquare[1][1] + theSquare[2][1] + theSquare[3][1] != 34)
  141.     {
  142.         valid = false;
  143.         System.out.print("1 ");
  144.     }
  145.  
  146.     if (theSquare[0][2] + theSquare[1][2] + theSquare[2][2] + theSquare[3][2] != 34)
  147.     {
  148.         valid = false;
  149.         System.out.print("2 ");
  150.     }
  151.  
  152.  
  153.     if (theSquare[0][3] + theSquare[1][3] + theSquare[2][3] + theSquare[3][3] != 34)
  154.     {
  155.         valid = false;
  156.         System.out.print("3 ");
  157.     }
  158.         if ( valid == true)
  159.     {
  160.         System.out.print("VALID");
  161.     }
  162.     System.out.println("");
  163.     return valid;
  164.        
  165. }
  166.  
  167. public static boolean checkDiagonals (int theSquare[] [])
  168. {
  169. System.out.print("  DIAG: ");
  170. boolean valid = true;
  171.    
  172.     if (theSquare[0][0] + theSquare[1][1] + theSquare[2][2] + theSquare[3][3] != 34)
  173.     {
  174.         valid = false;
  175.         System.out.print("0 ");
  176.     }
  177.  
  178.  
  179.     if (theSquare[3][0] + theSquare[2][1] + theSquare[1][2] + theSquare[0][3] != 34)
  180.     {
  181.         valid = false;
  182.         System.out.print("1 ");
  183.     }
  184.         if ( valid == true)
  185.     {
  186.         System.out.print("VALID");
  187.     }
  188.     System.out.println("");
  189.     return valid;
  190.  
  191. }
  192.  
  193.  
  194. public static boolean checkRange (int theSquare[][])
  195. {
  196. System.out.print("  RANG: ");
  197. boolean booleanArray[] = new boolean [17];
  198.     for (int i = 0; i < 17; i++)
  199.     {
  200.         booleanArray[i] = false;
  201.     }
  202.  
  203. boolean valid = true;
  204.  
  205.    
  206.     for (int r = 0; r < theSquare.length; r++)
  207.     {
  208.         for (int c = 0; c < theSquare[r].length; c++)
  209.         {
  210.             if (theSquare[r][c] < 17 && theSquare[r][c] > 0 &&  booleanArray[theSquare[r][c]] == false)
  211.             {
  212.                  booleanArray[theSquare[r][c]] = true;
  213.  
  214.             }
  215.             else
  216.             {
  217.                   System.out.printf("%d ", theSquare[r][c]);
  218.                               valid = false;
  219.  
  220.             }
  221.  
  222.  
  223.         }
  224.     }
  225. if (valid == true)
  226. {
  227.    
  228.     System.out.print("VALID");
  229. }
  230.  
  231.    
  232.  
  233. System.out.println("");
  234. return valid;
  235.  
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement