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.

Page 2 of 2 FirstFirst 12
Results 26 to 38 of 38

Thread: Sending two strings into a constructor:

  1. #26
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Sending two strings into a constructor:

    This should assign variables to my private variables.

    public class WinningHand extends PokerCalculator
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];

    WinningHand(ArrayList<String> p1, ArrayList<String> p2)
    	{
    		String[] player1 = new String[p1.size()]; 	
    		String[] player2 = new String[p2.size()];
     
    		player1 = p1.toArray(player1);
    		player2 = p2.toArray(player2);
     
    		this.p1Size= p1.size();
    		this.p2Size= p2.size();
     
    		this.p1Hand = player1;
    		this.p2Hand = player2;
    	}

    However it is not, why?

    If I do this:

    public static void main(String[] args)
    	{
    		ArrayList<String> p1 = new ArrayList<String>();
    		ArrayList<String> p2 = new ArrayList<String>();
     
    		p1.add("Mike");
    		p1.add("Drew");
     
    		PokerCalculator poker = new PokerCalculator();
    		WinningHand hand = new WinningHand(p1,p2);
     
    		poker.readFile();
    		hand.print();
     
    	}

    It works and I get string values for player one.

    So how can I get the String array from this method:
    void separateHands(String cards)
    	{
    		ArrayList<String>playerOne = new ArrayList<String>();
    		ArrayList<String>playerTwo = new ArrayList<String>();
     
    		String[] parts = cards.split(" ");
     
    			for(int i=0;i<5;i++)
    			{	
    				playerOne.add(parts[i]);
    			}
     
    			for(int j=5;j<10;j++)
    			{
    				playerTwo.add(parts[j]);
    			}
     
    			new WinningHand(playerOne,playerTwo);
    	  }

    To assign these private variables:
    private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
     
    	}
     
    	WinningHand(ArrayList<String> p1, ArrayList<String> p2)
    	{
    		String[] player1 = new String[p1.size()]; 	
    		String[] player2 = new String[p2.size()];
     
    		player1 = p1.toArray(player1);
    		player2 = p2.toArray(player2);
     
    		this.p1Size= p1.size();
    		this.p2Size= p2.size();
     
    		this.p1Hand = player1;
    		this.p2Hand = player2;
    	}

    I even tried a set method but that didn't work either.

    WinningHand(ArrayList<String> p1, ArrayList<String> p2)
    	{
    		String[] player1 = new String[p1.size()]; 	
    		String[] player2 = new String[p2.size()];
     
    		player1 = p1.toArray(player1);
    		player2 = p2.toArray(player2);
     
    		this.p1Size= p1.size();
    		this.p2Size= p2.size();
     
    		this.p1Hand = player1;
    		this.p2Hand = player2;
     
    		setHands(p1Hand,p2Hand);
    	}
    	void setHands(String[] p1Hand,String[] p2Hand)
    	{
    		this.p1Hand = p1Hand;
    		this.p2Hand = p2Hand;
    	}

    I put a loop inside the setHand method that I called in the constructor and I got the desired output. But I want to assign those variables to the private variable so I can use those variable in the rest of the methods.
    void setHands(String[] p1Hand,String[] p2Hand)
    	{
    		this.p1Hand = p1Hand;
    		this.p2Hand = p2Hand;
     
    		System.out.println("Printing Hand setHands");
    		for(String s :this.p1Hand)
    		{
    			System.out.println(s);
    		}
    	}

    I even used a getter method and it still didn't work when I called it in main:
    String[] getHand1()
    	{
    		return this.p1Hand;
    	}
     
    	String[] getHand2()
    	{
    		return this.p2Hand;
    	}
     
    //output looks like this when I call it:
    //  [Ljava.lang.String;@7ecec0c5

    I even thought what if I set the values in the default constructor like this:
    WinningHand()
    	{
    		p1Hand=getHand1();
    		p2Hand=getHand2();
    	}
    NOPE DIDN"T WORK EITHER!!!!!!

    So in closing how can I assign private variables in the WinningHand class by calling WinningHand's constructor in a Method from a different class. Like the above examples try to do without having to place variables into the constructor in main?
    Last edited by jocdrew21; June 14th, 2014 at 02:48 AM.

  2. #27
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sending two strings into a constructor:

    Nice job of laying out and explaining your confusion. I'll do my best to answer without fouling Norm's plan.

    However it is not, why?
    The part that's missing in this question is the contents of p1 and p2 being sent to the WinningHand() constructor, and you haven't added the test prints that Norm has suggested in order to see what might be going wrong. Try my version of the constructor below to understand the usefulness of some test prints to reveal errors. The modified constructor combined with the main() method that follows provides the results I believe you've been looking for:

    The constructor:
        // constructor
        public WinningHand(ArrayList<String> p1, ArrayList<String> p2)
        {
    	    // test messages to confirm the state at the beginning
    	    System.out.println( "In the constructor . . . " );
            System.out.println( "p1 = " + p1 );
            System.out.println( "p2 = " + p2 );
     
    	    // declare local variables player1 and player2 to contain
    	    // as many values as the parameters p1 and p2, respectively
            String[] player1 = new String[p1.size()];   
            String[] player2 = new String[p2.size()];
     
            // convert the ArrayLists to arrays of the same type
            player1 = p1.toArray(player1);
            player2 = p2.toArray(player2);
     
            // these variables are unnecessary, because they can always
            // be derived from the lengths of the arrays
            // ('this.' is unnecessary - try it without)
            this.p1Size= p1.size();
            this.p2Size= p2.size();
     
            // assign the local arrays to the instance variables
            // ('this.' is unnecessary - try it without)
            this.p1Hand = player1;
            this.p2Hand = player2;
     
            // add test statements to verify the results
            System.out.println( "Leaving the constructor . . . " );
            System.out.println( "p1Hand = " + Arrays.toString( p1Hand ) );
            System.out.println( "p2Hand = " + Arrays.toString( p2Hand ) );
        }
    The main() method to test:
            // a main() method to test the WinningHand() constructor
    	public static void main( String[] args )
    	{
    	    // declare the list variables
    	    ArrayList<String> p1 = new ArrayList<String>();
    	    ArrayList<String> p2 = new ArrayList<String>();
     
    	    // create string arrays of two hands using split()
    	    String[] handOne =  ( "AB BC CD DE EF" ).split( " " );
    	    String[] handTwo =  ( "FG GH HI IJ JK" ).split( " " );
     
    	    // add the contents of the arrays to the ArrayLists
    	    for ( String handOneItem : handOne )
    	    {
    	        p1.add( handOneItem );
    	    }
     
            for ( String handTwoItem : handTwo )
            {
                p2.add( handTwoItem );
            }
     
    	    // send the ArrayLists to the constructor
    	    new WinningHand( p1, p2 );
    	}

    So how can I get the String array from this method:
    I tried to answer this, or at least show an approach that answers the question, in the main() method above. If that approach is still not giving you the shove you need, please focus on this part further.

  3. #28
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Sending two strings into a constructor:

    I have used your first approach before to confirm I was getting values.

    In your second example I thought I was doing that in the other method:

    void separateHands(String cards)
    	{
    		ArrayList<String>playerOne = new ArrayList<String>();
    		ArrayList<String>playerTwo = new ArrayList<String>();
     
    		String[] parts = cards.split(" ");
     
    			for(int i=0;i<5;i++)
    			{	
    				playerOne.add(parts[i]);
    			}
     
    			for(int j=5;j<10;j++)
    			{
    				playerTwo.add(parts[j]);
    			}
    			new WinningHand(playerOne,playerTwo);
     
    	  }

    I do not want to send the constructor anything in main. I want everything to be handled in the program. Which is why I need to initialize the private variables within the constructor. I have always initialize private variables within the constructor, but now it is not working. I have a feeling one of those "I am a idiot" moments are heading my way.

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

    Default Re: Sending two strings into a constructor:

    I need to initialize the private variables within the constructor
    Don't you need some data to put in the arraylists? The program needs to get all the data, build the array lists and then use the constructor to save the array lists in the class's private variables.

    Try passing the two player array lists to the separateHands() method. See post#5
    It will fill them with data that the caller of the separateHands() method can then use as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sending two strings into a constructor:

    I do not want to send the constructor anything in main.
    It can be done from any method of your choice. I used main() for convenience and brevity.

    Verify that playerOne and playerTwo contain values when leaving separateHands(), before the WinningHand() object is created.

    BTW - Since you're not assigning the new WinningHand() result to a variable, what is the WinningHand object used for? Or is the WinningHand() constructor really being used as a method to move the ArrayList elements into String[] arrays? I haven't read all of the code for a while, so looking at just this small piece I'm wondering if the overall design is a bit more complex than it needs to be.

  6. #31
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Sending two strings into a constructor:

    I took out the constructor with two parameters. I moved everything to the default constructor and it has a lot going on inside of it.

    public class WinningHand extends PokerFile
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
    		System.out.println("Entering the constructor");
    		ArrayList<String> p1 = new ArrayList<String>();
    		ArrayList<String> p2 = new ArrayList<String>();
     
    		//these getter methods return a ArrayList<String> from the
    		//super class PokerFile each containing a hand of five cards
    		p1 = getPlayerOne();
    		p2 = getPlayerTwo();
     
    		System.out.println("In the constructor "+getPlayerOne());
     
    		// declare local variables player1 and player2 to contain
    	    // as many values as the parameters p1 and p2, respectively
    		String[] player1 = new String[p1.size()]; 	
    		String[] player2 = new String[p2.size()];
     
    		//convert the ArrayLists to arrays of the same type
    		player1 = p1.toArray(player1);
    		player2 = p2.toArray(player2);
     
    		p1Size= p1.size();
    		p2Size= p2.size();
     
    		p1Hand = player1;
    		p2Hand = player2;
     
    		System.out.println("Leaving the constructor");
    	}

    The getter methods are working in the super class and I confirms by a printing them out inside that class from a different method. HOWEVER they're not sending anything to my constructor. Because this is my output:

    Entering the constructor
    In the constructor []
    Leaving the constructor

    How does that work. I extend the class, call a method from that class and get nothing. However I call that same method in its own class and it works great.

    void separateHands(String cards)
    	{
    		String[] parts = cards.split(" ");
     
    			for(int i=0;i<5;i++)
    			{	
    				playerOne.add(parts[i]);
    			}
     
    			for(int j=5;j<10;j++)
    			{
    				playerTwo.add(parts[j]);
    			}
     
    			setPlayerOne(playerOne);
    			setPlayerTwo(playerTwo);	
    	  }
     
    	private void setPlayerOne(ArrayList<String> playerOne){
    		this.playerOne = playerOne;
    	}
     
    	public ArrayList<String> getPlayerTwo() {
    		return playerTwo;
    	}
     
    	private void setPlayerTwo(ArrayList<String> playerTwo) {
    		this.playerTwo = playerTwo;
    	}
     
    	public ArrayList<String> getPlayerOne() {
     
    		return playerOne;
    	}

    I decided to use this constructor because when I did this:
     
    	WinningHand()
    	{
                 p1.Size = 400;
     
             }

    Then printed it out in a different method it worked like I thought it should.

  7. #32
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sending two strings into a constructor:

    I extend the class, call a method from that class and get nothing. However I call that same method in its own class and it works great.
    Can you post a short, runnable example that displays this behavior? What you've posted does not provide enough info to understand the questions you're asking.

    It appears that in your default WinningHand() constructor p1Size and p2Size will be zero, which will cause problems with the creation of your String[] arrays, but I'm not sure that's the cause of the behavior you describe.

  8. #33
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Sending two strings into a constructor:

     
    void printGetter() // called in the super class
    	{
    		System.out.println(getPlayerOne());
    	}
     
     
    public static void main(String[] args)
    	{	
    		PokerFile poker = new PokerFile();
    		poker.readFile();
    		poker.printGetter();
     
    		WinningHand hand = new WinningHand();
    		hand.print();
    	}
     
    [8C, TS, KC, 9H, 4S, 5C, AD, 5D, AC, 9C, 3H, 7H, 6S, KC, JS, TH, 8H, 5C, QS, TC, 7C, 5H, KC, QH, JD, 5H, KS, 9C, 7D, 9H, 6H, 4H
    Entering the constructor
    In the constructor []
    Leaving the constructor
    Printing Hand 1
    Printing Hand 2
    0

  9. #34
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sending two strings into a constructor:

    Please describe how I can compile and run what you've posted to get the results you've shown.

  10. #35
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Sending two strings into a constructor:

    package pokerHandCalculator;
     
    import java.io.*;
    import java.util.ArrayList;
     
    public class PokerFile 
    {
    	protected ArrayList<String>playerOne = new ArrayList<String>();
    	protected ArrayList<String>playerTwo = new ArrayList<String>();
     
    	void readFile()
    	{
    		String line;
    		try
    		{
    		FileReader file = new FileReader("poker.txt");
    		BufferedReader buffer = new BufferedReader(file);
     
    		while((line=buffer.readLine()) != null)
    		{
    			separateHands(line);
    		}
     
    		buffer.close();
    		}
    		catch(Exception ex)
    		{
    			System.out.println("The file was not read or not found "+ ex);
    		}	
    	}
     
    	void separateHands(String cards)
    	{
    		String[] parts = cards.split(" ");
     
    			for(int i=0;i<5;i++)
    			{	
    				playerOne.add(parts[i]);
    			}
     
    			for(int j=5;j<10;j++)
    			{
    				playerTwo.add(parts[j]);
    			}
     
    			setPlayerOne(playerOne);
    			setPlayerTwo(playerTwo);	
    	  }
     
    	private void setPlayerOne(ArrayList<String> playerOne){
    		this.playerOne = playerOne;
    	}
     
    	public ArrayList<String> getPlayerTwo() {
    		return playerTwo;
    	}
     
    	private void setPlayerTwo(ArrayList<String> playerTwo) {
    		this.playerTwo = playerTwo;
    	}
     
    	public ArrayList<String> getPlayerOne() {
     
    		return playerOne;
    	}
     
    	void printGetter()
    	{
    		System.out.println(getPlayerOne());
    	}
     
    }

    package pokerHandCalculator;
     
    import java.util.ArrayList;
     
    public class WinningHand extends PokerFile
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
     
    	WinningHand()
    	{
    		System.out.println("Entering the constructor");
    		ArrayList<String> p1 = new ArrayList<String>();
    		ArrayList<String> p2 = new ArrayList<String>();
     
    		//these getter methods return a ArrayList<String> from the
    		//super class PokerFile each containing a hand of five cards
    		p1 = getPlayerOne();
    		p2 = getPlayerTwo();
     
    		System.out.println("In the constructor "+getPlayerOne());
     
    		// declare local variables player1 and player2 to contain
    	    // as many values as the parameters p1 and p2, respectively
    		String[] player1 = new String[p1.size()]; 	
    		String[] player2 = new String[p2.size()];
     
    		//convert the ArrayLists to arrays of the same type
    		player1 = p1.toArray(player1);
    		player2 = p2.toArray(player2);
     
    		p1Size= p1.size();
    		p2Size= p2.size();
     
    		p1Hand = player1;
    		p2Hand = player2;
     
    		System.out.println("Leaving the constructor");
    	}
     
    	boolean pair(String[] hand)
    	{
    		return true;
    	}
     
    	boolean threeOfaKind(String[] hand)
    	{
    		return true;
    	}
     
    	boolean twoPair(String[] hand)
    	{
    		return true;
    	}
     
    	boolean flush(String[] hand)
    	{
    		return true;
    	}
     
    	boolean fullHouse(String[] hand)
    	{
    		return true;
    	}
     
    	boolean fourOfaKind(String[] hand)
    	{
    		return true;
    	}
     
    	boolean straightFlush(String[] hand)
    	{
    		return true;
    	}
     
    	boolean royalFlush(String[] hand)
    	{
     
    		return true;
    	}
     
    	void print()
    	{
    		System.out.println("Printing Hand 1");
    		for(String s :p1Hand)
    		{
    			System.out.println(s);
     
    		}
     
    		System.out.println("Printing Hand 2");
    		for(String s :p2Hand)
    		{
    			System.out.println(s);
    		}
     
    		System.out.println(p1Size);
    	}
     
    	public static void main(String[] args)
    	{	
    		PokerFile poker = new PokerFile();
    		poker.readFile();
    		poker.printGetter();
     
    		WinningHand hand = new WinningHand();
    		hand.print();
    	}
     
    }

    8C TS KC 9H 4S 7D 2S 5D 3S AC
    5C AD 5D AC 9C 7C 5H 8D TD KS
    3H 7H 6S KC JS QH TD JC 2D 8S
    TH 8H 5C QS TC 9H 4D JC KS JS
    7C 5H KC QH JD AS KH 4C AD 4S
    5H KS 9C 7D 9H 8D 3S 5D 5C AH
    6H 4H 5C 3H 2H 3S QH 5S 6S AS
    TD 8C 4H 7C TC KC 4C 3H 7S KS
    7C 9C 6D KD 3H 4C QS QC AC KH
    JC 6S 5H 2H 2D KD 9D 7C AS JS
    AD QH TH 9D 8H TS 6D 3S AS AC
    2H 4S 5C 5S TC KC JD 6C TS 3C
    QD AS 6H JS 2C 3D 9H KC 4H 8S
    KD 8S 9S 7C 2S 3S 6D 6S 4H KC
    3C 8C 2D 7D 4D 9S 4S QH 4H JD
    8C KC 7S TC 2D TS 8H QD AC 5C
    3D KH QD 6C 6S AD AS 8H 2H QS
    6S 8D 4C 8S 6C QH TC 6D 7D 9D
    2S 8D 8C 4C TS 9S 9D 9C AC 3D
    3C QS 2S 4H JH 3D 2D TD 8S 9H
    5H QS 8S 6D 3C 8C JD AS 7H 7D
    6H TD 9D AS JH 6C QC 9S KD JC
    AH 8S QS 4D TH AC TS 3C 3D 5C

  11. #36
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sending two strings into a constructor:

    Ahh, this illustrates an interesting lesson in OOP. I hope that I can explain it well.

    The main() method in WinningHand creates an instance of PokerFile called 'poker'. The instance variables in 'poker' are then filled in and displayed by calling poker.readFile() and poker.printGetter(). Those instance variables, playerOne and playerTwo BELONG to the instance poker and no other instance of PokerFile.

    The main() method then creates an instance of WnningHand called 'hand'. hand is an extension of the class PokerFile, but it does not inherit the instance variables already belonging to OTHER instances of PokerFile, like 'poker'. The reason you see empty outputs when called from the WinningHand constructor is because the instance variables for PokerFile IN WinningHand have not been defined. The instance poker is unknown inside WinningHand().

    There are a couple way to solve this, 1) define and populate an instance of PokerFile INSIDE WinningHand, 2) pass the existing instance 'poker' to the WinningHand constructor, or 3) use a WinningHand setter to pass an instance of PokerFile. Here's a demonstration of the second approach (probably because it's the easiest to do in a single file of your code). You should be able to replace your WinningHand with this one and then continue on. Let me know if you have any questions, especially if you'd like to use method 1 or 3 outlined above.
    public class WinningHand extends PokerFile
    {
        // a private reference to an instance of PokerFile
        private PokerFile pokerHand;
        private int p1Size;
        private int p2Size;
        private String[] p1Hand = new String[p1Size];
        private String[] p2Hand = new String[p2Size];
     
        // a constructor that accepts an instance of PokerHand
        WinningHand( PokerFile pokerHand )
        {
            this.pokerHand = pokerHand;
            System.out.println("Entering the constructor");
            ArrayList<String> p1 = new ArrayList<String>();
            ArrayList<String> p2 = new ArrayList<String>();
     
            //these getter methods return a ArrayList<String> from the
            //super class PokerFile each containing a hand of five cards
            // GB: either this.pokerHand or pokerHand can be used here.
            // GB: i chose this. to illustrate use of instance variable
            p1 = this.pokerHand.getPlayerOne();
            p2 = this.pokerHand.getPlayerTwo();
     
            // GB: either this.pokerHand or pokerHand can be used here.
            // GB: i chose this. to illustrate use of instance variable
            System.out.println("In the constructor "+ 
                    this.pokerHand.getPlayerOne());
     
            // GB: any changes beyond this point are up to you
     
            // declare local variables player1 and player2 to contain
            // as many values as the parameters p1 and p2, respectively
            String[] player1 = new String[p1.size()];   
            String[] player2 = new String[p2.size()];
     
            //convert the ArrayLists to arrays of the same type
            player1 = p1.toArray(player1);
            player2 = p2.toArray(player2);
     
            p1Size= p1.size();
            p2Size= p2.size();
     
            p1Hand = player1;
            p2Hand = player2;
     
            System.out.println("Leaving the constructor");
        }
     
        boolean pair(String[] hand)
        {
            return true;
        }
     
        boolean threeOfaKind(String[] hand)
        {
            return true;
        }
     
        boolean twoPair(String[] hand)
        {
            return true;
        }
     
        boolean flush(String[] hand)
        {
            return true;
        }
     
        boolean fullHouse(String[] hand)
        {
            return true;
        }
     
        boolean fourOfaKind(String[] hand)
        {
            return true;
        }
     
        boolean straightFlush(String[] hand)
        {
            return true;
        }
     
        boolean royalFlush(String[] hand)
        {
     
            return true;
        }
     
        void print()
        {
            System.out.println("Printing Hand 1");
            for(String s :p1Hand)
            {
                System.out.println(s);
     
            }
     
            System.out.println("Printing Hand 2");
            for(String s :p2Hand)
            {
                System.out.println(s);
            }
     
            System.out.println(p1Size);
        }
     
        public static void main(String[] args)
        {   
            PokerFile poker = new PokerFile();
            poker.readFile();
            poker.printGetter();
     
            WinningHand hand = new WinningHand( poker );
            hand.print();
        }
     
    }

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    jocdrew21 (June 15th, 2014)

  13. #37
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Sending two strings into a constructor:

    WOW..... Thank you so much...

    I didn't even know I could pass a class in like that. I also thought if a class inherited the other you have access to all of its methods. You just helped me in many ways!!!!!! Several light bulbs just went off.

  14. #38
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sending two strings into a constructor:

    You're welcome. I know that feeling.
    I didn't even know I could pass a class in like that.
    Just to be clear, you're passing an object, specifically a reference to an object, not the object itself.
    I also thought if a class inherited the other you have access to all of its methods.
    This thinking is correct. Access to the parent class' methods and access to other instances of the parent class are completely different. I do think you were confusing inheritance with something else that I don't have a name for. I hope you understand the difference better now.

    Don't be surprised if it there a few more misunderstandings to clear up before you really get it.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. sending email
    By pradipthite57 in forum Java Networking
    Replies: 3
    Last Post: April 16th, 2014, 02:53 AM
  2. Sorting lowercase strings before uppercase strings
    By keepStriving in forum Java Theory & Questions
    Replies: 4
    Last Post: March 26th, 2014, 03:33 PM
  3. Sending and Receiving File
    By beer-in-box in forum Java Networking
    Replies: 8
    Last Post: March 31st, 2013, 07:57 PM
  4. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  5. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM