Dice Betting Game Working With Different Classes Questions

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 7347 Views - Last Post: 18 September 2011 - 01:40 PM Rate Topic: -----

#1 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 11:19 AM

Alright so new weekend new assignment, Here I have to create a dice game using different classes. I have to have classes for the players known as bank accounts, class for the die(given to us by professor), and then a driver programing calling on those. Now I am new to multiple classes so even the littlest details are important I feel like we will be using it more so I want a good grasp. Now I think that I have set up my bank account/player class right so that it will allow the user to input a name and be able to handle withdrawals and deposits from the wagering.
import java.util.Scanner;


public class Bankaccount {
	
	private String name;	
	
	private int balance;
		
	public Bankaccount(){
		balance = 1000;
		Scanner keyboard = new Scanner(System.in);
		String name = keyboard.nextLine();
	}
	
	public String showName(){
		return name ;
		
				
	}
	
	

		public void deposit(int wager){
			balance = balance + wager;
		}
			
		public void withdrawl(int wager){
			balance = balance - wager;
		}	
	
}


Now my question here is how would I create and int value for 1 of the bank account/players and then compare it to the other player?
Driver code
public class Playingdice {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("Playing A Game Of Dice");
		
		Bankaccount b1 = new Bankaccount();
		Bankaccount b2 = new Bankaccount();
		Die d1 = new Die();
		Die d2 = new Die();
		int r1 = d1.roll();
		int r2 = d2.roll();
		System.out.println("You rolled a " + r1 + " and a " + r2);
		int total = r1 + r2;
		
			
		
		if (total == 7) {
			System.out.println("You win!");
			
		}
		else if (total == 2) System.out.println("Snake eyes");
		else System.out.println("Try again");
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Dice Betting Game Working With Different Classes Questions

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 11:35 AM

What's the Scanner doing in your BankAccount class? What are you trying to do there? If you need to pass a user input to your class, I suggest you do it through the class' constructor, but it's not clear what you're trying to do there.

Can you explain this? Again, what are you trying to do?

Quote

Now my question here is how would I create and int value for 1 of the bank account/players and then compare it to the other player?

Was This Post Helpful? 0
  • +
  • -

#3 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 11:41 AM

I would like to focus on getting it to assign a name to a bank account with that scanner. so i need it to be able to ask the user to input a name then have that name outputted to the driver program
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 01:09 PM

When you create an instance of a class, the code that runs during class creation is the creator. That's the method with the same name as the class, and there can be more than one with different signatures. So for your Bankaccount class, you have a constructor set up to collect the String name every time an instance of the class is created. You should add a prompt for the input so that the user knows what's going on. As you have it now, the program will pause when an instance of Bankaccount is created, and it won't be clear why.

Even better (per my interpretation of Java convention), define the String name before creating an instance of the class and pass the name to the instance through the constructor:

String name = "BankAccount1Name";
Bankaccount bankaccount1 = new Bankaccount( name );

Then you'll need a Bankaccount() constructor that accepts a String argument:
public Bankaccount( String name );
{
    this.name = name;

    //  etc. . . .
}


I still don't understand your final question about assigning an int value to each (?) bankaccount instance so that you can compare it to others. I suggest you could create an Bankaccount instance field, int accountNumber; that you could use to easily identify each instance, but I'm not sure what you're asking.

Come back as you need more help.

This post has been edited by GregBrannon: 16 September 2011 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7008
  • View blog
  • Posts: 14,661
  • Joined: 16-October 07

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 01:16 PM

Make only one Scanner instance. Give the name to the BankAcount:
public class Bankaccount {
	private String name;	
	private int balance;
		
	public Bankaccount(String name){
		balance = 1000;
		// this is wrong in any case,
		// you've created a local variable with the name name
		//String name = keyboard.nextLine();
		this.name = name;
	}
	
	public String getName(){ return name ; }
	public int getBalanace() { return balance; }

	public void deposit(int wager){ balance += wager; }
	public void withdrawl(int wager){ balance -= wager; }
	
}

Scanner keyboard = new Scanner(System.in);

Bankaccount b1 = new Bankaccount(keyboard.nextLine());
Bankaccount b2 = new Bankaccount("Computer");
Die die = new Die();
// Die d2 = new Die(); why more than once instance?
int r1 = die.roll();
int r2 = die.roll();
System.out.println(b1.getName() + " rolled a " + r1 + " and a " + r2);
int total = r1 + r2;


Was This Post Helpful? 0
  • +
  • -

#6 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 03:19 PM

Alright so lots of changes now made some headway. I am having trouble having the different players wagers subtracted from there own bank accounts.
Bank Account Class
import java.util.Scanner;


public class Bankaccount {

	private String name;
	private int balance;

	public Bankaccount()
	{
		balance = 1000;
	
	}
	public int balance(){
		return balance;
	}
	public void showBalance(String balance){
	}
	public void showName(String name){
	}
	public int withdraw(int wager1, int wager2) {
		return balance - wager1;	
	}

	public int deposit(int win1, int win2){
		return balance =  balance += deposit(win1,win2);
	}


}


Driver Program
import java.util.Scanner;


public class Playingdice {

	/**
	 * 
	 */
	public static void main(String[] args) {
		
		System.out.println("Playing A Game Of Dice");
		
		Scanner keyb = new Scanner(System.in);

		
		//Players
		Bankaccount b1 = new Bankaccount();
		Bankaccount b2 = new Bankaccount();

			//Name
			System.out.println("Please enter name: ");
				String name1 = keyb.nextLine();
				b1.showName(name1);
			//Balance
				b1.balance();
				int balance1 = b1.balance();
				System.out.println("Your Balance is: " + balance1);
			//Wager
				System.out.println("Please enter your wager: ");

				int wager1 = Integer.parseInt(keyb.next());
				b1.withdraw(wager1,wager1);
				
				System.out.println("Your remaining balance is: " b1.withdraw(wager1,wager1)); 

			//Name
				System.out.println("Please enter name: ");
				String name2 = keyb.nextLine();
				b2.showName(name2);
			//Balance
				b2.balance();
				int balance2 = b2.balance();
				System.out.println("Your Balance is: " + balance2);
			//Wager
				int wager2 = Integer.parseInt(keyb.next());
				b2.withdraw(wager2, wager2);

			
			
			
		
		
			
	
			

			
			//Balance
				//int balance2 = b2.balance();
			
			//Deposit
				//int win2 = b2.deposit();



		//int wager1 = Integer.parseInt(keyboard.nextLine());

		
		
		Die d1 = new Die();
		Die d2 = new Die();
		int r1 = d1.roll();
		int r2 = d2.roll();
		int total = r1 + r2;
		
		
		
	

		
		
      
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 03:36 PM

This code,
//Name
System.out.println("Please enter name: ");
String name1 = keyb.nextLine();
b1.showName(name1);


Should be more like:

//Name
System.out.println("Please enter name of first player: ");
String name = keyb.nextLine();
Bankaccount b1 = new Bankaccount( name );
System.out.println("Please enter name of second player: ");
String name = keyb.nextLine();
Bankaccount b2 = new Bankaccount( name );


Other comments:
Why does your withdraw() method have 2 arguments? Wouldn't one do?

Now you have a class with methods that could provide your driver program with the ability to modify and retrieve class instance variables. For example, if you want to reduce the player's account by the amount of his/her wager (after they'd lost the bet), you would call a method,

b1.reduceBalance( wager );

The reduceBalance() method would adjust the user's balance by the amount of the wager. Then, if the driver program wants to know the user's balance, you'd call
b1.getBalance();

And on for other instance data that needed to be changed or retrieved.
Was This Post Helpful? 0
  • +
  • -

#8 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 05:15 PM

Alright so now I am attempting to reconfigure my code so that i have one int of wager, but it has to have 2 different values for the different players. How would I accomplish this? This is what I have right now
System.out.println("Please enter your wager " +name1+ ": ");
				int wager = Integer.parseInt(keyb.next());
				b1.withdraw(wager);
				System.out.println("Please enter your wager "+name2+": ");
				b2.withdraw(wager);


This is my method class for withdraw
public int withdraw(int wager) {
		return balance=balance - (wager);	
	}


This post has been edited by laxman2809: 16 September 2011 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 05:21 PM

The code you've shown can do what you describe with a small change:

System.out.println("Please enter your wager " +name1+ ": ");
int wager = Integer.parseInt(keyb.next());
b1.withdraw(wager);
System.out.println("Please enter your wager "+name2+": ");
wager = Integer.parseInt(keyb.next());
b2.withdraw(wager);


Edit: Responding to your edit: The code above doesn't use the value returned by the withdraw() method. Unless you use it elsewhere, returning a value serves no purpose.

This post has been edited by GregBrannon: 16 September 2011 - 05:26 PM

Was This Post Helpful? 1
  • +
  • -

#10 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 05:28 PM

Ok weird but now it seems to me like my wager is being multiplied by 2

This post has been edited by laxman2809: 16 September 2011 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 05:31 PM

You're seeing OOP in action. You have 2 objects, b1 and b2. b1.withdraw( wager ) reduces b1's balance by amount wager, b2.withdraw( wager ) reduces b2's balance by amount wager. Keep thinking it through, the light bulb is about to come on, and you'll begin to see the beauty of OOP.
Was This Post Helpful? 1
  • +
  • -

#12 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 05:45 PM

Alright so now I have it like this and it seems to be working, but my gut is telling me that this is going to cause me problems in the future when I create the loops for the game and the balance needs to change with it
Driver
System.out.println("Please enter your wager " +name1+ ": ");
				int wager = Integer.parseInt(keyb.next());
				b1.withdraw(wager);
				System.out.println("Your remaining balance " + name1 + "is: " + b1.withdraw(wager)); 

				System.out.println("Please enter your wager "+name2+": ");
				wager = Integer.parseInt(keyb.next());
				b2.withdraw(wager);
				System.out.println("Your remaining balance " + name2 + "is: " + b2.withdraw(wager)); 



Method
	public int withdraw(int wager) {
		 int balance1 = balance - (wager);
		return balance1;	
	}


This post has been edited by laxman2809: 16 September 2011 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 05:55 PM

Congrats to your gut instincts!

I was hinting at that earlier but didn't press it. A wager shouldn't be deducted from the user's account until the bet is lost. I would structure the logic something like:

(repeat below for both users)
get user wager,
verify the user has funds to cover wager
if enough funds, save as wager but do not deduct from account
if not enough funds, let user know and get new wager

event of chance happens
determine if user wins or loses
if user loses, deduct wager amount from balance
if user wins, add wager amount to balance (deduct from house?)

run next event until user quits or runs out of funds
Was This Post Helpful? 1
  • +
  • -

#14 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 06:10 PM

Alright now thinking about so should be if statements for the first bit and then a loop for the second?
Was This Post Helpful? 0
  • +
  • -

#15 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

Re: Dice Betting Game Working With Different Classes Questions

Posted 16 September 2011 - 06:26 PM

Here is what I wrote to satisfy the first part of what you said I have some things that I had to change so it would satisfy the requirements of my assignments
System.out.println("Please enter your wager " +name1+ ": ");
				int wager = Integer.parseInt(keyb.next());
				b1.withdraw(wager);
				if (wager==0){
					System.out.println("Why are you quitting");
					System.exit(0);
				}
				else if (wager>b1.balance()){
					System.out.println("You are a cheater");
					System.exit(0);
				}
				
				else if (wager>0){
						int wager1 = b1.withdraw(wager);
						}
				
				System.out.println("Please enter your wager "+name2+": ");
				wager = Integer.parseInt(keyb.next());
				if (wager==0){
					System.out.println("You don't have enough money");
					System.exit(0);
				}
				else if(wager>b2.balance()){
					System.out.println("You are a cheater");
					System.exit(0);
				}
				else if (wager>0){
					int	wager2=b2.withdraw(wager);
						}
			{

This post has been edited by laxman2809: 16 September 2011 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2