Guest User

Untitled

a guest
Mar 13th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. /*
  2. * cookProg3.java
  3. * @author ashleycook Author: Ashley Cook
  4. * Created on February 26, 2008 7:01 AM
  5. *
  6. */
  7.  
  8. import java.util.*;
  9. import java.io.*;
  10. import java.lang.*;
  11.  
  12. class MagicSquareChecker{
  13.  
  14. private double magicNumber;
  15. private ArrayList list;
  16. private int incrementer =0;
  17. private int lastNumber, currentNumb =0, sum =0;
  18. private int a, b;
  19.  
  20. public double checkMagicNumber(double n){
  21. double power = Math.pow(n,2);
  22. double multiple = n*(power +1);
  23. double magicNumber = multiple/2;
  24. return magicNumber;
  25. }
  26. public boolean checkMagicSquare(int numberOfLines, int currentNumb, ArrayList msArray){
  27. for(int count=0; count <=numberOfLines; count++){
  28. sum+= msArray.get(currentNumb);
  29. ++currentNumb;
  30. }
  31.  
  32. return false;
  33. }
  34. public boolean checkTraditional(int currentNumb, int numberOfLines, Object intArray[]){
  35. // for(int i=0;i<ia.length;i++)
  36. // sum+=((Integer)intArray[i]).intValue();
  37.  
  38.  
  39.  
  40. // while(currentNumb <= numberOfLines){
  41. //must sum up both row, and diagonal
  42. return true;
  43.  
  44. }
  45.  
  46. public boolean checkCornered(int numberOfLines, int currentNumb){
  47. while(currentNumb <=4){
  48. return true;
  49. //(0,0)+(0,last column)+(last row, 0)+(last row, last column) == magicNumber;
  50. }
  51. return false;
  52. }
  53. public boolean checkPanDiag(int numberOfLines, int currentNumb){
  54. while(currentNumb <= numberOfLines){
  55. return true;
  56. //(0,0)+(1,1)+(2,2)...
  57. }
  58. return false;
  59. }
  60. // public boolean checkForEquality(ArrayList list, int lastNumber, in firstNumber, String secondNumber){
  61.  
  62. // while(list.indexOf(lastNumber)<incrementer){
  63. // String a = firstNumber;
  64. // String b = secondNumber;
  65.  
  66. // if(list.indexOf(a).equals(list.indexOf(b))){
  67. // return false;
  68. // }
  69. // else return true;
  70. // ++incrementer;
  71. // }
  72. // }
  73.  
  74. }//end class MagicSquareChecker
  75. public class cookProg3 {
  76.  
  77. public static void main(String [] args) {
  78.  
  79. String line, title = null;
  80. int counter=0, row, numberOfLines = 0, column, rowcount, stringLength, nextNumber, lineMarker = 0, countsWhileLoop=0;
  81. double numbersOnLineCounter = 0;
  82. double arraycounter = 0;
  83. ArrayList<ArrayList<Integer>> msArray = new ArrayList<ArrayList<Integer>>();
  84. System.out.println("Initial size of msArray : " + msArray.size());
  85.  
  86.  
  87. try{
  88.  
  89. if (args.length >= 0) {
  90. File file = new File("C:/Users/Ashley Cook/Desktop/magicsquaretester.txt");
  91. Scanner scanfile = new Scanner(file);
  92.  
  93. // Compute means for the first NumComp integers
  94. while(scanfile.hasNextLine()){
  95. ++counter;
  96. System.out.println("\n"+ "\n"+"LOOP " +counter);
  97.  
  98. //will run the if loop if it is the second line otherwise it will print out the first line
  99. if (counter >1){
  100.  
  101. // sets the next line == line:: then splits the line into tokens (different numbers)
  102. line = scanfile.nextLine().replace(',',' ').replace(';',' '); //used to get rid of any commas or semicolons
  103. StringTokenizer splitsline = new StringTokenizer(line, " ");
  104.  
  105. System.out.println("***numbers on line = " +line+"\n");
  106.  
  107. rowcount = counter -2; // used to assign the row
  108. //System.out.println("<<<<<<<<<<< prelimcount =" +prelimcount);
  109.  
  110. stringLength = line.length();
  111. //System.out.println("String length =" + stringLength);
  112.  
  113. int ifCounter = (int) counter;
  114. countsWhileLoop = 0;
  115.  
  116. while(splitsline.hasMoreTokens()){
  117. // System.out.println("^^^reiterate, counts while loop=" + countsWhileLoop);
  118.  
  119. ++numbersOnLineCounter;
  120. lineMarker = (stringLength +1)/2;
  121. System.out.println("lineMarker = "+ lineMarker);
  122.  
  123. nextNumber = Integer.parseInt(splitsline.nextToken());
  124. //System.out.println("nextNumber =" + nextNumber);
  125.  
  126. // System.out.println(ifCounter+"\n" + "Beginning of \"if\" statement:");
  127. if(ifCounter ==2){
  128. row = 0;
  129. column = countsWhileLoop;
  130. // System.out.println("ROW = " +row +" COLUMN = "+column);
  131.  
  132. } else {
  133. row = rowcount;
  134. column = countsWhileLoop;
  135. //System.out.println("Rows = " +row +" Columns = "+column);
  136. }
  137.  
  138. msArray.add(new Integer(nextNumber));
  139. System.out.println(msArray);
  140. System.out.println("Size of msArray : " + msArray.size());
  141. ++ifCounter;
  142. // System.out.println(ifCounter);
  143. ++countsWhileLoop;
  144. //System.out.println("column(counts while loop) = " + countsWhileLoop);
  145.  
  146. //System.out.println("nextNumber on the line = " + nextNumber);
  147. //System.out.println("Number of numbers:" +numbersOnLineCounter);
  148.  
  149. } // end while loop
  150. ++rowcount;
  151. // System.out.println("R O W C O U N T (row)=" +rowcount);
  152.  
  153. } // end if statement
  154. else {
  155. title = scanfile.nextLine();
  156. System.out.println(title);
  157. }
  158. }// end while statement
  159.  
  160. numberOfLines = counter-1;
  161. double checkError = Math.sqrt(numbersOnLineCounter);
  162.  
  163. Object array[] = msArray.toArray();
  164.  
  165. if(numberOfLines != checkError){
  166. System.out.println("Error within the file, please check the file again for any missing numbers.");
  167. }//end if Statement
  168. else{
  169. MagicSquareChecker firstObject = new MagicSquareChecker();
  170. double magicNumber = firstObject.checkMagicNumber(numberOfLines);
  171. //System.out.println("magic number =" + magicNumber);
  172. if(firstObject.checkTraditional(numberOfLines, lineMarker, ArrayList<Integer> msArray) == true){
  173. System.out.println("Is a traditional magic square.");
  174. }
  175.  
  176. else System.out.println("Is not a traditional magic square.");
  177. if(firstObject.checkCornered(numberOfLines, lineMarker) == true){
  178. System.out.println(title + " is a cornered magic square.");
  179. }
  180. else System.out.println("Is not a cornered magic square.");
  181. if(firstObject.checkPanDiag(numberOfLines, lineMarker) == true){
  182. System.out.println("Is a pan diagonal magic square.");
  183. }
  184. else System.out.println("Is not a pan diagonal magic square.");
  185. if(firstObject.checkMagicSquare(numberOfLines, lineMarker, msArray) == true){
  186. System.out.println("Is a magic square.");
  187. }
  188. else System.out.println("Is not a magic square.");
  189.  
  190. }//end else
  191.  
  192. }// end if statement
  193.  
  194. } // end try block
  195. catch(FileNotFoundException e){
  196. System.err.println("File cannot be opened ");
  197. System.exit(0);
  198. } // end catch FileNotFoundException
  199. catch(IOException e){
  200. System.err.println("File contains non-computable data");
  201. System.exit(0);
  202. } // end catch IOException
  203. catch(ArrayIndexOutOfBoundsException e){
  204. System.err.println("Array index is out of bounds");
  205. System.exit(0);
  206. }
  207.  
  208. } //end main()
  209. }// end class cookProg3
Add Comment
Please, Sign In to add comment