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

Wednesday, October 24, 2012

Java Exercise #32: Binary Search

Java JDK provides Binary Search. This program is to implement such a binary search.

Exercise: Write an implementation of binary search and test as below.


List is : [2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79 ]
Binary Search of 2: 0
Binary Search of 11: 4
Binary Search of 12: -6
Binary Search of 1: -1
Binary Search of 3: -2


Download Code: Binary Search

Java Exercise #31: Count Letters

This program is to count the occurrences of each letter in an array.

Exercise: Write a program that generates 100 random lower case letters and then count the occurrence of each letter.


The lowercase letters are: 
m w u e b l g r p z v f h e n l u t c t 
e d k j k i r p a x g f p m v m x u c y 
w f b l k l k m w v c b l k s q a z h f 
g e v e p m v t g l j k u r b z k g q i 
s o z x y d f c n j u r c y j a l g q p 

The occurrence of each letter are:
3 a 4 b 5 c 2 d 5 e 5 f 6 g 2 h 2 i 4 j
7 k 7 l 5 m 2 n 1 o 5 p 3 q 4 r 2 s 3 t
5 u 5 v 3 w 3 x 3 y 4 z 

Download Code: Count Letters

Java Exercise #30: Assign Grades

This program is to read student scores and assign grades to each student

Exercise: Write a program that reads student scores, get the best score and then assign grade to each student using the following scheme:


       Grade is A if score is >= best - 10;
       Grade is B if score is >= best - 20;
       Grade is C if score is >= best - 30;
       Grade is D if score is >= best - 40;
       Grade is F otherwise.
  


Enter the number of students: 4

Please enter a score: 40
Please enter a score: 50
Please enter a score: 60
Please enter a score: 70
Student 0 score is 40 and grade is C
Student 1 score is 50 and grade is B
Student 2 score is 60 and grade is A
Student 3 score is 70 and grade is A

Download Code: Assign Grades

Saturday, October 20, 2012

Java Exercise #29: Max Number & Count

This program is to find the largest number in an array and then count its occurrences.

Exercise: Write a program that reads 6 integers from the user, then find the largest of them and count its occurrences.


Enter the number: 3
Enter the number: 5
Enter the number: 2
Enter the number: 5
Enter the number: 5
Enter the number: 4

The array is 5 2 5 5 4 
The largest number is 5
The number of occurrence is 3



Download Code: Max and its Count

Java Exercise #28: Print Calendar

This program is to display the calendar for given month of the year.

Exercise: Write a program that prompts the user to enter the year and the month, and then displays the entire calendar for the month as shown below.


Enter year as int (eg: 2003): 2006
Enter month as int (eg: 1 for January): 6
           June  2006
-----------------------------
 Sun Mon Tue Wed Thu Fri Sat
               1   2   3   4
   5   6   7   8   9  10  11
  12  13  14  15  16  17  18
  19  20  21  22  23  24  25
  26  27  28  29  30


Download Code: Print Calendar

Friday, October 19, 2012

Java Exercise #27: Random Characters

This program is to print random characters

Exercise: Write a program that prints random characters.


Random Character: ?
Random Lower Case Character: d
Random Upper Case Character: M
Random Upper Case Digit Character: 0



Download Code: Random Characters

Thursday, October 18, 2012

Java Exercise #26: Sentinel Value (Dialog Box)

This exercise is to use sentinel value using Dialog Box.

Exercise: Write a program that takes input from users using Dialog Box. The program takes input integers and then sum them up and display the sum value.

Download Code: Sentinel Value (Dialog Box)

Wednesday, October 17, 2012

Java Exercise #25: 50 Prime Numbers

This program is to print prime numbers.

Exercise: Write a program that prints first 50 prime numbers in five lines (10 prime numbers per line)


The first 50 prime numbers are: 
2  3  5  7  11  13  17  19  23  29
31  37  41  43  47  53  59  61  67  71
73  79  83  89  97  101  103  107  109  113
127  131  137  139  149  151  157  163  167  173
179  181  191  193  197  199  211  223  227  229


