views:

83

answers:

3
public class Main {

    public static void main(String[] args) {
     int[] x = {1, 2, 3};
     increase(x);
     Reverse rev = new Reverse();
     rev.reverse(x);
     System.out.println(x[0] + " " + x[1] + " " + x[2]);
   }

   //Increase every element in the array by 1
   //For example: array : 0, 1, 2 will become 1, 2, 3
   public static void increase(int[] a) {
       //TODO: FILL ME
   }
}

class Reverse {
     //Reverse the array
     //For example: array 0, 1, 2 will become 2, 1, 0
     public void reverse(int[] a) {
           //TODO: FILL ME
     }
}

What does increase(x); do ? Basically I have to fill in where he wrote fill in. But its kind of hard when i dont even understand what is going on.

A: 

The statement increase(x) calls the static method increase(int[] a).

As the method just contains a comment, it effectively does nothing. Looks like your tutor wants you to replace the //TODO: FILL ME comment with code that implements the increase method correctly. Your tutor has described what the code should do in the comments above the method;

//Increase every element in the array by 1
//For example: array : 0, 1, 2 will become 1, 2, 3

Good luck!

Brabster
hm.... ookay icic so its equivalent to a comment
CuriousStudent
yep, // starts a comment.
Brabster
hm..im still stuck can you help me out even more
CuriousStudent
You just need to do what your tutor has indicated; in the increase method, increment the int values in the array you are passed as the parameter. In the reverse method, reverse the order of the elements in the array you are passed. Aside from actually writing the code for you, I'm not sure how much more help I can give?
Brabster
public static void increase(int[] a) { int i = 0; for ( a[i] = a[0]; a[i] < 3; i++ ); { System.out.println(a[0] + " " + a[1] + " " + a[2]); } am i on the right track?
CuriousStudent
aiyahh oh well i give up thanks tho. i'll understand it one day lol gooooood night
CuriousStudent
+1  A: 

For now increase() does nothing for now.
Your assignment is to write the content of the method in a way that will increase the content of every cell of your array.

What does your code do?

 int[] x = {1, 2, 3}; // Create an array with 3 elements, "1", "2" and "3"
 increase(x); // Call the increase() method which for now does nothing
 Reverse rev = new Reverse(); // Create an instance of the Reverse class, which contains a method to reverse arrays (but does nothing for now)
 rev.reverse(x); // Call the famous reverse() method.
 System.out.println(x[0] + " " + x[1] + " " + x[2]); //Print the content of you array x[0] is the first cell, x[1] the second, etc.
Colin Hebert
hmm...so i guess i have to use it i was just going to do the easier way and assign new arrays but i guess i have to find a way to use it
CuriousStudent
You can't create new arrays, all you have to do is loop through your array (you have already seen that I hope) and do an operation to increment the content of the cell.
Colin Hebert
hm..does it matter that there's 2 public static void? thats the first time I see it so far. Whats the benefit of having 2 versus doing it all under one like my other homework before?
CuriousStudent
It's isn't a problem as long as the two methods don't have the same *prototype*. There is only one `main()` and it is your entry point the other is just another method called in your application.
Colin Hebert
so increase(x) should have meaning in the code that im suppose to write?
CuriousStudent
i can do System.out.println(a[1] + " " + a[2] + " " + (a[1]+a[1]));lol it gives me the result but i know for sure that is not what he wants me to do lol
CuriousStudent
http://www.roseindia.net/java/java-get-example/java-clone-method.shtml am i suppose to do it like that?
CuriousStudent
No you're just suppose to modify directly the array you get in parameter
Colin Hebert
but i thought that when you modify whatever is in the parameter you essentially modify both x and a since they both point to the same reference?
CuriousStudent
exactly, that's the point. As your increase method doesn't return anything you have to handle the modification directly on the array.
Colin Hebert
public static void increase(int[] a) { int i = 0; for ( a[i] = a[0]; a[i] < 3; i++ ); { System.out.println(a[0] + " " + a[1] + " " + a[2]); }
CuriousStudent
i just ended up doing that...its probably wrong but ...a for effort i guess
CuriousStudent
instead of doing a System.out, you have to increment the value of a[i] :)
Colin Hebert
A: 

What you want to do is in the increase method, iterate through all the values in the integer array "a" using a for loop. You should then assign an integer that's 1 more than the current integer to itself.

I'm so tempted to post a solution but that'll teach you nothing!

Adam
OH the deadline already passed so you can lol at least this way I can learn it. The tutor isn't going to go over until the next lecture
CuriousStudent
so i'm guessing that you never got to do it.
Adam
well i did it..just not the correct way
CuriousStudent