Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * cookProg3.java
- * @author ashleycook Author: Ashley Cook
- * Created on February 26, 2008 7:01 AM
- *
- */
- import java.util.*;
- import java.io.*;
- import java.lang.*;
- class MagicSquareChecker{
- private double magicNumber;
- private ArrayList list;
- private int incrementer =0;
- private int lastNumber, currentNumb =0, sum =0;
- private int a, b;
- public double checkMagicNumber(double n){
- double power = Math.pow(n,2);
- double multiple = n*(power +1);
- double magicNumber = multiple/2;
- return magicNumber;
- }
- public boolean checkMagicSquare(int numberOfLines, int currentNumb, ArrayList msArray){
- for(int count=0; count <=numberOfLines; count++){
- sum+= msArray.get(currentNumb);
- ++currentNumb;
- }
- return false;
- }
- public boolean checkTraditional(int currentNumb, int numberOfLines, Object intArray[]){
- // for(int i=0;i<ia.length;i++)
- // sum+=((Integer)intArray[i]).intValue();
- // while(currentNumb <= numberOfLines){
- //must sum up both row, and diagonal
- return true;
- }
- public boolean checkCornered(int numberOfLines, int currentNumb){
- while(currentNumb <=4){
- return true;
- //(0,0)+(0,last column)+(last row, 0)+(last row, last column) == magicNumber;
- }
- return false;
- }
- public boolean checkPanDiag(int numberOfLines, int currentNumb){
- while(currentNumb <= numberOfLines){
- return true;
- //(0,0)+(1,1)+(2,2)...
- }
- return false;
- }
- // public boolean checkForEquality(ArrayList list, int lastNumber, in firstNumber, String secondNumber){
- // while(list.indexOf(lastNumber)<incrementer){
- // String a = firstNumber;
- // String b = secondNumber;
- // if(list.indexOf(a).equals(list.indexOf(b))){
- // return false;
- // }
- // else return true;
- // ++incrementer;
- // }
- // }
- }//end class MagicSquareChecker
- public class cookProg3 {
- public static void main(String [] args) {
- String line, title = null;
- int counter=0, row, numberOfLines = 0, column, rowcount, stringLength, nextNumber, lineMarker = 0, countsWhileLoop=0;
- double numbersOnLineCounter = 0;
- double arraycounter = 0;
- ArrayList<ArrayList<Integer>> msArray = new ArrayList<ArrayList<Integer>>();
- System.out.println("Initial size of msArray : " + msArray.size());
- try{
- if (args.length >= 0) {
- File file = new File("C:/Users/Ashley Cook/Desktop/magicsquaretester.txt");
- Scanner scanfile = new Scanner(file);
- // Compute means for the first NumComp integers
- while(scanfile.hasNextLine()){
- ++counter;
- System.out.println("\n"+ "\n"+"LOOP " +counter);
- //will run the if loop if it is the second line otherwise it will print out the first line
- if (counter >1){
- // sets the next line == line:: then splits the line into tokens (different numbers)
- line = scanfile.nextLine().replace(',',' ').replace(';',' '); //used to get rid of any commas or semicolons
- StringTokenizer splitsline = new StringTokenizer(line, " ");
- System.out.println("***numbers on line = " +line+"\n");
- rowcount = counter -2; // used to assign the row
- //System.out.println("<<<<<<<<<<< prelimcount =" +prelimcount);
- stringLength = line.length();
- //System.out.println("String length =" + stringLength);
- int ifCounter = (int) counter;
- countsWhileLoop = 0;
- while(splitsline.hasMoreTokens()){
- // System.out.println("^^^reiterate, counts while loop=" + countsWhileLoop);
- ++numbersOnLineCounter;
- lineMarker = (stringLength +1)/2;
- System.out.println("lineMarker = "+ lineMarker);
- nextNumber = Integer.parseInt(splitsline.nextToken());
- //System.out.println("nextNumber =" + nextNumber);
- // System.out.println(ifCounter+"\n" + "Beginning of \"if\" statement:");
- if(ifCounter ==2){
- row = 0;
- column = countsWhileLoop;
- // System.out.println("ROW = " +row +" COLUMN = "+column);
- } else {
- row = rowcount;
- column = countsWhileLoop;
- //System.out.println("Rows = " +row +" Columns = "+column);
- }
- msArray.add(new Integer(nextNumber));
- System.out.println(msArray);
- System.out.println("Size of msArray : " + msArray.size());
- ++ifCounter;
- // System.out.println(ifCounter);
- ++countsWhileLoop;
- //System.out.println("column(counts while loop) = " + countsWhileLoop);
- //System.out.println("nextNumber on the line = " + nextNumber);
- //System.out.println("Number of numbers:" +numbersOnLineCounter);
- } // end while loop
- ++rowcount;
- // System.out.println("R O W C O U N T (row)=" +rowcount);
- } // end if statement
- else {
- title = scanfile.nextLine();
- System.out.println(title);
- }
- }// end while statement
- numberOfLines = counter-1;
- double checkError = Math.sqrt(numbersOnLineCounter);
- Object array[] = msArray.toArray();
- if(numberOfLines != checkError){
- System.out.println("Error within the file, please check the file again for any missing numbers.");
- }//end if Statement
- else{
- MagicSquareChecker firstObject = new MagicSquareChecker();
- double magicNumber = firstObject.checkMagicNumber(numberOfLines);
- //System.out.println("magic number =" + magicNumber);
- if(firstObject.checkTraditional(numberOfLines, lineMarker, ArrayList<Integer> msArray) == true){
- System.out.println("Is a traditional magic square.");
- }
- else System.out.println("Is not a traditional magic square.");
- if(firstObject.checkCornered(numberOfLines, lineMarker) == true){
- System.out.println(title + " is a cornered magic square.");
- }
- else System.out.println("Is not a cornered magic square.");
- if(firstObject.checkPanDiag(numberOfLines, lineMarker) == true){
- System.out.println("Is a pan diagonal magic square.");
- }
- else System.out.println("Is not a pan diagonal magic square.");
- if(firstObject.checkMagicSquare(numberOfLines, lineMarker, msArray) == true){
- System.out.println("Is a magic square.");
- }
- else System.out.println("Is not a magic square.");
- }//end else
- }// end if statement
- } // end try block
- catch(FileNotFoundException e){
- System.err.println("File cannot be opened ");
- System.exit(0);
- } // end catch FileNotFoundException
- catch(IOException e){
- System.err.println("File contains non-computable data");
- System.exit(0);
- } // end catch IOException
- catch(ArrayIndexOutOfBoundsException e){
- System.err.println("Array index is out of bounds");
- System.exit(0);
- }
- } //end main()
- }// end class cookProg3
Add Comment
Please, Sign In to add comment