DEV Community

Beatriz Maciel
Beatriz Maciel

Posted on • Edited on

1 1

HackerRank #23 | Instanceof keyword | 🇧🇷

Essa publicação é só para explicar um objeto simples (mas que eu não conhecia) chamado instanceof. A sua tradução para português seria "é um" ou "é uma" e é usado como um construtor/identificador.

Neste problema do HackerRank, ele pede para que, em três linhas, você consiga dizer que um element é Student, Rockstar ou Hacker. Como já existem essas classes declaradas, basta escrevermos dentro do if que cada um desses elementos é uma dessas classes.

=========

Sendo assim, o que é implementado dessa forma:

         if(element instanceof Student)
            a++;
         if(element instanceof Rockstar)
            b++;
         if(element instanceof Hacker)
            c++;
Enter fullscreen mode Exit fullscreen mode

Pode ser também lido dessa forma:

         if(element é um Student)
            a++;
         if(element é um Rockstar)
            b++;
         if(element é um Hacker)
            c++;
Enter fullscreen mode Exit fullscreen mode

=========

O código final fica assim:

import java.util.*;

class Student{}
class Rockstar{}
class Hacker{}

public class InstanceOFTutorial{

   static String count(ArrayList mylist){
      int a = 0,b = 0,c = 0;
      for(int i = 0; i < mylist.size(); i++){
         Object element=mylist.get(i);
         if(element instanceof Student)
            a++;
         if(element instanceof Rockstar)
            b++;
         if(element instanceof Hacker)
            c++;
      }
      String ret = Integer.toString(a)+" "+ Integer.toString(b)+" "+ Integer.toString(c);
      return ret;
   }

   public static void main(String []args){
      ArrayList mylist = new ArrayList();
      Scanner sc = new Scanner(System.in);
      int t = sc.nextInt();
      for(int i=0; i<t; i++){
         String s=sc.next();
         if(s.equals("Student"))mylist.add(new Student());
         if(s.equals("Rockstar"))mylist.add(new Rockstar());
         if(s.equals("Hacker"))mylist.add(new Hacker());
      }
      System.out.println(count(mylist));
   }
}
Enter fullscreen mode Exit fullscreen mode

=========

Referências

============

Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.