Friday, June 8, 2018

Hey, Mom! Talking to My Mother #1068 - CODING CHALLENGE #1: A Java Puzzle with Switch statements




Hey, Mom! Talking to My Mother #1068 -  CODING CHALLENGE #1: A Java Puzzle with Switch statements





















Hi Mom,

Pesky coding questions in interviews got you down?

So I bought this book for KINDLE. It sold me on the promise of lots of coding questions and their answers in Java.

I claim to know Java well enough to say that it's a skill of mine, that I know it with some fluency, but is that true? I figure this book will tell.

So, I figured this book would be a good thing for task switching. If I want to take a work break, I can focus on some of the questions in the book, which are like puzzles.

First questions, no problem.

Second question? I was stumped. It's not that I did not understand the code. I understood the code. But it did not produce the output in my mind that the authors shared in the answer.

I even tried running it in an IDE. Ultimately, I managed to do that (had trouble with Eclipse, which is no surprise), and then I got the same answer.

And then, I saw it.

Can you see it?

Here's the code. It's Java.


Public class SwitchTrial
{
final static short caseVal = 2;

public static void main (String[] args)
{
for (int iterNo=0; iterNo < 3; iterNo++)
{
switch(iterNo)
{
case caseVal: System.out.print("a ");
case caseVal-1: System.out.print("b ");
case caseVal-2: System.out.print("c");
}
}
}

Answer: c bc a b c

I did not see that answer at all, and then I noticed. No "break" statements in the switch.

And because there's no "break" statements, it goes to which ever is true first and then executes the rest, so

c is output in the first iteration because 2-2=0 and iterNo is 0, and then b c is printed next in the second time through because 2-1 = 1 and then no break so the next runs and prints c. Finally, all three are printed because now the first case is true because caseVal = 2 and iterNo = 2, so a is printed but then b and c print, also, because there's no break statements exiting the switch after a prints.

That's our puzzle today.

I know that yesterday I promised my Kotlin post to my one lone and sole reader, you, Mom, but I was too busy today to finish it. I have pushed it back to Sunday because tomorrow's is all set as I am volunteering at an all day Ultimate tournament. But I might have to push the Kotlin post back farther to manage work, chores, and family stuff on Sunday.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Reflect and connect.

Have someone give you a kiss, and tell you that I love you, Mom.

I miss you so very much, Mom.

Talk to you tomorrow, Mom.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

- Days ago = 1070 days ago

- Bloggery committed by chris tower - 1806.08 - 10:10

NEW (written 1708.27) NOTE on time: I am now in the same time zone as Google! So, when I post at 10:10 a.m. PDT to coincide with the time of your death, Mom, I am now actually posting late, so it's really 1:10 p.m. EDT. But I will continue to use the time stamp of 10:10 a.m. to remember the time of your death, Mom. I know this only matters to me, and to you, Mom.

No comments:

Post a Comment