back. Any sugReturn statement printing back String and default int of 5. I only want the String to printgestions?

Viewed 34

We are learning oop in my programming class and this was our first assignment for it, the problem I'm having is that it is only supposed to return "good" but i'm getting back "good 5". I'm still very new with all of this and would love some suggestions or tips on how to fix this.

public class Main{
 public static void main(String[] args){
  Dog dog = new Dog();
  System.out.println(dog.checkWeight());
 }
}

//Two seperate files main.java and Dog.java

public class Dog{
 String name = "unknown";
 String breed = "mutt";
 int weight = 5;
 
 public int checkWeight(){
  if (weight <2){
   System.out.println("under-weight");
  }else if (weight > 10){
   System.out.println("over-weight");
  }else{
   System.out.println("good");
  }return weight;
 }
}
2 Answers

Change

System.out.println(dog.checkWeight());

to

dog.checkWeight();

Currently, the method returns 5 and you print it.

Change the definition of checkWeight as follows:

public String checkWeight() {
    String health = "";
    if (weight < 2) {
        health = "under-weight";
    } else if (weight > 10) {
        health = "over-weight";
    } else {
        health = "good";
    }
    return health;
}
Related