Java conditional

Asked

Viewed 34 times

-2

package aplicativo_enel;

public class A1_Residencial {
    //Atributos entrada de dados
    int imposto1;
    final int ICMS1 = 0;
    private float leitura;
    private final float TE = 0.25588f;
    private final float TUSD = 0.25971f;
    private final float COSIP = 9.70F;
    
    int icmsResidencial() {
        return imposto1;
    }
    
    String situacao( ) {
        if  (icmsResidencial() <= ICMS1) {
            System.out.println("Você esta Isenta de imposto ICMS");
        }else if(icmsResidencial() > ICMS1 && icmsResidencial() == ICMS1) {
            System.out.println("12% de ICMS");
        }else {
            System.out.println("25% de ICMS");
        }       
    }
            
    // Construtores 
    public A1_Residencial () {
        this.leitura = 0.0f;
    }

    public A1_Residencial (float leitura) {
        this.leitura = leitura;
    }

    //Métodos de acesso
    public float getLeitura() {
        return this.leitura;
    }

    public float getTe() {
        return this.TE;
    }

    public float getTusd() {
        return this.TUSD;
    }

    public void setLeitura(float leitura) {
        this.leitura = leitura;
    }

    // Comportamento - saída
    public float consumo () {
        return getTe() + getTusd() * getLeitura();
    }

    public float PrecoParcial() {
        return consumo() + COSIP;
    }
    
    @Override
    public String toString(){
        String residencial = "Conta Enel";
        residencial += "\nLeitura " + getLeitura();
        residencial += "\nConsumo + Eletrica + Sistema de Distribuição R$" + consumo();
        residencial += "\nValor Parcial R$" + PrecoParcial();
        residencial += "\nIMCS " + situacao();
                
        return residencial;     
    }
}

Hello guys, my code makes a mistake and talks "this method should return a string type result". The error is in String situacao().... Could someone help me how to fix this please ?

  • Your if is weird. First you test if icmsResidencial() <= ICMS1 (up here, ok). But then you do else if(icmsResidencial() > ICMS1 && icmsResidencial() == ICMS1) - that is, if icmsResidencial is larger and equal to ICMS1 (There’s no way he’s bigger and equal at the same time, so you’ll never get into that if). And as the first if already tests whether it is less or equal, the only alternative that remains for the else is that he’s bigger, so that’s else if doesn’t make sense.

1 answer

-1

Your method must return a string. It is printing in the method itself instead of returning the text as a result.

String situacao( ) {
    String retorno;

    if  (icmsResidencial() <= ICMS1) {
        retorno = "Você esta Isenta de imposto ICMS";
    }else if(icmsResidencial() > ICMS1 && icmsResidencial() == ICMS1) {
        retorno = "12% de ICMS";
    }else {
        retorno = "25% de ICMS";
    }       

    return retorno;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.