Saturday, 28 September 2013

A small mistake in Liang book

There is a small mistake in liang book. I was going through this heap Data Structure in the book,I found out that parent index , left child index and right child index needs correction
In the book
Parent index = (currentIndex - 1)/2
leftChild Index = (2 * currentIndex);
rightChildIndex = (2 * currentIndex + 1);
this is giving wrong results
Proof:
but in actual it should be:
package com.liang.datastructures;

import java.util.ArrayList;

public class Heap {
    private  ArrayList list = new ArrayList();
    public Heap(){
    }
    public ArrayList getList() {
        return list;
    }
    public void setList(ArrayList list) {
        this.list = list;
    }
    public Heap(Object []objects){
        for(int i=0;i<objects.length;i++){
            add(objects[i]);
        }
    }
    public  void add(Object obj){
        list.add(obj);
        int currentIndex = list.size() - 1;
        int parentIndex;
        while(currentIndex > 0){
            parentIndex = (currentIndex - 1)/2;
            if(((Comparable)(list.get(currentIndex))).compareTo(list.get(parentIndex)) > 0){
                Object temp = list.get(currentIndex);
                list.set(currentIndex,list.get(parentIndex));
                list.set(parentIndex,temp);
            }
            else
            {
                break;
            }
            currentIndex = parentIndex;
        }
    }
    public Object remove(){
        if(list.size() == 0) return null;
        Object removedObject = list.get(0);
        list.set(0,list.get(list.size() - 1));
        list.remove(list.size() - 1);
        int currentIndex = 0;
        while(currentIndex < list.size()){
            int leftChildIndex = 2 * currentIndex;
            int rightChildIndex = 2 * currentIndex + 1;
            if(leftChildIndex >= list.size()){
                break;
            }
            int maxIndex = leftChildIndex;
            if(rightChildIndex < list.size()){
                if(((Comparable)(list.get(leftChildIndex))).compareTo(list.get(rightChildIndex)) < 0){
                    maxIndex = rightChildIndex;
                }
            }
            if(((Comparable)(list.get(currentIndex))).compareTo(list.get(maxIndex))<0){
                Object temp = list.get(currentIndex);
                list.set(currentIndex,list.get(maxIndex));
                list.set(maxIndex,temp);
                currentIndex = maxIndex;
            }
            else{
                break;
            }
        }
        return removedObject;
    }
    public int getSize(){
        return(list.size());
    }
}


package com.liang.datastructures;

public class HeapSort {
    public static void main(String[] args) {
        Heap heap = new Heap(new Integer[]{2,3,1,4,7,9,8,6,5});
        while(heap.getSize() > 0){
            System.out.println(" "+heap.remove());
        }
    }
}

Result:
 9
 6
 8
 5
 7
 4
 3
 2
 1



Parent index = (currentIndex )/2
leftChild Index = (2 * currentIndex + 1);
rightChildIndex = (2 * currentIndex + 2);
this is giving correct results
proof: 

package com.liang.datastructures;

import java.util.ArrayList;

public class Heap {
    private  ArrayList list = new ArrayList();
    public Heap(){
    }
    public ArrayList getList() {
        return list;
    }
    public void setList(ArrayList list) {
        this.list = list;
    }
    public Heap(Object []objects){
        for(int i=0;i<objects.length;i++){
            add(objects[i]);
        }
    }
    public  void add(Object obj){
        list.add(obj);
        int currentIndex = list.size() - 1;
        int parentIndex;
        while(currentIndex > 0){
            parentIndex = currentIndex /2;
            if(((Comparable)(list.get(currentIndex))).compareTo(list.get(parentIndex)) > 0){
                Object temp = list.get(currentIndex);
                list.set(currentIndex,list.get(parentIndex));
                list.set(parentIndex,temp);
            }
            else
            {
                break;
            }
            currentIndex = parentIndex;
        }
    }
    public Object remove(){
        if(list.size() == 0) return null;
        Object removedObject = list.get(0);
        list.set(0,list.get(list.size() - 1));
        list.remove(list.size() - 1);
        int currentIndex = 0;
        while(currentIndex < list.size()){
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;

            if(leftChildIndex >= list.size()){
                break;
            }
            int maxIndex = leftChildIndex;
            if(rightChildIndex < list.size()){
                if(((Comparable)(list.get(leftChildIndex))).compareTo(list.get(rightChildIndex)) < 0){
                    maxIndex = rightChildIndex;
                }
            }
            if(((Comparable)(list.get(currentIndex))).compareTo(list.get(maxIndex))<0){
                Object temp = list.get(currentIndex);
                list.set(currentIndex,list.get(maxIndex));
                list.set(maxIndex,temp);
                currentIndex = maxIndex;
            }
            else{
                break;
            }
        }
        return removedObject;
    }
    public int getSize(){
        return(list.size());
    }
}
package com.liang.datastructures;

public class HeapSort {
    public static void main(String[] args) {
        Heap heap = new Heap(new Integer[]{2,3,1,4,7,9,8,6,5});
        while(heap.getSize() > 0){
            System.out.println(" "+heap.remove());
        }
    }
}


Result:
 9
 8
 7
 6
 5
 4
 3
 2
 1

