Determining whether an Array is full

Second question about a simple project

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 27636 Views - Last Post: 17 July 2009 - 01:06 AM Rate Topic: -----

#1 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Determining whether an Array is full

Post icon  Posted 15 July 2009 - 10:42 AM

Ok i posted a problem before about storing objects in Arrays, first of all thanks ALOT to those of you who helped, i've got that working now.

It's been a while since i posted my last problem and my code looks alot different now so started a new thread instead instead of carrying on the old one (and the problems different).

The project
I have to design a Sweepstake game that has 2 classes Entrant and Sweepstake. Entrant is simply a class that holds the name and a contact number of someone entering the sweepstake.
The sweepstake class has two fields an array to store upto 20 entrants and a boolean to determine whether it is full or not.
Once 15 entrants or more have been added to the array then the sweepstake can be run, a random number 1-20 is chosen and the winner is printed out to the terminal.

Entrant class. (Only constructor, methods are just for returning the names and stuff)

public class Entrant
{
	private String name;
	private String contact;
	
	//Construct a new entrant storing a name and a contact number
	public Entrant(String enterName, String enterContact)	
	{
		if(enterContact.length() >11 || enterContact.length()<11){
			System.out.println("Contact numbers must be 11 digits");
		}
		else{
			name =  enterName;
			contact = enterContact;
		}
	}


Sweepstake Class. (Haven't actually made the part that randomly selects the number and prints out the winner, but my problem doesn't concern that)

public class Sweepstake
{
	//An array that stores upto 20 entrants.
	private Entrant[] numbers;
	// Boolean to determine whether all twenty numbers have been filled.
	private boolean full;
	
	/**
	 * Create a new sweepstake with no numbers
	 * and full set to false.
	 */	
	public Sweepstake()
	{
		numbers = new Entrant[20];
		full = false;
	}
	
	/**
	 * Entrant enters a ticketnumber of there choosing,
	 * their details are recorded against their number.
	 */
	public void buy_ticket(int number, Entrant entrant)
	{
		int indexPos = number -1;		
		if(number >=1 && number <= 20 && numbers[indexPos]== null){
			numbers[indexPos] = entrant;
		}
		else{
			System.out.println("Sorry your number has been taken already");
			System.out.println("Or you have entered a number that isn't between 1 and 20");
		}
	}
	
	/**
	 * Returns the numbers of the tickets that haven't
	 * yet been sold.
	 */		   
	public void get_number_unsold()
	{
		int indexCount = 0;
		while(indexCount <=19){
			if(numbers[indexCount] == null){
				System.out.println("Number "+ (indexCount+1) +" is free");
			}
			indexCount = indexCount+1;
		}
	}
				 
	/**
	 * Entrant recieves a random number that isn't already taken,
	 * their details are recorded against their number.
	 */
	public void buy_random_ticket(Entrant entrant)	
	{
		int randomTicket = 0;
		while(numbers[randomTicket] != null){
			Random random = new Random();
			randomTicket = (int)(Math.random()*20);
		}
		numbers[randomTicket] = entrant;
	
	}
	
	/**
	 * Check to see if the sweepstake is full.
	 */
	public boolean full()



[b]My problem

I'm not sure how to go about with the boolean full. This in turn makes the buy random ticket go into an inifinite loop if the array is full as it just repeats until it finds an empty number (i can sort this out once i know how to do the full boolean.

The only way i can think of doing this is to have a count and do it like the way i did the get_unsold method. E.g. if the array index has an element in it a number is added onto a variable, once this variable hits 20 then it must be full so the boolean full = true. This just doesn't seem like the right way to do it though as surely if it's not full and you run it twice then the local variable would still have the exisiting number in it. So any guidance of how i go about doing it would help.

Thanks! :^:

Oh one other thing this line
			randomTicket = (int)(Math.random()*20);


from the buy random ticket method. Was wondering whether someone could just write this in pesuedo code for me, I searched the forums and found out how to write the method from another projects code. It works i just don't get what it means!

Is This A Good Question/Topic? 0
  • +

Replies To: Determining whether an Array is full

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Determining whether an Array is full

Posted 15 July 2009 - 10:50 AM

hmm.. not sure im following what your doing, but if you know that their are only going to be 20 max, couldn't you do something like this?
boolean full = false;
public void checkFull()
{
if(arrayname.length == 20)
	 full = true;
}

Was This Post Helpful? 0
  • +
  • -

#3 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Determining whether an Array is full

Posted 15 July 2009 - 10:51 AM

I can answer the last one right off the bat.

Math.random() outputs a number between 0 and 1. So you are multiplying 20 by fraction giving a final number between 0 and 20.

Since Math.random() will spit out a decimal number, you need to take the fractional part off. The (int) makes it a whole integer so there is no decimals left.

This post has been edited by syfran: 15 July 2009 - 10:54 AM

Was This Post Helpful? 1
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Determining whether an Array is full

Posted 15 July 2009 - 10:57 AM

or you could just use Random class nextInt method

Random r = new Random();
int randomNum = r.nextInt(20); // a number between 1-19


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,399
  • Joined: 27-December 08

Re: Determining whether an Array is full

Posted 15 July 2009 - 02:16 PM

Use an ArrayList<Entrant> to manage your contacts. Try something like this:

ArrayList<Entrant> entrants = new ArrayList<Entrant>();

public boolean canRunContest(ArrayList<Entrant> e){
   if(e.size() >= 15) return true;
}
public boolean isFull(ArrayList<Entrant> e){
   if(e.size() == 20) return true;
}



Use the isFull() method before adding more entrants to the contest, and the canRunContest() method to determine if there are enough people to initiate the sweepstakes. Like so:

ArrayList<Entrant> e = get Entrants; //using pseudo-code here. 
Sweepstakes s = null;
if(canRunContest(e)) s = new Sweepstakes();


Was This Post Helpful? 0
  • +
  • -

#6 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Determining whether an Array is full

Posted 15 July 2009 - 03:40 PM

Ok i had posted that doing if(array.length ==20){ full = true; worked, this was a mistake. this is saying if the size of the array is 20 then set full to true, the array size is always 20.

Someone also mentioned using an arrayList. Only reason i haven't used one is because i assumed a fixed sized array is better? I thought an array list stored an unlimited amount of elements. How could i add an entrant to index 9 of an arrayList without having anything in index 1-8.

so unless anyone can confirm that an arrayList would be better i still need to know how to check to see whether my fixed size array is full with objects.

From what i understand:

I've created an array of size 20.
An entrant enters there details as an Entrant object.
When the entrant picks a number it's saved in the relevant array index (so #1 would be in array[0])
Until this is done the array[0] would of been null as nothing would of been in there.

I need to create a check that asks if every array index has an object in it. Which would mean array is full and the boolean can be set to true!

Hope this is explained better now.

This post has been edited by markeatworld: 16 July 2009 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Determining whether an Array is full

Posted 16 July 2009 - 05:52 AM

Oh i forgot, thanks for the help on the random class explination, i get it now. It was mainly why the (int) was there.
Also the nextInt is something i might use later as it's actually what comes up in my textbook. I'll leave it how it is until it starts working though then play about with it later.
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4201
  • View blog
  • Posts: 18,432
  • Joined: 24-September 08

Re: Determining whether an Array is full

Posted 16 July 2009 - 07:15 AM

With a regular array, you could test each element of the array to see if that element contains null or not.

The API can tell you everything. That is for arrayList. The way an arrayList works is you add an element as you need it, so the size is how many contestants. You would just have to put it in some type of loop.

while(contestants.size() < 20) {//add contestant}

if(contestants.size() >= 15 && contestants.size() < 20) {//pull random number for winner}
Was This Post Helpful? 0
  • +
  • -

#9 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Determining whether an Array is full

Posted 16 July 2009 - 07:31 AM

View PostNeoTifa, on 16 Jul, 2009 - 06:15 AM, said:

With a regular array, you could test each element of the array to see if that element contains null or not.

The API can tell you everything. That is for arrayList. The way an arrayList works is you add an element as you need it, so the size is how many contestants. You would just have to put it in some type of loop.

while(contestants.size() < 20) {//add contestant}

if(contestants.size() >= 15 && contestants.size() < 20) {//pull random number for winner}



yah i get that if i used the arrayList i could use the .size method. It's just if i do that i don't understand how i make each entrant have a number. Because surely the arrayList orders each entrant so. .

[0] = Entrant 1
[1] = Entrant 2
[2] = Entrant 3

However if Entrant 4 picks number 8 as there ticket surely it's gonna go into index [3](or the next one in the list) so then i wouldn't know what number the entrant picked :S!
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,399
  • Joined: 27-December 08

Re: Determining whether an Array is full

Posted 16 July 2009 - 07:33 AM

View PostNeoTifa, on 16 Jul, 2009 - 08:15 AM, said:

With a regular array, you could test each element of the array to see if that element contains null or not.

The API can tell you everything. That is for arrayList. The way an arrayList works is you add an element as you need it, so the size is how many contestants. You would just have to put it in some type of loop.

while(contestants.size() < 20) {//add contestant}

if(contestants.size() >= 15 && contestants.size() < 20) {//pull random number for winner}


Basically for ArrayLists, the can hold theoretically unlimited elements, but you can use conditionals and such to limit the number of elements added to the list.
Was This Post Helpful? 0
  • +
  • -

#11 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4201
  • View blog
  • Posts: 18,432
  • Joined: 24-September 08

Re: Determining whether an Array is full

Posted 16 July 2009 - 07:34 AM

index + 1 of course! :bananaman:
Was This Post Helpful? 1
  • +
  • -

#12 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Determining whether an Array is full

Posted 16 July 2009 - 07:38 AM

Well i found a way of doing it, it just seems weird that i've found notihng like it on the internet or in my java textbook so i assume it isn't the convetional way.

	public void runFull()
	{
		int totalEntrants = 0;
		for(Entrant entrant : numbers){
			if(entrant != null){
				totalEntrants = totalEntrants+1;
			}
		}
		if(totalEntrants ==20){
			full = true;
		}
		else{
			full = false;
		}
	}


Was This Post Helpful? 0
  • +
  • -

#13 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4201
  • View blog
  • Posts: 18,432
  • Joined: 24-September 08

Re: Determining whether an Array is full

Posted 16 July 2009 - 07:41 AM

That's what I said earlier, only w/o the code! :\
Was This Post Helpful? 0
  • +
  • -

#14 markeatworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-May 09

Re: Determining whether an Array is full

Posted 16 July 2009 - 09:22 AM

View PostNeoTifa, on 16 Jul, 2009 - 06:41 AM, said:

That's what I said earlier, only w/o the code! :\



hehe thanks, i did actually do that a few hours ago it just didn't feel right even though it worked. You confirming that it can be done like this helps!

Got it working now so that it picks the winner too. Just got to make it user friendly now so it all can be controlled through the text terminal, hopefully i'll post the finished copy later if i can!
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4201
  • View blog
  • Posts: 18,432
  • Joined: 24-September 08

Re: Determining whether an Array is full

Posted 16 July 2009 - 09:39 AM

Well great that you figured it out ^__^ click the this post was helpful button now in order to keep getting my help for free, else it's $19.99/hour. :pirate:
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2