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

    Java Exercise #96: Recursive Selection Sort

    Exercise: Write a program that uses selection sort with recursion to sort a list.

    
    Before sort:
    2.0 5.0 3.0 1.2 9.8 3.0 
    After sort:
    1.2 2.0 3.0 3.0 5.0 9.8 
    
    

    Download Code: Recursive Selection Sort

    Monday, December 24, 2012

    Java Exercise #95: Palindrome With Recursion

    Exercise: Write a program that determines whether a String is a Palindrome. Use recursion.

    
    Is moon a Palindrome: false
    Is noon a Palindrome: true
    
    
    

    Download Code: Palindrome Using Recursion

    Java Exercise #94: Fibonacci With Recursion

    Exercise: Write a program that asks user input for a positive integer and then print the Fibonacci number for that index. Use recursion.

    
    Enter the Fibonacci Index: 7
    Fibonacci at index 7 is 13
    
    

    Download Code: Fibonacci Using Recursion

    Sunday, December 23, 2012

    Java Exercise #93: Factorial Using Recursion

    Exercise: Write a program that asks user input for a positive integer and then print the factorial of the input. The program should use recursion to determine factorial.

    
    Enter a non-negative number
    10
    Factorial of 10 is 3628800
    
    
    

    Download Code: Factorial Using Recursion

    Saturday, December 22, 2012

    Java Exercise #92: RandomAccessFile

    RandomAccessFile class allows a file to be read from or written to at random locations. Constructor takes String arguments "r"/"rw" for read only/read-write access to file. The method seek(int) can be used to place the pointer where the read and write can be done.

    Exercise: Write a program that demonstrates RandomAccessFile

    
    Current file length is 800
    First number is 0
    Second number is 1
    Tenth number is 9
    Modifying the eleventh number to 555
    Current file length is 800
    Eleventh number is 555
    
    

    Download Code: Random Access File

    Java Exercise #91: Reading & Writing Objects

    ObjectInput/OutputStreams can be used to read and write objects. These are filter streams that can completely replace data stream because it has all the functions available in the data streams like reading and writing integer, double, UTF etc. Apart from those, this also has functionality to read and write objects.

    However, please note that only Serializable objects (Classes or super classes of which implements Serializable interface) can be read or written to file.

    Exercise: Write a program that uses object streams to read and write objects.

    
    John 85.5
    Sat Dec 22 13:56:00 IST 2012
    
    
    

    Download Code: Object Stream Demo

    Java Exercise #90: Copy File Using IO

    BufferedInput/OutputStreams help performance by reducing the read and write from/to files. These are filter streams that read to or write from a byte array instead of doing it one by one.

    Exercise: Write a program that uses buffered streams to copy a file to a target file.

    
    The file ButtonDemo.java has 1578 bytes.
    Copy Done!
    
    

    Download Code: Copy

    Thursday, December 20, 2012

    Java Exercise #89: Data Stream Demo

    Data streams are actually wrappers on existing input and output streams. They are inherited from the filter streams, which in facts are the wrappers. Ordinary input/output streams can work only with bytes. Filter streams gives the capability to work with integers, doubles, strings etc.

    Exercise: Write a program that writes and reads from a binary file using data filter streams.

    
    John 85.5
    Jim 185.5
    George 105.5
    
    

    Download Code: Data Stream

    Java Exercise #88: FileStream Demo

    FileInputStream/FileOutputStream is for reading and writing bytes from/to files. All the methods are inherited from InputStream and OutputStream respectively.

    Exercise: Write a program that writes and reads from a binary file using input output streams

    
    0 1 2 3 4 5 6 7 8 9 
    
    

    Download Code: File Stream

    Wednesday, December 19, 2012

    Java Exercise #87: Image And Audio Demo

    Exercise: Write a program that creates an applet which will show the national flag of a selected country and play its national anthem. The applet will automatically display the next country and play its national anthem once the previous national anthem audio clip is completely played.

    Download Code: National Flats & National Anthems

    Java Exercise #86: Bouncing Ball

    Exercise: Write a program that creates an applet that has a bouncing ball. You can suspend/resume the ball using the buttons and also change the speed of the ball using the scroll bar.

    Download Code: Bouncing Ball Applet

    Sunday, December 16, 2012

    Java Exercise #85: Tic Tac Toe Applet

    Exercise: Write a program that creates an applet for playing tic tac toe game between two players.

    Download Code: Tic Tac Toe Applet

    Saturday, December 15, 2012

    Java Exercise #84: Compute Loan with Applet

    Exercise: Write a program that creates an applet for computing loan re-payment.

    Download Code: Loan Applet

    Java Exercise #83: Simple Applet

    Exercise: Write a program that creates a simple Applet that displays a message. Use AppletViewer to run it as a standalone window.

    Download Code: Simple Applet

    Java Exercise #82: Multiple Window Demo

    Exercise: Write a program that has a text area and a button where the user can enter a message in the text area. Clicking the button should open a new window which has a histogram on the count of each letter in the text area message.

    Download Code: Histogram

    Wednesday, December 12, 2012

    Java Exercise #81: Slider Demo

    Exercise: Write a program that creates a message in a panel. The message can be moved across the panel using a vertical and a horizontal slider (instead of scroll bar which was used in exercise# 81).

    Download Code: Slider Demo

    Tuesday, December 11, 2012

    Java Exercise #80: Scroll Bar Demo

    Exercise: Write a program that creates a message in a panel. The message can be moved across the panel using a vertical and a horizontal scroll bar.

    Download Code: Scroll Bar Demo

    Monday, December 10, 2012

    Java Exercise #79: List Demo

    Exercise: Write a program that uses a drop down list from which the user can select one or more countries. The program will then populate the flags of respective countries in the panel.

    Download Code: List Demo

    Sunday, December 9, 2012

    Java Exercise #78: Combo Box Demo

    Exercise: Write a program that enhances exercise 77 with a combo box from which the country can be selected to display its corresponding flag and flag description.

    Download Code: Combo Box Demo

    Java Exercise #77: Text Area Demo

    Exercise: Write a program that creates the following GUI which uses a non-editable text area.

    Download Code: Text Area Demo

    Saturday, December 8, 2012

    Java Exercise #76: Text Field Demo

    Exercise: Write a program on top of Exercise 75, where a text field is used to create the displayed message.

    Download Code: Text Field Demo

    Java Exercise #75: Radio Button Demo

    Exercise: Write a program on top of Exercise 74, where radio buttons are used to give color to the message.

    Download Code: Radio Button Demo

    Java Exercise #74: Check Box Demo

    Exercise: Write a program on top of Exercise 73, where check boxes are added to center, bold and italic the message

    Download Code: Check Box Demo

    Tuesday, December 4, 2012

    Java Exercise #73: Button Demo

    Exercise: Write a program that creates a frame with a message panel and two buttons - Left and Right. Clicking on the buttons make the message to move in the respective directions.

    Download Code: Button Demo

    Monday, December 3, 2012

    Java Exercise #72: Button Icons

    Exercise: Write a program that creates a frame with a button with icons. The icon will change when mouse rolls over and when clicked (as in the images below).

    Download Code: Button Icons

    Sunday, December 2, 2012

    Java Exercise #71: Live Clock that ticks

    Exercise: Write a program that creates a live analog clock that ticks every second.

    Download Code: Live Clock

    Saturday, December 1, 2012

    Exercise #70: Moving Message demo using Timer class

    The javax.swing.Timer class is a source component that can trigger ActionEvent at a predetermined interval. Listeners to this class needs to implement ActionListener. Listener can be added in the constructor of the Timer class or more listeners can be added using the addActionListener method on the class. Also the delay/interval can be adjusted at a later point of time using the setDelay method. Timer is started using the start() and stopped using the stop methods.

    Exercise: Write a program that demonstrates javax.swing.Timer class where a message moves across a panel at an interval of 1 second.

    Download Code: Moving Message with Timer