sexta-feira, 28 de maio de 2010

Recursividade em Java

public class Recurso {

public static void main(String[] args) {

System.out.println("Resultado "+ conta(10));

}

public static int conta(int i)
{
System.out.print(i);
if( i==0 )return 1;
else return (1 + conta(i-1));
}

}

quarta-feira, 12 de maio de 2010

Mudanças

Olá pessoal o blog umcastec.blogspot.com agora tem seu próprio domínio umcastec.com/blog
lembrando que esse blog continuará e aos poucos irei levar os conteúdos para o outro blog!

Obrigado pelas visitam e voltem sempre!

Muito Obrigado!

Att:
Uandisson M. C.

quarta-feira, 5 de maio de 2010

Gerador de senha em Java



Bom pessoal esse progrma foi feito em Java.
Para executa-lo é preciso ter istalado na máquina a JVM (Java Virtual Machine)


Feito por: UMC

Manipulador de arquivos em Java


Bom pessoal esse progrma foi feito em Java trabalhando principalmente com a Classe File.
Leva o nome de Organizador!
Para executa-lo é preciso ter istalado na máquina a JVM (Java Virtual Machine)


Feito por: UMC

quinta-feira, 29 de abril de 2010

Escrever valor monetário por extenso

package umcastec;

import java.math.BigDecimal;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.Iterator;

public class Escrver_Extenso{
private ArrayList nro;


private BigInteger num;
private String Qualificadores[][] = {{"centavo", "centavos"},

{"", ""}, {"mil", "mil"},
{"milhão", "milhões"},
{"bilhão", "bilhões"},
{"trilhão", "trilhões"},
{"quatrilhão", "quatrilhões"}, {"quintilhão", "quintilhões"},
{"sextilhão", "sextilhões"}, {"septilhão", "septilhões"}};
private String Numeros[][] = {
{"zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete",
"oito", "nove", "dez", "onze", "doze", "treze", "quatorze",
"quinze", "desesseis", "desessete", "dezoito", "desenove"},
{"vinte", "trinta", "quarenta", "cinquenta", "sessenta",
"setenta", "oitenta", "noventa"},
{"cem", "cento", "duzentos", "trezentos", "quatrocentos",
"quinhentos", "seiscentos", "setecentos", "oitocentos",
"novecentos"}};
/** * Construtor */
public JExtenso() {
nro = new ArrayList();

}
/** * Construtor
*
* @param dec
* valor para colocar por extenso */
public JExtenso(BigDecimal dec) {
this();
setNumber(dec);
}
/**
* Constructor for the JExtenso object
* @param dec
* valor para colocar por extenso */
public JExtenso(double dec) {
this();
setNumber(dec); }
/**
* Sets the Number attribute of the JExtenso object
*
* @param dec
* The new Number value */
public void setNumber(BigDecimal dec) {
// Converte para inteiro arredondando os centavos
num = dec.setScale(2, BigDecimal.ROUND_HALF_UP).multiply(
BigDecimal.valueOf(100)).toBigInteger();
// Adiciona valores
nro.clear();
if (num.equals(BigInteger.ZERO)) {
// Centavos
nro.add(new Integer(0));
// Valor
nro.add(new Integer(0));
} else {
// Adiciona centavos
addRemainder(100);
// Adiciona grupos de 1000
while (!num.equals(BigInteger.ZERO)) {
addRemainder(1000);
}
}
}
public void setNumber(double dec) {
setNumber(new BigDecimal(dec)); }
/** * Description of the Method */
public void show() {
Iterator valores = nro.iterator();
while (valores.hasNext()) {
System.out.println(((Integer) valores.next()).intValue());
}
System.out.println(toString()); }
/**
* Description of the Method *
* @return Description of the Returned Value */
@Override public String toString() {
StringBuffer buf = new StringBuffer();
int numero = ((Integer) nro.get(0)).intValue();
int ct;
for (ct = nro.size() - 1; ct > 0; ct--) {
// Se ja existe texto e o atual n�o � zero
if (buf.length() > 0 && !ehGrupoZero(ct)) {
buf.append(" e "); }
buf.append(numToString(((Integer) nro.get(ct)).intValue(), ct)); }
if (buf.length() > 0) {
if (ehUnicoGrupo()) {
buf.append(" de "); }
while (buf.toString().endsWith(" ")) {
buf.setLength(buf.length() - 1); }
if (ehPrimeiroGrupoUm()) {
buf.insert(0, ""); }
if (nro.size() == 2 && ((Integer) nro.get(1)).intValue() == 1) {
buf.append(" real");
} else {
buf.append(" reais"); }
if (((Integer) nro.get(0)).intValue() != 0) {
buf.append(" e "); } }
if (((Integer) nro.get(0)).intValue() != 0) {
buf.append(numToString(((Integer) nro.get(0)).intValue(), 0)); }
return buf.toString(); }
private boolean ehPrimeiroGrupoUm() {
if (((Integer) nro.get(nro.size() - 1)).intValue() == 1) {
return true; } return false; }
/**
* Adds a feature to the Remainder attribute of the JExtenso object
*
* @param divisor
* The feature to be added to the Remainder attribute */
private void addRemainder(int divisor) {
// Encontra newNum[0] = num modulo divisor, newNum[1] = num dividido
// divisor
BigInteger[] newNum = num.divideAndRemainder(BigInteger.valueOf(divisor));
// Adiciona modulo
nro.add(new Integer(newNum[1].intValue()));
// Altera numero
num = newNum[0]; }
/** * Description of the Method *
* @param ps
* Description of Parameter
* @return Description of the Returned Value */
private boolean temMaisGrupos(int ps) {
for (; ps > 0; ps--) {
if (((Integer) nro.get(ps)).intValue() != 0) {
return true; } }
return false; }
/** * Description of the Method
*
* @param ps
* Description of Parameter
* @return Description of the Returned Value */
private boolean ehUltimoGrupo(int ps) {
return (ps > 0) && ((Integer) nro.get(ps)).intValue() != 0
&& !temMaisGrupos(ps - 1); }
/**
* Description of the Method
*
* @return Description of the Returned Value */
private boolean ehUnicoGrupo() {
if (nro.size() <= 3) {
return false; }
if (!ehGrupoZero(1) && !ehGrupoZero(2)) {
return false; }
boolean hasOne = false;
for (int i = 3; i < hasone =" true;">= nro.size()) {
return true;
} return ((Integer) nro.get(ps)).intValue() == 0; }
/** * Description of the Method
*
* @param numero
* Description of Parameter
* @param escala
*Description of Parameter
* @return Description of the Returned Value */
private String numToString(int numero, int escala) {
int unidade = (numero % 10);
int dezena = (numero % 100); // * nao pode dividir por 10 pois
// verifica de 0..19
int centena = (numero / 100);
StringBuffer buf = new StringBuffer();
if (numero != 0) {
if (centena != 0) {
if (dezena == 0 && centena == 1) {
buf.append(Numeros[2][0]);
} else {
buf.append(Numeros[2][centena]); }
} if ((buf.length() > 0) && (dezena != 0)) {
buf.append(" e "); }
if (dezena > 19) {
dezena /= 10;
buf.append(Numeros[1][dezena - 2]);
if (unidade != 0) {
buf.append(" e ");
buf.append(Numeros[0][unidade]);
}
} else if (centena == 0 || dezena != 0) {
buf.append(Numeros[0][dezena]);
}
buf.append(" ");
if (numero == 1) {
buf.append(Qualificadores[escala][0]);
} else {
buf.append(Qualificadores[escala][1]);
}
}
return buf.toString();
} }


