Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Desperate for some help! Please

    I am working on this project for 5 days now and keep going in circles. It's supposed to draw lotto balls, the 1st four need to be between 1-33. And give a response if entering the wrong number. The 5th is 1-31 and again another response. Then ask user if the want kicker numbers. First few 0-9 and last number 0-4. But user enters 1 for yes they want kicker and 2 for no. This is mostly where I'm confused. Also my array... I keep getting error about using double but require integer?? CashBall class I don't need to use the mutator methods but need the accessor methods. Can some please help? It would be GREATLY appreciated.
    Code:
    import java.util.Scanner;
    public class CashBall
    {
    
    	int cashBall;
    	int ball1,ball2,ball3,ball4;
    	int kicker;
    	int index;
    
    	/* Acessor methods
    	*/
    
    
    	public int getCashBall()
    	{
    		return cashBall;
    	}
    	public int getBall1()
    	{
    		return ball1;
    	}
    	public int getBall2()
    	{
    		return ball2;
    	}
    	public int getBall3()
    	{
    		return ball3;
    	}
    	public int getBall4()
    	{
    		return ball4;
    	}
    
    	/*Mutator methods
    	*/
    
    
    	public String setCashBall()
    	{
    		if ((cashBall < 1) && (cashBall > 31))
    		{
    			return ("Cash Ball number needs to be between 1 and 31. Please enter new number.");
    		}
    	}
    
    	public String setBall1()
    	{
    		if ((ball1 < 1) && (ball1 > 33))
    		{
    			return("Lotto number needs to be between 1 and 33. Please enter new number.");
    		}
    	}
    
    	public String setBall2()
    	{
    		if ((ball2 < 1) && (ball2 > 33))
    		{
    			return("Lotto number needs to be between 1 and 33. Please enter new number.");
    		}
    	}
    
    	public int setBall3()
    	{
    		if ((ball3 < 1) && (ball3 > 33))
    		{
    			System.out.println("Lotto number needs to be between 1 and 33. Please enter new number.");
    		}
    	}
    
    	public String setBall4()
    	{
    		if ((ball4 < 1) && (ball4 > 33))
    		{
    			System.out.println("Lotto number needs to be between 1 and 33. Please enter new number.");
    		}
    	}
    
    
    	/* Read and Write output methods
    	*/
    
    	public String outputSmaller( )
    	{
    		return ("Lotto Cash Ball needs to be 33 or smaller.");
    	}
    
    	public String outputHigher()
    	{
    		return ("Lotto Cash Ball needs to be equal to 1 or higher.");
    	}
    
    	public String equals()
    	{
    		return ("Lotto Ball needs to be unique from any previous balls.");
    	}
    
    	public String outputLower()
    	{
    		return (" Cash Ball needs to be lower than 31.");
    	}
    
    
    	public String readInput()
    	{
    		if(kicker == 1)
    		{
    			return ("No Kicker Picked.");
    		}
    		else
    			return("Your kicker numbers are: ");
    	}
    }
    Here is the main class:
    Code:
    /* This is the main method
    */
    import java.util.Scanner;
    public class CashBallTest
    {
    	public static void main(String[] args)
    	{
    		int cashBall;
    
    		Scanner keyboard = new Scanner(System.in);
    		int[] ball = new int[4];
    		int[] kickerBall = new int[5];
    
    		int index;
    		int b;  //ball
    		int n;
    		int kicker;
    
    		System.out.println("/t/t/tCash Lotto Ball");
    		System.out.println("Players pick Four numbers from 1 to 33 called Lotto Cas Balls. Lotto Cash Ball /n numberss must be unique from all other Lotto Cash Balls.");
    		System.out.println();
    		System.out.println("The Players must also pick a Cash Ball from 1 to 31.");
    		System.out.println();
    		System.out.println("Kicker is a brand new feature you can purchase with your Cash Ball ticket which /nlets you get in on an extra drawing.");
    
    
    		/*A do while loop that runs until index is greater than 4
    		*/
    		for(index = 0;index<4;index++)
    		{
    	 		do
    	 		{
    				System.out.println(" Please enter number for lotto ball " + (index+1) + ":");
    				ball[index] = keyboard.nextInt();
    				if(ball[index]<1)
    					System.out.println(cashBall.outputHigher);
    				else if((ball[index] > 33))
    					System.out.println(cashBall.outputSmaller);
    				else if((ball[index]) == (ball[index]))
    					System.out.println(cashBall.equals);
    					index++;
    			}while((ball4));
    		}
    
    		System.out.println(" Please enter number for cash ball:");
    		cashBall = keyboard.nextInt();
    		if (cashBall < 1)
    		{
    			System.out.println(cashBall.outputHigher);
    		}
    		else if (cashBall > 31)
    		{
    			System.out.print(cashBall.outputLower);
    		}
    
    		System.out.println("Do you want the Kicker, 1 for no or 2 for yes? The first four numbers is from a serperate set of 0 to 9 and the fifth number is from a set of 0 to 4.");
    		kicker = keyboard.nextInt;
    		cashBall.readInput();
    
    		for (kickerBall = 0; kickerBall <= 3; kickerBall++)
    		{
    			n[kickerBall] =(int)(Math.random() * 9);
    			System.out.print(n[kickerBall]+ " ");
    			}
    			n[4] =(int)(Math.random() * 4);
    			System.out.print(n[4]);
    			kickerBall = 1 + kickerBall;
    		}
    	}
    Last edited by Fou-Lu; 05-03-2011 at 04:39 PM. Reason: Added /code tags

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There is quite a number of errors in here, so we'll only address those until you fix and update.
    There are 5 methods in CashBall under setBallX. These need to return strings, but only one branch does. They don't really appear to do anything though, typically a mutator would take a parameter matching the member, return void, alter the underlying data (which really should be private, especially since you have indicated rules in use for what they are allowed to be), and throw exceptions not return strings.
    The tester has quite a number of errors as well, there are several method calls that have been treated as members, so you need to fix those up. There are also a few undefined variables, check out the while loop - there is no ball4 declared for example.
    And finally, there is a lot if incorrect usage in the for loop in regards to variables. You are treating arrays as scalars with the kickerBall is an int[] being treated as an int, and n is an int treated as an int[]. I'd assume that the CashBall variable should also be of type CashBall, and not of type int.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The set methods are not used, I just needed to show them so not to concerned about those being correct. I am not sure what you mean by the methods being members. How else would I call the method from cashBall class? Also I changed the end of the while loop from ball4 to index4. Sorry kinda a noobie and not sure what scalar means when refering to the kicker array and the for loop involved. Could you give me an example please?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its less a matter if the mutators are used or not. When the signature of the method doesn't match the body (in particular, the return result here), the class will fail to compile.
    Methods != members, what I mean is you are treating methods as members:
    PHP Code:
    cashBall.outputHigher // Should be cashBall.outputHigher()
    keyboard.nextInt // Should be keyboard.nextInt() 
    In the first example, it will fail anyway (you'd see similar behaviour in the do/while loop with cashBall.equals as well), since it is an integer, and not an object (should be a CashBall object).

    For scalar and complex; a Scalar holds a single value, and is primitive:
    int i = 0; // Scalar
    Integer i = new Integer(5); // Complex
    int[] i = new int[4]; // Complex array of scalar
    Object[] o = new Object[4]; // Complex array of Complex
    Classes / objects are considered to be complex in nature, as is structs and unions in C.

    In regards to what you have:
    PHP Code:
    int n;
    ...
    n[kickerBall] =(int)(Math.random() * 9); 
    That is illegal as there is no offset available in n to assign a value to. That has to either be declared as an int[] (with a size during instantiation), or the n[kickerBall] needs to become a scalar instead of an array.

    You'll need to fix up the compilation errors first before we can begin to debug what the actual runtime is.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay fixed all errors in cashBallTest but the end of the while loop. I'm getting an error of expected. Here is the new code.
    Code:
    /* This is the main method
    */
    import java.util.Scanner;
    public class CashBallTest
    {
    	public static void main(String[] args)
    	{
    
    
    		Scanner keyboard = new Scanner(System.in);
    		int[] ball = new int[4];
    		CashBall yourBall = new CashBall();
    		int index;
    		int in;
    		int[] n;
    		int kicker;
    		int cashBall1;
    
    		System.out.println("/t/t/tCash Lotto Ball");
    		System.out.println("Players pick Four numbers from 1 to 33 called Lotto Cas Balls. Lotto Cash Ball /n numberss must be unique from all other Lotto Cash Balls.");
    		System.out.println();
    		System.out.println("The Players must also pick a Cash Ball from 1 to 31.");
    		System.out.println();
    		System.out.println("Kicker is a brand new feature you can purchase with your Cash Ball ticket which /nlets you get in on an extra drawing.");
    
    
    		/*A do while loop that runs until index is greater than 4
    		*/
    		for(index = 0;index<4;index++)
    		{
    	 		do
    	 		{
    				System.out.println(" Please enter number for lotto ball " + (index+1) + ":");
    				ball[index] = keyboard.nextInt();
    				if(ball[index]<1)
    					yourBall.outputHigher();
    				else if((ball[index] > 33))
    					System.out.println(yourBall.outputSmaller());
    				else if((ball[index]) == (ball[index]))
    					System.out.println(yourBall.equals());
    					index++;
    			}while(index<4)
    		}
    
    		System.out.println(" Please enter number for cash ball:");
    		cashBall1 = keyboard.nextInt();
    		if (cashBall1 < 1)
    		{
    			System.out.println(yourBall.outputHigher());
    		}
    		else if (cashBall1 > 31)
    		{
    			System.out.print(yourBall.outputLower());
    		}
    
    		int[] kickerBall = new int[5];
    
    		System.out.println("Do you want the Kicker, 1 for no or 2 for yes? The first four numbers is from a serperate set of 0 to 9 and the fifth number is from a set of 0 to 4.");
    		kicker = keyboard.nextInt();
    		yourBall.readInput();
    
    		for (in = 0; in <= 3; in++)
    		{
    			n[in] =(int)(Math.random() * 9);
    			System.out.print(n[in]+ " ");
    			}
    			kickerBall[4] =(int)(Math.random() * 4);
    			System.out.print(n[4]);
    			index = 1 + index;
    
    			System.out.print("Your lotto balls are: " + ball[index]);
    			System.out.println("Cashball is: " + cashBall1);
    		}
    	}
    Not sure how to write my mutators considering they are not used. I know I need to take out the string but not sure what exactly to change it to.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The only thing(s) I see wrong from a compilation standpoint is that the do/while is incomplete (add a semi-colon to the end of the while), and that the n hasn't been initialized (as an array you need to initialize it to a constant size).

    If the mutators aren't used, just remove them. But I myself would flag every member in the CashBall class as private and write the mutators:
    PHP Code:
    private void validateBall(int i)
    {
        if (
    || 33)
        {
            throw new 
    Exception("Lotto number needs to be between 1 and 33. Please enter new number.");
        }
    }
    public 
    void setBall1(int i)
    {
        
    this.validateBall(i);
        
    this.ball1 i;

    And a similar approach to the cashBall member (which I noticed is a different set of valid numbers). Set would then be done as:
    PHP Code:
    CashBall cb = new CashBall();
    try
    {
        
    cb.setBall1(13);
        
    cb.setBall2(54);
        
    cb.setBall3(4);
        
    cb.setBall4(7);
    }
    catch (
    Exception ex)
    {
        
    ex.printStackTrace();

    The above should fail on the setting of ball2.

    BTW, though this is valid in Java:
    PHP Code:
        public String equals()
        {
            return (
    "Lotto Ball needs to be unique from any previous balls.");
        } 
    I'd actually recommend renaming that function. java.lang.Object has an equals declared as:
    PHP Code:
    public boolean equals(Object obj); 
    Which with the one you have above creates a semi-overload on the method equals. While not an actual overload since the return type is different, this is still ambiguous since the parameters are also different:
    PHP Code:
    CashBall cb1 = new CashBall();
    CashBall cb2 = new CashBall();
    System.out.println(cb1.equals(cb2)); // is false
    System.out.println(cb1.equals()); // prints a string about uniqueness 
    I'd keep it in line with what you have for other names, and call that method outputUnique instead of equals.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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