Tuesday, May 26, 2015

Java Program - Snakes and Ladders

I have written simple program for Snakes and Ladders in java using java swing. And same i have converted into javascript as well. You can play Snakes and Ladders here. Java source code is given below, you can copy and play in your machine.

Start playing Snakes and Ladders

                   
                   
                   
                   
                   
                   
                   
                   
                   
                 


Computer


Java Code

package javaexperts;

import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Random;

public class SnakeAndLadder extends JFrame implements ActionListener{

JLabel lblComp = new JLabel("Computer");
JButton btnYou = new JButton("Play");
JButton newGame = new JButton("New Game");

JLabel lblCompNo = new JLabel("");
JLabel lblYouNo = new JLabel("");

JLabel lblCompPos = new JLabel("");
JLabel lblYouPos = new JLabel("");

HashMap<Integer,Integer> ladder = new HashMap<Integer,Integer>();
HashMap<Integer,Integer> snake = new HashMap<Integer,Integer>();

int youCount = 1;
int compCount = 1;

int w=15,h=15;
int x=0,y=0;

StringBuffer compList = null;
StringBuffer youList = new StringBuffer();

Random dies = new Random();

public SnakeAndLadder()  throws Throwable{
super("SnakeAndLadder");
setLayout(null);
// setLayout(new BorderLayout());
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//getContentPane().setBackground(Color.LIGHT_GRAY);
setBounds(10, 10, 700, 580);
JLabel background=new JLabel(new ImageIcon("E:\\blog\\pgms\\snakesandladders\\a.png"));
background.setBounds(0, -10, 550, 570);
add(background);

btnYou.setBounds(560, 200, 60, 30);
add(btnYou);

lblComp.setBounds(560, 250, 60, 30);
add(lblComp);

lblYouNo.setBounds(640, 200, 60, 30);
lblYouNo.setOpaque(true);
lblYouNo.setBackground(Color.BLUE);
lblYouNo.setForeground(Color.GREEN);
add(lblYouNo);

lblCompNo.setBounds(640, 250, 60, 30);
lblCompNo.setOpaque(true);
lblCompNo.setBackground(Color.RED);
lblCompNo.setForeground(Color.GREEN);
add(lblCompNo);

newGame.setBounds(580, 140, 90, 30);
add(newGame);
coinPosition(1,compCount);
lblCompPos.setBounds(x, y, w, h);
lblCompPos.setOpaque(true);
lblCompPos.setBackground(Color.RED);
background.add(lblCompPos);

coinPosition(2,youCount);
lblYouPos.setBounds(x, y, w, h);
lblYouPos.setOpaque(true);
lblYouPos.setBackground(Color.BLUE);
background.add(lblYouPos);

repaint();

ladder.put(3, 51);
ladder.put(6, 27);
ladder.put(20, 70);
ladder.put(36, 55);
ladder.put(63, 95);
ladder.put(68, 98);

snake.put(25, 5);
snake.put(34, 1);
snake.put(47, 19);
snake.put(65, 52);
snake.put(87, 57);
snake.put(91, 61);
snake.put(99, 69);

newGame.addActionListener(this);
btnYou.addActionListener(this);


}
  public static void main(String args[])  throws Throwable {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new SnakeAndLadder();
  }
  public void actionPerformed(ActionEvent e) {
 if (e.getSource() == newGame) {
 youCount = 1;
 compCount = 1;
 coinPosition(1,compCount);
 lblCompPos.setBounds(x, y, w, h);
 coinPosition(2,youCount);
 lblYouPos.setBounds(x, y, w, h);
 lblYouNo.setText("");
 lblCompNo.setText("");
 btnYou.setVisible(true);
 repaint();
 } else if (e.getSource() == btnYou) {
 compList = new StringBuffer();

 int playAgain = playDies(2);
 coinPosition(2,youCount);
 lblYouPos.setBounds(x, y, w, h);
 repaint();
 if(ladder.containsKey(youCount)) {
 youCount = ladder.get(youCount);
 } else if (snake.containsKey(youCount)){
 youCount = snake.get(youCount);
 }
 coinPosition(2,youCount);
 lblYouPos.setBounds(x, y, w, h);
 repaint();
 if(youCount == 100) {
 btnYou.setVisible(false);
 JOptionPane.showMessageDialog(this, "You win");
 } else if(playAgain == 0) {
 youList = new StringBuffer();
 do {
 playAgain = playDies(1);
 coinPosition(1,compCount);
 lblCompPos.setBounds(x, y, w, h);
 repaint();
 if(ladder.containsKey(compCount)) {
 compCount = ladder.get(compCount);
 } else if (snake.containsKey(compCount)){
 compCount = snake.get(compCount);
 }
 coinPosition(1,compCount);
 lblCompPos.setBounds(x, y, w, h);
 repaint();
 if(compCount == 100) {
 btnYou.setVisible(false);
 JOptionPane.showMessageDialog(this, "Computer win");
 break;
 }

 } while(playAgain == 1);
 }

 }
  }
 