Download Code: 50 Prime Numbers

Tuesday, October 16, 2012

Java Exercise #24: Sum Skipping Numbers

This program is an exercise to use the keyword continue in loops.

Exercise: Write a program that sums integers from 0 to 20 but skipping the numbers 10 and 11.


The Sum is: 189


Download Code: Sum Skipping Numbers

Monday, October 15, 2012

Java Exercise #23: Sum Till 100

This program is an exercise to use the keyword break in loops.

Exercise: Write a program that sums integers from 0 to up, until the sum is greater than or equal to 100.


Sum is: 105
Number is: 14

Download Code: Sum Till 100

Sunday, October 14, 2012

Java Exercise #22: Number Pyramid

This program is to print a number pyramid.

Exercise: Write a program that prompts the user to enter an integer and then prints the pyramid wuth those many number of rows as shown below.


Enter the number of lines (between 1 and 15): 11
                                1
                             2  1  2
                          3  2  1  2  3
                       4  3  2  1  2  3  4
                    5  4  3  2  1  2  3  4  5
                 6  5  4  3  2  1  2  3  4  5  6
              7  6  5  4  3  2  1  2  3  4  5  6  7
           8  7  6  5  4  3  2  1  2  3  4  5  6  7  8
        9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9
    10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10
 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11


Download Code: Number Pyramid

Saturday, October 13, 2012

Java Exercise #21: Greatest Common Divisor

This program is to print Greatest Common Divisor(GCD) for two integers.

Exercise: Write a program that takes two integers as input from user and prints the GCD as below.


Enter the first integer: 125
Enter the second integer: 2525
Greatest Common Divisor for 125 and 2525 is 25

Download Code: GCD

Friday, October 12, 2012

Java Exercise #20: Multiplication Table

This program is to print multiplication table. This uses nested for loop

Exercise: Write a program that prints the multiplication table as below.


        Multiplication Table
       1   2   3   4   5   6   7   8   9
----------------------------------------
1 |    1   2   3   4   5   6   7   8   9
2 |    2   4   6   8  10  12  14  16  18
3 |    3   6   9  12  15  18  21  24  27
4 |    4   8  12  16  20  24  28  32  36
5 |    5  10  15  20  25  30  35  40  45
6 |    6  12  18  24  30  36  42  48  54
7 |    7  14  21  28  35  42  49  56  63
8 |    8  16  24  32  40  48  56  64  72
9 |    9  18  27  36  45  54  63  72  81

Download Code: Multiplication Table

Thursday, October 11, 2012

Java Exercise #19: Sentinel Value

This program is to take series of input from user as ints and then sum them up.

Exercise: Write a program that requests user to enter integers util the value entered is zero (which is used as sentinel value). Sum all the integer values and display it.

Enter an int value (Progam exits if the value is 0): 5
Enter an int value (Progam exits if the value is 0): 3
Enter an int value (Progam exits if the value is 0): 2
Enter an int value (Progam exits if the value is 0): 1
Enter an int value (Progam exits if the value is 0): 0
The sum is 11


Download Code: Sentinel Value

Wednesday, October 10, 2012

Java Exercise #18: Subtraction Quiz Loop

This program is to create a loop of subtraction quiz for primary level.

Exercise: Write a program that uses a loop to create 5 subtraction quizes at a time. It then generates a report and number of correct answers. Sample run below.

What is 8 - 7? 1
You are correct!

What is 7 - 1? 5
Your answer is wrong.
7 - 1 should be 6

What is 2 - 1? 1
You are correct!

What is 7 - 1? 6
You are correct!

What is 9 - 7? 1
Your answer is wrong.
9 - 7 should be 2


Correct count is 3 
Test time is 24 seconds

8 - 7 = 1 correct
7 - 1 = 5 wrong
2 - 1 = 1 correct
7 - 1 = 6 correct
9 - 7 = 1 wrong


Download Code: Subtraction Quiz Loop

