conjuntos donde se añade elementos de A y B y luego se haya el complemento, la union la interseccion etc utilizando hasthset.
para esto primero creamos una clase llamada hast y ponemos el siguiente codigo:
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import javax.swing.JOptionPane;
/**
*
* @author andres2288
*/
public class hast {
public static void ingresa (){
Set h=new HashSet();
h.add("hola");
h.add("como estas");
h.add("hola");
for(String x:h)
System.out.println("x"+x);
}
public Set ACOMPLEMENTOUsetomaUcomouniverso(LinkedList A,LinkedList B){
Set h=new HashSet();
for(String x:B){
int cont=0;
for(String y:A){
if(x.equals(y)){
// JOptionPane.showMessageDialog(null, x);
cont+=1;
}
}
if(cont==0){
h.add(x);
cont=0;
}
}
return h;
}
public Set AsubconjuntoB(LinkedList A,LinkedList B){
Set h=new HashSet();
for(String x:B){
for(String y:A){
if(x.equals(y)){
// JOptionPane.showMessageDialog(null, x);
h.add(x);
}
}
}
return h;
}
public Set AinterseccionB(LinkedList A,LinkedList B){
Set h=new HashSet();
for(String x:A){
for(String y:B){
if(x.equals(y)){
// JOptionPane.showMessageDialog(null, x);
h.add(x);
}
}
}
return h;
}
public Set AdiferenciasimetricaB(LinkedList A,LinkedList B){
Set h=new HashSet();
for(String x:A){
int cont=0;
for(String y:B){
if(x.equals(y)){
// JOptionPane.showMessageDialog(null, x);
cont+=1;
}
}
if(cont==0){
h.add(x);
cont=0;
}
}
// ahora miramos el b
for(String x:B){
int cont=0;
for(String y:A){
if(x.equals(y)){
// JOptionPane.showMessageDialog(null, x);
cont+=1;
}
}
if(cont==0){
h.add(x);
cont=0;
}
}
return h;
}
public Set AdiferenciaB(LinkedList A,LinkedList B){
Set h=new HashSet();
for(String x:A){
int cont=0;
for(String y:B){
if(x.equals(y)){
// JOptionPane.showMessageDialog(null, x);
cont+=1;
}
}
if(cont==0){
h.add(x);
cont=0;
}
}
return h;
}
public Set AUnionB(LinkedList A,LinkedList B){
Set h=new HashSet();
for (int i = 0; i < A.size(); i++) {
h.add(A.get(i));
}
for (int i = 0; i < B.size(); i++) {
h.add(B.get(i));
}
return h;
}
public static void main(String[] args) {
ingresa ();
}
}
luego creamos un jframe veanse del siguiente codigo e imagen para guiarse, para saber ustedes que deben agregar que botones ,textos y areas en el jframe deben agregar
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
/**
*
* @author andres2288
*/
public class visual extends javax.swing.JFrame {
hast t =null;
LinkedList listaA= new LinkedList();
LinkedList listaB= new LinkedList();
/**
* Creates new form visual
*/
public visual() {
t= new hast();
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
txt1 = new javax.swing.JTextField();
txt2 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
area1 = new javax.swing.JTextArea();
B1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("añadir elementos A");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("añadir elementos B");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
area1.setColumns(20);
area1.setRows(5);
jScrollPane1.setViewportView(area1);
B1.setText("AUB");
B1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B1ActionPerformed(evt);
}
});
jButton3.setText("A/B");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("auto");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("AnB");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("A∁");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("AΔB");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("A ⊆ B");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4)
.addGap(25, 25, 25))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
.addComponent(txt1))
.addGap(0, 142, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
.addComponent(txt2))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(B1)
.addGap(29, 29, 29)
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton8))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(B1)
.addComponent(jButton3)
.addComponent(jButton5)
.addComponent(jButton6))
.addGap(48, 48, 48)
.addComponent(jButton8)
.addGap(32, 32, 32)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))
.addGroup(layout.createSequentialGroup()
.addGap(90, 90, 90)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton7)
.addContainerGap())))
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(!txt1.getText().equals("")){
listaA.add(txt1.getText());
}
area1.setText(String.valueOf("elementos de A"+listaA+"\n"+"elementosd de B"+listaB));
txt1.setText("");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(!txt2.getText().equals("")){
listaB.add(txt2.getText());
}
area1.setText(String.valueOf("elementos de A"+listaA+"\n"+"elementosd de B"+listaB));
txt2.setText("");
}
private void B1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area1.setText(String.valueOf("elementos de A"+listaA+"\n"+"elementosd de B"+listaB+"\n"+t.AUnionB(listaA, listaB)));
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area1.setText(String.valueOf("elementos de A"+listaA+"\n"+"elementosd de B"+listaB+"\n"+t.AdiferenciaB(listaA, listaB)));
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
listaA.add("8");
listaA.add("1");
listaA.add("2");
listaA.add("3");
listaA.add("4");
listaA.add("5");
listaB.add("6");
listaB.add("7");
listaB.add("8");
listaB.add("9");
listaB.add("1");
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area1.setText(String.valueOf("elementos de A"+listaA+"\n"+"elementosd de B"+listaB+"\n"+t.AinterseccionB(listaA, listaB)));
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area1.setText(String.valueOf("se tomo B como universo "+"\n"+"elementos de A"+listaA+"\n"+"elementosd de B"+listaB+"\n"+t.ACOMPLEMENTOUsetomaUcomouniverso(listaA, listaB)));
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area1.setText(String.valueOf("se tomo B como universo "+"\n"+"elementos de A"+listaA+"\n"+"elementosd de B"+listaB+"\n"+t.AdiferenciasimetricaB(listaA, listaB)));
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area1.setText(String.valueOf("se tomo B como universo "+"\n"+"elementos de A"+listaA+"\n"+"elementosd de B"+listaB+"\n"+t.AsubconjuntoB(listaA, listaB)));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//
/*
* 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(visual.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(visual.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(visual.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(visual.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new visual().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton B1;
private javax.swing.JTextArea area1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txt1;
private javax.swing.JTextField txt2;
// End of variables declaration
}
y ahora la imagen y listo :
0 Comments