HI there was wondering if anyone can help me with this question please. been stuck with it for a while.
2. Add code to the following main method which which shuffles the variables' values so o first ends up being equal to 99, o next ends up being equal to 1, and o last ends up being equal to 2. You are only allowed to use =, the four int variables (first, next, last and newInt) and semi-colons. Nothing else, including for example math operators, numbers, or other variables: public class Test {
public static void main(String[] args) { int first=1, next=2, last=3; int newInt=99;
I won't do it for you, but I'll try to help you see how to do it yourself.
First, notice that only three of the four original values are accounted for when you are finished: 1, 2 and 99. It doesn't matter what happens to the 3 that is currently stored in last. The following code will assign the value stored in second to last.
last = second; //Now both last and second store 2!
As I noted, both of those variables now have the same value stored, so it doesn't really matter if you overwrite the value stored in second! It needs to store the value that is currently in first, which in turn needs the value that is stored in newInt. When you are done, you will have two variables with the same value, but that is OK.
Hopefully, you see how to finish this yourself. Good luck!
> Twc Thx for the post. I get it in a way and dont' get it > in another way. > > this is what i get from the code you posted. > > last = second; //now both last and second store 2! > > last = first; //now both last and first store 1!
Yes, but you don't want last to store 1, you want next to store 1!
> > newInt[] = first; //now both first and newInt store > store 3! >
Two problems here. Firstly, you want newInt, not newInt[]. The [] makes newInt an array. Secondly, you are supposed to get first to store 99, not to get newInt to store 3!. Right idea, wrong order.
This is quite easy. More importantly, think about how you would generalize this algorithm for use in shuffling the values in an array. 3. Fill in the two blank lines in the main method so the following output is produced: 4. FOLLOWING EXCEPTION THROWN: java.lang.ArrayIndexOutOfBoundsException: 10 5. STACK TRACE: 6. java.lang.ArrayIndexOutOfBoundsException: 10 7. at Test.main(Test.java:11) 8. 9. RETRYING WITH PROPER INDEX ... 10. 10 public static void main(String[] a) { int[] array = new int[10]; array[array.length-1] = 10;
publicclass Test {
publicstaticvoid main(String[] args) {
int first = 1;
int next = 2;
int last = 3;
int newInt = 99;
System.out.println("First: " + first + "\nNext: " + next + "\nLast: " + last);
System.out.println("\nSwitching values...");
//Switch the values
last = next;
next = first;
first = newInt;
System.out.println("\nFirst: " + first + "\nNext: " + next + "\nLast: " + last);
} //main()
} //Test
Holy man. now i'm really confuse i can't use those fancy wording. they only allow me to use:
You are only allowed to use =, the four int variables (first, next, last and newInt) and semi-colons. Nothing else, including for example math operators, numbers, or other variables...
but thx that really impressive.
can someone tell me what program you all use to make it please. Java or C++???? if its Java what is it call? thx