Guest User

Untitled

a guest
Mar 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 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. private Object c;
  20.  
  21.  
  22.  
  23. public double checkMagicNumber(double n){
  24. double power = Math.pow(n,2);
  25. double multiple = n*(power +1);
  26. double magicNumber = multiple/2;
  27. return magicNumber;
  28. }
  29. public boolean checkMagicSquare(int numberOfLines, int currentNumb, ArrayList <Integer> msArray){
  30. for(int count=0; count <=numberOfLines; count++){
  31. c = msArray.get(currentNumb);
  32.  
  33. sum = sum + c;
  34. ++currentNumb;
  35. }
  36.  
  37. return false;
  38. }
  39. public boolean checkTraditional(int currentNumb, int numberOfLines){
  40. while(currentNumb <= numberOfLines){
  41. //must sum up both row, and diagonal
  42. return true;
  43. }
  44. return false;
  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. static int traceLevel = 1;
  78. static double firstCounter;
  79.  
  80. public static void main(String [] args) {
  81.  
  82. String line, title = null;
  83. int counter=0, row, numberOfLines = 0, column, rowcount, stringLength, nextNumber, lineMarker = 0, countsWhileLoop=0;
  84. double numbersOnLineCounter = 0;
  85. double arraycounter = 0;
  86. ArrayList msArray = new ArrayList();
  87. System.out.println("Initial size of msArray : " + msArray.size());
  88.  
  89.  
  90. try{
  91.  
  92. if (args.length >= 0) {
  93. File file = new File("/Users/ashleycook/Desktop/magicsquaretester.txt");
  94. Scanner scanfile = new Scanner(file);
  95.  
  96. // Compute means for the first NumComp integers
  97. while(scanfile.hasNextLine()){
  98. ++counter;
  99. System.out.println("\n"+ "\n"+"LOOP " +counter);
  100.  
  101. //will run the if loop if it is the second line otherwise it will print out the first line
  102. if (counter >1){
  103.  
  104. // sets the next line == line:: then splits the line into tokens (different numbers)
  105. line = scanfile.nextLine().replace(',',' ').replace(';',' '); //used to get rid of any commas or semicolons
  106. StringTokenizer splitsline = new StringTokenizer(line, " ");
  107.  
  108. System.out.println("***numbers on line = " +line+"\n");
  109.  
  110. rowcount = counter -2; // used to assign the row
  111. //System.out.println("<<<<<<<<<<< prelimcount =" +prelimcount);
  112.  
  113. stringLength = line.length();
  114. //System.out.println("String length =" + stringLength);
  115.  
  116. int ifCounter = (int) counter;
  117. countsWhileLoop = 0;
  118.  
  119. while(splitsline.hasMoreTokens()){
  120. // System.out.println("^^^reiterate, counts while loop=" + countsWhileLoop);
  121.  
  122. ++numbersOnLineCounter;
  123. lineMarker = (stringLength +1)/2;
  124. System.out.println("lineMarker = "+ lineMarker);
  125.  
  126. nextNumber = Integer.parseInt(splitsline.nextToken());
  127. //System.out.println("nextNumber =" + nextNumber);
  128.  
  129. // System.out.println(ifCounter+"\n" + "Beginning of \"if\" statement:");
  130. if(ifCounter ==2){
  131. row = 0;
  132. column = countsWhileLoop;
  133. // System.out.println("ROW = " +row +" COLUMN = "+column);
  134.  
  135. } else {
  136. row = rowcount;
  137. column = countsWhileLoop;
  138. //System.out.println("Rows = " +row +" Columns = "+column);
  139. }
  140.  
  141. msArray.add(nextNumber);
  142. System.out.println(msArray);
  143. System.out.println("Size of msArray : " + msArray.size());
  144. ++ifCounter;
  145. // System.out.println(ifCounter);
  146. ++countsWhileLoop;
  147. //System.out.println("column(counts while loop) = " + countsWhileLoop);
  148.  
  149. //System.out.println("nextNumber on the line = " + nextNumber);
  150. //System.out.println("Number of numbers:" +numbersOnLineCounter);
  151.  
  152. } // end while loop
  153. ++rowcount;
  154. // System.out.println("----------------------------------------R O W C O U N T (row)=" +rowcount);
  155.  
  156. } // end if statement
  157. else {
  158. title = scanfile.nextLine();
  159. System.out.println(title);
  160. }
  161. }// end while statement
  162.  
  163. numberOfLines = counter-1;
  164. double checkError = Math.sqrt(numbersOnLineCounter);
  165. int array [] = new int [(int)numbersOnLineCounter];
  166. msArray.toArray();
  167.  
  168. if(numberOfLines != checkError){
  169. System.out.println("Error within the file, please check the file again for any missing numbers.");
  170. }//end if Statement
  171. else{
  172. MagicSquareChecker firstObject = new MagicSquareChecker();
  173. double magicNumber = firstObject.checkMagicNumber(numberOfLines);
  174. //System.out.println("magic number =" + magicNumber);
  175. if(firstObject.checkTraditional(numberOfLines, lineMarker) == true){
  176. System.out.println("Is a traditional magic square.");
  177. }
  178. else System.out.println("Is not a traditional magic square.");
  179. if(firstObject.checkCornered(numberOfLines, lineMarker) == true){
  180. System.out.println(title + " is a cornered magic square.");
  181. }
  182. else System.out.println("Is not a cornered magic square.");
  183. if(firstObject.checkPanDiag(numberOfLines, lineMarker) == true){
  184. System.out.println("Is a pan diagonal magic square.");
  185. }
  186. else System.out.println("Is not a pan diagonal magic square.");
  187. if(firstObject.checkMagicSquare(numberOfLines, lineMarker, msArray) == true){
  188. System.out.println("Is a magic square.");
  189. }
  190. else System.out.println("Is not a magic square.");
  191.  
  192. }//end else
  193.  
  194. }// end if statement
  195.  
  196. } // end try block
  197. catch(FileNotFoundException e){
  198. System.err.println("File cannot be opened ");
  199. System.exit(0);
  200. } // end catch FileNotFoundException
  201. catch(IOException e){
  202. System.err.println("File contains non-computable data");
  203. System.exit(0);
  204. } // end catch IOException
  205. catch(ArrayIndexOutOfBoundsException e){
  206. System.err.println("Array index is out of bounds");
  207. System.exit(0);
  208. }
  209.  
  210. } //end main()
  211. }// end class cookProg3
  212.  
  213.  
  214.  
  215. /*things left to do:
  216. *-hashcode::: ?? figure out what it is and how to use it to determine if there are repeating numbers
  217. *-write methods for figuring out the squares
  218. *
  219. */
Add Comment
Please, Sign In to add comment