Wednesday, November 7, 2012

Java Exercise #46: Anagram

The problem is to write a method that checks whether two strings are Anagrams.

Two words are anagrams if they contain the same letters in any order. For example, "silent" and "listen" are anagrams.

Exercise: Write a program that prompts the user to enter two strings and verify whether they are anagrams


Enter the first String: garden
Enter the second String: ranged
garden and ranged are anagrams


Enter the first String: gardens
Enter the second String: dangers
gardens and dangers are anagrams


Download Code: Anagrams

Tuesday, November 6, 2012

Java Exercise #45: Counting Digits in a String

The problem is to write a method that counts the number of digits in a String.

This implementation uses Character.isDigit() method to verify whether a character is a digit.

Exercise: Write a program that prompts the user to enter a String and then prints out the number of digits in the String.


Enter string: d#1@bcd23f1gb
Number of digits is: 4

Download Code: Count Digits

Java Exercise #44: Checking Substring

Java provides indexOf method in the String class to check whether a String is a sub-string of another string. The problem is to implement such a function.

This implementation helped me to come to know about labeled break and continue in Java. Default break/continue (non-labeled) breaks or continue from the inner most for, switch, while, do-while statements. However labeled ones help to break from/continue with the outer statements.

Exercise: Write a program that prompts the user to enter two Strings and then verify whether the first String is a sub-string of the second.


Enter the first string: el
Enter the second string: hello
el is a substring of hello

Enter the first string: hello
Enter the second string: al
No substring found



Download Code: Check Sub-String

Sunday, November 4, 2012

Java Exercise #43: Common Prefix

Write a method that returns the common prefix for two strings. For example, the common prefix for "distance" and "disappoint" is "dis". If the strings don't have a common prefix, print there is no common prefix between them.

Exercise: Write a program that prompts the user to enter two strings and display their common prefix.


Enter first string: paddy
Enter second string: wheat
No common prefix!

Enter first string: Display
Enter second string: disappoint
Common prefix is : dis


Download Code: Common Prefix

Saturday, November 3, 2012

Java Exercise #42: Stack of Integers

A stack is a data structure that holds objects in a last-in, first-out fashion.

A stack has many applications. Java Compiler uses a stack to process method invocations. When a method is invoked, its parameter and local variables are pushed to the stack. When the mentod calls another method, the new method's parameters and local variables are pushed into the stock. When the method finishes its work and return to the caller, its associated space is released.

Exercise: Write a program that implements a stack for storing integers. Stack should internally use an array to hold the elements.


Pushing: 
0 1 2 3 4 5 6 7 8 9 
Popping: 
9 8 7 6 5 4 3 2 1 0 



Download Code: Stack of Integers

Friday, November 2, 2012

Java Exercise #41: Choose and Then Read From File

This exercise is to write a program that chooses a file using the dialog box and then prints the content of the file. This program uses JFileChooser to create the dialog box for choosing file and Scanner class for printing the output

Exercise: Write a program that chooses a text file using dialog box and then prints the output



BODY {
font-family: Segoe UI;
font-size: 10pt;
color: 000000;
margin-left: 10 px;
margin-top: 15 px;
background-position: top left;
background-repeat: repeat;
}



Download Code: Choose and Read File

Thursday, November 1, 2012

Java Exercise #40: Replace Text In File

The problem is to write a program that replaces all occurrences of a String in the file. The program uses Scanner class to read from the file and PrintWriter to write to the file.

Exercise: Write a program that replaces all occurrences of a String in a file. Replace "world" with "universe".


####hello_world.txt###
Hello world!
Good Morning world!

###hello_universe.txt###
Hello universe!
Good Morning universe!



Download Code: Replace Text In File