Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

homework assignment for writing program question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Im stumped on this homework assignment. I could really use some help. Heres the problem;
Below is an almost completely working program. The body of the isFound method has been removed, and your job is to complete the program, compile and run it, and hand-in a print out of your completed working source code. Keep in mind that this program must be writted in a source file named prog1.java since the class name is prog1.

//Program Starts here
import java.util.*;

public class prog1
{
//Integer array
static int[] data = new int[50];

public static void main(String[] args)
{
// fill array with random numbers
Random r = new Random();
for(int i = 0; i < data.length; i++)
{
data = r.nextInt();
}

// print out 10 true/false value depending if the random number is found
for(int i = 0; i < 10; i++)
{
System.out.println(isFound(r.nextInt()));
}
}

//Method named isFound that takes an integer as a parameter
//and returns true if that interger is found in the integer
//array data, and false otherwise.

static boolean isFound(int val)
{

}
}
//Program Ends here

 
Hi Tammy351
There's a solution below that meets the bare-bones of the requirements. Note that there was a mistake in the main method, (assigning the result of r.nextInt() to the array rather than to a position in the array).

Hope this helps

//Program Starts here
import java.util.*;

public class prog1
{
//Integer array
static int[] data = new int[50];

public static void main(String[] args)
{
// fill array with random numbers
Random r = new Random();
for(int i = 0; i < data.length; i++)
{
data = r.nextInt();
}

// print out 10 true/false value depending if the random number is found
for(int i = 0; i < 10; i++)
{
System.out.println(isFound(r.nextInt()));
}
}

//Method named isFound that takes an integer as a parameter
//and returns true if that interger is found in the integer
//array data, and false otherwise.

static boolean isFound(int val)
{
int size = data.length;
for(int i =0; i < size; i++){
if(data==val){
return true;
}
}
return false;
}
}
//Program Ends here
 
and another in my listing of isFound ;->

Replace if(data==val) with if(data==val)
 
jm99,
You should really place your code within the TGML
Code:
[
Code:
code
Code:
]
tags. This will stop
Code:
[i]
from being interpreted as a request to make all successive fonts italic
Cheers, Neil
 
Wherever you see:
[tt]data = xxx[/tt]
or:
[tt]data == xxx[/tt]
Replace this with:
Code:
data[i] = xxx
or:
Code:
data[i] == xxx
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top