Question

How do I set up attributes in Java with "range values? " for example. GameField is...

How do I set up attributes in Java with "range values? "

for example. GameField is my class.

GameField will have the following attributes:

  • hitPoints – range 0 to 125
  • strength – range 0 to 20

Homework Answers

Answer #1
public class GameField {

    private int hitPoints;
    private int strength;

    /*
    Modify hitPoints and strength only if they are in valid range.
    so, every time you want to modify one of these attributes, make sure you use these setter methods below.
    don't modify them directly 
     */
    public void setHitPoints(int hitPoints) {
        if (hitPoints >= 0 && hitPoints <= 125)
            this.hitPoints = hitPoints;
    }

    public void setStrength(int strength) {
        if (strength >= 0 && strength <= 20)
            this.strength = strength;
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Part 1 I need to make an example class that I make up on my own....
Part 1 I need to make an example class that I make up on my own. It should have some variables, at least one function and at least one procedure in it. And then I should create two instances of the class and demonstrate how to use the class by calling some of the procedures and functions. What I make my class about is up to me. Part 2 Do a simple example of ByVal vs ByRef. I can do...
In class I have brought up the concept of "living" kinesiology. I gave an example how...
In class I have brought up the concept of "living" kinesiology. I gave an example how walking with my wife that I naturally look at others dysfunctions and try to analyze it. Please share an example of how you have recently incorporated this (Kinesiology) into your life.
*HOW WOULD I SET THIS UP IN A CHI-SQUARE TEST* An example for numerical to categorical...
*HOW WOULD I SET THIS UP IN A CHI-SQUARE TEST* An example for numerical to categorical data off the top of my head: Light of different color has different wavelengths, but certain ranges of wavelengths qualify as certain shades/hues/tints/etc. You can generalize and say a certain range can be called "blue" or "red". Red is usually attributed to light that has a wavelength between 780 and 622 nanometers, whereas blue light is between 492 and 455 nm. To the average...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
JAVA QUESTION I want to know how to combine different Shape objects in Java into one....
JAVA QUESTION I want to know how to combine different Shape objects in Java into one. For example, if I wanted to make a class called CoffeeCup, and it was supposed to be a to go coffee cup, how would I combine a Rectangle, and two Ellipses (on the top and bottom) and then use that as one object in a different class? Thank you in advance!
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Java Generic Linked List Problem: How do I remove a slice of a linked list and...
Java Generic Linked List Problem: How do I remove a slice of a linked list and add its data to another linked list in java? Example: Private<T> head; Private int size; Public List<T> slice(int from, int to) { // This method will remove the data from the given range (inclusive) and add it to a new List and return this new list. }
in JAVA Write a class Store which includes the attributes: store name and sales tax rate....
in JAVA Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn Store class, also code a method returning the...
Give an example of how the range of a data set might not be an accurate...
Give an example of how the range of a data set might not be an accurate measure of spread.
How do I describe an oligopsonistic model for a labor market? How do you set up...
How do I describe an oligopsonistic model for a labor market? How do you set up the profit equation? And how do you find how firms decide what the amount of Labor will be ?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT