Home arrow static arrow Java Programming [Archive] - array frequency
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - array frequency
This topic has 9 replies on 1 page.

Posts:357
Registered: 8/5/04
array frequency  
Aug 7, 2004 11:04 AM



 
Hi,
I am making a Die class i've got a little problem in the >frequency< method. please have a look at the
comments
import type.lang.*; public class Die{  private final int MIN_FACES = 6;   private int numFaces;   // number of sides on the die   private int faceValue;  // current value showing on the die      public Die ()   {  numFaces = MIN_FACES;      faceValue = 1;   }    public Die (int faces)   {  if (faces < MIN_FACES)         numFaces = MIN_FACES;      else         numFaces = faces;      faceValue = 1;   }    public int roll ()   {  faceValue = (int) (Math.random() * numFaces) +1;            return faceValue;   }      //this will count the no. of time a no. on the face of the dice occured when it got rolled "n" no. of times//index of freq[] equals the no. on the dice //i.e. freq[1]  contains the no. of times no. 1 occured when dice got rolled.  //Any ideas on how to implement this method? public int[]  frequency(int n){			int[] freq = new int[numFaces+1];	int count = 0;	for (int i = 0; i < n; i++){			freq[this.roll()] = count++;	}		  	 	return freq; } }
 

Posts:8,813
Registered: 10/4/00
Re: array frequency  
Aug 7, 2004 11:10 AM (reply 1 of 9)



 
My suspicion is that your code would get an ArrayIndexOutOfBounds exception. You might look where you are using an array and see if you can find out where you might be referencing an array element that is out of bounds.
 

Posts:179
Registered: 7/30/04
Re: array frequency  
Aug 7, 2004 11:19 AM (reply 2 of 9)



 
//Any ideas on how to implement this method?
public int[] frequency(int n){

It looks to me as if you already have implemented it, so this is not really what you ask?
Do you get errors?

BTW: That code looks like it will work very well.
 

Posts:179
Registered: 7/30/04
Re: array frequency  
Aug 7, 2004 11:22 AM (reply 3 of 9)



 
You may want to change
freq[this.roll()] = count++;
to
freq[this.roll()]++;
 

Posts:357
Registered: 8/5/04
Re: array frequency  
Aug 7, 2004 11:51 AM (reply 4 of 9)



 
You may want to change
freq[this.roll()] =count++;
to
freq[this.roll()]++;

Hi,

How can I make return value in arrray [] ? thanks!
 

Posts:357
Registered: 8/5/04
Re: array frequency  
Aug 7, 2004 12:08 PM (reply 5 of 9)



 
nevermind ...
 

Posts:8,813
Registered: 10/4/00
Re: array frequency  
Aug 7, 2004 12:11 PM (reply 6 of 9)



 
This might help
http://members.fortunecity.com/sully4/nevrmind.wav
 

Posts:357
Registered: 8/5/04
Re: array frequency  
Aug 7, 2004 12:17 PM (reply 7 of 9)



 
This might help
http://members.fortunecity.com/sully4/nevrmind.wav

You've got pretty nice collections of things from the Internet.

I wonder how you keep these collections so well organized and so easy to retrieve?

 

Posts:8,813
Registered: 10/4/00
Re: array frequency  
Aug 7, 2004 12:29 PM (reply 8 of 9)



 
OK, valid question. As I travel through life, I encounter things which I think are applicable to life in general. I tend to remember these things. Not exact names and times and places, but just the concept. In this case, there was a comedy show where they had an obnoxious woman who would misunderstand everything told to her for several minutes until she finally 'got' it. When she 'got' it, she would say '[url http://members.fortunecity.com/sully4/nevrmind.wav]Nevermind...[/url]. I thought someone might think the same as me and have a wav file of this somewhere on the internet. I asked myself what I would name this file if I was going to put it on my web site. It turns out that I would name it 'nevermind.wav'. I did a [url http://www.google.com/search?sourceid=navclient&ie=UTF-8&oe=UTF-8&q=nevermind%2Ewav]Google search for nevermind.wav[/url] and there it was!
 

Posts:18,384
Registered: 21.03.00
Re: array frequency  
Aug 7, 2004 12:33 PM (reply 9 of 9)



 
Google is the developers best friend :)

/Kaj
 
This topic has 9 replies on 1 page.