Results 1 to 5 of 5
  1. #1
    jdiesel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default Calling a variable from a different java class

    Hi:
    I have created a class and inside the class a setter method. This setter method doesnt seem to recognize the variables within the constructor method.
    Here is the code of the main program:
    Java Code:
    import java.util.*;
    class Roulette
    {
      static int [] moneyLeft = new int [50];
      static String [] players = new String [50];
      
      public static void main (String [] args)
      {
        System.out.println("\n                         WELCOME TO ROULETTE");
        System.out.println("---------------------------------------------------------------");
        System.out.println("First type the name of the players.");
        System.out.println("When you are done entering their names, enter \"no\" to continue.\n");
        
        Scanner keyboard = new Scanner (System.in);
        
        for(int i=1; i< 50; i++)
        { 
          System.out.println("What is the name of player #" + i + "?");
          String s = keyboard.next();
          if(s.equalsIgnoreCase("no"))
          {
             i = 50;
          }
          else
          {
          players[i] = s;
          System.out.println("How much money does " + players[i] + " want to start off with?");
          moneyLeft[i] = keyboard.nextInt();
          }
        }
    
       int h =1;
       Bet actuaBet;
       System.out.println("How much would you like to bet?");
       int moneySpent = keyboard.nextInt();
       //moneyLeft[h] = moneyLeft[h] - moneySpent;
       
       System.out.println("What Type of bet would you like to do?");
       String response = keyboard.next();
       if (response.equalsIgnoreCase("N"))
       {
          System.out.println("What number do you choose?");
          int choice = keyboard.nextInt();
          Bet actualBet = new Bet (moneySpent, choice);
          actualBet.number();
       }
          else if (response.equalsIgnoreCase("O"))
          {
             Bet actualBet = new Bet (moneySpent);
             actualBet.odd();
          }  
          else if (response.equalsIgnoreCase("E"))
          {
             Bet actualBet = new Bet (moneySpent);
             actualBet.even();
          }  
          else if (response.equalsIgnoreCase("H"))
          {
             Bet actualBet = new Bet(moneySpent);
             System.out.println(actualBet.high());
          }
          else if (response.equalsIgnoreCase("L"))
          {
             Bet actualBet = new Bet (moneySpent);
             actualBet.low();
          }
          else if (response.equalsIgnoreCase("R"))
          {
             rules();
       }
       else
       {
          System.out.println("That is not a choice");
       }
     }
    
       static void rules()
       {
          System.out.println("ROULETTE RULES");
          System.out.println("-----------------");
          System.out.println("If you bet correctly on a NUMBER from 1 to 36 you will earn 10 times your bet\n");
          System.out.println("If you bet correctly on a EVEN, ODD, HIGH, or LOW number you will double your bet\n");
          System.out.println("IF THE ROULETTE SPINS 0 YOU ALWAYS LOSE\n\n");
          System.out.println("BETTING OPTIONS");
          System.out.println("-------------------");
          System.out.println("N for betting on a NUMBER between 1 and 36");
          System.out.println("H for betting on a HIGH number (between 19 and 36)");
          System.out.println("L for betting on a LOW number (between 1 and 18)");
          System.out.println("O for betting on a ODD number (1, 3, 5, ...)");
          System.out.println("E for betting on a EVEN number (2, 4, 6, ...)");
       }
      
    }
    Here is the new class
    Java Code:
    class Bet
    {
       private int spent;
       private int choice;
       public Bet(int s)
       {
          int spent = s;
       }
    
       public Bet(int s, int c)
       {
          int spent = s;
          int choice = c;
       }
    
       public int number()
       {
          int earning, number = (int) (Math.random() * 36);
          if (number == choice)
          {
             earning = spent*10;
          }
          else
          {
             earning = spent*-1;
          }
          return earning;
       }
    
       public Bet high()
       {
          int earning, number = (int) (Math.random() * 36);
          if (number > 18)
          {
             earning = this.spent * 2;
          }
          else
          {
             earning = this.spent * -1;
          }
          return new Bet(earning);
       }
    
        public int low()
       {
          int earning, number = (int) (Math.random() * 36);
          if (number <= 18)
          {
             earning = spent*2;
          }
          else
          {
             earning = spent*-1;
          }
          return earning;
       }
    
       public int odd()
       {
          int earning, number = (int) (Math.random() * 36);
          if (number%2 != 0)
          {
             earning = spent*2;
          }
          else
          {
             earning = spent*-1;
          }
          return earning;
       }
    
       public int even()
       {
          int earning, number = (int) (Math.random() * 36);
          if(number%2 == 0)
          {
             earning = spent*2;
          }
          else
          {
             earning = spent*-1;
          }
          return earning;
       }
    
       public String toString()
       {
          return "" + spent;
       }
    }
    I am currently just working with one setter method, high();
    When I ask the method to print the output, it print 0.
    Any help?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem I'm seeing on quick look at your code is that you redeclare your spent and choice int variables inside of the Bet constructors. When you do this, the actual fields will not be updated as you would desire. Note the difference between this:

    Java Code:
    class Bet
    {
      private int spent;
      private int choice;
    
      public Bet(int s)
      {
        int spent = s; // an int variable local only to this constructor
      }
    and this:
    Java Code:
    class Bet
    {
      private int spent;
      private int choice;
    
      public Bet(int s)
      {
        spent = s; // the actual spent field
      }

  3. #3
    jdiesel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    I changed this, as you suggested, but i still dont get the value of the variable.

  4. #4
    jdiesel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    actually it did work. thanks a million

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  2. excecuting a jar file by calling a java class
    By Lavanya.vitria in forum Advanced Java
    Replies: 1
    Last Post: 12-13-2008, 12:11 PM
  3. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  4. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 03:35 PM
  5. calling a java class from html
    By Ed in forum Advanced Java
    Replies: 1
    Last Post: 07-08-2007, 12:58 AM

Posting Permissions

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