Pregunta

As the title already states, I am writing a Java class that is supposed to create dogs.

The code is in Swedish, but it is not too hard to understand.

My problem is in the "setSvanslängd" method which basically means "setTailLength".

The method is supposed to look at the breed (ras) and if the breed is a tax it should set the tail Length to 3.7, but if the dog is a different breed it should calculate the weight (vikt) x age (ålder) and then return the value to the variable "tailLength" (svanslängd).

But when I try to create three dogs it only returns 0.0 for the tailLength.

Where or what is the problem...?

Thank you in advance!

This is the code so far:

public class Hund {

    private String namn;
    private int ålder;
    private int vikt;
    private String ras;
    private double svanslängd;


    public Hund(String namn , int ålder , int vikt, String ras) {
        this.namn = namn;
        this.ålder = ålder;
        this.vikt = vikt;
        this.ras = ras;
    }

    public double setSvanslängd(){
        if(ras.equalsIgnoreCase("tax")) {
            svanslängd = 3.7;
            return svanslängd;
        } else {
            svanslängd = (ålder * vikt)/10.0;
        }
        return svanslängd;
    }

    public String getNamn(){
        return namn;
    }

    public int getÅlder(){
        return ålder;
    }

    public int getVikt(){
        return vikt;
    }

    public String getRas(){
        return ras;
    }

    public double getSvanslängd(){
        return svanslängd;
    }

    public String toString(){
        return namn+" "+ras+" "+ålder+" år "+vikt+" kg Svans= "+svanslängd;
    }

    public static void main (String[] args) {
        Hund hund1 = new Hund ("Fido", 2, 3, "tax" );
        Hund hund2 = new Hund ("Peter", 4, 6, "varg");
        Hund hund3 = new Hund ("Roger", 2, 3, "chiwava");

        System.out.println (hund1);
        System.out.println (hund2);
        System.out.println (hund3);
    }

}
¿Fue útil?

Solución

This is because you have a method that you never call.

Although you can add calls to setSvanslängd method to your main to fix this problem, this is not the best approach: it would be much better to set the length of the tail in the constructor, and never touch it again. This would be a much more intuitive solution:

public Hund(String namn , int ålder , int vikt, String ras) {
    this.namn = namn;
    this.ålder = ålder;
    this.vikt = vikt;
    this.ras = ras;
    // Set the length of the tail
    if(ras.equalsIgnoreCase("tax")) {
        svanslängd = 3.7;
    }else{
        svanslängd = (ålder * vikt)/10.0;
    }
}

Otros consejos

The problem is simple: the method is never called. This method shouldn't be public. It should be private and be called by the constructor: an object should be in a correct state from the start. There's no reason to construct it in an intermediate state and to force the caller to call an additional method to make the state of the dog correct. And the method should return void as well.

Side note: please write your code in English. Most keyboards don't even have the characters used in your code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top