Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help from a good instructor  RSS feed

 
Craig Cantell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I'm new to this website. I'm in computer science 1 and we are working in Java Eclipse. I had no time to ask my teacher today how to solve the lesson were on with, Arrays. If someone could give me a detailed explanation on how to solve this I would really appreciate it.
//What will the code below print?

int arry[] = new int[5];

arry[0] = 5;
arry[1] = 3;
arry[2] = 1;

for(int i = 0; i < arry.length; i ++){
arry[i] = i;
}

System.out.print(arry[2]);
a. 0
b. 3
c. 1 (wasn't the answer)
d. 2
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

We encourage students to do their own homework. If you're working in Eclipse, why don't you just run the program and see for yourself?
 
Craig Cantell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Welcome to the Ranch!

We encourage students to do their own homework. If you're working in Eclipse, why don't you just run the program and see for yourself?


I want to independently do my homework but I don't understand how to evaluate it mentally or by hand, we have tests and we cannot run the text through the program. I just need someone to explain the process to me please.
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

That is not an excuse. Apart from the fact you didn't ask your tutor, you still should have an ability to look up on the slides or in recommended course book (should be some in your institution library). Or you can go through Oracle Java Tutorial, you could learn a lot from there.

Never mind, examine next example:
Output: 4

Try to analyse and think how that could be calculated.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you know how a for-loop works. If not, you can find information here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

To hand trace code like this:

You would set up a trace table similar to the example below. You fill in the ?? and the rest of the values of i. Each row will reflect values/output for one iteration of the loop.

Hand trace:
 
Craig Cantell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the example! I was not thinking right, realizing this now; this programming is a whole different type of mindset you have to get used to.

Thank you to all that helped!
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Cantell wrote:
Junilu Lacar wrote:Welcome to the Ranch!

We encourage students to do their own homework. If you're working in Eclipse, why don't you just run the program and see for yourself?


I want to independently do my homework but I don't understand how to evaluate it mentally or by hand, we have tests and we cannot run the text through the program. I just need someone to explain the process to me please.


You are correct that the code you posted will not run if copied directly into Eclipse, however you can create a simple test class and paste the code into the main method and run it.

 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel, I think OP meant they were not allowed to actually run the code but rather they have to trace the program execution by hand as a learning exercise.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!