How do I get the value true or false in the esVocal () method? Java POO

4

I defined the class Letter that has an attribute of type char. Develop a method called esVocal() that returns true or false as appropriate.

My doubt is that the esVocal() method was not done well, because here I have to use switch , and I do not know if I do it correctly.

Within each case I put a if saying if the letter variable is equal to: a, e, i, or, u is vowel, I put in that the state is true so when I enter a vowel, I get true. And in the other case I put the same thing but if the letter variable is different from the vowels, so I get false because if I enter a consonant I have to pull false, but it always pulls me false.

public class Letra {

private char letra;
private boolean valor1 ;
private boolean valor ; 



public Letra(char letra) {

    this.letra =letra;
    this.valor = false;
}

public boolean esVocal(int letra) {

    switch(letra) {

    case 1 :

         if ( letra == 'a' || letra == 'A' ||
         letra == 'e' || letra == 'E' ||
         letra == 'i' || letra == 'I' ||
         letra == 'o' || letra == 'O' ||
         letra == 'u' || letra == 'U' ) {

             valor = true;


            }

        break;

    case 2:

         if ( letra != 'a' || letra != 'A' ||
         letra != 'e' || letra != 'E' ||
         letra != 'i' || letra != 'I' ||
         letra != 'o' || letra != 'O' ||
         letra != 'u' || letra != 'U' ) {

            valor = false;
            }

        break;


        default : System.out.println("Error!");

        break;



    }

    return this.valor;


}

}
public class PruebaLetra {

public static void main(String[] args) {
    Letra l1  = new Letra ('A');


    System.out.println(l1.esVocal(2));

}

}
    
asked by computer96 31.10.2018 в 00:23
source

4 answers

3

Actually you only need to validate if the letter you defined when instantiating class Letra is a vowel, so you do not need switch .

If this condition matches a vowel, otherwise you return a value false :

          if ( letra == 'a' || letra == 'A' ||
             letra == 'e' || letra == 'E' ||
             letra == 'i' || letra == 'I' ||
             letra == 'o' || letra == 'O' ||
             letra == 'u' || letra == 'U' ) {
                 //*ES VOCAL!
                 valor = true;        
             }else{
                 //*NO ES VOCAL!
                 valor = false;
             }

this would be the modification to the class:

public class Letra {

private char letra;
private boolean valor ; 


public Letra(char letra) {

    this.letra =letra;
    this.valor = false;
}

public boolean esVocal() {

         if ( letra == 'a' || letra == 'A' ||
         letra == 'e' || letra == 'E' ||
         letra == 'i' || letra == 'I' ||
         letra == 'o' || letra == 'O' ||
         letra == 'u' || letra == 'U' ) {
             //*ES VOCAL!
             valor = true;        
         }else{
             //*NO ES VOCAL!
             valor = false;
         }

    return this.valor;

  }

} 

In this way you can instantiate and the esVocal() method will return a value true if the character is a vowel, otherwise it will return false .

Examples:

Letra l1  = new Letra ('A');
System.out.println(l1.esVocal());

exit:

true
Letra l1  = new Letra ('Z');
System.out.println(l1.esVocal());

exit:

false

I add operation using switch where the method esVocal(...) would receive the way to evaluate if the character is vowel:

public class Letra {

private char letra;
private boolean valor ; 


public Letra(char letra) {

    this.letra =letra;
    this.valor = false;
}

public boolean esVocal(int opcion) {

    switch(opcion) {

    case 1 :
         if ( letra == 'a' || letra == 'A' ||
         letra == 'e' || letra == 'E' ||
         letra == 'i' || letra == 'I' ||
         letra == 'o' || letra == 'O' ||
         letra == 'u' || letra == 'U' ) {
             //*Es vocal 
             valor = true;
         }else{
              //*NO es Vocal
             valor = false;
         }
        break;

    case 2:        
         if ( letra != 'a' && letra != 'A' &&
         letra != 'e' && letra != 'E' &&
         letra != 'i' && letra != 'I' &&
         letra != 'o' && letra != 'O' &&
         letra != 'u' && letra != 'U' ) {
            //*NO es Vocal 
            valor = false;
         }else{
            //*Es vocal 
            valor = true;  
         }

        break;

        default : System.out.println("Error!");

        break;
    }

    return this.valor;

}

}
    
answered by 31.10.2018 / 00:39
source
4

You can use the indexOf function of an ArrayList in this way

public static boolean esVocal(String letra){
    ArrayList<String> arrlist = new ArrayList<String>(4);
    arrlist.add("a");
    arrlist.add("e");
    arrlist.add("i");
    arrlist.add("o");
    arrlist.add("u");
    if(arrlist.indexOf(letra.toLowerCase()) >= 0){
        return true;
    }else{
        return false;
    }
}

You receive the letter parameter of type String and you search it within your vowel array, if it finds it returns the position of the array where the data you are looking for is found if it does not find it returns -1, and when trying to do the said search you use toLowerCase () in case you get the case to pass a capital case does not matter and perform the search correctly, I hope it helps.

    
answered by 31.10.2018 в 00:53
1

You always get false because you send a number 2 to the method and go to the case of that value. Another clarification is that you use an input parameter of type INT, but in the switch you compare it with values of type CHAR. Another thing is that the use of case seems vain if there are only two options (or it is vocal or it is not). It is enough with an if in case it is or else in the case that it is not. You can try this:

if(letra == 'a' || letra == 'A' || letra == 'e' || letra == 'E' || letra == 'i' || letra == 'I' || letra == 'o' || letra == 'O' || letra == 'u' || letra == 'U' ){
  valor = true;
}
else
  valor = false;

This would be the logic of your method. Obviously the input parameter would be char type, the rest of your method depends on you.

    
answered by 31.10.2018 в 00:34
1

As a reference, a quick and elegant way that I like to check if a character is a vowel is this:

public boolean esVocal(char character){
    return "AEIOUaeiou".indexOf(character) != -1;
}
    
answered by 31.10.2018 в 09:49