  private int playDies(int player) {
 int playAgain = 0;
 int diesResult = 0;
 while(diesResult == 0) {
 diesResult = dies.nextInt(7);
 }
 if(player == 2){
 youList.append(String.valueOf(diesResult));
 youList.append(",");
 lblYouNo.setText(youList.toString());
 if(youCount+diesResult <= 100) {
 youCount = youCount+diesResult;
 if(diesResult == 1 || diesResult == 5 || diesResult == 6) {
 playAgain = 1;
 }
 }
 } else {
 compList.append(String.valueOf(diesResult));
 compList.append(",");
 lblCompNo.setText(compList.toString());
 if(compCount+diesResult <= 100) {
 compCount = compCount+diesResult;
 if(diesResult == 1 || diesResult == 5 || diesResult == 6) {
 playAgain = 1;
 }
 }
 }
 return playAgain;
  }
 
  private void coinPosition(int compOrYou, int count) {

 int xpos = count%10;
 int ypos = count/10;
 if(xpos == 0) {
 xpos = 10;
 ypos = ypos-1;
 }
 if(compOrYou == 1) {
 x = 5 + (xpos*55) - 55;
 } else {
 x = 25 + (xpos*55) - 55;
 }
 y = 540 - (ypos*57);
  }
}

Tuesday, April 28, 2015

Java Program for Cricket

I have written simple program for cricket in java using java swing. And same i have converted into javascript as well. You can play cricket here. Java source code is given below, you copy and play in your machine and you can enhance as much as possible.

Start playing cricket

              Computer               
Score: 0
Over: 0.0
 
 
              You              
