fred rosenberger wrote:
Jeong Ryu wrote:
I thought that whatever path that is not defined would be just ignored, but I guess not. How's this?
re: paths ignored
Nope. You need a return statement for every possible execution path. The method declaration states "I will return an int", therefore, you have to return an int for every possible path through your code.
re: how's this
Well, my first thought is "what happened when you ran/tested it?". My next thought is that the compiler will still complain. What happens if your suit is not a Spades, Clubs, Diamonds, or Hearts?
I would also suggest you get in the habit of ALWAYS using curly braces, even if not strictly needed. I would but your "return 0;" on line 1 inside a pair of braces.
Matthew Brown wrote:OK, now: what do you return if the card is the 3 of Spades, for instance? You need to make sure that something is returned from every possible path through the function. There are a few different ways you can fix it.
(The while statement could work, but it's a loop construct - you use it when you might want to execute the body multiple times. If you don't, like in this case, if is better).
fred rosenberger wrote:what will your code return if face does not equal Face.Three?
Matthew Brown wrote:And why is that a while statement instead of an if statement?
fred rosenberger wrote:without looking at a single line of code, my thoughts go like this:
In the real world, what would an actual bank of elevators look like? Would one elevator know anything about the location/movement of any of the others? Or would there be some kind of central controller that knew the current position and destination of every elevator, as well as handling all the requests (i.e. "call to this floor" or "go to that floor").
Winston Gutkowski wrote:
Jeong Ryu wrote:I am trying to return a complex number without creating a new Complex, as Rob Spoor had previously suggested...
It may be worth mentioning that doing that makes your class mutable, which has issues of its own (specifically Thread safety). In general, creating new objects is extremely fast, so I wouldn't worry too much about it at the moment; and making your Complex class immutable may allow the compiler and/or JVM to add some "efficiency magic" behind the scenes.
Doing so makes your class a bit more "functional" in style though, eg:but doing things this way generally makes your design very 'clean'.
Other "value" classes, such as BigInteger and BigDecimal use precisely this approach.
It also seems to me that this impedance calculation is quite specific, so you might want to think about an Impedance class (or method).
HIH
Winston
Matthew Brown wrote:
Jeong Ryu wrote:P.S. How do you pass a fraction number (1/6)? Just writing 1/6 passes the value as 0.0.
If you write 1/6 it will use integer arithmetic, so it gets rounded down to zero before you convert it to a double. You can avoid that by forcing one or both into a double in the first place - e.g. 1.0/6.0.
Matthew Brown wrote:Yes, total = null initialises the reference. And if you hadn't initialised it, you'd be getting a compiler error. The compiler's smart enough to work that much out. But it initialises it to null. That is, it's referencing no object. You get a NullPointerException whenever you try to call a method on a null reference. So here:
Which object are you calling add() on the first time round the loop? You can think of calling a method as sending a message to an object, but here you're sending a message to nothing.
This approach to adding up the impedences should work, but you need to initialise total to an actual object. You need a zero complex number. That's not the same as null, that's an instance of Complex with its real and imaginary parts set to zero.
By the way, you're heading for another ArrayIndexOutOfBounds in that for loop. The maximum index of the impedance array is impedance.length - 1 (because arrays are zero-indexed).
Matthew Brown wrote:Your ArrayIndexOutOfBoundsException is fairly straightforward:
You create an array containing two elements, then try to access three of them.
Henry Wong wrote:
Jeong Ryu wrote:a) How would you add the constructor?
That is a really generic question. If you don't know how to create a constructors, perhaps you should go back a few chapters in your book. You really should get that part understood before you create and use objects.
Jeong Ryu wrote:And if I don't have a constructor that takes a single Complex, is it possible to change the calculation to return a double Complex or whatever it may be so that it does not return an error?
It is *your* class. Whether it is possible will depend on whether you allow it, or not allow it.... however, it is probably a good idea to do it the correct way, then to hope that you have a bug in your code that you can work around -- to get to compile without error.
Henry