0

I am creating a virtual cat. The user should be able to name it and have three categories for interaction with the user which are health, food, happiness. The range is (lowest) 1-50 (highest) The int value is 30. Ideally the user should be able raise or lower the categories with interaction.

How do I create this range and declare 50 as max and 1 as the lowest it can go?

I have declared the three categories.

String catName;
int food = 30;
int health = 30;
int happiness = 30;

System.out.println(" type cat name");

catName = input.nextLine();
2
  • 1
    You can't, unless you create a method which first validates the input value before setting it. You should create a VirtualCat class with those three properties. Java is an object-oriented language. Use that to your benefit. You could start here. Commented Sep 25, 2019 at 18:49
  • 2
    Possible duplicate of How can I represent a range in Java? Commented Sep 25, 2019 at 19:13

4 Answers 4

0

Yes, as said in the comment by @MC Emperor, Java is OO language, so you should use it as such. For instance you can create the following class (just a starting point to get you a kickstart):

public class VirtualCat{

    private int food;

    //... other properties, constructors etc

    // here validate the modification of the property
    public void increaseFood(){
        if(food + 1 > 50){
            throw new Exception("Invalid value for food");
        } else {
            food ++;
        }
    }

    public void decreaseFood(){
        if(food - 1 < 1){
            throw new Exception("Invalid value for food");
        } else {
            food --;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

For those that have access to Guava Java Libraries consider:

https://guava.dev/releases/19.0/api/docs/com/google/common/collect/Range.html

Comments

0

I understand if you're just starting in Java. The correct way to make your program is with OOP. That's.. creating an object of Virtual Cat:

public class VirtualCat {
    private String catName;
    private int food;
    private int health;
    private int happiness;

    public VirtualCat(String catName){
        this.catName = catName;
        this.food = 30;
        this.health = 30;
        this.happiness = 30;
    }

    public String increaseFood(){
        if(this.food+1 > 50)
            return "Error. Food range is [1-50].";
        else{
            this.food++;
            return "Food increased.";
        }
    }

    public String increaseHealth(){
        if(this.health+1 > 50)
            return "Error. Health range is [1-50].";
        else{
            this.health++;
            return "Health increased.";
        }
    }

    public String increaseHappiness(){
        if(this.happiness+1 > 50)
            return "Error. Happiness range is [1-50].";
        else{
            this.happiness++;
            return "Happiness increased.";
        }
    }

    public String decreaseFood(){
        if(this.food-1 < 0)
            return "Error. Food range is [1-50].";
        else{
            this.food--;
            return "Food decreased.";
        }
    }

    public String decreaseHealth(){
        if(this.health-1 < 0)
            return "Error. Health range is [1-50].";
        else{
            this.health--;
            return "Health decreased.";
        }
    }

    public String decreaseHappiness(){
        if(this.happiness-1 < 0)
            return "Error. Happiness range is [1-50].";
        else{
            this.happiness--;
            return "Happiness decreased.";
        }
    }

    @Override
    public String toString() {
        return "VirtualCat{" + "catName=" + catName + ", food=" + food + ", health=" + health + ", happiness=" + happiness + '}';
    }
}

So, your main program needs to be like as:

public static void main(String[] args) {
        int answer = 1;
        int increaseOrDecrease = 0;
        VirtualCat virtualCat = new VirtualCat(JOptionPane.showInputDialog(null, "Please insert cat name"));

        while(answer == 1){
            System.out.println("Actual status for virtual cat: "+virtualCat.toString());
            increaseOrDecrease = Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want to increase food? Type 1 for increase, type 2 for decrease"));
            if(increaseOrDecrease == 1)
                System.out.println(virtualCat.increaseFood());
            else if(increaseOrDecrease == 2)
                System.out.println(virtualCat.decreaseFood());
            else
                System.out.println("Incorrect option. It need to be 1 or 2");

            increaseOrDecrease = Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want to increase health? Type 1 for increase, type 2 for decrease"));
            if(increaseOrDecrease == 1)
                System.out.println(virtualCat.increaseHealth());
            else if(increaseOrDecrease == 2)
                System.out.println(virtualCat.decreaseHealth());
            else
                System.out.println("Incorrect option. It need to be 1 or 2");

            increaseOrDecrease = Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want to increase happiness? Type 1 for increase, type 2 for decrease"));
            if(increaseOrDecrease == 1)
                System.out.println(virtualCat.increaseHappiness());
            else if(increaseOrDecrease == 2)
                System.out.println(virtualCat.decreaseHappiness());
            else
                System.out.println("Incorrect option. It need to be 1 or 2");

            answer = Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want continue? Type 1 for yes, type 2 for no"));
        }
        System.out.print("Final status for virtual cat: "+virtualCat.toString());
    }

1 Comment

it doesn't allow me to declare a private int also I am getting a red underline in the if else statement under this and return
0

As others already wrote, Java is an Object Oriented Programming Language and you should take advantage of that.

A good way of ensuring that a value stays within a defined range is to implement a type where you can set an upper limit, a lower limit and a changeable value. A basic implementation could look like this:

public class RangedValue {
    private final int lowerLimit;
    private final int upperLimit;
    private int value;

    public RangedValue(int lowerLimit, int upperLimit, int value) {
        if (lowerLimit >= upperLimit) {
            throw new IllegalArgumentException("The lowerLimit must be less " +
                    "than the upperLimit!");
        }
        this.lowerLimit = lowerLimit;
        this.upperLimit = upperLimit;
        this.value = RangedValue.ensureRange(value, upperLimit, lowerLimit);
    }

    /**
     * Helper method to ensure that a value stays within the defined range.
     *
     * @param value      The value to be kept in the defined range
     * @param upperLimit The upper limit of the range
     * @param lowerLimit The lower limit of the range
     * @return The value or the lowerLimit or the upperLimit
     */
    private static int ensureRange(int value, int upperLimit, int lowerLimit) {
        if (value > upperLimit) {
            return upperLimit;
        } else if (value < lowerLimit) {
            return lowerLimit;
        }

        return value;
    }

    public int increaseValueBy(int value) {
        int increased = this.value + value;
        this.value = RangedValue.ensureRange(increased, upperLimit, lowerLimit);
        return this.value;
    }

    public int reduceValueBy(int value) {
        return increaseValueBy(-value);
    }

    public int setValue(int value) {
        return RangedValue.ensureRange(value, upperLimit, lowerLimit);
    }

    public int getLowerLimit() {
        return lowerLimit;
    }

    public int getUpperLimit() {
        return upperLimit;
    }

    public int getValue() {
        return value;
    }
}

Then you could use this class for your existing code:

RangedValue food = new RangedValue(0, 50, 30);
RangedValue health = new RangedValue(0, 50, 30);
RangedValue happiness = new RangedValue(0, 50, 30);

System.out.println(" type cat name");

catName = input.nextLine();

You could define the cat in a separate class as well, of course, but that's up to you.

2 Comments

it doesn't allow me to use RangedValue the word is highlighted in red
Create a file called RangedValue.java in the same directory that contains the file with your existing code. This file has to contain the example I wrote (the one starting with public class RangedValue {...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.