Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

Desenvolva um programa que apresente os números perfeitos existentes entre 0 e 9999. Para cada faixa de mil valores, crie uma Thread e dispare o processo para a faixa de valores correspondente.

 

Diagrama de Classes na Linguagem de Programação Java PerfectNumber.java Application.java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo PerfectNumber.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio11;

/**
 * Thread para apresentar os números perfeitos
 */
public class PerfectNumber extends Thread
{
  /**
   * Número de elementos de cada thread
   */
  public final static int LENGTH = 1000;

  /**
   * Identificador da thread
   */
  private int identifier;

  /**
   * Construtor para inicialiar a thread
   *
   * @param identifier identificador da thread
   */
  public PerfectNumber(int identifier)
  {
    this.identifier = identifier;
  }

  /**
   * Verificar se o número é perfeito
   *
   * @param number número a ser verificado
   * @return true caso o número seja perfeito, false caso contrário
   */
  private boolean isPerfect(int number)
  {
    int sum = 0;

    for(int i = 1; i < number; i++)
    {
      if((number % i) == 0)
      {
        sum = sum + i;
      }
    }

    return sum == number;
  }

  /* (non-Javadoc)
   * @see java.lang.Thread#run()
   */
  @Override
  public void run()
  {
    int begin = identifier * LENGTH + 1;
    int end = (identifier + 1) * LENGTH;

    for(int number = begin; number < end; number++)
    {
      if(isPerfect(number))
      {
        System.out.println(identifier + ": " + number);
      }
    }
  }
}

Arquivo Application.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio11;

/**
 * Classe responsável pela execução da solução
 */
public class Application
{
  /**
   * Construtor para inicializar a execução da solução
   */
  private Application()
  {

  }

  /**
   * Método principal da linguagem de programação Java
   *
   * @param args argumentos da linha de comando (não utilizado)
   */
  public static void main(String[] args)
  {
    for(int i = 0; i < 10; i++)
    {
      new PerfectNumber(i).start();
    }
  }
}

Saída

0: 6
0: 28
0: 496
8: 8128