Sunday, 7 April 2013

Fan Which can be started,stoped and reversed

FanControl.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FanControl extends JPanel{
    private JButton btnStart = new JButton("Start");
    private JButton btnStop = new JButton("Stop");
    private JButton btnReverse = new JButton("Reverse");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    public Fan fan = new Fan();
    private JScrollBar jscbHort = new JScrollBar(JScrollBar.HORIZONTAL);
    public FanControl(){
        setLayout(new BorderLayout());
        p1.setLayout(new GridLayout(1,3,0,0));
        p1.add(btnStart);
        p1.add(btnStop);
        p1.add(btnReverse);
        p2.setLayout(new GridLayout(1,1,0,0));
        p2.add(jscbHort);
        add(p1,BorderLayout.NORTH);
        add(fan,BorderLayout.CENTER);
        add(p2,BorderLayout.SOUTH);
        btnStart.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                fan.start();
            }
        });
        btnStop.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                fan.stop();
            }
        });
        btnReverse.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                fan.reverse();
            }
        });
        jscbHort.addAdjustmentListener(new AdjustmentListener(){
            public void adjustmentValueChanged(AdjustmentEvent ae){
                double value = jscbHort.getValue();
                double maximumValue = jscbHort.getMaximum();
                int delay = p2.getWidth() - (int)(value * p2.getWidth()/maximumValue);
                delay = delay/10;
                System.out.println(delay);
                fan.setSpeed(delay);
            }
        });
    }
}


FanControlApplet.java

import java.awt.*;
import javax.swing.*;
public class FanControlApplet extends JApplet{
    FanControl fanControl = new FanControl();
    public void init(){
        add(fanControl);
    }
    public static void main(String []args){
        JFrame frame = new JFrame("Fan Control");
        FanControlApplet applet = new FanControlApplet();
        applet.init();
        frame.add(applet);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

An Alarm Clock

AlarmClock.java

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.net.URL;
import java.applet.*;
public class AlarmClock extends JPanel{
    private JLabel jlblHour = new JLabel("Hour");
    private JLabel jlblMinute = new JLabel("Minute");
    private JLabel jlblSecond = new JLabel("Second");
    private JTextField jtfHour = new JTextField();
    private JTextField jtfMinute = new JTextField();
    private JTextField jtfSecond = new JTextField();
    private JPanel p1 = new JPanel();
    private JCheckBox jchk = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    AudioClip audioClip;
    private JPanel p2 = new JPanel();
    private int hour;
    private int minute;
    private int second;
    SetAlarm frame = new SetAlarm();
    int i = 0;
    private static Calendar gCalendar = new GregorianCalendar();
    public static int alarmHour = 0;
    public static int alarmMinute = 0;
    public static int alarmSecond = 0;
    Timer timer2;
    public AlarmClock(){
        setLayout(new BorderLayout());
        Timer timer1 = new Timer(1000,new TimerListener1());
        timer1.start();
        p1.setLayout(new GridLayout(3,2,5,5));
        p1.add(jlblHour);
        p1.add(jtfHour);
        p1.add(jlblMinute);
        p1.add(jtfMinute);
        p1.add(jlblSecond);
        p1.add(jtfSecond);
        gCalendar = new GregorianCalendar();
        hour = gCalendar.get(Calendar.HOUR);
        minute = gCalendar.get(Calendar.MINUTE);
        second = gCalendar.get(Calendar.SECOND);
        jtfHour.setText("" + hour);
        jtfMinute.setText("" + minute);
        jtfSecond.setText("" + second);
        p1.setBorder(new LineBorder(Color.BLACK,1));
        p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
        p2.add(jchk);
        p2.add(btnSetAlarm);
        jchk.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                if(jchk.isSelected())
                {
                    timer2 = new Timer(0,new TimerListener2());
                    timer2.start();
                }
            }
        });
        btnSetAlarm.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
        add(p1,BorderLayout.CENTER);
        add(p2,BorderLayout.SOUTH);
    }
    public class TimerListener2 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            if((hour == alarmHour)&&(minute == alarmMinute)&&(second == alarmSecond))
            {
                URL urlForAudio = getClass().getResource("image/Bbpledge.wav");
                audioClip = Applet.newAudioClip(urlForAudio);
                audioClip.loop();
            }
        }
    }
    public void setTime(){
        setCurrentTime();
        jtfHour.setText("" + hour);
        jtfMinute.setText("" + minute);
        jtfSecond.setText("" + second);
    }
    class TimerListener1 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            setTime();
        }
    }
    public void setCurrentTime(){
        gCalendar = new GregorianCalendar();
        hour = gCalendar.get(Calendar.HOUR);
        minute = gCalendar.get(Calendar.MINUTE);
        second = gCalendar.get(Calendar.SECOND);
    }
    public static void setAlarmTime(int hour,int minute,int second){
        alarmHour = hour;
        alarmMinute = minute;
        alarmSecond = second;
    }
}


AlarmClockFrame.java

import javax.swing.*;
public class AlarmClockFrame extends JFrame{
    public AlarmClockFrame(){
        add(new AlarmClock());
    }
    public static void main(String []args){
        AlarmClockFrame frame = new AlarmClockFrame();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}