Help please
Does anyone happen to know the answer to this question?
Assume that the program below is invoked with the following statement: java mystery 1 2 3 4 11 360 108
public class mystery {
public static void main(String args[]) {
int x[] = new int[args.length - 3];
int y[];
int w = 1;
y = x;
y[y.length-1] = Integer.parseInt(args[4]);
for (int i:x)
w *= i;
System.out.print(w);
}
}
What does the above program print?
Hope i have enough info, thanks in advance.
# 1
Did you try running the program?
# 2
It is a question on one of my worksheets for a Java class.I have tried to run the program but dont know where to add in the numbers that are "invoked". I hope that makes sense.
# 3
Read this tutorial about commandline arguments ("args"): http://java.sun.com/docs/books/tutorial/essential/environment/cmdLineArgs.htmlIt has the answer.
# 4
Is there some code missing? Because I don't see anywhere that values get stored in the x array.
# 5
> Is there some code missing? Because I don't see
> anywhere that values get stored in the x array.
Well, an int array will be initialised with 0 values. But, since y and x reference the same array, the last "field" will get the fifth command line argument. But, since 0 times anything is 0, the program will print 0. At least that is my thought about it, from just a quick look at the code.
I have been known to be wrong before though.
Edit:
Then, again, hopefully you provide at least five arguments, with the fifth being a number or your in trouble.
# 6
> Then, again, hopefully you provide at least five> arguments, with the fifth being a number or your in> trouble.It looks like hes providing seven arguments. So it would just end up doing 1*2*3*4.
# 7
> > Then, again, hopefully you provide at least five
> > arguments, with the fifth being a number or your
> in
> > trouble.
>
> It looks like hes providing seven arguments. So it
> would just end up doing 1*2*3*4.
Am I wrong in assuming that at least the first three elements in the array will be 0, thereby making the result 0 regardless of what comes after since he does w *= i in the loop which will result in 0 the first time, which, in turn, would produce 0 on all subsequent equations?
Maybe he didn't copy all the code, but from the code provided, he creates an int array (should be initialized with 0 values, right?) and only sets a value to the last element using the fifth command line argument.
# 8
Hi,
You will not be able to run this code, because of many factors.
1) you need to initalize the class mystery m=new mystery{};
2) there is a syntax error for (int i:x) i believe that colon should be replaced with equal sign.
3) arguments have to be input somehow for the program to read them.
your number of arguments is 7, basically the number of those given numbers.
int x[] = new int[args.length - 3];
this line assigns 1, 2, 3, 4 to X
y[y.length-1] = Integer.parseInt(args[4]);
this line means that y at position 3 has a value=11 becasue 11 is the fourth argument Integer.parseInt(args[4]);
for (int i:x) this line is wrong.
I think it was meant to loop x times and print W*W
Hope helps.
N
# 9
A little correctiony[y.length-1] = Integer.parseInt(args[4]);this line means that y at position 3 has a value=11 becasue 11 is the fourth argument Integer.parseInt(args[4]); and assigns 11 to y at position 3. so now y[3]={11}Nosh
# 10
> Hi,
>
> You will not be able to run this code, because of
> many factors.
> 1) you need to initalize the class mystery m=new
> mystery{};
No he doesn't, first clue that this poster is clueless!
> 2) there is a syntax error for (int i:x) i believe
> that colon should be replaced with equal sign.
Java 5 uses this syntax to loop thru arrays / collections
> 3) arguments have to be input somehow for the program
> to read them.
They are supplied on the command line
> your number of arguments is 7, basically the number
> of those given numbers.
>
> int x[] = new int[args.length - 3];
> this line assigns 1, 2, 3, 4 to X
>
What? this only declare taht x is an array of ints with a size of args.length- 3 (7 - 3 = 4)
> y[y.length-1] = Integer.parseInt(args[4]);
> this line means that y at position 3 has a value=11
> becasue 11 is the fourth argument
> Integer.parseInt(args[4]);
OK you sort of corrected this mistake in your next post
> or (int i:x) this line is wrong.
> I think it was meant to loop x times and print W*W
> Hope helps.
> N
NO, it was meant ot be as posted.
Please avoid posting answers when you don't have a clue. It only makes things worse for the OP
~Tim
Message was edited by:
SomeoneElse
Message was edited by:
SomeoneElse
# 11
Tim,Is your critisizm the answer to his problem??
# 12
> Tim,
>
> Is your critisizm the answer to his problem?
>
> ?
The answer was posted in earlier posts. Your answer/explanation was wrong on so many points that I felt it required correction. If he took your response as correct, he would be wrong, and I was just pointing that out to him.
It is great that you want to help, and God knows I have given some questionable answers. Don't take it too personally. Sorry if the criticism was a bit too harsh.
~Tim
# 13
Apology exepted.
We are here to help each other. Sorry if I gave wrong suggestions.
Peace made.
I just joined the Forum and I am trying to keep up with Java bc i have been out for a while. My job has made me concentrate in other stuff, so i am out of loop.
This explains why I wasnt familiar with (i:x).
You can explain me maybe the core fo this line.
Thanks and regards
NG
# 14
Since 1.5 came out, you can use this form of a loop to itereate thru arrays and collections
List<String> listOfStrings = Arrays.asList("Larry","Moe","Curly");
for(String s : listOfStrings)
{
System.out.println(s);
}
# 16
its a simple program...
that doesnt matter if the program references y[] array or x[] array....
the program sets the size of x[] array to 4... ( args.length - 3)...but the array is not initialised.. so the default value of the array is zero...
and the system.out statement prints w*=i; where i iterates x and since the value of x is 0, the program prints 0 :)