Main.java
package umcastec;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {

String extenso;

Double totProd = 1000.0;

JExtenso je = new JExtenso(new BigDecimal(totProd));
extenso = je.toString().toUpperCase();

System.out.println(extenso + " - "+ je);

//sída console

//UM MIL REAIS - um mil reais

//

} }
Código enviado por Ciro S. Santos

Criando uma ArrayList do tipo Inteiro

ArrayList suporta arrays dinâmicos, que pode crescer conforme necessário.
Em Java, arrays são padrão de um comprimento fixo. Depois de arrays são criados,
eles não podem crescer ou encolher, o que significa que você deve saber antecipadamente
quantos elementos uma matriz irá realizar. Mas, às vezes, você não pode saber com
precisão o tempo de execução até o tamanho de uma matriz que você precisa. Para lidar com esta situação, a estrutura das coleções define ArrayList.



package umcastec;

import java.util.ArrayList;

public class Principal {

public static void main(String[] args) {
// TODO code application logic here

ArrayList array_list_int = new ArrayList();
//adiciona itens na lista manualmente
array_list_int.add(0);
array_list_int.add(1);
array_list_int.add(2);
array_list_int.add(3);
//imprime de acordo com a posição informada manualmente no array list
System.out.println("Posição 0 :"+array_list_int.get(0));
System.out.println("Posição 1 :"+array_list_int.get(1));
//----------
saída no console
//Posição 0 :0
//Posição 1 :1
//----------

//exibe todos valores da lista
for(int i = 0; i "<" array_list_int.size(); i++){
//dessa for irá imprimir todos os registros do array list
System.out.println(array_list_int.get(i));
//----------saída no console
// 0
// 1
// 2
// 3
//----------
}
}
}

Obs: no for() o sinal "<" esta entre aspas!
Porque o sistema do Blogger interpreta como tag de HTML