Tuesday, October 9, 2012

Java Exercise #17: Guess Number

This program is for the user to guess the number computer has randomly generated

Exercise: Write a program that randomly generates an integer between 0 and 100 (inclusive). The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each input, the program tells the user whether the input is too high or too low, so that user can make intelligent guess. Following is a sample run.

Guess a number between 0 and 100
Enter your guess: 50
The number is too low
Enter your guess: 75
The number is too high
Enter your guess: 63
The number is too low
Enter your guess: 70
The number is too high
Enter your guess: 67
The number is too high
Enter your guess: 65
The number is too low
Enter your guess: 66
Yes, the number is 66

Download Code: Guess Number

Monday, October 8, 2012

Java Exercise #16: Lottery

This program is to develop a simple lottery program.

Exercise: Write a program that randomly generates a two-digit lottery integer and asks user to guess it. The winner is determined based on following:

  1. If the user input matches exactly with lottery, the award is $10,000.
  2. If the user input matches in digits, the award is $5000.
  3. If one input digits matches with lottery, the award is $2500.

Enter your lottery pick: 34
Lottery is : 17
Sorry. No match.
Enter your lottery pick: 82
Lottery is : 12
Match one digit! You won $2500

Download Code: Lottery

Sunday, October 7, 2012

Java Exercise #15: Subtraction Quiz

This program is for a first grader to practice subtraction.

Exercise: write a program that randomly two single-digit integers, num1 and num2 where num1 >= num2. It then requests the user to enter the difference

What is 3 - 0? 4
Your answer is wrong.
3 - 0 should be 3
What is 8 - 2? 6
You are correct!

Download Code: Subtraction Quiz

Saturday, October 6, 2012

Java Exercise #14: Leap Year

A year is a leap year if it is divisible by 4 but not by 400 or if it is divisible by 400.

Exercise: write a program that lets the user enter a year and checks whether it is a leap year?

Enter a year: 2008
2008 is a leap year? true
Enter a year: 2002
2008 is a leap year? false

Download Code: Leap Year

Friday, October 5, 2012

Java Exercise #13: Lowercase to Uppercase letter

This exercise is write a program that converts a lowercase letter to uppercase.

Exercise: write a program that converts a lowercase letter to uppercase.

Input a lowercase char: q
Uppercase is Q

Download Code: Lower to Upper

Thursday, October 4, 2012

Java Exercise #12: Sum Digits

This exercise is write a program that sum all digits of an integer.

Exercise: Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer

Enter an integer between 0 and 1000: 932
Sum of digits is: 14

Download Code: Sum Digits

Wednesday, October 3, 2012

Java Exercise #11: Compute Loan (Dialog Box)

This exercise is to calculate the monthly & total payments on the loan where input is taken from and output is displayed on dialog box.

Exercise: Write a program that takes input from user for loan amount, annual interest rate and tenure and display the total and monthly payment on dialog boxes.

Download Code: Compute Loan (Dialog Box)

Tuesday, October 2, 2012

Java Exercise #10: Show Current Time (GMT)

This exercise is write a program that displays current time in GMT

Exercise: Write a program that displays current time in GMT in the format hour:minute:second

Current time in GMT is 23:11:2

Download Code: Current Time

Monday, October 1, 2012

Java Exercise #9: Compute Change

This exercise is write a program that classifies given amount of money into smaller monetary units.

Exercise: Write a program that takes user input as a double value representing total in dollars and cents and outputs a report listing monteary equivalent in dollars, quarters, dimes, nickel and pennies.

Enter an amount in double (Eg: 11.56) : 11.56
 Your amount 11.56 consists of :
 11 dollars
 2 quarters
 0 dimes
 1 nickels
 1 pennies


Caution: One problem with the program is the loss of precision. Running the program for amount of 10.03, 10.03 * 100 to become 1002.9999999999. The program hence will output 10 dollars and 2 pennies. The program will need to re-written to take input in integers (say 1002 for 10.02 for instance).

Download Code: Compute Change