Friday, January 2, 2015

Program - FLAMES

I have written FLAMES program around 6 years back when i was started to learn Java programming.
Initially i have written in 'C' language and later the same logic to be used in java program with JFrame for GUI. This program seems to be very easy for those who are having good understanding and basic knowledge in java, but i was struggled to find out this logic when was started this in 'C' language.

I found an issue in this program just before posting this code. Can you please trace and find out the issue?


Try below and find the relationship with your loved one

Name 1:
Name 2:



import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Flames extends JFrame implements ActionListener{
JLabel lbl1 = new JLabel("Name 1");
JLabel lbl2 = new JLabel("Name 2");
JTextField fld1 = new JTextField();
JTextField fld2 = new JTextField();;
JButton btn = new JButton("Submit");
JLabel rlt = new JLabel("");
public Flames()  throws Throwable{
super("Flames");
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.LIGHT_GRAY);
setBounds(100, 100, 300, 300);
lbl1.setBounds(50, 40, 100, 40);
lbl2.setBounds(50, 100, 100, 40);
fld1.setBounds(120, 40, 150, 30);
fld2.setBounds(120, 100, 150, 30);
btn.setBounds(110,150,100,30);
rlt.setBounds(110,200,100,30);
rlt.setFont(new Font("Courier New", Font.BOLD, 18));
rlt.setForeground(Color.RED);
add(btn);
add(lbl1);
add(lbl2);
add(fld1);
add(fld2);
add(rlt);
btn.addActionListener(this);
}
  public static void main(String args[])  throws Throwable {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new Flames();
  }
  public void actionPerformed(ActionEvent e) {
rlt.setText("");
String name1 = fld1.getText().toUpperCase();
String name2 = fld2.getText().toUpperCase();
if(name1 ==null || name1.equals("")) {
JOptionPane.showMessageDialog(null, "Name 1 must not be empty.", "Error", JOptionPane.ERROR_MESSAGE);
} else if(name2 ==null || name2.equals("")) {
JOptionPane.showMessageDialog(null, "Name 2 must not be empty.", "Error", JOptionPane.ERROR_MESSAGE);
} else if(name1.equals(name2)) {
JOptionPane.showMessageDialog(null, "Name 1 and Name 2 must not be same.", "Error", JOptionPane.ERROR_MESSAGE);
} else {
rlt.setText(getFlames(name1, name2));
}
  }
  public String getFlames(String name1, String name2)  {
    String result = "";
    char[] f = { 'F', 'L', 'A', 'M', 'E', 'S' };
    char[] n1 = name1.toCharArray();
    char[] n2 = name2.toCharArray();
    int len = 0;
    for (int i = 0; i < n1.length; i++) {
      for (int j = 0; j < n2.length; j++) {
        if (n1[i] == n2[j])  {
          n1[i] = '0';
          n2[j] = '0';
          break;
        }
      }
    }
    for (int i = 0; i < n1.length; i++) {
      if (n1[i] != '0') {
        len++;
      }
    }
    for (int i = 0; i < n2.length; i++) {
      if (n2[i] != '0') {
        len++;
      }
    }
    int k = 1;int l = 0;int c = 1;
    for (;;)  {
      if (k == len)  {
        if (f[l] != '0')  {
          f[l] = '0';
          k = 1;
          l++;
          c++;
          if (l >= 6) {
            l = 0;
          }
          if (c == 6) {
            break;
          }
        } else {
          l++;
          if (l >= 6) {
            l = 0;
          }
        }
      } else  {
        if (f[l] != '0') {
          k++;
        }
        l++;
        if (l >= 6) {
          l = 0;
        }
      }
    }
    for (int i = 0; i < f.length; i++) {
      if (f[i] != '0') {
        if (f[i] == 'F') {
          result = "FRIEND";
        } else if (f[i] == 'L') {
          result = "LOVE";
        } else if (f[i] == 'A') {
          result = "AFFECTION";
        } else if (f[i] == 'M') {
          result = "MARRIAGE";
        } else if (f[i] == 'E') {
          result = "ENEMY";
        } else if (f[i] == 'S') {
          result = "SISTER";
        }
      }
    }
    return result;
  }
}

No comments:

Post a Comment