The Artima Developer Community
Sponsored Link

Java Answers Forum
Need Help please.

8 replies on 1 page. Most recent reply: May 11, 2004 5:53 AM by SRV001

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Lan

Posts: 5
Nickname: mitsulance
Registered: May, 2004

Need Help please. Posted: May 9, 2004 11:25 PM
Reply to this message Reply
Advertisement
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;

____________________________

____________________________

____________________________
}
}

Thank you so much.
i'm just new to this. thx


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Need Help please. Posted: May 10, 2004 5:47 AM
Reply to this message Reply
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!

Lan

Posts: 5
Nickname: mitsulance
Registered: May, 2004

Re: Need Help please. Posted: May 10, 2004 11:22 AM
Reply to this message Reply
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!

newInt[] = first; //now both first and newInt store 3!

please correct me if i'm wrong thx.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Need Help please. Posted: May 10, 2004 11:58 AM
Reply to this message Reply
> 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.

twc

Lan

Posts: 5
Nickname: mitsulance
Registered: May, 2004

Re: Need Help please. Posted: May 10, 2004 12:28 PM
Reply to this message Reply
thank you again Twc.
I also have something can you proof read it for me please.. this problem that suck with not sure if i did it right.

thx again

Lan

Posts: 5
Nickname: mitsulance
Registered: May, 2004

Re: Need Help please. Posted: May 10, 2004 12:34 PM
Reply to this message Reply
Here is what i write:

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;

1) ErrorCodeType Test.main {
System.out.println(array[10]);
}
2) catch (ArrayIndexOutOfBoundsException exc) {
System.out.println("FOLLOWING EXCEPTION THROWN: " + exc);
System.out.println("STACK TRACE: ");
exc.printStackTrace();

System.out.println("\nRETRYING WITH PROPER INDEX ...");
System.out.println(array[array.length-1]);
}
}

the 2 area that in Bold are the answer i got. please double check please if you have time. thx.

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Need Help please. Posted: May 10, 2004 1:13 PM
Reply to this message Reply
public class Test {
    public static void 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

Lan

Posts: 5
Nickname: mitsulance
Registered: May, 2004

Re: Need Help please. Posted: May 10, 2004 1:32 PM
Reply to this message Reply
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

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Need Help please. Posted: May 11, 2004 5:53 AM
Reply to this message Reply
Lan,

take out the System.out.println()statements and that is
your answer. They were put there for your benefit.

Flat View: This topic has 8 replies on 1 page
Topic: Run a C program from Java Previous Topic   Next Topic Topic: Garbage Collection...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use