Jeong Ryu

Greenhorn
+ Follow
since Sep 25, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Jeong Ryu

Hello,

I am making a Game of Life app, and I am having trouble with the nested for loop needed to draw and retain rectangles.
My current code allows me to draw the right size rectangle at the location I touch on the screen, but it does not persist when I click somewhere else.





I know I have to use the nested for loop, but I am having trouble setting it up. Right now, I am setting the coordinates as float X and float Y. I figured that I should use an array or an arraylist, but I am not sure on how to incorporate it.



Could someone please help me? I would appreciate it very much.

Thank you,
-jNova
10 years ago
Thanks for the replies everyone.

I am going to scrap my program and do it all over again. This time with proper planning.
11 years ago

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.



I got the same error, haha. I tried adding another else to account for if my suit is not a spades, clubs, diamonds, or hearts, and it got rid of the error. Is this the right code to check if there are any threes of diamonds or hearts in the hand dealt? Also, what do you mean by "I would but your "return 0"; on line 1 inside a pair of braces"?

11 years ago

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).



I thought that whatever path that is not defined would be just ignored, but I guess not. How's this?

11 years ago

fred rosenberger wrote:what will your code return if face does not equal Face.Three?


It would return an integer of 0, or at least that's what I need my program to do. I am trying to create a canasta card game, where the program calculates how much points you have based on what cards you have. For this method, I want to try to check the hand to see if there are any threes of diamond or heart.


[EDIT]: I think I know what you were trying to point out. My else return statement should be outside of the while statement. I think.


Matthew Brown wrote:And why is that a while statement instead of an if statement?


I am not sure. Don't they give the same results? Is it better to use nested if statements?



[EDIT]: I think I see why if statement would be more appropriate than the while statement. I should use the nested if statement?

[EDIT2]: Eclipse still tells me that I need to add a return statement, and I don't know why D:
11 years ago
Hello,

Could someone please tell me why I get an error saying that I need to make a return statement, when I already did?



Thank you!
11 years ago

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").



Thanks! Here, I was trying to see whether or not the elevator is stopped (in the takeAction method, the stop time decrements every time it is moving up or down. The problem, though, is that STOP_TIME will always be 3 because it will not decrement until the elevator is moving up or down. So, I thought that this is where the variable that acts as a position holder is used (for the control center you had mentioned). But, I am not sure how to create this variable that holds the positions of the elevator. I tried to create an Building class object, but I keep getting an error that says constructor is not defined. How do I use an array of another class by creating an object to access it, without it giving me this error (I want to use it, not send new arguments. I need to access the elevator array from the Building class so that I could set the elevator array in Elevator class to the one from the Building class)?

This is what I currently have:




EDIT: I just realized that I need to have to for loop keep looping back to 0, or else no elevators will move after i becomes 8. And that FLOOR_TIME == 3 will always be true, since the FLOOR_TIME never gets the chance to decrement, since this for loop happens before the elevators moving up and down(which causes the FLOOR_TIME to decrement and then reset to 3 after). Sigh.
12 years ago
[UPDATE]: I think I found the problem. I will post here if I can't figure it out, along with a more specific question.


Hi CR,

I am working on an elevator project where I have to move 8 elevators independently to different floors to pick up and drop off passengers. I have most of the code written, and it works sort of when I am only using one elevator. When I try to move all 8 elevators, they just move to the same floor at the same time.

I think the problem is that because there is no condition to invoke the takeAction method in the elevator class, each time the elevators are looped, they all just go through the takeAction method, which is why they are all moving together. How could I add a condition before the switch statement, so that only the elevators that are NOT in motion goes through the takeAction method?

Any insights would be greatly appreciated!







12 years ago
Hi CR,

For my elevator assignment, I need to keep the old average wait time and average travel time, and compare them with the new average wait time and average travel time (by changing the time it takes for the elevator to ascend or descend from 2 seconds to 1 second. The variable is floorTime). However, I am not sure how I could do this without replacing the old numbers that are passed to the class that sets the wait time and the travel time.

How could I have my program calculate two sets of different numbers without having to create more methods just for the new values? (Using old values, calculate, then save average time. Old values changes to new, calculates, then saves as new average time. All using the same methods)

Here's the code that sends the values to the Building class:


Building class that initializes the elevators with the values above:



I tried to create another object to pass in the new values, but that would replace the old one. The only other way that I can think of doing this is to have separate calculation for old values and new values, which would double the amount of code lines.

Any input would be greatly appreciated!
12 years ago

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



Thank you!
I tried changing my Complex class to match your code, but it broke a lot of my codes. So, I just left that class as it was.
For the impedance calculation, I just had to add them up and then do division. My program now correctly produces the answer without any errors. My program for this project had a lot of useless codes like the set/get methods. All I had to do was assign the arrays to the formula to get the complex number, add all three of them, and then do phasor voltage,v, (which was (3,0)) divided by the total of the sum.

Thanks everyone!
12 years ago

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.



Thank you!
12 years ago
Okay, I've found the problem. It seems that I am not correctly assigning each arrays. It produces zeros, when it shouldn't - which is why I got (NaN,NaN).
What would be the correct way to assign complex numbers to arrays?

UPDATE: It seems that the values from the main method weren't passing right. I've fixed it, and now I am not getting any NaN or -Infinity values. But, it seems that the three Complex values are not adding right. After I fix this, I will post back here after class.

Thanks everyone!


P.S. How do you pass a fraction number (1/6)? Just writing 1/6 passes the value as 0.0.
12 years ago

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).



That makes sense, thank you! I initialized to an object in the Complex class, and I also fixed the loop so that it does not go out of bound (I think). I am now able to run the program without any errors, but I get the values of (NaN, NaN). This probably means that I did not code correctly to separate the answers to (real, imaginary), and is probably being calculated as one number or something. I am not sure...

Here's revised code portion:

12 years ago

Matthew Brown wrote:Your ArrayIndexOutOfBoundsException is fairly straightforward:



You create an array containing two elements, then try to access three of them.



I see. Thank you!
I had it at 3 originally, but I switched to 2 because I thought that [2] would create array[0]. array[1], and array[2].

After fixing the array issue, I am coming across more errors... sigh, haha...


I am trying to return a complex number without creating a new Complex, as Rob Spoor had previously suggested. However, I couldn't do that unless I initalized total to 'null'. But, it seems that that is giving me this error.
Here is my revised code that gives this error (line 71):

[Edit - fixed line length to prevent horizontal scrolling]


And for line 14:
12 years ago

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



Thank you!
Unfortunately, I am in school so I can't access my textbook until later tonight. But, from what I understand, we just need to create two constructors : One that takes in four double values and sets to four instances, and another one without any arguments.

12 years ago