Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Doesnt have Main Method?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Doesnt have Main Method?

    A guy in my cs class sent me an answer to a program that I didn't get working correctly last week. He's not responding to my emails so I figured I'd ask here. So, i copied and pasted the code tried to run it to see what the result would be, and i get an error saying there is no main method?

     
    package mat2670;
     
    import java.util.*;
     
    public class MagicSquare {
        // The n/sidelength value of the magicsquare 
     
        private static int magicNumber;
        // Array of integers holding the user's inputs   
        private static int[] inputs;
        // Double array containing the magic square 
        private static int[][] magicSquare;
        private static int count = 0;
     
        public static void main(int args) {
     
            magicNumber = args;
            magicSquare = new int[args][args];
            inputs = new int[(int) Math.pow(Double.valueOf(args), 2)];
     
        }
     
        /**
         *
         * @return the magic constant m using the formula m = n * (n^2 + 1) / 2 ex.
         * n = 3 m = 3 (3^2 +1) 2 m = 15
         */
        private int computeMagicConstant() {
     
            return (int) (magicNumber * (Math.pow(magicNumber, 2) + 1) / 2);
     
        }
     
        /**
         *
         * @return true if the sum of rows equals magic constant
         */
        private boolean checkRows() {
            for (int row = 0; row < magicSquare.length; row++) {
                int tempSum = 0;
                for (int col = 0; col < magicSquare.length; col++) {
                    tempSum += magicSquare[row][col];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
            }
     
            return true;
        }
     
        /**
         *
         * @return true if the sum of columns equals magic constant
         */
        private boolean checkColumns() {
            for (int col = 0; col < magicSquare.length; col++) {
                int tempSum = 0;
                for (int row = 0; row < magicSquare.length; row++) {
                    tempSum += magicSquare[row][col];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
            }
     
            return true;
        }
     
        /**
         *
         * @return true if sum of diagonals equals magic constant
         */
        private boolean checkDiagonals() {
     
            // Check bottom left to top right
            for (int cornerRow = getMagicSquare().length - 1; cornerRow >= 0; cornerRow--) {
                int tempSum = 0;
                for (int cornerColumn = 0; cornerColumn < getMagicSquare().length; cornerColumn++) {
                    tempSum += magicSquare[cornerRow][cornerColumn];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
     
            }
     
            // Check bottom right to top left
            for (int cornerRow = getMagicSquare().length - 1; cornerRow >= 0; cornerRow--) {
                int tempSum = 0;
                for (int cornerColumn = getMagicSquare().length - 1; cornerColumn >= 0; cornerColumn--) {
                    tempSum += magicSquare[cornerRow][cornerColumn];
                }
                if (tempSum != computeMagicConstant()) {
                    return false;
                }
            }
     
            return true;
        }
     
        /**
         * Checks if userinput is the full range of numbers from n to n^2 Checks if
         * number of inputs equals n^2
         *
         * @return
         */
        public boolean checkInput() {
            // Convert the array to a Set of integers. Duplicates will be eliminated   
            Set<Integer> setMagicSquare = new HashSet<Integer>();
            for (int i = 0; i < inputs.length; i++) {
                setMagicSquare.add(inputs[i]);
            }
     
            // Check if the number of values equals n^2
            if (setMagicSquare.size() != Math.pow(magicNumber, 2)) {
                return false;
            }
     
     
            // Check if all values from 1 to n^2 is present
            for (int i = 1; i <= Math.pow(magicNumber, 2); i++) {
                if (!setMagicSquare.contains(i)) {
                    return false;
                }
            }
            return true;
        }
     
        /**
         *
         * @param i a value typed in by the user
         */
        public void addInt(int i) {
            inputs[count] = i;
            count++;
        }
     
        /**
         *
         * @return true if the numbers form a magicsquare
         */
        public boolean isMagic() {
            fillMagicSquare();
     
            return checkInput() && checkRows()
                    && checkColumns()
                    && checkDiagonals();
        }
     
        /**
         *
         * @return an instance of the magicsquare
         */
        public int[][] getMagicSquare() {
            return this.magicSquare;
     
        }
     
        /**
         * Creates a magicsquare array
         */
        private void fillMagicSquare() {
            int counter = 0;
            for (int row = 0; row < magicNumber; row++) {
                for (int col = 0; col < magicNumber; col++) {
                    magicSquare[row][col] = inputs[counter];
                    counter++;
                }
            }
     
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,391
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Doesnt have Main Method?

    Instead of cheating off of another student, and then dumping code on a forum, I suggest you actually learn Java.

    The Java™ Tutorials
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Doesnt have Main Method?

    .. I understand that, I was only asking because my professor told me to get the code from another student and see how they accomplished it.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,391
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Doesnt have Main Method?

    Take a look at the tutorials I posted, specifically this part: "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. How to call a value from a different method to main
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 22nd, 2011, 06:29 PM
  3. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  4. [SOLVED] method inside main()
    By rohan22 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2011, 11:42 PM
  5. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM