LÓGICA:
Clase Cadena.
package Logica;
public class Cadena
{
private String frace;
public Cadena()
{
}
public Cadena(String frace)
{
this.frace = frace;
}
public String getFrace()
{
return frace;
}
public void setFrace(String frace)
{
this.frace = frace;
}
public int contadordeletras()
{
int contador=0,longitud=0;
char letra;
longitud=frace.length();
for(int i=0;i<longitud;i++)
{
letra=frace.charAt(i);
if((letra>='a')&&(letra<='z')||(letra>='A')&&(letra<='z'))
if((letra!='a')&&(letra!='e')&&(letra!='i')&&(letra!='o')&&(letra!='u'))
if((letra!='A')&&(letra!='E')&&(letra!='I')&&(letra!='O')&&(letra!='U'))
contador++;
}
return contador;
}
}
PRESENTACIÓN:
Clase Nombre:
package Presentacion;
import Logica.Cadena;
import javax.swing.JOptionPane;
public class Nombre extends java.applet.Applet
{
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
this.setSize(400,300);
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
lblletrero = new javax.swing.JLabel();
txtfrace = new javax.swing.JTextField();
btncontador = new javax.swing.JButton();
setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 51, 51));
jLabel1.setText("Ejercicio con frace");
lblletrero.setBackground(new java.awt.Color(51, 255, 255));
lblletrero.setForeground(new java.awt.Color(153, 0, 51));
lblletrero.setText("Digite frace");
btncontador.setBackground(new java.awt.Color(51, 51, 255));
btncontador.setForeground(new java.awt.Color(255, 0, 255));
btncontador.setText("contar letra");
btncontador.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btncontadorActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblletrero, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(133, 133, 133)
.addComponent(txtfrace, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28))
.addGroup(layout.createSequentialGroup()
.addGap(128, 128, 128)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btncontador)
.addComponent(jLabel1))
.addContainerGap(169, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(70, 70, 70)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblletrero)
.addComponent(txtfrace, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(72, 72, 72)
.addComponent(btncontador)
.addGap(67, 67, 67))
);
}
private void btncontadorActionPerformed(java.awt.event.ActionEvent evt) {
Cadena obj1=new Cadena();
obj1.setFrace(this.txtfrace.getText());
JOptionPane.showMessageDialog(null,obj1.contadordeletras() );
}
private javax.swing.JButton btncontador;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblletrero;
private javax.swing.JTextField txtfrace;
}