9 Replies - 4139 Views - Last Post: 18 April 2009 - 12:53 PM Rate Topic: -----

#1 broncosfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 09

fill the int array with random data

Post icon  Posted 16 April 2009 - 10:28 PM

Hello. I am a student just learning java coding.
Here is the description of my assignment:
Statistics Array Assignment. In this assignment you will be creating a new class called StatsArray based on the UML below. This class will use what you have learned about arrays and building classes. Use the StatsArray.java skeleton class provided as a starting point. This skeleton class has empty methods for you to finish. (empty methods are also called stubs) The StatsArray class works with the StatsArrayGUI.java tester class provided . As you complete each method in StatsArray, compile your class and check by running StatsArrayGUI.
The first step is to fill an int array: +fillArray() : void //fill the int array with random data in the range 0 - 100
I tried to set the random generator but I get the following error:
StatsArray.java:45: cannot find symbol
symbol : method nextInt(int[])
location: class java.util.Random
stats = random.nextInt (stats) + (101); //This is where I am at. Does not compile.
^
StatsArray.java:45: operator + cannot be applied to java.util.Random.nextInt,int
stats = random.nextInt (stats) + (101);//This is where I am at. Does not compile.
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
I don't know what to do from here.
Any advise and/or assistance will be appreciated, Thanks, broncosfan.


import java.awt.*;
import java.util.Random;  //for our random number generator




public class StatsArray 
{

	//instance variables
	private int size;  //how big is the array
	private int[] stats; // an array of integers 

	//default constructor -overloaded method
	StatsArray() 
	{
		size = 10;
		stats = new int[size];  //instantiate the array called stats
	}

	public void display(Graphics g)
	{
		int x = 50;  //coordinates for displaying
		int y = 40;

		//display the array with position number
		for(int i = 0; i < stats.length; i++)
		{
			g.drawString("Stats [" + i + "] = "+ stats[i],
			 x,	(y + 15 * i));
		}
	}

	public void fillArray()
   {
		/*fill the array with random numbers (int) in the range 0 - 100.*/
		Random random = new Random();

	  stats = random.nextInt (stats) + (101);  //This is where I am at. Does not compile.
	}

	public int getSum() 
	{
		//add up all the values in the array

		return 0;
	}


	public int getMax() 
	{
		//return the maximum value in the array

			return 0;
	}

	public int getMin()
	{
		//return the minimum value in the array

			return 0;
	}

	public double getAverage() 
	{
		//return the average
			return 0;
	}

	public int countValues(int lowRange, int highRange) 
	{
		//count how many numbers are >= lowRange and <= highRange

		return 0;
	}


	public boolean isValueFound(int someNumber) 
	{
		//check to see if someNumber is in the array

		return true;
	}


	public void sortArray() 
	{
		/*sort the array in ascending order - selection sort*/

		int tempValue;
		int min;

		for (int i = 0; i < (stats.length - 1); i++)
		{
			min = i;
			for (int j = (i + 1); j < (stats.length); j++)
			{
				if (stats[j] < stats[min])
				{
					min = j;
				}
			}
			tempValue = stats[min];
			stats[min] = stats[i];
			stats[i] = tempValue;


		}

	}


	public void sortBArray() {
		/*Here is another common sort algorithm.
		  It sorts the array in ascending order.
		  This is called a bubble sort.  I have added it
		  so that you can take a look at it.*/

		int tempValue;

		for (int i = 0; i < (stats.length - 1); i++)
		{
			for (int j = (i + 1); j < (stats.length); j++)
			{
				if (stats[j] < stats[i])
				{
					tempValue = stats[i];
					stats[i] = stats[j];
					stats[j] = tempValue;
				}
			}
		}


	}





}//end of StatsArray Class definition


Is This A Good Question/Topic? 0
  • +

Replies To: fill the int array with random data

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: fill the int array with random data

Posted 16 April 2009 - 10:48 PM

Have you tried simply doing (stats+101)
Was This Post Helpful? 0
  • +
  • -

#3 broncosfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 09

Re: fill the int array with random data

Posted 16 April 2009 - 11:06 PM

View PostFuzzyness, on 16 Apr, 2009 - 09:48 PM, said:

Have you tried simply doing (stats+101)


No, I have not but I will. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 broncosfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 09

Re: fill the int array with random data

Posted 16 April 2009 - 11:15 PM

Hello Fuzzyness. I tried your suggestion. Still not liking it. Thanks.

StatsArray.java:45: not a statement
(stats + 101);
^
1 error

----jGRASP wedge2: exit code for process is 1.

This post has been edited by broncosfan: 16 April 2009 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 broncosfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 09

Re: fill the int array with random data

Posted 17 April 2009 - 11:20 AM

Hello. I have tried a few variations of Fuzzyness' suggestion. So far no success. The following are some of the errors generated.

StatsArray.java:45: operator + cannot be applied to int[],int
stats = stats + 101;
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


StatsArray.java:45: operator + cannot be applied to int[],int
stats = (stats + 101);
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

StatsArray.java:45: not a statement
(stats + 101);
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: fill the int array with random data

Posted 17 April 2009 - 02:12 PM

Oh, I didn't notice that stats was an array. you can only us integers. so you would have to use indexes

Random r = new Random();
for(int i = 0; i<stats.length;i++)
	  stats[i] = r.nextInt(100+1);


Was This Post Helpful? 2
  • +
  • -

#7 broncosfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 09

Re: fill the int array with random data

Posted 17 April 2009 - 02:37 PM

Thank you, Fuzzyness. The compiler likes that much better. Now on to the next step.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: fill the int array with random data

Posted 17 April 2009 - 09:58 PM

:blink:
for(int i = 0; i < stats.length; i++)
	stats[i] = random.nextInt(100);


Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: fill the int array with random data

Posted 18 April 2009 - 11:00 AM

I did 100 +1 so that it didnt do 0 and it included 100. Havent used random in awhile but doesn't it go less then what it is set to?
r.nextInt(100);
would go from 0-99 would it not? Its been months since I used so yeah kinda forgot ;)
Was This Post Helpful? 0
  • +
  • -

#10 broncosfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-April 09

Re: fill the int array with random data

Posted 18 April 2009 - 12:53 PM

View PostFuzzyness, on 18 Apr, 2009 - 10:00 AM, said:

I did 100 +1 so that it didnt do 0 and it included 100. Havent used random in awhile but doesn't it go less then what it is set to?
r.nextInt(100);
would go from 0-99 would it not? Its been months since I used so yeah kinda forgot ;)

you are correct. This works:
public void fillArray()
   {
		/*fill the array with random numbers (int) in the range 0 - 100.*/
		Random random = new Random();

   	for (int i = 0; i < stats.length; i++)
		
	  stats[i] = random.nextInt(100+1);
	}


Thanks for the replies and assistance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1