<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vandana Gupta</title>
    <description>The latest articles on DEV Community by Vandana Gupta (@guptavandana).</description>
    <link>https://dev.to/guptavandana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F426261%2F17161ffc-b518-47ba-86d0-4d1a588ddc83.jpeg</url>
      <title>DEV Community: Vandana Gupta</title>
      <link>https://dev.to/guptavandana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guptavandana"/>
    <language>en</language>
    <item>
      <title>Typing Tutor in Spring </title>
      <dc:creator>Vandana Gupta</dc:creator>
      <pubDate>Sat, 28 Aug 2021 12:01:42 +0000</pubDate>
      <link>https://dev.to/guptavandana/typing-tutor-in-spring-11a5</link>
      <guid>https://dev.to/guptavandana/typing-tutor-in-spring-11a5</guid>
      <description>&lt;p&gt;hey guys! I'm back again with an interesting game in spring. In this  simple game you have to copy and paste the whatever word that shows on the screen. Try it! it's actually fun.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;

public class TypingTutor extends JFrame implements ActionListener, KeyListener {
    private Timer clockTimer = null;
    private Timer wordTimer = null;

    private boolean running = false;
    private int timeRemaining = 0;
    private int score = 0;

    private JLabel lblTimer;
    private JLabel lblScore;
    private JLabel lblWord;
    private JTextField txtWord;
    private JButton btnStart;
    private JButton btnStop;

    private String[] words = null;

    public TypingTutor(String[] args) {
        words = args;

        GridLayout layout = new GridLayout(3, 2);
        super.setLayout(layout);

        Font font = new Font("Comic Sans MS", 1, 150);

        lblTimer = new JLabel("Time");
        lblTimer.setFont(font);
        super.add(lblTimer);

        lblScore = new JLabel("Score");
        lblScore.setFont(font);
        super.add(lblScore);

        lblWord = new JLabel("");
        lblWord.setFont(font);
        super.add(lblWord);

        txtWord = new JTextField("");
        txtWord.setFont(font);
        txtWord.addKeyListener(this);
        super.add(txtWord);

        btnStart = new JButton("Start");
        btnStart.setFont(font);
        btnStart.addActionListener(this);
        super.add(btnStart);

        btnStop = new JButton("Stop");
        btnStop.setFont(font);
        btnStop.addActionListener(this);
        super.add(btnStop);

        super.setTitle("Typing Tutor");
        super.setExtendedState(MAXIMIZED_BOTH);
        super.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        super.setVisible(true);

        setupthegame();
    }

    private void setupthegame() {
        clockTimer = new Timer(1000, this);
        clockTimer.setInitialDelay(0);

        wordTimer = new Timer(3000, this);
        wordTimer.setInitialDelay(0);

        running = false;
        timeRemaining = 50;
        score = 0;

        lblTimer.setText("Time: " + timeRemaining);
        lblScore.setText("Score: " + score);
        lblWord.setText("");
        txtWord.setText("");
        btnStart.setText("Start");
        btnStop.setText("Stop");

        txtWord.setEnabled(false);
        btnStop.setEnabled(false);
    }

    @Override
    public synchronized void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnStart) {
            handleStart();
        } else if (e.getSource() == btnStop) {
            handleStop();
        } else if (e.getSource() == clockTimer) {
            handleClockTimer();
        } else if(e.getSource() == wordTimer){
            handleWordTimer();
        }
    }

    private void handleStart() {
        if (running == false) {
            clockTimer.start();
            wordTimer.start();

            running = true;
            btnStart.setText("Pause");
            txtWord.setEnabled(true);
            btnStop.setEnabled(true);

            txtWord.setFocusCycleRoot(true);
            super.nextFocus();
        } else {
            clockTimer.stop();
            wordTimer.stop();

            running = false;
            btnStart.setText("Start");
            txtWord.setEnabled(false);
            // btnStop.setEnabled(false);
        }
    }

    private void handleStop() {
        clockTimer.stop();
        wordTimer.stop();

        int choice = JOptionPane.showConfirmDialog(this, "Want to replay?");
        if (choice == JOptionPane.YES_OPTION) {
            setupthegame();
        } else if (choice == JOptionPane.NO_OPTION) {
            super.dispose();
        } else if (choice == JOptionPane.CANCEL_OPTION) {
            if (timeRemaining &amp;gt; 0) {
                clockTimer.start();
                wordTimer.start();
            } else {
                setupthegame();
            }
        }
    }

    private void handleWordTimer() {
        int ridx = (int) (Math.random() * words.length);
        lblWord.setText(words[ridx]);
        txtWord.setText("");
    }

    private void handleClockTimer(){
        timeRemaining--;

        if (timeRemaining == -1) {
            handleStop();
            return;
        }

        lblTimer.setText("Time: " + timeRemaining);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        String wanted = lblWord.getText();
        String real = txtWord.getText();
        if (wanted.length() &amp;gt; 0 &amp;amp;&amp;amp; wanted.equals(real)) {
            score++;

            lblScore.setText("Score: " + score);

            wordTimer.restart();
            handleWordTimer();
        }
    }

           public static void main(String[] args) {
        TypingTutor tt = new TypingTutor(args);
    }


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>gamedev</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Huffman - Coder Algorithm</title>
      <dc:creator>Vandana Gupta</dc:creator>
      <pubDate>Sat, 28 Aug 2021 11:41:31 +0000</pubDate>
      <link>https://dev.to/guptavandana/huffman-coder-algorithm-381p</link>
      <guid>https://dev.to/guptavandana/huffman-coder-algorithm-381p</guid>
      <description>&lt;p&gt;Implementing huffman algo might tough for newbies but it is one of the most prominent algorithm out there and one must have it's proper knowledge. So, I thought I should share a simple algo to implement it&lt;br&gt;
