Craig Tyler

Ranch Hand
+ Follow
since Jan 15, 2006
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 Craig Tyler

Originally posted by Jherald Lacambra:
i see your problem here

public class Lab13C {
public static void main(String[] args) {
System.out.println("Larger number: " + larger(7, 3.5));
}
}


in this class you are calling a method outside the class so you have to instantiate the Calc class in order fo you the method on the calc class



larger() is static so an instance isn't needed.

Going back to the original question, the reason the compiler is complaining that it cannot resolve the symbol larger is that you never declared a variable of that name. Also...oh...it's easier for me to post a code sample.



I really think you should spend some time with a good Java book or find a tutorial online. It would probably save you some frustration. Good luck.
18 years ago
...and you missed an extra curly brace to close class Calc. and you never return anything from larger. and you need to use Calc.larger(7, 3.5) since it otherwise wouldn't know where to find the method. and your second if needs to be an else if or you'll get two messages printing out whenever the first double is larger than the second.

EDIT:

Forgot that you couldn't touch the code in main, so you have two options:

1) Make the larger function part of the Lab13C class.
2) Use a static import for larger. Probably overkill.
[ February 22, 2006: Message edited by: Craig Tyler ]
18 years ago
If you just wrap it in a try/catch block but do nothing to handle the error in the catch block, then you're always going to have problems later down the line if you attempt to do something that depends on the code in the try block completing successfully.

In the case of creating a Robot, if you get an AWTException as you have, remember that this means no Robot was created so you can't rely on it later.
18 years ago
It's just a plain old has-a relationship. Although it's a little bit more involved than this example, a common real life application use for this would be the Decorator Design Pattern.
18 years ago
Could you tell us what book this came from?

As the compiler said, EOFException is never thrown within the try block, so it's smart enough to tell you that your catch block for an EOFException is never going to be used and is simply code that will never be used. As for getting an error when you call main1(), since main1() declares that it throws two checked exceptions, the call to main1() must either be wrapped in a try/catch block or main() itself must declare that it also throws these exceptions.
This is because i is what is called a compile-time constant. Since the value of i is known at compile time and can never change, it can be assigned to a byte (as long as it is within a byte's range).
18 years ago
This is as much of a precedence issue as it is the difference between short and "long" circuit operators. The short-circuit operators have a lower precedence, so the two expressions end up like so:



In the first expression, as the & operator is of higher precedence than |, the two falses get grouped together, but the entire expression results in a true value since "false | true" is true.

The second expression gives higher precedence to the | operator, so "false | true" get grouped together. Here, however, the short-circuit operator stops at the first false since it wouldn't matter what the second grouping would result in.

Hope this helps.

Originally posted by bharath kumar narayanan:
anyway is there any one book that i cud study to get through scjp at 80% or above. im a student and have much academic work.. and my campus interviews are fast approaching... i need to finish it in 2 to 3 months...



The SCJP5 book by K&B would serve you well in studying and getting at least an 80%. With 2 to 3 months, you could probably study it at half a chapter per day or so. Make sure to write a lot of little programs (<100 lines) to help you cement your understanding. I'd make notes to myself while reading the book to try something out later in code or to look up something in the API documentation to get a deeper understanding.

The coverage in the book is excellent, so using it as your sole reference will work. The only problem with it (I think) was that there was not an example of using the values() method to iterate through an enum. I might be wrong about that, but I only recall a sentence explaining that the method existed and not to worry about it in that chapter.

Good Luck.
18 years ago
When you create a thread you can use setDaemon() to make the thread either a daemon thread or a user thread. When every user thread has finished its run method (and your main thread has finished main()), the program will quit, regardless of whether or not any threads you set to daemon are still running.

Hope that helps.

Originally posted by Seema Ahuja:
I am sorry i know this is a rather old topic but

if(b1 & b2 | b2 & b3 | b4)
System.out.println("Crazy stuff");

gives a compile time error. says operator & cannot be applied to Boolean and boolean

I created b1 as

Boolean b1 = new Boolean(false);
as it does not allow me to create a Boolean object like this:

Boolean b1= false;

Will anyone plz clarify

Thanks



It doesn't work if you are using java 1.4 or below. With java 1.5, the autoboxing and unboxing allows a boolean value to be directly assigned to a Boolean and for Boolean objects to be used with the & and | operators.
Since you assign a value to n in the try block, an exception (in this case a NumberFormatException) might get thrown before you actually assign the value to n. If that happens, when you want to use n, it would still be uninitialized. To avoid this, assign a default value to n (like 0) when you declare it. That way, even if you do get an exception that prevents n being assigned to anything, you still have a valid value in n.
[ February 13, 2006: Message edited by: Craig Tyler ]
18 years ago
Have the class implement Comparable and make the compareTo method sort on the city name. Then, all you have to do is use Collections.sort(). Or, make a Comparator and essentially do the same thing.
18 years ago
On the master exams from the K&B5 book, I scored about 80%, but got a 91% on the real exam, but I never reviewed my answers on the mock exams, which probably would have increased my score to something close to what I made on the real exam where I did spend time looking over my answers.

Hope this helps and good luck on the exam.
Having not tried it out, I'm really not sure if this would be any faster, but it might be worth a shot. Create a regex expression based on the scrambled string you're making. In the case of "tac", it would be something like ([tac])+ so at least the algorithm would be faster since you wouldn't have to copy strings from the list of words to a character array.
18 years ago
I'm not sure I follow you in this case. When you do (t222.studentCode = "T222"), you are essentially giving a reference to t222.studentCode that points to a String object containing "T222". You're giving that reference to the studentCode member of the t222 object you made earlier. I'm not sure if this answers your question or not.
18 years ago