Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion K&B7 OCA Self Test Chapter 1, Question 3

 
Mark Kevin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
13

This is my first post, so please forgive me if I'm doing something wrong. I'm working through the self-test of K & B's OCA 7 book but am stumped on the third question in Chapter 1, page 76. The answer to the code snippet B. is A; in other words, running this piece of code, which I'm including below, is supposed to print out the letter A. I'm using JGrasp, not the command line, but that shouldn't make any difference as I simply switched out the __A_V_ array from main() and used a String.
The problem seems to be the original loop increment, in my confused mind. Here is the original:


When I run it I get nothing printed out, which is what is inside the local String variable $. And that makes sense because x is incremented from 0 to 1 and then tested against an array with length of one. Well, if I'm only typing in one letter, A, then that is the first element in my array at index 0 (or the first character in my String). Increment that to 1 and test, then it must fail because 1 is not less than the length of the array, 1, so nothing is assigned to $ which is the next statement which is therefore skipped, (and, which would result in an indexOutOfBoundsException I would think;) it therefore prints nothing instead of the letter A.

What am I getting wrong here? Instead of a command line (I'm using barebones JGrasp) I used a String variable instead of the main() args array but that shouldn't make any difference. So I entered A, but shifted the incrementor ++x to the right, and tested if x (which has a value of 0) is less than the length of the String A, which it was, and it printed out the A just fine. Just not when the ++x counter variable is incremented to one (1) for use in the less-than test. Does that make sense?


So my point is, the way it's written in the book, it won't print out the letter A, just nothing. And the answer can't be A.

This is what I wrote and it worked.



Here is the old version. Prints nothing.



Going crazy, here. I thought I knew some of this stuff.

Mark
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark Kevin,

First of all, a warm welcome to CodeRanch!

Mark Kevin wrote:This is my first post, so please forgive me if I'm doing something wrong.

You posted your question in the appropriate forum, so nothing wrong at all Just one recommendation: always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I've gone ahead and added the code tags for you. See how much easier the code is to read?

Mark Kevin wrote:I'm working through the self-test of K & B's OCA 7 book but am stumped on the third question in Chapter 1, page 76. The answer to the code snippet B. is A; in other words, running this piece of code, which I'm including below, is supposed to print out the letter A. I'm using JGrasp, not the command line, but that shouldn't make any difference as I simply switched out the __A_V_ array from main() and used a String.
The problem seems to be the original loop increment, in my confused mind.

The correct answer (as mentioned in the study guide) is B, so class _ (a weird but legal name for a class) will print A. But you'll only get this output if you execute java _ - A . on the command line, so you execute class _ and pass three arguments to the program: -, A and . So you could simulate this command line invocation by adding this line of code as the first line of the main method

Mark Kevin wrote:When I run it I get nothing printed out, which is what is inside the local String variable $. And that makes sense because x is incremented from 0 to 1 and then tested against an array with length of one. Well, if I'm only typing in one letter, A, then that is the first element in my array at index 0 (or the first character in my String). Increment that to 1 and test, then it must fail because 1 is not less than the length of the array, 1, so nothing is assigned to $ which is the next statement which is therefore skipped, (and, which would result in an indexOutOfBoundsException I would think;) it therefore prints nothing instead of the letter A.

Your understanding is almost correct. So if you have an array with just 1 element, its length is 1. Now because x is pre-incremented, its value is 1 as well. So you are still spot-on. Now x (with value 1) is tested if it's less than the array's length (also 1). Because 1 is not less than 1, the for loop is exited (and that's where you are wrong). Because the condition fails, the loop is exited and thus the statement in the loop body (here just one: $ += __A_V_[x];) are not executed anymore. And that's why you don't get an indexOutOfBoundsException.

Hope it helps!
Kind regards,
Roel
 
Mark Kevin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you, Roel. I understand now. I was entering one character, the letter A, but they wanted three characters for the array,
"-", "A", and "." , which makes perfect sense. Thanks for clarifying all of that, and cleaning up my code. I ran it and it works just fine.

I'll format my code properly the next time.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic