Stephen-Austin Murphy

Greenhorn
+ Follow
since Mar 11, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
17
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephen-Austin Murphy

Thanks, Winston and Raymond. It makes more sense now.
10 years ago
Hi,

Silly question, but I need to ask it.

Given the following code:

The output of this is:
120
121
127

Of course what happens is that control hits the test for 120 and prints 120. Because there is no "break" associated with that case, then flow continues with the 121 and 127 tests, exiting with the break in the 127 case.
But it is counter-intuitive that the println's associated with 121 and 127 would actually execute. It seems more logical that the makers of Java would have decided that statements associated with non-matches would not execute. So in other words, in this example, 120 does not match 121 so it is more logical that the println("121") should not execute.

So I'm wondering why the makers of Java would allow a switch statement to behave this way. Does anyone have any thoughts?

I hope that made sense. (Ravings of a mad man )

Kind regards,
Steve




10 years ago
Thanks, Matthew.

I modified my code as follows and it works fine:



At first it puzzled my why you have to both pass the target array as an argument and also have it on the left side of the = sign in the statement, but then I read in the documentation:

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)



So apparently the method uses the passed in array to determine the type of the array to return, and also to determine if there is "room to spare" in the array.

Thanks again -- much appreciated!
SA
When I try to execute the code below I get an IllegalClassCastException on line 7. Can anybody explain why the cast is not allowed?



Thanks.
I'm working my way through the programmer level one tutorial at the Oracle website. Although the tutorial is good, it's usually just a beginning -- a way to know what topics will be on the exam. I've been supplementing my learning with exercises from the Deitel Java book. (Thanks, Deitels -- it's great.) For the two List implementations mentioned in the tutorial (ArrayList and LinkedList) the book doesn't seem to be really focused. I need some challenging assignments for these two implementations to really make concepts sink in. I've searched the web but everything is in the larger context of generics. Can anyone recommend a resource (preferably web) where I can find assignments specific to ArrayList and LinkedList?

Thanks in advance.
Thank you everyone. Yes, my problem was that I was confusing floating point division and integer division.

I am clear on this now.

Kind regards
10 years ago
Thanks everyone who replied.
<red-face>OMG, I can't believe I had forgotten some basic math.</red-face>

My understanding of the % operator is that it returns the remainder after division.

Here was my reasoning that led to my post:
---------------------------------------------------------------------------------------------------------------
So 1 mod 10 is the remainder of 1 divided by 10.
1 divided by 10 is .1 with no remainder.
---------------------------------------------------------------------------------------------------------------
But just now I actually did the long division:
1 divided by 10 is .1 with .9 as a remainder. So Java rounds to 1.
Similarly, 2 divided by 10 is .2 with 1.8 as a remainder. So Java rounds to 2.
Is the above correct?

Thanks.
10 years ago
The following code is yielding non-zero results. I thought that 10% any number less than 10 and >=0 should be zero.




Following is output:




Thanks
10 years ago
Hi Roel and Mala,

Thank you so much for responding to my query and providing such helpful responses.

I really like the helpfulness and friendliness of both of you, and of this forum in general.

Mala, I saw your book while researching Java study books. Looks really nice!

Regards,
Steve
I'm not being lazy, but I would like to know:

I'm studying for the OCA and I'm trying to determine how much fact memorization I have to do.

Does the OCA ask questions like "which of the following are the relational operators" and "which of the following are equality operators"?

Thanks,
Steve