Programa Leer Cadenas

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1de 13

Portada

Tecnológico nacional de México


Instituto Tecnológico de Villahermosa

Ing. Sistemas Computacionales

Lenguajes autónomas (12:00-13:00)

Tarea 2.2: Practica 2.1

Alumno: Kevin Cuevas Aguilar

Profesor: Manuel Antonio Rodríguez Magaña

Fecha de entrega: 26/02/2022


Reporte

Trabaje con el IDE de neatBeants en el lenguaje de java porque se me facilita su uso además de ser
el único lenguaje de programación que domino.
Para realizar este programa utilice un formulario jFrame donde diseñe la interfaz de mi aplicación

Utilizando diversos métodos y botones realice lo solicitado en la practica


Adjunto el código y su visualización
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Practica2_1 extends javax.swing.JFrame {

String cadena1 = "", cadena2 = "", inverso1 = "", inverso2 = "", comparacion = "";
int longitud1 = 0;
String resul = "";

public Practica2_1() {
initComponents();
this.setLocationRelativeTo(null);
this.setLayout(null);
}

//Metodo limpiar
private void limpiar() {
cadena1txt.setText(null);
cadena2txt.setText(null);
long1.setText(null);
inversolbl.setText(null);
inversolbl2.setText(null);
longitudlbl.setText(null);
comparacionlbl.setText(null);
}
//Metodo comparación
private void comparar() {
if (cadena1txt.getText().equals(cadena2txt.getText())) {
comparacionlbl.setText("iguales");
} else {
comparacionlbl.setText("diferentes");
}
}

//Metodo longitud
private int longitud(String cadena1) {
for (int i = 0; i < cadena1.length(); i++) {
if (Character.isLetter(cadena1.charAt(i))) {
longitud1++;
}
}
return longitud1;
}

//Metodo invertir
private String invertir(String cad){
for(int i=cad.length()-1; i>=0; i--){
char actual=cad.charAt(i);
resul=resul+actual;
}
return resul;
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

textArea1 = new java.awt.TextArea();


jLabel9 = new javax.swing.JLabel();
inversolbl1 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
cadena1txt = new javax.swing.JTextField();
cadena2txt = new javax.swing.JTextField();
longitudlbl = new javax.swing.JLabel();
botoniniciar = new javax.swing.JButton();
botonlimpiar = new javax.swing.JButton();
botonsalir = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
comparacionlbl = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
inversolbl = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
long1 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
inversolbl2 = new javax.swing.JLabel();

jLabel9.setText("Inverso");

inversolbl1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N


inversolbl1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
inversolbl1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N


jLabel2.setText("Practica 2.1");

jLabel3.setText("Cadena 1:");

jLabel4.setText("Cadena 2:");

cadena1txt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cadena1txtActionPerformed(evt);
}
});

cadena2txt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cadena2txtActionPerformed(evt);
}
});

longitudlbl.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N


longitudlbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);
longitudlbl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

botoniniciar.setText("iniciar");
botoniniciar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botoniniciarActionPerformed(evt);
}
});

botonlimpiar.setText("limpiar");
botonlimpiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botonlimpiarActionPerformed(evt);
}
});

botonsalir.setText("Salir");
botonsalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
botonsalirActionPerformed(evt);
}
});

jLabel5.setText("Longitud 2");

comparacionlbl.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N


comparacionlbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);
comparacionlbl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel6.setText("Comparacion");

inversolbl.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N


inversolbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);
inversolbl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel7.setText("Inverso cadena 1");

long1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N


long1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
long1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

jLabel8.setText("Longitud 1");

jLabel10.setText("Inverso cadena 2");

inversolbl2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N


inversolbl2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
inversolbl2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1))
.addComponent(cadena1txt, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(104, 104, 104)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cadena2txt, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)))
.addComponent(jLabel6)
.addComponent(longitudlbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(comparacionlbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(inversolbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel8)
.addComponent(long1, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
.addComponent(jLabel10)
.addComponent(inversolbl2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel7)))
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(botoniniciar, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(botonlimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(botonsalir, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cadena1txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cadena2txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(long1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(longitudlbl, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(comparacionlbl, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(inversolbl, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(inversolbl2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(botoniniciar)
.addComponent(botonlimpiar)
.addComponent(botonsalir))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void cadena1txtActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void cadena2txtActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void botonsalirActionPerformed(java.awt.event.ActionEvent evt) {


Icon icono = new ImageIcon(getClass().getResource("adios.gif"));
JOptionPane.showMessageDialog(null, "\n by: Kevin C.A :)", "¡Hasta luego!", JOptionPane.PLAIN_MESSAGE, icono);
System.exit(0);
}

private void botonlimpiarActionPerformed(java.awt.event.ActionEvent evt) {


limpiar();
}

private void botoniniciarActionPerformed(java.awt.event.ActionEvent evt) {


//cadena1 = cadena1txt.getText();
// cadena2 = cadena2txt.getText();
comparacion="";
comparar();
longitud1=0;
long1.setText("la cadena 1 tiene: " + longitud(cadena1txt.getText()) + " caracteres");
longitud1=0;
longitudlbl.setText("la cadena 2 tiene: " + longitud(cadena2txt.getText()) + " caracteres");
resul="";
inversolbl.setText(invertir(cadena1txt.getText()));
resul="";
inversolbl2.setText(invertir(cadena2txt.getText()));
}

public static void main(String args[]) {


/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Practica2_1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Practica2_1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Practica2_1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Practica2_1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Practica2_1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton botoniniciar;
private javax.swing.JButton botonlimpiar;
private javax.swing.JButton botonsalir;
private javax.swing.JTextField cadena1txt;
private javax.swing.JTextField cadena2txt;
private javax.swing.JLabel comparacionlbl;
private javax.swing.JLabel inversolbl;
private javax.swing.JLabel inversolbl1;
private javax.swing.JLabel inversolbl2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JLabel long1;
private javax.swing.JLabel longitudlbl;
private java.awt.TextArea textArea1;
// End of variables declaration
}
Para la parte de la demostración utilice nombres como primer ejemplo con las cadenas “kevin” y
“manuel” para el ejemplo con cadenas distintas
Aquí podemos apreciar el resultado de su longitud su comparación y sus inversos
Como segundo ejemplo utilice una secuencia de letras “abba” iguales para corroborar que el
programa acepta y responde diferente según las cadenas introducidas

De igual manera nos muestra su longitud que es igual, su comparativa afirmando la igualdad y sus
inversos que en este caso se mantiene de igual manera

Por ultimo el botón de salir que nos ayuda a finalizar la operacion y además da los créditos de mi
programa

También podría gustarte