Jarred Olson

Ranch Hand
+ Follow
since Jul 31, 2009
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
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jarred Olson

You can use the instanceof operator to to see if the object you're trying to cast is castable (is that a word) to the type you want. It will prevent you from getting the ClassCastException.
14 years ago

fred rosenberger wrote:

interface B implements A



Just a nitpick, but interfaces extend other interfaces, not implement.
14 years ago
Jay... read the post above your last one. Also your doStuff() method isn't doing anything with the variable... you would need to set it's value to a instance variable and then read that from your implemented toString() if that's your desired result.
14 years ago
Not sure why it can't find the Interface in a separate file... maybe try adding a package statement at the top of both files to explicitly state the package.

For the print statement resulting in: "FooImpl@19821f"
When you place an Object inside of System.out.println() it implicitly class the .toString() method. Since FooImpl doesn't override the Object implementation of toString(), it gives you that garbled piece of useless text. Try adding this to FooImpl and keeping your print statement the same:
14 years ago
This also creates an endless loop:



From what I can gather from your code it looks like it isn't required either.
14 years ago
Jay, welcome to the Java Ranch. Please use Code Tags when posting code.

Looking at your code, I don't think it should compile in any version of Java. Foo is an Interface and you're not allowed to instantiate Interfaces. You are allowed to declare the type of a reference variable as the Interface type but you have to instantiate it is a class that implements that interface, not the interface itself. Also, your FooImpl class doesn't override the doStuff(int y) method so the Interface contract isn't satisfied. Try reworking this out and posting your results.
14 years ago
When parsing Strings of variable (potentially large values) it's better to use Long.parseLong() method to avoid the NumberFormatException:


You might want to wrap it in a try/catch block as well because if the String happens to have a non-numerical character it will throw the same exception.
14 years ago
Parenthesis are what you would use to cast: (String)
14 years ago

Ankit Garg wrote:Shouldn't this



be



If you omit the class keyword, then it will not compile (in addition to the missing s in ClasOne)...



Yup omitted the class on accident, I fixed it though. Thanks for everyone's feedback.
I would probably make a StringUtils class that has a isNullOrEmpty(String str) { } method. If you're going to be doing a lot of String validation you'll use it a lot. Then you could replace lines 5 through 9 with:

14 years ago
That looks pretty useful, better than some of the nested HashMap/ArrayList/HashMap structures I've run across before
14 years ago
I was wondering for those who've taken the actual exam. Are there ever intentional spelling errors that would result in compile errors? Example:


I was just curious because sometimes when I'm trying to read fast I skim more than I should Thanks!
Please use Code Tags when posting code. As far as a solution for your question. You could add logic to take into account your end of the year scenario since you know it will occur only on week 52 (or 53 in rare cases).

14 years ago
This example may help. You should use static methods when you don't need any information from the current instance of an object in order to complete the work. Utility or helper classes are a prime example of when to use static methods. Take a look at the Person class below. getAge() uses information from the instance in order to calculate the age of a person. The calculateAge() method takes a date argument (date of birth) and uses that date to calculate the age for any date that is passed in, and no reference to an instance of person is needed.

14 years ago