Score: 0
Over: 0.0
 
 



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.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class Cricket extends JFrame implements ActionListener{

JLabel lblComp = new JLabel("Computer");
JLabel lblYou = new JLabel("You");
JLabel lblCompScore = new JLabel("Score");
JLabel lblYouScore = new JLabel("Score");
JLabel dispCompScore = new JLabel("0/0");
JLabel dispYouScore = new JLabel("0/0");
JLabel lblCompOver = new JLabel("Over");
JLabel lblYouOver = new JLabel("Over");
JLabel dispCompOver = new JLabel("0");
JLabel dispYouOver = new JLabel("0");

JLabel compResult = new JLabel("");
JLabel youResult = new JLabel("");
JLabel result = new JLabel("");

JButton playCompBtn = new JButton("Play Computer");
JButton playYouBtn = new JButton("Play");
JButton newGame = new JButton("New Game");

int score = 0;
int wick = 0;
int over = 0;
int ball = 0;
int compOrYou = 0;
int compScore = 0;

Random cricketPlay = new Random();

public Cricket()  throws Throwable{
super("Cricket");
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.LIGHT_GRAY);
setBounds(10, 10, 600, 400);

newGame.setBounds(200, 10, 150, 25);
newGame.setFont(new Font("Courier New", Font.BOLD, 18));
newGame.setForeground(Color.GRAY);

lblComp.setBounds(80, 50, 100, 25);
lblComp.setFont(new Font("Courier New", Font.BOLD, 18));
lblComp.setForeground(Color.BLUE);

lblCompScore.setBounds(50, 90, 100, 25);
dispCompScore.setBounds(200, 90, 100, 25);
lblCompOver.setBounds(50, 130, 100, 25);
dispCompOver.setBounds(200, 130, 100, 25);

playCompBtn.setBounds(50,200,180,30);
playCompBtn.setFont(new Font("Courier New", Font.BOLD, 18));
playCompBtn.setForeground(Color.RED);


lblYou.setBounds(380, 50, 100, 25);
lblYou.setFont(new Font("Courier New", Font.BOLD, 18));
lblYou.setForeground(Color.BLUE);

lblYouScore.setBounds(350, 90, 100, 25);
dispYouScore.setBounds(500, 90, 100, 25);
lblYouOver.setBounds(350, 130, 100, 25);
dispYouOver.setBounds(500, 130, 100, 25);

playYouBtn.setBounds(350,200,180,30);
playYouBtn.setFont(new Font("Courier New", Font.BOLD, 18));
playYouBtn.setForeground(Color.RED);

compResult.setBounds(60,250,180,30);
compResult.setFont(new Font("Courier New", Font.BOLD, 18));
compResult.setForeground(Color.MAGENTA);

youResult.setBounds(370,250,180,30);
youResult.setFont(new Font("Courier New", Font.BOLD, 18));
youResult.setForeground(Color.MAGENTA);

result.setBounds(250,300,180,30);
result.setFont(new Font("Courier New", Font.BOLD, 18));
result.setForeground(Color.MAGENTA);



add(lblComp);
add(lblCompScore);
add(dispCompScore);
add(lblCompOver);
add(dispCompOver);
add(playCompBtn);

add(lblYou);
add(lblYouScore);
add(dispYouScore);
add(lblYouOver);
add(dispYouOver);
add(playYouBtn);

add(compResult);
add(youResult);
add(result);

add(newGame);
playYouBtn.setVisible(false);
playCompBtn.addActionListener(this);
playYouBtn.addActionListener(this);
newGame.addActionListener(this);
}
  public static void main(String args[])  throws Throwable {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new Cricket();
  }
  public void actionPerformed(ActionEvent e) {
if (e.getSource() == playCompBtn) {
compOrYou = 1;
for(int i=0; i<30; i++) {
playBall();
dispCompScore.setText(score+"/"+wick);
dispCompOver.setText(over+"."+ball);
if(over == 5 || wick ==10) {
compResult.setText("");
break;
}
}
playCompBtn.setVisible(false);
playYouBtn.setVisible(true);
compScore = score;
score = 0;
wick = 0;
over = 0;
ball = 0;
} else if(e.getSource() == playYouBtn) {
compOrYou = 2;
playBall();
dispYouScore.setText(score+"/"+wick);
dispYouOver.setText(over+"."+ball);
} else {
compScore = 0;
compOrYou = 0;
score = 0;
wick = 0;
over = 0;
ball = 0;
dispCompScore.setText(score+"/"+wick);
dispCompOver.setText(over+"."+ball);
dispYouScore.setText(score+"/"+wick);
dispYouOver.setText(over+"."+ball);
compResult.setText("");
youResult.setText("");
result.setText("");
playYouBtn.setVisible(false);
playCompBtn.setVisible(true);
}
  }
  private void playBall() {
int ballResult = cricketPlay.nextInt(8);
ball = ball + 1;
if(ball == 6) {
over = over + 1;
ball = 0;
}
if((ballResult >= 0 && ballResult <=4) || ballResult == 6) {
score = score + ballResult;
if(compOrYou == 1) {
compResult.setText(ballResult+" Run(s)");
} else {
youResult.setText(ballResult+" Run(s)");
}
} else {
wick = wick + 1;
if(compOrYou == 1) {
compResult.setText("Wicket");
} else {
youResult.setText("Wicket");
}
}


if(compOrYou == 2) {
if(over == 5 || wick == 10) {
if(compScore == score) {
result.setText("Match Draw");
} else if(compScore < score) {
result.setText("You Win");
} else {
result.setText("Computer Win");
}
playYouBtn.setVisible(false);
} else if(compScore < score) {
result.setText("You Win");
playYouBtn.setVisible(false);
}
}
  }
}