Learning it will help you gain knowledge about about heaps, trees and data compression algorithms.&lt;br&gt;
Below is a simple method to implement it form scratch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Comparator;
import java.util.HashMap;


public class HEncoder {
    private HashMap&amp;lt;Character, String&amp;gt; encoder = new HashMap&amp;lt;&amp;gt;();
    private HashMap&amp;lt;String, Character&amp;gt; decoder = new HashMap&amp;lt;&amp;gt;();

    private static class Node{
        Character data;
        int freq;
        Node left;
        Node right;
        private static final NodeComparator Ctor = new NodeComparator();

        private static class NodeComparator implements Comparator&amp;lt;Node&amp;gt;{
            @Override
            public int compare(Node o1, Node o2) {
                // TODO Auto-generated method stub
                return o2.freq - o1.freq;
            }
        }
    }

    // 1. freq map
    // 2. prepare the heap from keyset
    // 3. prepare tree - remove two, merge, add it back
    // 4. traverse
    public HEncoder(String feeder){
        // 1. freq map
        HashMap&amp;lt;Character, Integer&amp;gt; fm = new HashMap&amp;lt;&amp;gt;();
        for(int i = 0; i &amp;lt; feeder.length(); i++){
            char ch = feeder.charAt(i);

            if(fm.containsKey(ch)){
                fm.put(ch, fm.get(ch) + 1);
            } else {
                fm.put(ch, 1);
            }
        }

        // 2. create the heap
        PriorityQueue&amp;lt;Node&amp;gt; heap = new PriorityQueue&amp;lt;&amp;gt;(Node.Ctor);
        ArrayList&amp;lt;Character&amp;gt; keys = new ArrayList&amp;lt;&amp;gt;(fm.keySet());
        for(Character key: keys){
            Node node = new Node();
            node.data = key;
            node.freq = fm.get(key);

            heap.add(node);
        }

        // 3. create the binary tree - remove two, merge, put it back till size is 1
        while(heap.size() != 1){
            Node one = heap.removeHP();
            Node two = heap.removeHP();

            Node merged = new Node();
            merged.freq = one.freq + two.freq;
            merged.left = one;
            merged.right = two;

            heap.add(merged);
        }

        // 4. traverse the tree
        Node finalNode = heap.removeHP();
        traverse(finalNode, "");
    }

    private void traverse(Node node, String osf) {
        // work
        if(node.left == null &amp;amp;&amp;amp; node.right == null){
            encoder.put(node.data, osf);
            decoder.put(osf, node.data);
            return;
        }

        traverse(node.left, osf + "0");
        traverse(node.right, osf + "1");
    }

    public String compress(String str) throws Exception {
        String rv = "";

        for(int i = 0; i &amp;lt; str.length(); i++){
            rv += encoder.get(str.charAt(i));
        }

        byte[] barr = null;

        if(rv.length() % 8 == 0){
            barr = new byte[rv.length() / 8];
        } else {
            barr = new byte[rv.length() / 8 + 1];
        }

        int counter = 0;
        for(int i = 0; i &amp;lt; rv.length(); ){
            barr[counter] = (byte)Integer.parseInt(rv.substring(i, Math.min(i + 8, rv.length())), 2);
            counter++;
            i = i + 8;
        }

        Path path = Paths.get("E:\\1.obj");
        Files.write(path, barr);

        return rv;
    }

    public String decompress(String str) throws Exception{
        Path path = Paths.get("E:\\1.obj");
        byte[] arr = Files.readAllBytes(path);

        for(int i = 0; i &amp;lt; arr.length; i++){
            str += String.format("%8s", Integer.toBinaryString(arr[i] &amp;amp; 0xFF)).replace(' ', '0');
        }

        String rv = "";

        String code = "";
        for(int i = 0; i &amp;lt; str.length(); i++){
            code += str.charAt(i);

            if(decoder.containsKey(code)){
                rv += decoder.get(code);
                code = "";
            }
        }

        return rv;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
