Ryan Beckett

Ranch Hand
+ Follow
since Feb 22, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
6
Received in last 30 days
0
Total given
7
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Ryan Beckett

This program works as it should.
A few things:



You don't need and shouldn't use an instance to invoke a static factory method (or any static method). It should be:



Static factory methods are used in patterns where you want object creation control. In you case, it appears that you want to use the singleton pattern: allow only one instance to be created and used. To do so, your method should be:



You were probably getting an exception in your servlet, because the object that was returned was a databaseConn and not a Connection, which has the method preparedStatement(). After those changes your code should be working or close to it. And, yes, it's probably not the best idea to try and implement your own pooling, especially when reliable solutions exist.
6 years ago
I initially thought 4 too. To show you that's it's not 3, replace the instantiation wit this:



If SelfTestParent.sayHello was overridden "Child" would be displayed, but it's not.

It's not 1, because interface methods have to be overridden. Final methods cannot be overridden. It's not 2, because only abstract and public methods are allowed in interfaces. If an interface had a static method, each implementer would have to define it, which defeats the purpose of a static method: a method that is stored with each class, not instance. And finally, since members of interfaces are public by default (private members would be contradictory), since you can access an interface variable using qualification (making them static), and since all interface variables must be initialized (making them final) , 4 is true.
The member x of each instance of Ship refers to same array instance created in init(). It's the same for y. You need to create a new ones.

6 years ago
Polymorphism is an OO-programming concept, where the programmer has the ability to handle related derived types with a base class type. If my argument's type is a base class, then the object can be any derived type and I can call any method inherited from that base class. If the object's type has overridden the inherited method, that version should be invoked.



drawShape's argument is polymorphic, it can operate on many types (Rectangle and Circle) and invoke the correct function (if it's overridden).
6 years ago


Your attempting to define a method inside a method which is illegal in Java.
6 years ago
First, I'd a buy book on compilers. I recommend Modern Compiler Implementation in Java or Compilers: Principles, Techniques, and Tools. Then, I'd check out JLex, JavaCC and CUP, because it's probably not the best idea to try and create a compiler from scratch. There's a lot of formal language theory, automaton theory, and assembly language used in building compilers from the ground up.
6 years ago


I've read that StringBuffer.equals compares the objects' references, not the content of their strings. The correct way is as follows.


6 years ago
It's because 9.45 is a float and "the ternary operator converts the numeric types to whatever is more generic." It's one of those hidden Java traps. Joshua Bloch exposes this in Advanced Topics in Programming Languages: Java Puzzlers @ 0:56 minutes and in his book "Effective Java."

I quote from JLS 15.25:

The type of a conditional expression is determined as follows: ... if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: ... Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.

.
Try this.

6 years ago


Since I've just given you the answer, at least let me explain it, so you can learn how I did it.

Start off by reviewing the literature in the Regex API linked above. It's a good reference, but if you've never done regular expressions, check out the tutorials first.

(1)

This is the range of the specific Latin unicode characters you expect to be in the input. Simple enough. See the Latin Unicode chart for details.

(2)

This means match (or allow) any word character (0-9, A-Z, or a-z)

(3)

Allow whitespace characters.

(4)

Allow any punctuation character.

(5)

Allow all of previously declared characters "and not" this one. Whatever punctuation you don't want needs to be included inside the brackets.

(6)

This is a greedy quantifier. It says to allow "one or more of all of these characters" in the string. Note that the regular expression must be enclosed in brackets when applying the quantifier.

Also, Note that all of these specifiers are escaped because they're within strings. Hope that helps. Good luck.
6 years ago
The post-increment operator executes after the expression is evaluated. It executes after s is assigned its new value. It can be re-written as follows.

You can forward using RequestDispatcher.forward and redirect using HttpServletResponse.sendRedirect.

Inside your Servlet:
6 years ago