Results 1 to 9 of 9
Thread: Objects and Classes
- 12-18-2007, 01:02 AM #1
Member
- Join Date
- Dec 2007
- Location
- Orlando, Florida
- Posts
- 4
- Rep Power
- 0
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; } }
- 12-18-2007, 01:57 AM #2Java 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; } }
- 12-18-2007, 08:26 AM #3
Member
- Join Date
- Dec 2007
- Location
- Orlando, Florida
- Posts
- 4
- Rep Power
- 0
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.
- 12-19-2007, 05:21 AM #4
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.
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.
- 12-31-2007, 04:36 AM #5
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.
- 12-31-2007, 08:19 AM #6
Member
- Join Date
- Dec 2007
- Location
- Orlando, Florida
- Posts
- 4
- Rep Power
- 0
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++.
- 12-31-2007, 08:29 AM #7
- 12-31-2007, 08:35 AM #8
Member
- Join Date
- Dec 2007
- Location
- Orlando, Florida
- Posts
- 4
- Rep Power
- 0
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.
- 12-31-2007, 09:05 AM #9
Similar Threads
-
Getting objects from a list
By markyoung1984 in forum New To JavaReplies: 4Last Post: 03-13-2008, 11:45 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 08:08 PM -
Importing classes
By Java Tip in forum Java TipReplies: 0Last Post: 11-06-2007, 03:27 PM -
When do we use inner classes?
By cruxblack in forum New To JavaReplies: 5Last Post: 08-10-2007, 06:00 PM -
Help with Objects!
By Shorinhio in forum New To JavaReplies: 1Last Post: 07-10-2007, 10:32 PM
Bookmarks