Sunday, April 28, 2013

Java Exercise #103: Simple RegEx

Exercise: Write a program that splits a string based on empty space and replace empty space with tab.

EXAMPLE_TEST is This is my small example string which I'm going to use for pattern matching.

EXAMPLE_TEST.matches("\w.*"): true

Splitting with space: EXAMPLE_TEST.split("\s+")
This
is
my
small
example
string
which
I'm
going
to
use
for
pattern
matching.

Replace all white space with tab: EXAMPLE_TEST.replaceAll("\s+", "\t")
This is my small example string which I'm going to use for pattern matching.

Download Code: Simple RegEx

Sunday, December 30, 2012

Java Exercise #102: ArrayList Implementation

Exercise: Write a program that implements ArrayList

(1)[America]
(2)[Canada, America]
(3)[Canada, America, Russia]
(4)[Canada, America, Russia, France]
(5)[Canada, America, Germany, Russia, France]
(6)[Canada, America, Germany, Russia, France, India]
(7)[America, Germany, Russia, France, India]
(8)[America, Germany, France, India]
(9)[America, Germany, France]

Download Code: ArrayList Implementation

Saturday, December 29, 2012

Java Exercise #101: Count Word Occurrences

Exercise: Write a program that count the occurrences of words in a String.

and : 1
day : 1
fun : 1
good : 1
great : 1
have : 2
keep : 1
learning : 1
morning : 1

Download Code: Count Words

Thursday, December 27, 2012

Java Exercise #100: Eight Queens Problem (Recursion)

Eight Queens problem is to find a solution o place a queen in each row on a chess board such that no two queens attach each other

Exercise: Use recursion to write a program that finds a solution to Eight Queens problem.

Download Code: Eight Queens

Wednesday, December 26, 2012

Java Exercise #99: Sierpinski Triangle

Sierpinski triangle is created as follows:

    • Begin with an equilateral triangle, which is Sierpinski triangle of order 0.
      Connect the midpoints of the sides of triangle of order 0 to create Sierpinski triange of order 1.
      Leave the center triangle intact.
      Repeat the same process recursively to create Sierpinski triangle of order 3, 4 etc..
  • Exercise: Use recursion to write a program that creates Sierpinski triangles of the order which is input by the user.

    Download Code: Sierpinski Triangle

    Tuesday, December 25, 2012

    Java Exercise #98: Towers of Hanoi (Recursion)

    Towers of Hanoi is a classic recursion problem. The problem involves moving a specified number of disks of distinct sizes from one tower to another while observing the following rules.

    • There are n disks labeled 1,2,3...n and three towers labeled A, B and C.
      No disk can be on top of a smaller disk at any time.
      All the disks are initially placed on tower A.
      Only one disk can be moved at a time and it must be the top disk on the tower.
  • The objectve of the problem is to move all the disks from A to B with the assistance of C.

    Exercise: Use recursion to write a program that will take user input for number of disks and solve the problem for that number of disks.

    
    Enter the number of disks:3
    The moves are:
    Move disk 1 from A to B
    Move disk 2 from A to C
    Move disk 1 from B to C
    Move disk 3 from A to B
    Move disk 1 from C to A
    Move disk 2 from C to B
    Move disk 1 from A to B
    
    
    
    

    Download Code: Towers of Hanoi

    Java Exercise #97: Determine Directory Size

    Exercise: Use recursion to write a program that will determine the size of a given directory.

    
    Enter a file or directory: C:\Users\Maya\Favorites
    17711 bytes.
    
    Enter a file or directory: C:\Users\Maya\Desktop\aneesh\Java Exercises\Bouncing Ball.png
    26651 bytes.
    
    Enter a file or directory: C:\Users\Maya\Desktop\aneesh\Java Exercises\No File.txt
    0 bytes.
    
    
    

    Download Code: Directory Size