Wednesday, October 31, 2012

Java Exercise #39: Read From Text File

The problem is to write a program that reads from a text file using Scanner.

Exercise: Write a program that reads from a text file.


First Name: John Middle Name: T Last Name: Smith: 90
First Name: Eric Middle Name: K Last Name: Jones: 50

Download Code: Read From Text File

Java Exercise #38: Write to Text File

The problem is to write a program that writes to a text file using PrintWriter.

Exercise: Write a program that writes to a text file.

Download Code: Write to Text File

Monday, October 29, 2012

Java Exercise #37: Count Each Letter of String

The problem is to write a program that prompts the user to enter a string and counts the number of occurrences of each letter of the string regardless of the case.

The following are the steps involved.

  1. Convert the String to lowercase
  2. Create an array of size 26 for storing the count of each letter
  3. Increment the count for each letter after confirming that each letter is an alphabet

Exercise: Write a program that implements the above to count each letter of a string


Enter string: ababax
a appears 3 times
b appears 2 times
x appears 1 time



Download Code: Count Each Letter

Sunday, October 28, 2012

Java Exercise#36: Check Palindrome

A Palindrome is a string that reads the same when read forward or backward. Few examples are "Malayalam", "Dad" , "Mom".

The problem is to write a program that prompts the user to enter a string and then check whether it is a Palindrome or not. One intuitive solution to the problem is to compare the first and last characters of the string. If they are same, then compare the second and second-last and so on. This can continue until all characters are matched or until a mismatch is found

Exercise: Write a program that implements the above to check whether a String is a Palindrome


Enter a String: moon
moon is  not  a Palindrome!

Enter a String: malayalam
malayalam is  a Palindrome!


Download Code: Check Palindrome

Saturday, October 27, 2012

Java Exercise #35: Grading Multiple Choice Test

This exercise is to write a program that grades multiple choice test. The focus of this program is to use multi-dimensional arrays

Exercise: Write a program the grades the multiple choice test as below.


            Students' Answers to the Questions 
            0  1  2  3  4  5  6  7  8  9
Student 0 : A  B  A  C  C  D  E  E  A  D
Student 1 : D  B  A  B  C  A  E  E  A  D
Student 2 : E  D  D  A  C  B  E  E  A  D
Student 3 : C  B  A  E  C  D  E  E  A  D
Student 4 : A  B  D  C  C  D  E  E  A  D
Student 5 : B  B  E  C  C  D  E  E  A  D
Student 6 : B  B  A  C  C  D  E  E  A  D
Student 7 : E  B  E  C  C  D  E  E  A  D

             Key to the Questions
             0   1   2   3   4   5   6   7   8   9
    Key:     D   B   D   C   C   D   A   E   A   D

########### OUTPUT #################

For student 0's correct count is 7
For student 1's correct count is 6
For student 2's correct count is 5
For student 3's correct count is 6
For student 4's correct count is 8
For student 5's correct count is 7
For student 6's correct count is 7
For student 7's correct count is 7



Download Code: Grade Exam

Friday, October 26, 2012

Java Exercise #34: Insertion Sort

Insertion sort algorithm sorts a list of values by repeatedly inserting a new element into a sorted sublist until the whole list is sorted. Starting out, the sorted sublist is the list of size 1 which is the first element in the list. It then inserts the second element to the list by comparing with the element in the list and so on. For the thirde element, it compares the element with the sorted sub-list of size 2. It will move the larger elements to the right and insert the element in the appropriate position in the sub list. With each element being processed, the sublist size increments by one until all the elements are processed.

Exercise: Write an implementation for Insertion Sort.


Before sort: 
List is : [69, 4, 2, 10, 60, 50, 59, 11, 7, 70, 66, 79, 45 ]
After sort: 
List is : [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79 ]



Download Code: Insertion Sort

Thursday, October 25, 2012

Java Exercise #33: Selection Sort

Selection sort finds the largest number in the list and places it last. It then finds the largest number from the remaining list and places it second last and so on.

Exercise: Write an implementation for Selection Sort.


Before sort: 
List is : [69, 4, 2, 10, 60, 50, 59, 11, 7, 70, 66, 79, 45 ]
After sort: 
List is : [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79 ]



Download Code: Selection Sort