Saturday, April 11, 2015

Java final access modifier

Purpose of final access modifier knows everyone, that is if any variable is declared as final, value of that variable can not be changed during run time. Normally all the constant variables will be declared in a constant class and that class will be referred throughout the project.
In this case, if any constant value need to be changed which was declared as final, we have to compile all the class files which all are referring that constant class. Because final variables will be referred at the time of compilation only. During run time, final variables will not be referred.

We will see below example,

Constant java file:
     public class JavaConstant {
           public static final int MIN_VAL = 100;
     }

Java file which is using above constant:
     public class JavaPrint {
           public static void main(String args[]) {
                System.out.println(JavaConstant.MIN_VAL);
           }
     }

We will compile both java files and execute the JavaPrint class file.
Output will be 100

Now , we are modifying JavaConstant java file as below
     public class JavaConstant {
           public static final int MIN_VAL = 150;// value modified from 100 to 150
     }

Now, we are compiling JavaConstant java file alone and execute JavaPrint class file.
Output will be 100 again.
Because modified value of MIN_VAL will not be referred during run time since it is declared as final

Now, we will compile JavaPrint java file and execute JavaPrint class file.
Output will be 150

Thursday, April 2, 2015

Interesting java codes

Most of the software engineers are living with java programs daily for their big projects. But the real fact is, we all are doing Copy Paste job, because mostly your working projects will be well designed. As we are very busy with Copy Paste job, some times we failed to understand the some basic interesting things in java. Some of us might be experienced with interesting things in java programs. I am sharing few things here which i have learned.

Some of us might though that, below code will not compile. But below code will compile and execute

     public class Curiosity {
           public static void main (String[ ] args) {
                System.out.println ("A curiosity");
                http://google.com
                System.out.println ("compile Ok!");
          }
     }
Output:
A curiosity
compile Ok!

Reason is simple, many of us aware of label statement in java. So here "http:" is label and // is comment line.

The next one is strange. Is is possible to call any method or field by null reference?. Most of us will give answer as NO. We have seen NullPointerException many times, so we thought that, below code will give run time exception. But below code will compile and execute successfully.

     public class Foo {
           static int fubar = 30;
           public static void main(String args[]) {
                Foo foo = null;
                System.out.println(foo.fubar);
           }
     }
Output:
30

Reason: Here, we have to notice variable declaration of "fubar". "fubar"is declared as static, so this will be stored in Method Area. Static variables are accessed via class and though the object is null, the type Foo is what is used to determine reference to the variable.

Friday, January 23, 2015

onbeforeonload event in firefox

There is a difference in onbeforeunload event across the version of Firefox browser. Normally we are using "alert" function to identify whether that particular function is calling or not. But in case of onbeforeunload event, alert will work upto firefox version 26.0. If the firefox version is greater than 26.0, alert funtion will not work. So if you want to use onbeforeunload event in firefox which is greater version of 26.0, then don't use alert function, just write your logic and test it.

