Vivienne Ryan

Greenhorn
+ Follow
since Dec 12, 2013
Vivienne likes ...
Chrome Java Windows
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
10
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vivienne Ryan

Winston Gutkowski wrote:

Vivienne Ryan wrote:Ok so I fixed the DiceRoller(I think)so it should return a number between 1 - 6. I also changed the if statements to switch and while I still get the negative numbers my code is much more readable and look much more better. Anyways So I added 2 if statements where they check if the characters hp is <= 0 and if so to assign them to 0.


Aaargh! Do NOT do this.

Do things step by step, and test thoroughly after every single change you make; otherwise how can you ever know what caused a problem if you get one?

However I'm still getting the negative numbers, is this down to variable scope?


Possibly, but TBH it's difficult to tell, so I certainly wouldn't assume it.

The main problem, as I see it, is that you're doing all your logic in main(), which is a bad way to go. I suspect that most of this code should actually be in your Player class.

Try this: Turn off your computer and explain to me (or us) IN DETAIL and in English, exactly what an "attack" is supposed to do?

Winston



Essentially what I want to happen is : Two characters have a base Health points = 150. First player does an attack, which depending on the dice roll does 10/30/50 damage. This amount is taken from the other players Health points(so an attack that deals 30 damage -> removes 30 from opponents Health, leaving 120 health), and vice versa. This continues until one characters health has reached 0, then the computer displays who has won.

As to the player names... twitch plays pokemon is stuck on my mind :P
10 years ago

Winston Gutkowski wrote:

Vivienne Ryan wrote:Ok, so I have edited my code so the loop does end but...


It's still wrong. It will now produce a result between 0 and 6 (inclusive).

Also my variable names are temporary they will be changed to better ones(I just want to test the thing out and get it working)


Unfortunately, if your names are bad, it may well hamper your testing as well.

My advice: fix them NOW (you might want to look at my signature quote for inspiration ).

Also: Your if statements are not exclusive, so there's a lot of redundant (and probably incorrect) code being executed. You might want to have a look at the switch statement.

Winston



Ok so I fixed the DiceRoller(I think)so it should return a number between 1 - 6. I also changed the if statements to switch and while I still get the negative numbers my code is much more readable and look much more better. Anyways So I added 2 if statements where they check if the characters hp is <= 0 and if so to assign them to 0. However I'm still getting the negative numbers, is this down to variable scope?

10 years ago

Stephan van Hulst wrote:Hi Vivienne.

The reason you're seeing negative values is because your loop will probably never terminate. Let's say one player has 10 hp left over, and you score a hit that deals 30 damage. This player will then have -20 hp, and the condition in the while loop will not reach 0.

There are also some other matters you should address. Right now the loop is going on while EITHER player has hitpoints other than 0. So if the loop terminates (IF it terminates indeed), both players are guaranteed to have 0 hitpoints. Combined with the following if statement, this means P1 will never win.

The inside of your loop does *lots* of things you probably didn't intend it to do. You're re-rolling the die for each condition (you probably meant to save the first result) and some if statements probably should only be executed if the previous ones failed. Did you mean to use if-else?

Also, you should NOT create a class named Character. Character is already defined in the Standard API. Name it something like Person, Fighter, Player or something similar instead.

Your DiceRoller generates values between 0 and 5, included. This may be confusing, when you think of what most dice look like. Your variable names are similarly confusing. Don't call player 1 "c", and player 2 "c1". Call them "player1" and "player2".

Finally, I'd suggest you'd write your loop in such a way that in its body, one player gets to attack, and at the end of it, the turn goes to the other player.



Ok, so I have edited my code so the loop does end but still have a problem with minus numbers. Also my variable names are temporary they will be changed to better ones(I just want to test the thing out and get it working). Have also changed Character to Player. Here is my new code:

As you see I have assigned 2 variables p1Roll and p2Roll to d.roll(). Will this generate a new number everytime a turn ends?
Also my apologies for starting the new thread, know now for future reference.
EDIT: Forgot to change character to player
10 years ago
Hi there I am working on a piece of code that takes a number(150), then depending on the roll of the dice takes away 10, 30 or 50. If the roll is less than 2, it takes away nothing and passes over to the next person where the same thing happens again. This is in a do while loop that has an exit condition where if either of the numbers reaches 0, it exits and displays the winner.

The problem I'm having is I'm outputting large negative numbers eg: -2266740.
I don't know what I am doing wrong and if I can get some guidance that would be cool, thanks.

10 years ago
Hi there,
I am currently in the process of completing a project for class. My partner and I are nearly finished it but we need to sort out one or 2 things.

This is my code. What I am having trouble with is: In the attack methods I want to pass in the opponents hp, deal damage and return the adjusted hp. However the hp is not changing. I originally passed the hp in as an int, but remembered the int is a primitive and a copy is only changed. So I changed to int to an Integer, however the same problem is arising. I am getting more and more stressed about this part as my deadline is due this friday and it is the only thing preventing us from handing up a complete project.

Any ideas as to where I am going wrong?

EDIT: Ignore the DiceRoller class, that serves no purpose at the moment.
10 years ago

Henry Wong wrote:

Vivienne Ryan wrote:
Could somebody explain to me what is going on? I don't want to be spoonfed, I just cannot wrap my head around this bit.
Why is the method not overloaded?
Also why doesn't the second version of main not output the expected output(false, true)?



Actually, the two methods are overloaded. What they are not, and what you were expecting, were for them to be overridden. Choosing between overloaded methods is done at compile time, and the compiler does so with the reference types.

Henry



Thanks for catching my mistake. I meant to say overridden.
10 years ago
Hi there,
I am currently studying Overriding methods using the "Oracle Certified Professional Java SE7 Programmer Exams 1z0-804 and 1z0-805" and I am a bit confused.
I am studying the examples given on pages 70 - 73 of the book and I was wondering if ye guys can help me to understand the information.

The original method is:


The first piece of code:


Which outputs:

p1 equals p2 is false
p1 equals p3 is true



Then the main method is changed to:


Which outputs:

p1 equals p2 is false
p1 equals p3 is false



Could somebody explain to me what is going on? I don't want to be spoonfed, I just cannot wrap my head around this bit.
Why is the method not overridden?
Also why doesn't the second version of main not output the expected output(false, true)?
10 years ago

Campbell Ritchie wrote:

Jayesh A Lalwani wrote: . . . You will see what Campbell is saying about a "red herring"

I didn't say, “red herring”.

I said, “red herrings”.
Challenge to Vivienne Ryan: count the red herrings.



Will do!
Right now though I am celebrating passing the Programmer I exam
10 years ago

Jayesh A Lalwani wrote:The answer is rather elementary if you know how to read the code. I am suspecting that your instructor wants you to learn how to read code. So, I'm goi9g to give you tips on how to read code rather than spoon feed the answer.



Thank you very much! You have just helped me to fully understand what the code is supposed to do!
10 years ago
when I break, do I then break out of C: or do I break out to the very first for?
10 years ago
Hi there came across this piece of code in a test:


why is the result 8?
My source is from a test I got in class. Our instructor just printed out the questions on a sheet, no idea where they come from sorry.
10 years ago
Hi there,
As part of Exercise 9-5, I am posting up a simple NoClassDefFoundError:


In this code I declared that the HelloPeople file was located in the package spoon, but I didn't put it into a spoon directory.
It compiled fine but threw me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloPeople (wrong name: spoon/HelloPeople)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
10 years ago