Results 1 to 9 of 9
  1. #1
    Aleve is offline Member
    Join Date
    Dec 2007
    Location
    Orlando, Florida
    Posts
    4
    Rep Power
    0

    Default Objects and Classes

    I'm trying to learn Java as a side project by translating some of my C assignments into Java. After writing this last one (a faux casino), I realized that it would be much better suited to OO than procedural, but knowing next to nothing about OO, it didn't go so well.
    I think my problem is, how do I access objects from other classes after they have been created in Main?
    Here's my code, if it helps to see what I'm asking. I just started Java yesterday, and am new to this overall, so sorry if it is ugly.

    Java Code:
    import java.io.*;
    import java.util.Random;
    
    public class CasinoTest
    {
    	Player p = new Player();
    	Chips c = new Chips();
    	Games g = new Games();
    	Statistics s = new Statistics();
    
    	private static BufferedReader stdin = new BufferedReader (
    		new InputStreamReader ( System.in ) );
    		
    	public static void main ( String [] args ) throws IOException
    	{
    		byte opt = 0;		
    		
    		System.out.println( "Welcome to the Casino!" );
    		do{
    			
    			if( p.numchips == 0 && p.cash < 11)
    			{
    				break;
    			}
    			
    			System.out.println( "You currently have " + p.cash + " to spend" );
    			System.out.println( "What would you like to do?" );
    			System.out.println( "\n\t1. Buy Chips\n\t2. Sell Chips\n\t3. Status Report" );
    			System.out.println( "\t4. Play Craps\n\t5. Play Arup's Dice\n\t6. Cash in and Leave" );
    			
    			opt = Byte.parseByte( stdin.readLine() );
    			
    			switch( opt )
    			{
    				case 1:
    					c.Buy();
    					break;
    					
    				case 2:
    					c.Sell();
    					break;
    					
    				case 3:
    					s.Status();
    					break;
    					
    				case 4:
    					g.Craps();
    					break;
    					
    				case 5:
    					g.ArupsDice();
    					break;
    			}
    		}while ( opt != 6 );
    		
    		p.cash += p.numchips * 10;
    		p.numchips = 0;
    		
    		System.out.println( "Upon leaving, you have $" + p.cash );
    		
    		
    	}
    }
    
    class Player
    {
    	int cash = 1000, numchips = 0;
    }
    
    class Chips 
    {
    	private static BufferedReader stdin = new BufferedReader (
    		new InputStreamReader ( System.in ) );
    		
    	short count = 0;
    	int r1 = 0, r2 = 0;
    	String roll = null;
    	
    	Random generator = new Random( 8675309 );
    	
    	void Buy() throws IOException
    	{
    		System.out.println( "Chips cost $11.00 apiece. You have " + p.cash + " dollars." );
    		System.out.println( "How many would you like to purchase?" );
    		count = Short.parseShort( stdin.readLine() );
    		
    		if( p.cash >= count * 11 )
    		{
    			p.cash -= count * 11;
    			p.numchips += count;
    			return;
    		}
    		
    		if( p.cash <= count * 11 )
    		{
    			System.out.println( "I'm sorry, but you cannot afford that many." );
    			return;
    		}
    				
    		if( count == 0 )
    		{
    			System.out.println( "You have decided not to purchase any chips." );
    			return;			
    		}	
    	}
    	
    	void Sell() throws IOException
    	{
    		System.out.println( "Chips may be sold or $10.00 apiece. You have " + p.numchips + " chips." );
    		System.out.println( "How many would you like to sell?" );
    		count = Short.parseShort( stdin.readLine() );
    		
    		if( count == 0 )
    		{
    			System.out.println( "You have decided not to sell any chips." );
    			return;
    		}
    		
    		if( count <= numchips )
    		{
    			p.numchips -= count;
    			p.cash += count * 10;
    			return;
    		}
    		
    		if( count > numchips )
    		{
    			System.out.println( "I'm sorry, but you don't have that many." );
    			return;
    		}
    		return;
    	}
    	
    	byte Wager() throws IOException
    	{
    		System.out.println( "How many chips would you like to wager?" );
    		count = Short.parseShort( stdin.readLine() );
    		
    		if( count == 0 )
    		{
    			System.out.println( "Sorry, but you cannot play for free." );
    			return 0;
    		}
    		
    		if( count <= numchips )
    		{
    			return count;
    		}
    		
    		if( count > numchips )
    		{
    			System.out.println( "I'm sorry, but you don't have that many." );
    			return 0;
    		}
    		
    		return 0;
    	}
    	
    	byte Dice() throws IOException
    	{
    		r1 = generator.nextInt( 6 ) + 1;
    		System.out.println( "Press enter to roll." ); 
    		roll = stdin.readLine(); //Time between calls to random number generator
    		r2 = generator.nextInt( 6 ) + 1;
    		return ( byte ) ( r1 + r2 );
    	}
    
    }
    
    class Games
    {
    	private static BufferedReader stdin = new BufferedReader (
    		new InputStreamReader ( System.in ) );
    	
    	byte temp = 0, point = 0, wager = 0;
    	boolean wins = false;
    		
    	void Craps() throws IOException
    	{
    		wager = c.Wager();
    		
    		if( wager == 0 )
    		{
    			return;
    		}
    	
    		temp = c.Dice();
    		System.out.println( "You rolled " + temp );
    		
    		if( temp == 7 || temp == 11 )
    		{
    			System.out.println( "You win!" );
    			p.numchips += wager;
    			return;
    		}
    		
    		if( temp == 2 || temp == 3 || temp == 12 )
    		{
    			System.out.println( "I'm sorry, but you lost." );
    			p.numchips -= wager;
    			return;
    		}
    		
    		else
    		{
    			point = temp;
    			System.out.println( "Your point is " + point + ". Roll again.");
    			do{
    				temp = c.Dice();
    				System.out.println( "You rolled " + temp + "." );
    				
    				if( temp == point )
    				{
    					System.out.println( "You win!" );
    					p.numchips += wager;
    					return;
    				}
    				
    				if( temp == 7 )
    				{
    					System.out.println( "I'm sorry, but you lost." );
    					p.numchips -= wager;
    					return;
    				}
    				
    				else
    				{
    					System.out.println( "Please roll again." );
    				}
    			}while( temp != 7 && temp != point );
    		}
    		return;
    	}
    	
    	void ArupsDice() throws IOException
    	{
    		wager = c.Wager();
    		
    		if( wager == 0 )
    		{
    			return;
    		}
    				
    		temp = c.Dice();
    		System.out.println( "You rolled " + temp + "." );
    		
    		if( temp == 12 || temp == 11 )
    		{
    			System.out.println( "You win!" );
    			p.numchips += wager;
    			return;
    		}
    		
    		if( temp == 2 )
    		{
    			System.out.println( "I'm sorry, but you lost." );
    			p.numchips -= wager;
    			return;
    		}
    		
    		else
    		{
    			point = temp;
    			System.out.println( "Your point is " + point + ". Roll again." );
    			temp = c.Dice();
    			System.out.println( "You rolled " + temp + "." );
    			
    			if( temp > point )
    			{
    				System.out.println( "You win!" );
    				p.numchips += wager;
    				return;
    			}
    			
    			if( temp <= point )
    			{
    				System.out.println( "I'm sorry, but you lost." );
    				p.numchips -= wager;
    				return;
    			}
    			return;
    		}
    	}
    }
    
    class Statistics
    {
    	
    	void Status()
    	{
    		System.out.println( "Status Report:\n-----------------" );
    		System.out.println( "    Chips:  " + p.numchips );
    		System.out.println( "     Cash:  " + p.cash );
    		System.out.println( "-----------------" );
    		return;
    	} 
    }
    Thanks in advanced.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    import java.util.Random;
    
    public class CasinoTest
    {
    /*
    	Player p = new Player();
    	Chips c = new Chips();
    	Games g = new Games();
    	Statistics s = new Statistics();
    */
    	private static BufferedReader stdin = new BufferedReader (
    		new InputStreamReader ( System.in ) );
    		
    	public static void main ( String [] args ) throws IOException
    	{
    		// For minimal changes you can pass some references around.
    		Player p = new Player();
    		Chips c = new Chips(p);
    		Games g = new Games(p, c);
    		Statistics s = new Statistics(p);
    		byte opt = 0;		
    		
    		System.out.println( "Welcome to the Casino!" );
    		do{
    			
    			if( p.numchips == 0 && p.cash < 11)
    			{
    				break;
    			}
    			
    			System.out.println( "You currently have " + p.cash + " to spend" );
    			System.out.println( "What would you like to do?" );
    			System.out.println( "\n\t1. Buy Chips\n\t2. Sell Chips\n\t3. Status Report" );
    			System.out.println( "\t4. Play Craps\n\t5. Play Arup's Dice\n\t6. Cash in and Leave" );
    			
    			opt = Byte.parseByte( stdin.readLine() );
    			
    			switch( opt )
    			{
    				case 1:
    					c.Buy();
    					break;
    					
    				case 2:
    					c.Sell();
    					break;
    					
    				case 3:
    					s.Status();
    					break;
    					
    				case 4:
    					g.Craps();
    					break;
    					
    				case 5:
    					g.ArupsDice();
    					break;
    			}
    		}while ( opt != 6 );
    		
    		p.cash += p.numchips * 10;
    		p.numchips = 0;
    		
    		System.out.println( "Upon leaving, you have $" + p.cash );
    		
    		
    	}
    }
    
    class Player
    {
    	int cash = 1000, numchips = 0;
    }
    
    class Chips 
    {
    	Player p;
    
    	Chips(Player p)
    	{
    		this.p = p;
    	}
    	private static BufferedReader stdin = new BufferedReader (
    		new InputStreamReader ( System.in ) );
    		
    	short count = 0;
    	int r1 = 0, r2 = 0;
    	String roll = null;
    	
    	Random generator = new Random( 8675309 );
    	
    	void Buy() throws IOException
    	{
    		System.out.println( "Chips cost $11.00 apiece. You have " + p.cash + " dollars." );
    		System.out.println( "How many would you like to purchase?" );
    		count = Short.parseShort( stdin.readLine() );
    		
    		if( p.cash >= count * 11 )
    		{
    			p.cash -= count * 11;
    			p.numchips += count;
    			return;
    		}
    		
    		if( p.cash <= count * 11 )
    		{
    			System.out.println( "I'm sorry, but you cannot afford that many." );
    			return;
    		}
    				
    		if( count == 0 )
    		{
    			System.out.println( "You have decided not to purchase any chips." );
    			return;			
    		}	
    	}
    	
    	void Sell() throws IOException
    	{
    		System.out.println( "Chips may be sold or $10.00 apiece. You have " + p.numchips + " chips." );
    		System.out.println( "How many would you like to sell?" );
    		count = Short.parseShort( stdin.readLine() );
    		
    		if( count == 0 )
    		{
    			System.out.println( "You have decided not to sell any chips." );
    			return;
    		}
    		
    		if( count <= p.numchips )
    		{
    			p.numchips -= count;
    			p.cash += count * 10;
    			return;
    		}
    		
    		if( count > p.numchips )
    		{
    			System.out.println( "I'm sorry, but you don't have that many." );
    			return;
    		}
    		return;
    	}
    	
    	byte Wager() throws IOException
    	{
    		System.out.println( "How many chips would you like to wager?" );
    		count = Short.parseShort( stdin.readLine() );
    		
    		if( count == 0 )
    		{
    			System.out.println( "Sorry, but you cannot play for free." );
    			return 0;
    		}
    		
    		if( count <= p.numchips )
    		{
    			// You might consider increasing the precision of
    			// the return type (above byte) to avoid overflow.
    			return (byte)count;
    		}
    		
    		if( count > p.numchips )
    		{
    			System.out.println( "I'm sorry, but you don't have that many." );
    			return 0;
    		}
    		
    		return 0;
    	}
    	
    	byte Dice() throws IOException
    	{
    		r1 = generator.nextInt( 6 ) + 1;
    		System.out.println( "Press enter to roll." ); 
    		roll = stdin.readLine(); //Time between calls to random number generator
    		r2 = generator.nextInt( 6 ) + 1;
    		return ( byte ) ( r1 + r2 );
    	}
    
    }
    
    class Games
    {
    	Player p;
    	Chips c;
    
    	Games(Player p, Chips c)
    	{
    		this.p = p;
    		this.c = c;
    	}
    
    	private static BufferedReader stdin = new BufferedReader (
    		new InputStreamReader ( System.in ) );
    	
    	byte temp = 0, point = 0, wager = 0;
    	boolean wins = false;
    		
    	void Craps() throws IOException
    	{
    		wager = c.Wager();
    		
    		if( wager == 0 )
    		{
    			return;
    		}
    	
    		temp = c.Dice();
    		System.out.println( "You rolled " + temp );
    		
    		if( temp == 7 || temp == 11 )
    		{
    			System.out.println( "You win!" );
    			p.numchips += wager;
    			return;
    		}
    		
    		if( temp == 2 || temp == 3 || temp == 12 )
    		{
    			System.out.println( "I'm sorry, but you lost." );
    			p.numchips -= wager;
    			return;
    		}
    		
    		else
    		{
    			point = temp;
    			System.out.println( "Your point is " + point + ". Roll again.");
    			do{
    				temp = c.Dice();
    				System.out.println( "You rolled " + temp + "." );
    				
    				if( temp == point )
    				{
    					System.out.println( "You win!" );
    					p.numchips += wager;
    					return;
    				}
    				
    				if( temp == 7 )
    				{
    					System.out.println( "I'm sorry, but you lost." );
    					p.numchips -= wager;
    					return;
    				}
    				
    				else
    				{
    					System.out.println( "Please roll again." );
    				}
    			}while( temp != 7 && temp != point );
    		}
    		return;
    	}
    	
    	void ArupsDice() throws IOException
    	{
    		wager = c.Wager();
    		
    		if( wager == 0 )
    		{
    			return;
    		}
    				
    		temp = c.Dice();
    		System.out.println( "You rolled " + temp + "." );
    		
    		if( temp == 12 || temp == 11 )
    		{
    			System.out.println( "You win!" );
    			p.numchips += wager;
    			return;
    		}
    		
    		if( temp == 2 )
    		{
    			System.out.println( "I'm sorry, but you lost." );
    			p.numchips -= wager;
    			return;
    		}
    		
    		else
    		{
    			point = temp;
    			System.out.println( "Your point is " + point + ". Roll again." );
    			temp = c.Dice();
    			System.out.println( "You rolled " + temp + "." );
    			
    			if( temp > point )
    			{
    				System.out.println( "You win!" );
    				p.numchips += wager;
    				return;
    			}
    			
    			if( temp <= point )
    			{
    				System.out.println( "I'm sorry, but you lost." );
    				p.numchips -= wager;
    				return;
    			}
    			return;
    		}
    	}
    }
    
    class Statistics
    {
    	Player p;
    
    	Statistics(Player p)
    	{
    		this.p = p;
    	}
    	
    	void Status()
    	{
    		System.out.println( "Status Report:\n-----------------" );
    		System.out.println( "    Chips:  " + p.numchips );
    		System.out.println( "     Cash:  " + p.cash );
    		System.out.println( "-----------------" );
    		return;
    	} 
    }

  3. #3
    Aleve is offline Member
    Join Date
    Dec 2007
    Location
    Orlando, Florida
    Posts
    4
    Rep Power
    0

    Default

    If you don't mind me asking, how and why does this work?
    What exactly is being done? It looks to me like you're passing (by value) the object, but then again I have no idea what to look for.

    Thanks for the help.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    It looks to me like you're passing (by value) the object
    Yes. I did this so one class could use a reference to another class to
    access fields and call methods of the other class, egs,
    Java Code:
    // Create an instance of Player and save a reference to it in variable "p":
    Player p = new Player();
    // Pass "p" to new instance of Chips:
    Chips c = new Chips(p);
    // Now this instance of Chips can access the fields in the instance of 
    // Player for whom it received a reference.
    Games g = new Games(p, c);
    // This instance of Games now has access to both the Player and Chips 
    // instances, viz, it can access fields and call methods in these class instances.
    This is how one class can communicate with another class: give/pass
    it a reference. This is minimal functionality with your class design.
    To be more in the direction of OOP you can:
    1 back off and imagineer a little - a Player has some money/poker
    chips and can play games. So the player makes bets, participates in the
    games (throw dice, draw/deal cards, raise, fold, etc).
    2 the game or dealer or somebody oversees the game, keeps track
    of the player scores, who's dealing, whose turn it is, etc.
    These kind of thoughts help in designing classes and class
    structure/methods/fields to accommodate OOP. Imagination, play and
    experimenting lead to success.

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Aleve, just wanted to say that you can use the Sun tutorials as a great resource and that I find your goal of converting your C programs to Java is a great idea. I'm a bit surprised though you're going from C to Java right away without getting an OO study on the language you are presently used to. Going from C to C++, and then from C++ to Java, might be an easier transition... which is what I did a while back.

  6. #6
    Aleve is offline Member
    Join Date
    Dec 2007
    Location
    Orlando, Florida
    Posts
    4
    Rep Power
    0

    Default

    Actually, I'm not going from C to C++ to Java, it's more from Perl to C to Java. I have yet to touch C++.

  7. #7
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by Aleve View Post
    Actually, I'm not going from C to C++ to Java, it's more from Perl to C to Java. I have yet to touch C++.
    Aleve, possibly you misread my post? I understand you're not coming from C++, I only pointed out that I found it odd that you're not coming from C++. Which in my experience, would be an easier transition. That's all.

    Quote Originally Posted by Aleve View Post
    I'm trying to learn Java as a side project by translating some of my C assignments into Java....

  8. #8
    Aleve is offline Member
    Join Date
    Dec 2007
    Location
    Orlando, Florida
    Posts
    4
    Rep Power
    0

    Default

    Oh wow, I guess I can't trust the text preview the email gave me. I assumed that the ellipse was your own, and not a shortening of the text.
    Sorry about the misunderstanding, haha.

  9. #9
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by Aleve View Post
    Oh wow, I guess I can't trust the text preview the email gave me. I assumed that the ellipse was your own, and not a shortening of the text.
    Sorry about the misunderstanding, haha.
    I know what you mean.. I always select the link to go back to the thread and review the post, just in case. :) Sometimes Thunderbird leaves things out from me.

    No problem, heh.

Similar Threads

  1. Getting objects from a list
    By markyoung1984 in forum New To Java
    Replies: 4
    Last Post: 03-13-2008, 10:45 PM
  2. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 07:08 PM
  3. Importing classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-06-2007, 02:27 PM
  4. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 PM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •