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

Thread: Dice Wagering Game new to Multiple Classes

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Dice Wagering Game new to Multiple Classes

    Alright so I have a hw on multiple classes and I am having trouble with getting input from 1 to another. Now I don't know what the best way to do this so I am having the class ask for the input(error is here can't make it do it) and then having that outputted to a string in the driver program.
    Bankaccount Class
    import java.io.InputStream;
    import java.util.Scanner;
     
     
    public class Bankaccount {
     
    	private String name;
    	private int balance;
     
     
    	public Bankaccount()
    	{
    		int balance = 1000;
     
    		Scanner keyboard = new Scanner(System.in);
    		String name = keyboard(System.in);
    	}
     
     
    	public String showName(){
    		return name ;
    		}	
    		public int deposit(){
    			return balance += deposit();
    		}
     
    		public int withdraw(){
    			return balance -= withdraw();
    		}
     
     
     
     
    }
    Driver Program-Yes i started thinking to much and got ahead of myself
     
    import java.util.Scanner;
     
     
    public class Playingdice {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		System.out.println("Playing A Game Of Dice");
     
    		Scanner keyboard = new Scanner(System.in);
     
    		//int wager1 = Integer.parseInt(keyboard.nextLine());
     
    		Bankaccount b1 = new Bankaccount();
    		Bankaccount b2 = new Bankaccount();
    		//Names
    		String name1;
    		name1 = b1.showName();
    		System.out.println("PLease enter a name for Account 1: " + b1.nameShow());
     
    		String name2;
    		name2 = b2.showName();
    		System.out.println("PLease enter a name for Account 2: " + name2);
     
    		//int balance1 = b1.balance();
    		//int balance2 = b2.balance();
    		//int wager = b1.withdraw();
    		//int wager2 = b2.withdraw();
    		//int win1 = b1.deposit();
    		//int win2 = b2.deposit();
     
     
     
    		Die d1 = new Die();
    		Die d2 = new Die();
    		int r1 = d1.roll();
    		int r2 = d2.roll();
    		int total = r1 + r2;
     
     
     
     
     
     
     
     
    	}
    }


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

    Default Re: Dice Wagering Game new to Multiple Classes

    Please copy and paste here the full text of the error message.

  3. #3
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Hello Laxman2809,

    For future reference, please post any errors you get to ensure a speedy response. That being said, here are some of my thoughts:

    Bankaccount class:
    1. I see no reason to create a scanner instance in this class when it isn't used in said class.

    2. If you want to get the name then pass it as a parameter in the constructor (e.g. Bankaccount(String name)). Now if you want the user to input the name then use the input scanner 'keyboard' in your main() method. Like this: Bankaccount asdf = new Bankaccount(keyboard.nextLine());.

    Playingdice class:
    1. The method nameShow() does not exist, just a little mix up there.

    Well, that's pretty much it for now since you didn't post the Die class. One question I do have is which IDE are you using (if any)?

    EDIT: Norm beat me to the bit about the error

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

    Default Re: Dice Wagering Game new to Multiple Classes

    You've done a much better analysis of the OPs code. I usually wait to see if they come back.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Alright so no error really just having issues making it do as i would like
    Now I have it asking for 1st name but I can't get it to ask for the 2nd name
    import java.util.Scanner;
     
     
    public class Bankaccount {
     
    	private String name;
    	private int balance;
     
    	public Bankaccount()
    	{
    		int balance = 1000;
    		String name;
     
    	}
    	 public void showName(String name){
    		 System.out.println("Please enter a name ");
    	 }
     
     
     
    		public int deposit(){
    			return balance += deposit();
    		}
     
    		public int withdraw(){
    			return balance -= withdraw();
    		}
     
    import java.util.Scanner;
     
     
    public class Playingdice {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		System.out.println("Playing A Game Of Dice");
     
    		Scanner keyb = new Scanner(System.in);
    		Scanner keyb2 = new Scanner(System.in);
     
     
     
    		Bankaccount b1 = new Bankaccount();
    		Bankaccount b2 = new Bankaccount();
    		//Names
    		String name1 = keyb.nextLine();
    		b1.showName(name1);
     
    		String name2 = keyb2.nextLine();
    		b2.showName(name2);
     
    		//int balance1 = b1.balance();
    		//int balance2 = b2.balance();
    		//int wager = b1.withdraw();
    		//int wager2 = b2.withdraw();
    		//int win1 = b1.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;
     
     
     
     
     
     
     
     
    	}
    }
     
    // Java packages
    import javax.swing.*;
     
     class Die {
        /**
         * A die is defined in terms of its value, which must be between
         * 1 and 6
         */
        private int value;
     
        /**
         * Default constructor for a die.  The die is initialized to a
         * random integer value between 1 and 6
         */
        public Die() {
    		value = (int)(Math.random() * 6.0 + 1.0);
        }
     
        /** Returns the value of a die, without affecting it */
        public int getValue() {
    		return value;
        }
     
        /** Displays a die, i.e., its value */
        public ShowDie display(int x, int y) {
    		System.out.println("The value of the die is " + value);
    		ShowDie sd = new ShowDie(value, x, y);
    		sd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	return sd;
        }
     
        /** Removes the display of a die */
        public void undisplay(ShowDie sd) {
    		sd.dispose();
        }
     
        /** Rolls the die, sets its value, and returns the new value */
        public int roll() {
    		value = (int)(Math.random() * 6.0 + 1.0);
    		return value;
        }
     
    }// Die

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

    Default Re: Dice Wagering Game new to Multiple Classes

    I have it asking for 1st name but I can't get it to ask for the 2nd name
    What happens when you execute the program? Can you copy the console and paste it here?
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.


    Your methods are strangely named.
    The showName method prints a question. From the method's name, I'd expect it to print out the name.
    You pass a String to the showName method but don't use it for anything.
    You should have the asking for the name and the reading of the name in the same method.


    In the main() method you read in two Strings without telling the user what he is to enter.
    You should tell the user what you want him to enter before trying to read it.

  7. #7
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    I would suggest reading this tutorial on Classes and Objects. There is a lot of information there but I believe it will help out a lot.

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Alright so solved some of those issues now on how to subtract the wager from the balance entered by the user it is saing method withdraw is not applicable for the argument int, int

    import java.util.Scanner;
     
     
    public class Playingdice {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		System.out.println("Playing A Game Of Dice");
     
    		Scanner keyb = new Scanner(System.in);
    		Scanner keyb2 = 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);
    				System.out.println("Your remaining balance is " + b1.withdraw1(wager1)); 
     
    			//Name
    			System.out.println("Please enter name: ");
    			String name2 = keyb2.nextLine();
    			b2.showName(name2);
    			//Balance
    			b2.balance();
    			int balance2 = b2.balance();
    			System.out.println("Your Balance is: " + balance1);
    			//Wager
    			int wager2 = Integer.parseInt(keyb2.next());
    			b2.withdraw(wager2);
     
     
    import java.util.Scanner;
     
     
    public class Bankaccount {
     
    	private String name;
    	private int balance;
     
    	public Bankaccount()
    	{
    		balance = 1000;
    		String name;
    		String balance;
    		String wager;
    	}
    	public int balance(){
    		return balance;
    	}
    	public void showBalance(String balance){
    	}
    	public void showName(String name){
    	}
    	public int withdraw(int wager1, int wager2) {
    		return balance = balance - withdraw(wager1,wager2);
    	}
     
    	public int deposit(int win1, int win2){
    		return balance =  balance + deposit(win1,win2);
    	}
     
     
    }

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

    Default Re: Dice Wagering Game new to Multiple Classes

    it is saing method withdraw is not applicable for the argument int, int
    Please post the full text of the error message. Your edited version left off important information.

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Exception in thread "main" java.lang.StackOverflowError
    at Bankaccount.withdraw(Bankaccount.java:25)

    for when I run

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

    Default Re: Dice Wagering Game new to Multiple Classes

    It looks like you have a recursive call to the withdraw method. It should NOT be calling itself.

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    I hate to ask it but could you explain what you mean my recursive.

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

    Default Re: Dice Wagering Game new to Multiple Classes

    A method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    and that new invocation of the method calls itself
    until it runs out of space on the stack

  14. #14
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Alright so how would you call withdraw after the balance has been subtracted by the wager?

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

    Default Re: Dice Wagering Game new to Multiple Classes

    What is the withdraw method supposed to do?
    Where in your code do you need to call the withdraw method to do that job?

    Explain how you want your program to work.

  16. #16
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    I would like the method withdraw to subtract the wager from the balance and return me that int. it has to do this for 2 different bank accounts keeping the 2 balances separate
     
    //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());

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

    Default Re: Dice Wagering Game new to Multiple Classes

    Why does it call itself to do this?
    1) subtract the wager from the balance
    2) return me that int.

    What are the two statements that will do those two steps?

    it has to do this for 2 different bank accounts
    No, it should only do this for the class it is in. You need to do a withdrawal from each bank account separately by calling each bank account's withdraw method.

  18. #18
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Correct I was under the impression that using
    b1.withdraw(wager1,wager1);
    would be for account 1 and subtracting player 1 wager that he entered

    b2.withdraw(wager2,wager2);

    would be for account 2 and subtracting player 2 wager that he entered
    Last edited by Laxman2809; September 16th, 2011 at 05:25 PM.

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

    Default Re: Dice Wagering Game new to Multiple Classes

    b1.withdraw(wager1,wager1);
    Why do you pass the same amount as two arguments?

    How does the withdraw method do the two steps listed in post #17?
    What code do you have that does those two steps?

  20. #20
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    reducing the balance by the wager and then returning the balance with the wager subtracted
    public int withdraw(int wager) {
    	return balance=balance - (wager);
    }

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

    Default Re: Dice Wagering Game new to Multiple Classes

    Is it working now?

    What if the balance goes negative?

  22. #22
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    Yes but now it is subtracting double the wager i think it is cause I am taking the wager twice but I don't know how to fix it

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

    Default Re: Dice Wagering Game new to Multiple Classes

    I don't know how to fix it
    Nor do I without seeing the code.


    cause I am taking the wager twice
    You don't say that subtracting twice is the wrong thing to do. It could be the right thing if the player were to make two wagers.

  24. #24
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice Wagering Game new to Multiple Classes

    this seems to get rid of the issue of subtracting it twice, but my thought is that its not/ I am not coding it right to enact the method to b1.withdraw(wager) and b2.withdraw(wager) which i thought created 2 diffent integers for the class Bank account
    Driver
    		System.out.println("Please enter your wager " +name1+ ": ");
    				int wager = Integer.parseInt(keyb.next());
    				b1.withdraw(wager);
    				int pot1 = 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());
    				int pot2= 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;	
    	}

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

    Default Re: Dice Wagering Game new to Multiple Classes

    b1.withdraw(wager);
    int pot1 = b1.withdraw(wager);
    System.out.println("Your remaining balance " + name1 + "is: " + b1.withdraw(wager));
    Why do You call the withdraw method 3 times here?
    Seems like you should only call it once if it changes the balance of the account.


    int balance1 = balance - (wager);
    Does this code change the balance in the account?

Similar Threads

  1. Mobile Game Development Classes
    By skj.kamal in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 16th, 2012, 04:12 AM
  2. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:08 PM
  3. help with using multiple classes
    By finalfantasyfreak15 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 5th, 2011, 12:18 AM
  4. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM