Tuesday, 18 April 2017

Java Program to Solve Tower of Hanoi Problem using Stacks

     import java.util.*;

    

     /* Class TowerOfHanoiUsingStacks */

     public class TowerOfHanoiUsingStacks

     {

         public static int N;

         /* Creating Stack array  */

         public static Stack<Integer>[] tower = new Stack[4];

    

         public static void main(String[] args)

         {

             Scanner scan = new Scanner(System.in);

             tower[1] = new Stack<Integer>();

             tower[2] = new Stack<Integer>();

             tower[3] = new Stack<Integer>();

             /* Accepting number of disks */        

             System.out.println("Enter number of disks");

             int num = scan.nextInt();

             N = num;

             toh(num);

         }

         /* Function to push disks into stack */

         public static void toh(int n)

         {

             for (int d = n; d > 0; d--)

                 tower[1].push(d);

             display();

             move(n, 1, 2, 3);        

         }

         /* Recursive Function to move disks */

         public static void move(int n, int a, int b, int c)

         {

             if (n > 0)

             {

                 move(n-1, a, c, b);    

                 int d = tower[a].pop();                                            

                 tower[c].push(d);

                 display();                  

                 move(n-1, b, a, c);    

             }        

         }

         /*  Function to display */

         public static void display()

         {

             System.out.println("  A  |  B  |  C");

             System.out.println("---------------");

             for(int i = N - 1; i >= 0; i--)

             {

                 String d1 = " ", d2 = " ", d3 = " ";

                 try

                 {

                     d1 = String.valueOf(tower[1].get(i));

                 }

                 catch (Exception e){

                 }   

                 try

                 {

                     d2 = String.valueOf(tower[2].get(i));

                 }

                 catch(Exception e){

                 }

                 try

                 {

                     d3 = String.valueOf(tower[3].get(i));

                 }

                 catch (Exception e){

                 }

                 System.out.println("  "+d1+"  |  "+d2+"  |  "+d3);

             }

             System.out.println("\n");        

         }

     }

C program for Tower of Hanoi using Recursion



     #include <stdio.h>

         void towers(int, char, char, char);


    int main()

    {

        int num;

        printf("Enter the number of disks : ");

        scanf("%d", &num);

        printf("The sequence of moves involved in the Tower of Hanoi are :\n");

        towers(num, 'A', 'C', 'B');

        return 0;

    }

    void towers(int num, char frompeg, char topeg, char auxpeg)

    {

        if (num == 1)

        {

            printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);

            return;

        }

        towers(num - 1, frompeg, auxpeg, topeg);

        printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);

        towers(num - 1, auxpeg, topeg, frompeg);

    }


JavaFX ProgressBar Example

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ProgressBarEx extends Application {

    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        HBox root = new HBox(15);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        ProgressBar pbar = new ProgressBar(0);
        pbar.setPrefWidth(150);
       
        KeyFrame frame1 = new KeyFrame(Duration.ZERO,
                new KeyValue(pbar.progressProperty(), 0));
       
        KeyFrame frame2 = new KeyFrame(Duration.seconds(3),
                new KeyValue(pbar.progressProperty(), 1));       

        Timeline task = new Timeline(frame1, frame2);

        Button btn = new Button("Start");
        btn.setOnAction((ActionEvent actionEvent) -> {
            task.playFromStart();
        });

        root.getChildren().addAll(pbar, btn);

        Scene scene = new Scene(root);

        stage.setTitle("ProgressBar");
        stage.setScene(scene);
        stage.show();
    }
   
    public static void main(String[] args) {
        launch(args);
    }
}



JavaFX ComboBox Example

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChoiceBoxEx extends Application {

        public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        VBox root = new VBox(35);
        root.setPadding(new Insets(10));

        Label lbl = new Label();

        ChoiceBox chbox = new ChoiceBox(FXCollections.observableArrayList(
                "Ubuntu", "Redhat", "Arch", "Debian", "Mint"));

        SingleSelectionModel model = chbox.getSelectionModel();

        model.selectedItemProperty().addListener((ObservableValue observable,
                Object oldValue, Object newValue) -> {

            lbl.setText(newValue.toString());
        });

        root.getChildren().addAll(chbox, lbl);

        Scene scene = new Scene(root, 300, 250);

        stage.setTitle("ChoiceBox");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


JavaFX Slider Example

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class SliderEx extends Application {

    private ImageView iview;
    private Image muteImg;
    private Image minImg;
    private Image maxImg;
    private Image medImg;

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        HBox root = new HBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(15));

        loadImages();

        iview = new ImageView(muteImg);

        Slider slider = new Slider(0, 100, 0);
        slider.valueProperty().addListener(new MyChangeListener());

        Scene scene = new Scene(root);

        root.getChildren().addAll(slider, iview);

        stage.setTitle("Slider");
        stage.setScene(scene);
        stage.show();
    }

    private void loadImages() {

        muteImg = new Image("file:mute.png");
        minImg = new Image("file:min.png");
        maxImg = new Image("file:max.png");
        medImg = new Image("file:med.png");
    }

    private class MyChangeListener implements ChangeListener<Number> {

        @Override
        public void changed(ObservableValue<? extends Number> observable,
                Number oldValue, Number newValue) {

            Double value = newValue.doubleValue();

            if (value == 0) {
                iview.setImage(muteImg);
            } else if (value > 0 && value <= 30) {
                iview.setImage(minImg);
            } else if (value > 30 && value < 80) {
                iview.setImage(medImg);
            } else {
                iview.setImage(maxImg);
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}