Desenvolva um segmento que apresente os n primeiros termos da sequência yk+1 = yk + 2, sendo k = 1, 2, 3, ..., n e y1 = 1. O segmento deverá fazer uma pausa de um segundo antes da impressão de cada número.
Crie uma classe e estenda Thread. Como alternativa, você pode implementar a interface Runnable.
Substitua o método run() de Thread. É aí que entrará o código que exibirá os números.
Crie um loop for que seja executado n vezes.
Use o método estático Thread.sleep() para fazer uma pausa. Um número do tipo long representará os milissegundos.
/**
* Copyright (C) 2009/2026 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Classe para apresentar os elementos da serie
*/
public class SeriesThread extends Thread
{
/**
* Numero de termos da serie
*/
private int termos;
/**
* Construtor padrao
*
* @param termos numero de termos da serie
*/
public SeriesThread(int termos)
{
this.termos = termos;
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run()
{
int y = 1;
for(int k = 1; k <= this.termos; k++)
{
System.out.println("Y" + k + ": " + y);
y = y + 2;
try
{
Thread.sleep(1000);
}
catch(InterruptedException expection)
{
expection.printStackTrace();
}
}
}
}/**
* Copyright (C) 2009/2026 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Classe responsavel pela execucao da Thread
*/
public class Application
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
new SeriesThread(5).start();
}
}