<html> <head> <script> function onBrowserClose() { alert("Browser is closing"); // This will work, if your firefox version is less than or equal to 26.0 //Your logic } </script> </head> <body onbeforeonload="onBrowserClose()"> Browser close test... </body> </html>
If your Browse is latest version of firefox (greater than 26.0). Just remove the alert from above code.
<html> <head> <script> function onBrowserClose() { //Your logic } </script> </head> <body onbeforeonload="onBrowserClose()"> Browser close test... </body> </html>

Sunday, January 11, 2015

Java Exception Handling

An Exception is nothing but a problem which will occur during execution of program. An Exception can occur for many reasons. Below are the some of reasons,

  1. Invalid data from User
  2. Program try to read a file, but file is not available
  3. Program try to connect to Database, but credential is invalid

Exception Handling

Exception Handling is nothing but handle the Exception to avoid the abnormal termination of program. If an Exception is not handled, program will terminate abnormally.

To know the clear idea about an Exception handling, we will see the three categories of an Exceptions

  1. Checked Exceptions: Checked Exception is an Exception which can occur typically by user error. So programmer will not knowing it affront. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These Exceptions cannot be ignored by compiler. These kind of Exceptions are called "Checked Exception"
  2. Runtime Exceptions: Runtime Exception is an Exception which programmer can be knowing it affront based on critically of the code. Runtime Exceptions can be avoided during the development itself. But programmer is the responsible for that.
  3. Errors: There are not at all an Exceptions.The problem arise at beyond the user or programmer control. For example, JVM is running out of memory, this cannot be handled by program and cannot identify at compilation.

Exception Hierarchy

All Exceptions are subclasses of Exception class. Exception class is subclass of Throwable class. There is one more class which is derived from Throwable class is Error.


Catching an Exception

Exception handling is the combination of "try" and "catch" block. "try" will have the critical code which may arise an Exception. "catch" block should be written immediately next to "try" block. Whenever Exception is occurring inside the "try" block, control will go to respective "catch" block and execute the "catch" block.

     try {
           //critical code
     } catch(ExceptionName e) {
           //Handling block
     }

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in critical code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

Single catch block - Sample

Following code is having an array with size 2, when try to add 3rd element, program will throws an Exception.

     try {
           int[] i = new int[2];
           i[0] = 1;
           i[1] = 2;
           i[2] = 3;
     } catch(ArrayIndexOutOfBoundsException e) {
           System.out.println("Exception thrown :" + e);
     }

Multiple catch block - Syntax and Sample

Syntax
     try {
           // Critical code
     } catch(ExceptionType e1) {
           // catch block 1
     } catch(ExceptionType e2) {
           // catch block 2
     } catch(ExceptionType e3) {
           // catch block 3
     }
Sample
     try{
           file = new FileInputStream(fileName);
           x = (byte) file.read();
     } catch(IOException i) {
           i.printStackTrace();
     } catch(FileNotFoundException f) {
           f.printStackTrace();
     }

Sunday, January 4, 2015

Java Performance

This post is about performance factors in java. There are many performance factors in java. Here i am mentioning some of the performance factors which i have used in early days in java. I will keep posted some approach, experience on performance improvement.

some of the performance tips are,

  1. ArrayList is faster than Vector except when there is no lock acquisition required.
  2. Use StringBuffer in place of String if you are doing lots of string manipulation it will reduce memory by avoiding creating lots of string garbage. If you are using java5 then consider StringBuilder but that is not synchronized so beware.
  3. Try to make variable , class , method final whenever possible that’s allow compiler to do lots of optimization e.g. compile time binding so you will get faster output.
  4. Static methods are bonded compile time while non static methods are resolved at runtime based on object type so static method will be faster than non static.
  5. Don't call methods in for loop for checking condition e.g. length() size() etc. instead of doing this , use modified version.
  6. Use exceptions only where you really need them
  7. Local variables are the faster than instance variables, which are in turn faster than array elements
  8. Use the fastest available JVM
  9. '==' is faster than equals()
  10. Creating Doubles from strings is slow
  11. Combine similar loops
  12. Don't create too may objects
  13. Beware of object leaks (references to objects that are never nulled)
  14. Use Lazy loading to avoid unnecessary pre-loading of child data
  15. Do bulk updates to reduce database calls
  16. Use the print() method rather than the println() method

Saturday, January 3, 2015

Difference between JDK, JRE and JVM

Understanding the difference between JDK, JRE and JVM is important in Java. We are using JDK, JRE and JVM daily, but many of us not knowing the difference of them. Let we discuss the difference between JDK, JRE and JVM.

JVM

JVM is the short form of Java Virtual Machine. It is a specification that provides runtime environment to execute the java bytecode. JVM is platform dependent because configuration of each OS is differ. But we know that, java is platform independent because java bytecode can be executed in any platform.

JRE

JRE is the short form for Java Runtime Environment. JRE is providing the runtime environment. JRE is the implementation of JVM. It physically exist. It contains set of libraries and set of files that JVM uses at runtime.

JDK

JDK is the short form of Java Development Kit. It physically exist. It contains JRE and Development tools.



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;
  }
}