Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: I need help in this coding

1. I need help in this coding

Hello I am new in java. been working on it for 2months. I need to solve this by next week try going to youtube to learn but could not figure it out. Any help from the java community would be great.

Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn’t 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Then, the final array should be printed as output. There is no user input for this program.

Your program must include, at least, the following methods:

• insertNumbers, which will take as input one integer array and store the random numbers in it.
• createTremor, which will generate the random number as the location and return it.

A sample run of the program is shown below:

Sample output #1:
Array: 1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array: 1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1

2. Re: I need help in this coding

What have you tried so far? Post your effort and any problems your having

3. Re: I need help in this coding

Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

To help you, we need to see what you've been doing for 2 months with any specific questions, problems, or errors you'd like help with.

4. Re: I need help in this coding

Hello sorry this is all I understand about array.

for this part of the question Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

my code is

int array[]= new int[20];

array [1]= 3;
array [2]= 4;
array [6]= 1;
// and so on....

how do I make a array which is able to support it randomly rather than typing them out from 1 - 5

help..

5. Re: I need help in this coding

You probably meant that you've been studying Java for 2 months, not that you've been working on this problem for two months. Sorry if I interpreted your meaning incorrectly.

As for how to do what you've asked, use a loop to iterate the array:

```for ( int i = 0 ; i < myArray.length ; i++ )
{
// assign values to the array using statements like:
myArray[i] =
}```
And read up on generating random numbers by reviewing the Random class and the Math.random() method. Choose whichever one of those meets your needs the best, though I recommend using Random.

6. Re: I need help in this coding

This is my code so far am I in the right track I am really confused about this pls help me

int insertNumbers[]= new int[20];

insertNumbers[0]= 0;
insertNumbers[1]= 1;
insertNumbers[2]= 2;
insertNumbers[3]= 3;
insertNumbers[4]= 4;
insertNumbers[5]= 5;
insertNumbers[6]= 6;
insertNumbers[7]= 7;
insertNumbers[8]= 8;
insertNumbers[9]= 9;
insertNumbers[10]= 10;
insertNumbers[11]= 11;
insertNumbers[12]= 12;
insertNumbers[13]= 13;
insertNumbers[14]= 14;
insertNumbers[15]= 15;
insertNumbers[16]= 16;
insertNumbers[17]= 17;
insertNumbers[18]= 18;
insertNumbers[19]= 19;

int createTremor = 1 + (int)(Math.random()*5);

System.out.println(" " +insertNumbers[createTremor]);

}

}

7. Re: I need help in this coding

[code=java]
[/code]
to get highlighting and preserve formatting.

8. Re: I need help in this coding

```public static void main(String[] args) {
// TODO code application logic here
int randNum = 0;
int arr[] = new int [20];
System.out.print("Array: ");
insertNumbers(arr);
System.out.println(" ");
int pos = createTremor(num);
}
public static void insertNumbers(int arr[])
{
for(int i=0; i<arr.length;i++)
{
int randNum=(int)(Math.random()*5)+1;
arr[i]=randNum;
System.out.print(" " + randNum);
}
}
public static int createTremor(int num[])
{
int pos=(int)(Math.random()*20);
return pos;
}```

I also need help with this question. This is my code so far. But it cannot display the desired output.

9. Re: I need help in this coding

How was trying to figure just about the same thing, only it is for MD5 hash and it is a series of random numbers or letters. Key/values that goes together with an object. When the object is valued with a value, it is then hashed to an email. As you already know this is an interface that deals with a table of values. Just working it on NetBeans 8.0 to see if I can integrate the object into other framework structures. (or someone's web page)

Anybody know where I can find more information on this fascinating topic? Thanks a lot! Same type of problem as above, only more complex.

10. Re: I need help in this coding

```
public static void main(String[] args) {
// TODO code application logic here
int randNum = 0;
int arr[] = new int [20];
System.out.print("Array: ");
insertNumbers(arr);
System.out.println(" ");
int pos = createTremor(arr);
System.out.println("Random position: "+ pos);

if(randNum == 0)
{
arr[1] = 0;
arr[2] = 0;
arr[3] = 0;
}
else if (randNum == 1)
{
arr[0]=0;
arr[2] = 0;
arr[3] = 0;
arr[4] = 0;
}
else if (randNum == 2)
{
arr[0]=0;
arr[1] = 0;
arr[3] = 0;
arr[4] = 0;
arr[5] = 0;
}
else if (randNum == 19)
{
arr[16] = 0;
arr[17] = 0;
arr[18] = 0;
}
else if (randNum == 18)
{
arr[15] = 0;
arr[16] = 0;
arr[17] = 0;
arr[19] = 0;
}
else if (randNum == 17)
{
arr[18]=0;
arr[19] = 0;
arr[16] = 0;
arr[14] = 0;
arr[15] = 0;
}
else
{
arr[randNum+1] = 0;
arr[randNum+2] = 0;
arr[randNum+3] = 0;
arr[randNum-1] = 0;
arr[randNum-2] = 0;
arr[randNum-3] = 0;
}
}
public static void insertNumbers(int arr[])
{
for(int i=0; i<arr.length;i++)
{
int randNum=(int)(Math.random()*5)+1;
arr[i]=randNum;
System.out.print(" " + randNum);
}
}
public static int createTremor(int num[])
{
int pos=(int)(Math.random()*20);
return pos;
}```

I edited my code again. But the output is wrong.

11. Re: I need help in this coding

the output is wrong.
Please post the output and explain what is wrong with it and show what the output should be.

12. Re: I need help in this coding

I GOT IT!
```public class PRSPAssignQn3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int arr[]=new int[20];

System.out.print("Array: ");
insertNumbers(arr);

for(int x:arr)
System.out.print(" " + x);

System.out.println(" ");
createTremor(arr);

System.out.print("Final Array: ");
for (int y: arr)
System.out.print(" " + y);
}

public static void insertNumbers(int arr[]){

for(int i=0;i<arr.length;i++){
int num=(int)(Math.random()*(5))+1;
arr[i]=num;
}
}

public static void createTremor(int num[]){
int RandomPos = (int)(Math.random()*(20))+0;
System.out.println("Random Position: " + RandomPos);

for (int i=0;i<num.length;i++){
if (RandomPos == 19){
num[RandomPos-3]=0;
num[RandomPos-2]=0;
num[RandomPos-1]=0;
}
else if (RandomPos == 18){
num[RandomPos-3]=0;
num[RandomPos-2]=0;
num[RandomPos-1]=0;
num[RandomPos+1]=0;
}
else if (RandomPos == 17){
num[RandomPos-3]=0;
num[RandomPos-2]=0;
num[RandomPos-1]=0;
num[RandomPos+1]=0;
num[RandomPos+2]=0;
}
else if (RandomPos == 2){
num[RandomPos+3]=0;
num[RandomPos+2]=0;
num[RandomPos+1]=0;
num[RandomPos-1]=0;
num[RandomPos-2]=0;
}
else if (RandomPos == 1){
num[RandomPos+3]=0;
num[RandomPos+2]=0;
num[RandomPos+1]=0;
num[RandomPos-1]=0;
}
else if (RandomPos == 0){
num[RandomPos+3]=0;
num[RandomPos+2]=0;
num[RandomPos+1]=0;
}
else {
num[RandomPos+3]=0;
num[RandomPos+2]=0;
num[RandomPos+1]=0;
num[RandomPos-1]=0;
num[RandomPos-2]=0;
num[RandomPos-3]=0;
}
}
}
}```
And the output is correct.