Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm new to java and I need help completing the question below. If anyone could help me with a simpler solution of this question. I tried to provide a working solution but I got stuck at number #10.

1.Create a new file called Rebel.java;

2.Create two arrays. One of them must store integer numbers and the other one must store words. Each of them must be able to store 5 values;

3.Fill the first array with numbers from 1 to 5 and the second one with numbers from 6 to 10; Note: The words array should contain the value in the form of a string and not the actual word i.e -> words[1] = "1";

4.Swap the numbers in the first array with the ones in the second one (swap numbers with the same index);

5.Multiply all the numbers in the first array by 4;

6.Multiply all the numbers in the second array by 3;

7.Print out the contents of both arrays.

8.Swap all the numbers in the first array with the ones in the second one but this time only swap the numbers at even positions.

9.For every number from 1 to 40 print out if it is in the array that stores numbers, the one that stores words or none of them.

10.For every number in the numbers array:
•Print it out;
•Print out the positions of the divisors it has in numbers array.
•Print out the positions of the divisors it has in words array.

11.Repeat #10 for the words array.

Here's my code:

Java
public class Rebel
{  
     public static void main(String [] args)  
     {

         int[] numbers = {1,2,3,4,5};
         String[] words = {"6","7","8","9","10"};



      numbers[0] = 6;  

      System.out.println("\nThe values in the numbers are:");
      for(int x = 0; x<numbers.length;>      {
         numbers[x] = numbers[x] * 4;
         System.out.println(" " + numbers[x]);
      } 

      words[0] = "1";

      System.out.println("\nThe values in the words are:");
      for(int x = 0; x<words.length;>      {
         int wordsAsAnInt = Integer.parseInt(words[x]);
         wordsAsAnInt = wordsAsAnInt * 3;
         System.out.println(" " + wordsAsAnInt);
      }

      numbers[0] = 6;
      words[0] = "1";
      numbers[2] = 8;
      words[2] = "3";
      numbers[4] = 10;
      words[4] = "5";

      System.out.println("\nThe values in numbers from 1 to 40 are:");
      for(int x = 0; x<numbers.length;>      {
         if(numbers[x] == 1 || numbers[x] < 40)
         {
            System.out.println(" " + numbers[x]);
         }
      }

      System.out.println("\nThe values in the numbers are:");
      for(int x = 0; x<numbers.length;>      {
         System.out.println(" " + numbers[x]);
      } 
   }

}
Posted
Updated 21-Jul-15 8:49am
v3

1 solution

Hi there,
as far as i could read that code i'd say you lost your self on #8, you should swap the numbers between arrays ? So why do you insert values instead of a swap from array to array?

Number 9 is wrong because you should print if number is in numbers array, text array or in none of them. Your idea is correct but i'd suggest this change in code

C#
for(int i = 0; i < 40; i++)
{
    bool found = false;
    for(int x = 0; x < numbers.length; x++)
    {
        if(i == numbers[x]) {System.out.println(i + " is in numbers"); found = true;}
    }
    for(int x = 0; x < words.length; x++)
    {
        //Do a conversion from string to int!
        if(i == (int)words[x]) {System.out.println(i + " is in words"); found = true;}
    }
    if(!found) {System.out.println(i + " is in none of the arrays");}
}


#10 is something i don't understand so far, is your homework to show the divisors of every number in the array or do you have to print out the divisor of the last array number which is in the array?
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900