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 10 of 10

Thread: class function

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default class function

    hi

    i have created a class called Tuple that simply has two variables in it that are defined when the class is created. what i want to do now is add a function to the class that is called compare.

    i want this function to compare an array of Tuples and then return one from that array.

    i know i can call a function on a single instance but how am i able to make that function callable from the array

    this is what i have so far
    public class Tuple {
        int x;
        int y;
     
        public Tuple(int a, int b) {
              x = a;
              y = b;
        }
    }

    i have this right but i want to be able to call the function compare() this so
    Tuple[] tupleArray = new Tuple[2];
    //add stuff to tupleArray
    Tuple newTuple = tupleArray.compare();

    thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: class function

    Array's do not have methods. You will need to define the compare() method in an existing class and pass it the array as an argument.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: class function

    implement the Comparable interface to your class, then write a compareTo() method. Afterwards you can then determine what object you want to return.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: class function

    thanks for your help. i've ended up doing this inside the Tuple class

    static public Tuple compare(Tuple[] t) {
            return new Tuple(1,1);
     }
    which can be called like so
    Tuple x = Tuple.compare(arrayOfTuples)

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: class function

    Hello huddo!
    Quote Originally Posted by huddo View Post
    static public Tuple compare(Tuple[] t) {
            return new Tuple(1,1);
     }
    which can be called like so
    Tuple x = Tuple.compare(arrayOfTuples)
    Is this what you want your compare method to do? If so (just return a new Tuple), you don't need a method to do that. You can directly say:
    Tuple x = new Tuple(1,1);
    But in your first post you say you want to compare an array. Can you elaborate this?

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: class function

    sorry i hadn't finished writing the whole function before i put it up. But basically i'm making a memory game for school, so choose two cards and see if they match. i've made it so that the board is made programatically but i want to make the board as close as possible to a square as i can so i've created a Tuple class that holds the length and width of a possible game board. i get all the possibilities for the game board and then compare them using this function and return the Tuple that is closest to a square.
        static public Tuple compare(Tuple[] squares) {
            int minDifference = 9999999;//a crazy number to make sure a proper value is smaller
            Tuple finalTuple = new Tuple(0,0);
            for (Tuple option : squares ) {
                if (Math.abs(option.x-option.y) < minDifference) {
                    minDifference = Math.abs(option.x-option.y);
                    finalTuple = option;
                }
            }
            return finalTuple;
        }

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: class function

    Quote Originally Posted by huddo View Post
    sorry i hadn't finished writing the whole function before i put it up. But basically i'm making a memory game for school, so choose two cards and see if they match. i've made it so that the board is made programatically but i want to make the board as close as possible to a square as i can so i've created a Tuple class that holds the length and width of a possible game board. i get all the possibilities for the game board and then compare them using this function and return the Tuple that is closest to a square.
    If x,y are the 2D coordinates of a Tuple, what does Math.abs(option.x-option.y) represent? If you want to calculate the distance between two Tuples (2D points) you can try calculating the euclidean distance between them. Below is the formula
            // euclidean distance between Point1(x1,y1) and Point2(x2,y2)
            xd = x2 - x1
            yd = y2 - y1
            euclidean distance = SquareRoot (xd^2 + yd^2)

    What do you mean you want to make the board as close as possible to a square? Should your game have a "board" and "cards" placed in it? If that's the case, I think you should reconsider your design. The cards can be the Tuples that you already have and the board can be represented with a 2D array of cards (Tuples).
    Last edited by andreas90; June 11th, 2012 at 06:25 AM.

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: class function

    basically this is what my program does

    -gets the number of players and the game type they want to play with -multiplayer, beat the clock and a high score mode
    -depending on the number of players it works out the minimum number of squares along the width because i need to show labels that display information relating to the players turns and pairs found and what ever. it also set the minimum number of rows to 2
    -it then gets the dimensions of the screen and works out the maximum number of rows and columns.
    -then using these values i have a for loop in a for loop that works out all possibilities that will fit on the screen.
    ArrayList options = new ArrayList();
            for (int x=minWidth;x<=maxWidth;x++) {
                for (int y=minHeight;y<maxHeight;y++) {
                    if (x*y%2 == 0 && !options.contains(x*y)) {
                        options.add(x*y);
                    }
                }
            }

    -the user can then select the number of squares they want to play with from these values
    -once the user has selected the number of squares they want to work out all the different possibilities that the board can have
    so just say that the user wants 20 squares then the can either have a 2*10 board or a 4*5 board.
    -as i want the board to be as close to a square as possible so x*x (where x = y) will equal the number of squares.

    -this is where i use the compare function to get the possibility closest to a square.

    if that makes sense?

  9. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: class function

    I think I might get it now.
    But I can't see why you need the last posted code. Since the options doesn't keep x and y (coordinates) but only their product and the only condition is that it must be even. What if you just ask the user for an even number (in the desired range), calculate all possible combinations for this number and then select the one that is "closest to a square"? Am I missing something?

  10. #10
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: class function

    that code works out the number of squares the player can have. I could use a JOptionPane but I'm trying to make it so that it is easy for the user to use and they can't screw up by putting in a string. I know i can catch the error and get them to try again. I'm also trying to utilise as many possible objects in Java to show my range in skills to my teacher

Similar Threads

  1. Function in Function?
    By junxiqqq in forum Java Theory & Questions
    Replies: 5
    Last Post: May 18th, 2012, 08:05 PM
  2. need help with a function...
    By fallout87 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 2nd, 2011, 05:42 AM
  3. function timeDiff
    By bobby321 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 25th, 2011, 02:23 PM
  4. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM