Dan Longest

Greenhorn
+ Follow
since Apr 28, 2005
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 Dan Longest





Now, that's probably way off. When it compiles, it lets me know that it is expecting type OldBritishMoney, but type int is found.



The method add() is declared to receive a type OldBritishMoney and return that same time. Your local variable totalAdd is an integer, not OldBritishMoney, so you cannot just return it in place of OldBritishMoney. You could do something like



which instantiates a new OldBritishMoney object and initializes it with 0 pounds, 0 shillings, and totalAdd pence and then returns a reference to it. Code to call the add function might look like:



Seeing this, I might consider putting in a constructor that takes one integer value, the total number of pence you have, and converts that internally to appropriate quantities of pounds, shillings, and pence. At this point, you should be able to make appropriate modifications to what I attempted in case I didn't fully understand your class workings or intentions. I am no Java expert so if I erred, someone else can jump in here and teach us both.

Regards,
Dan
17 years ago
I'm preparing for SCJP and am no Java God I'll take a shot at this. That being said, I would have gotten it wrong without knowing the right answer. I was prepared to say the statement would yield 0.

The casting operator is right associative.
So in the statement (int)(char)(byte)-1, it will attempt to cast -1 to a byte, then cast that to a character, then cast that to an integer.

The cast of -1 to byte is fine. The problem is that char is an unsigned type. Proper values of char range from 0 to 65535. -1 is outside the allowable bounds of char. The compiler will allow this cast, but -1 will (apparently) "wrap around", like will happen when doing arithmetic with signed types. My problem is that I thought this was an impossibility given char being unsigned. At any rate, casting this result to integer is no big deal, giving the result. So the cast of a negative to char is what causes the difficulty. Is this behavior undefined, or will this statement always yield the same result?

Regards,
Dan
17 years ago
The meaning of multiple assignment is not hard to decipher when you remember that the assignment operator is the only binary operator that has right to left associativity.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

So as was pointed out previously,
d=c=b;
is equivalent to
c=b;
d=c;

Although I agree that doing something like that is probably not worth the confusion it could cause.

Regards,
Dan
I want to display an image using swing. How do I add the image to a jpanel or jframe? I've tried finding a way to do it with Image and ImageIcon with no success. A link to the right information would be fine.

Dan
18 years ago
By definition all an interface's methods must be abstract. However, all it takes to be an abstract class is to declare 1 method as abstract. Therefore you could potentially make your decision based solely on whether you wish to provide your own implementation for several methods or leave it all up to/force the end user to provide their own.

Keeping in mind that you can only extend 1 class but can implement several interfaces so that also enters the design consideration hopefully.

Regards
Dan
18 years ago