get and set
Ok I have this statement in my class
publicvoid setArmor(double newArmor)
{
if (newArmor < 0)
{
System.out.println("ERROR: Bad paramter in Character.setArmor()");
System.exit(0);
}
else
armor = newArmor;
I have the instance variable, armor, declared before this...so this code will let the user declare armor what they want it to be correct?
Now how do I get it to print back out the armor if armor is a private variable....do I have to have get methods for everything I write like these
Thanks in advance!
If you want to print it, then write a printArmor() method that just prints that value. If you want to retrieve the value, then write a getter that return the int.
By the way, why are you shutting down the whole program when there's an invalid input? Shouldn't you be doing something more graceful like setting it to the minimum, or throwing an invalid input exception?
Well I just have to be able to return the value if the programmer calls for it such as this:
public double getArmor()
{
return armor;
}
Yeah it seems silly, but it is my coding assignment for my class...we are just doing it for practive
The only time a field being declared as private will affect your programming is if that field was declared in a super class..class Character
{
private double armor;
// your code stuffs
}
vsclass Character
{
private double armor;
// others code stuffs (presumably includes a way to access armor)
}
class Guy extends Character
{
// your code stuffs
}
Hopefully you see the difference between those two. Anyway, if you have direct access to the private variable (as in it's declared in the same class that you're coding), you can access it normally.System.out.println("Your characters AC: " + armor);
But if you are coding in a different class, you have to use the methods in the super class to access it.System.out.println("Your characters AC: " + super.getArmor());
> Yeah it seems sillyDoesn't seem silly to me, unless you want to avoid setter and getters, because you've heard they are evil.
No, No, I meant that it seemed silly that we terminate the program if a bad value is entered...instead of telling the programmer what they did wrong....also, thanks JJCoolB. This brings me to another question...I have to write a set method for the programmer on whether the character is good or evil. Should I use a Boolean for that...or just use good and evil?. later we have to write a method that will switch the side of the character from good to evil or vice versa. Do you think Boolean or String would be easier for that?
If there are only 2 choices for good/evil, then a boolean is a good choice. It's definitely simpler than using a String. Then you can do something like this to reverse the gooditude:goodGuy != goodGuy;
> or just use good and evil
There's a joke in there somewhere.
If this is truly a boolean property (it wouldn't be expanded to include "berzerk" or "neutral" later) use boolean.
private boolean evil;
public void setEvil(boolean evil) {
this.evil = evil;
}
public boolean isEvil() {
return evil;
}
public void toggleEvil() {
setEvil(! isEvil()); //or evil = !evil;
}
Wow thanks guys for all the input...your all very good with this thinkin stuff
Me: boolean goodGuyLaszlo: boolean evilWhy are you so pessimistic Dr? Are you racist against good guys or something?
Quite the contrary. I expect my boolean fields to have a default value of false ;-)
> Quite the contrary. I expect my boolean fields to> have a default value of false ;-)Again with the pessimism. You've got to cheer up man!
[code]
> private boolean evil;
>
> public void setEvil(boolean evil) {
>this.evil = evil;
>
> public boolean isEvil() {
>return evil;
>
> public void toggleEvil() {
>setEvil(! isEvil()); //or evil = !evil;
[ /code]
Ok...I'm confused by this. I see no instance of "true" or "false"
Message was edited by:
darkmonk04
They are possible methods of your Character class. Elsewhere in your code you would have something like this:
Character davros = new Character();
davros.setEvil(false); // here's your boolean
davros.toggleEvil();
> Ok...I'm confused by this. I see no instance of "true" or "false"Also, just as int fields are initialized to 0, boolean fields are initialized to false.