Junilu Lacar

+ Follow
since Feb 26, 2001
Junilu likes ...
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
Forum Moderator
Columbus OH
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Junilu Lacar

salvin francis wrote:1. If the code only wants to track the states and do nothing else with the lamp, use an array of enums:

This has a distinctly procedural feel to it.

Compare the semantics of this version that uses streams and a simple Lamp class:

1 hour ago

Run results:

1, 4, 9, 16, 25, 36, 49, 64, 81, 100,

10 lights are on
15 hours ago
Yes, they are the same. The difference is that the second version uses the "diamond operator" to tell the compiler to infer the type on the right-hand side of the assignment based on the type declaration on the left-hand side.

See https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html
4 days ago
If you're wondering whether an abstract class can also be a subclass, then yes, it can be a subclass. It doesn't matter either that an abstract class is a subclass of a non-abstract class. The point of an abstract class is to define an API that will eventually be implemented by a subclass. However, to Paul's point, Java does not perform any checks to ensure that there actually is a subclass defined for any class you have declared as abstract. The reason is the same as with interfaces. Again, the main reason for an interface or abstract class is to define an API or contract for use.

Try defining these classes in JShell:

4 days ago
It's because of type erasure: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

And I think you might have gotten it backwards there, Campbell. Generic types are used by the compiler to provide tighter type checking. Type erasure will replace the generic types with ordinary types (classes, interfaces) and methods, typecasts to preserve type safety, or bridge methods to preserve polymorphism. It's the runtime that has no idea of generics, so the error is the compiler basically saying: "Oops, when I apply type erasure these two methods turn out to have the same exact signature. That's not going to work for the JVM."
4 days ago

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . "Java is to Javascript as Car is to Carpet"

No, it's, “Java is to JavaScript as Ham is to Hamster.”

When I start typing "Java is to Javascript as" in the Chrome browser, both come up but "Car is to Carpet" is the first suggestion, followed by "Ham is to Hamster"  
5 days ago
Use the right tool for the job. Check out TreeMap instead of HashMap. In particular, read the documentation for the constructor that takes a Comparator as its argument.
5 days ago
They say this about these two languages in question:
"Java is to Javascript as Car is to Carpet"
5 days ago
Hmmm... rolling your own method for this is unnecessary. See String indexOf(String str)

Edit: ok, I see now what OP said about "order does not matter"...
5 days ago

wayne brandon wrote:But he said that isn't necessary and had instructed the students that asked that three methods was enough

I'll tell you what's way more than enough: paying $500 to have a teacher who says something like that. WTH. Sorry, but No. Just NO.

He should be encouraging his students to break long methods into smaller ones. He should be encouraging you to find ways to eliminate duplicated logic. All those loops you have for checking for correct input is duplicated logic that makes your program more difficult to work with and more inviting to inconsistency and bugs.
6 days ago

Carey Brown wrote:

wayne brandon wrote:...but we are only allowed to use 3 methods

<shakes head sadly>

That is so bad. Don't let this instructor mold you into exactly the kind of programmer we do NOT need more of in this world: one who just takes "specifications" like that and follows them blindly.

Writing code is design and as such requires you to think and constantly adapt as your ideas get put into executable code. The requirement to use only three methods forces you into writing code that is horrifically long, unreadable, and difficult to work with, including the important tasks of testing and debugging.
6 days ago
Also, read through the String class API documentation: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

You'll find all the methods you need to call to do the things your instructor's output screenshot shows.

Good luck.
6 days ago

I wrote:... using the type system and object-orientation to make decisions and selections. Think more functional.

I realize this might seem contradictory, to use object-orientation, you have to think more functional. This may not fit the purists' definition of functional style programming but it's close enough for me.

Imagine being able to just write code like this:

Instead of having an imperative style selection to decide how to switch the value of currentPlayer, you call a "function" on the object that calculates that for you. And because one object's behavior can be different from another object's behavior for the same method, you are also using OO and arguably, polymorphism as well.
6 days ago

Winston Gutkowski wrote:

Junilu Lacar wrote:What if you had to implement some conditional logic but couldn't use any if statements. How would you do that?

Well, one thing I remember from my early days is that
x ^ x (and ONLY x) == 0
... probably far below the "objective" level you're talking about. :-)

Yeah, I was referring to something else. What you show is still very much at the nuts-and-bolts level.

Take for example a game of TicTacToe. This is pretty typical code for checking a row for a winner:

You can, of course, eliminate the if statement here:

This is just one way of avoiding if statements in your code. It's probably obvious to more experienced programmers but not so obvious to novices. But even this is at a lower level than what I was alluding to earlier.

Here's some more code you might typically see:

Now, think of how you might figure out how to switch turns from player X to player O and vice versa without using an if statement. And the ternary operator ?: is still considered an if-statement, by the way. Note that the currentPlayer variable here is assumed to be an enum type. This gets closer to using the type system and object-orientation to make decisions and selections. Think more functional.

There are other ways of eliminating if-statements and other selection control structures in your code, like replacing conditionals with polymorphism.
1 week ago