Logica ---> Cadena.
Presentacion ---> Formulario.
Descarga... click acá
Logica --->Cadena
- /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Logica;
import javax.swing.JOptionPane;
/**
*
* @author microsoft
*/
public class Cadena {
private String frase;
public Cadena(String frase) {
this.frase = frase;
}
public Cadena() {
}
public String getFrase() {
return frase;
}
public void setFrase(String frase) {
this.frase = frase;
}
public int contadordeletras ()
{
int contador=0,longitud;
longitud=frase.length();
for(int i=0;i<longitud;i++)
{
if(frase.charAt(i) >= 'a' && frase.charAt(i)<='z' ||frase.charAt(i) >= 'A' && frase.charAt(i)<='Z')
{
if(frase.charAt(i) != 'a'&&frase.charAt(i)!='e'&&frase.charAt(i)!='i'&&frase.charAt(i)!='o'&&frase.charAt(i)!='u' && frase.charAt(i)!= 'A'&&frase.charAt(i)!='E'&&frase.charAt(i)!='I'&&frase.charAt(i)!='O'&&frase.charAt(i)!='U')
{
contador++;
}
}
}
return contador;
}
public boolean palindromo()
{
int largo;
largo = frase.length();
String revez = "";
for (int j=(largo - 1) ; j>=0; j--)
{
revez = revez + frase.charAt(j);
}
if (revez.equals(frase))
{
return (true);
}else{
return (false);
}
}
}
- /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Formulario.java
*
* Created on 1/03/2011, 10:39:12 AM
*/
package Presentacion;
/**
*
* @author microsoft
*/
import Logica.Cadena;
import javax.swing.JOptionPane;
public class Formulario extends java.applet.Applet {
Cadena obj1 = new Cadena();
/** Initializes the applet Formulario */
public void init() {
try {
this.setSize(520,300);
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lbl_titulo = new javax.swing.JLabel();
btn_contador = new javax.swing.JButton();
txt_frase = new javax.swing.JTextField();
lbl_letrero = new javax.swing.JLabel();
btn_palindromo = new javax.swing.JButton();
lbl_titulo.setFont(new java.awt.Font("Comic Sans MS", 1, 24)); // NOI18N
lbl_titulo.setText(".:: EJERCICIO CON FRASES ::.");
btn_contador.setText("CONTAR LETRAS");
btn_contador.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_contadorActionPerformed(evt);
}
});
lbl_letrero.setText("Frase :");
btn_palindromo.setText("VERIFICAR SI ES PALINDROMO");
btn_palindromo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_palindromoActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(lbl_letrero)
.addGap(38, 38, 38)
.addComponent(txt_frase, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(40, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(lbl_titulo)
.addContainerGap(74, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(129, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_palindromo, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_contador, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(120, 120, 120))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(lbl_titulo, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(41, 41, 41)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbl_letrero)
.addComponent(txt_frase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(49, 49, 49)
.addComponent(btn_contador)
.addGap(32, 32, 32)
.addComponent(btn_palindromo)
.addContainerGap(32, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
}// </editor-fold>//GEN-END:initComponents
private void btn_contadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_contadorActionPerformed
obj1.setFrase(txt_frase.getText());
JOptionPane.showMessageDialog(null, "La palabra tiene " + obj1.contadordeletras() + " letras") ;
}//GEN-LAST:event_btn_contadorActionPerformed
private void btn_palindromoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_palindromoActionPerformed
obj1.setFrase(txt_frase.getText());
if (obj1.palindromo())
{
JOptionPane.showMessageDialog(null, "La palabra es PALINDROMA");
}else{
JOptionPane.showMessageDialog(null, "La palabra NO es PALINDROMA");
}
}//GEN-LAST:event_btn_palindromoActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btn_contador;
private javax.swing.JButton btn_palindromo;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lbl_letrero;
private javax.swing.JLabel lbl_titulo;
private javax.swing.JTextField txt_frase;
// End of variables declaration//GEN-END:variables
}
No hay comentarios:
Publicar un comentario