Search code examples
javaif-statementselectorgetter-setter

Basic JAVA app with setters and setters and increment an integer by 1


I'm just blanking on what to do next.

What I need to do:

  • The class should have the variables name, breed, age, and color

  • A constructor that sets all the variables

  • Getters and Setters for all the variables

  • A main method that creates an instance of the Dog and utilizes the constructor

  • You can pick values you feel appropriate for the variables

  • Increase the dog's age by 1

  • Print all the values to the screen

Heres what I've done.

public class DOG {
    String DogName;
    String DogBreed;
    int DogAge;
    String DogColor;

    public DOG(String name, String color, String breed, int age) {
        this.DogName=name;
        this.DogColor=color;
        this.DogBreed= breed;
        this.DogAge=age;

    }

    public static void main(String[] args) {
        DOG myDog = new DOG("Ares","Red","Rott",5);

        System.out.println(myDog.DogName+" " + myDog.DogColor+ " " + myDog.DogAge+ " " + myDog.DogBreed);

    }

    public void addOnetoAge() {
        if(DogAge >=6 DogAge++);
    }

    public String getDogName() {
        return DogName;
    }

    public void setDogName(String dogName) {
        DogName = dogName;
    }

    public String getDogBreed() {
        return DogBreed;
    }

    public void setDogBreed(String dogBreed) {
        DogBreed = dogBreed;
    }

    public int getDogAge() {
        return DogAge;
    }

    public void setDogAge(int dogAge) {
        DogAge = dogAge;
    }

    public String getDogColor() {
        return DogColor;
    }

    public void setDogColor(String dogColor) {
        DogColor = dogColor;
    }
    if(int i=1; i <= myDog.DogAge: i++) {
            System.out.println(myDog.DogAge);
        };

    }
}

I'm just getting back into java and have forgotten what goes where. I would be grateful for a bit of direction


Solution

  • For the most part, you have the right code, but your two if statements are not necessary, so you should remove both.

    If you follow the assignment instructions

    • Increase the dog's age by 1

    • Print all the values to the screen

    In your main method all you need to do is this

    DOG d = new DOG("x", "y", "z", 2);
    d.setAge(g.getAge() + 1); // Increase by one
    System.out.println(...); // print the values of the getters
    

    This would iterate a range of all the value between one and the dog age, not increase it

    for(int i=1; i <= myDog.DogAge: i++) {
    

    This only increases the ages for dogs 6 and older (which seems like a weird design)

    if(DogAge >=6) DogAge++;