JAVA LAB EXERCISES
WEEK-I
1. Write a Java Program to do the following:
(a) To output the question “Who is the inventor of Java?”
(b) To accept an answer
(c) To printout “GOOD” and then stop if the answer is correct
(d) To output the message “TRY AGAIN”, if the answer is wrong
(e) To display the correct answer, when the answer is wrong even at the third
Attempt
2. Write a Java Program that will compute the following series:
(a) 1 + 1/2 + 1/3+ …….+ 1/n
(b) 1 + 1/2 + 1/ 2 + 1/ 2 + … … + 1/ 2
2 3 n
(c) ex = 1 + x/1! + x /2! + x /3! + … …
2 3
WEEK-II
1. Write a Java Program to find the Simple Interest.
2. Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0.
Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display
a message stating that there are no real solutions.
3. The Fibonacci sequence is defined by the following rule:
The first two values in the sequence are 1 and 1. Every subsequent value is the sum of the
two values preceding it. Write a Java program that uses both recursive and non recursive
functions to print the nth value in the Fibonacci sequence.
4. Write a Java Program that prompts the user for an integer and then prints out all prime
numbers up to that integer.
WEEK - III
1. Write a program to differentiate access modifiers.
2. Create an inheritance hierarchy of Rodent, Mouse, Gerbil, Hamster etc. In the base class
provide methods that are common to all Rodents and override these in the derived classes
to perform different behaviors, depending on the specific type of Rodent. Create an array
of Rodent, fill it with different specific types of Rodents and call your base class
methods.
WEEK – IV
1. Write a class that implements the Iterator Interface found in the java.util package. The
ordered data for this exercise is the thirteen cards in a suit from a deck of cards. The
first call to next returns 2, the subsequent call returns the next highest card,3, and so
on, up to Ace. Write a small main method to test your class.
2. Create a package called main1, subclass and samepackage.
The main class should contain declarations of variables with different access
specifiers. Inside the constructor , print all the variables.
The subclass is derived from the main1 class.
The subclass and samepackage class should contain a constructor that tries to access
the same variables given above. Check if this is possible.
a. Create a class called PackTest, which imports the above package and creates an
object of class samepackage.
b. Create a class called Pack Test1, which also imports the same package, but
creates an object of class subclass.
WEEK - V
1. Write a program to demonstrate the use of StringIndexOutofBoundException.
2. Write a program to demonstrate the NullPointerException.
3. Write a program that creates a user interface to perform integer divisions. The user enters
two numbers in the text fields, Num1 and Num2. The division of Num1 and Num2 is
displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were
not an integer, the program would throw aNumberFormatException. If Num2 were Zero,
the program would throw an ArithmeticException Display the exception in a message
dialog box.
WEEK - VI
1. Write a Java program that checks whether the given string is palindrome or not
2. Write a Java program to sort a given list of names in ascending order
3. Write a Java program to multiply two given matrices.
4. Write a Java Program to find the 2 largest numbers of a given array
5. Write a Java program to make frequency count of words in a given text.
WEEK - VII
1. Write a Java program that creates three threads. First thread displays “Good Morning”
every one second, the second thread displays “Hello” every two seconds and the third
thread displays “Welcome” every three seconds.
2. Write a Java program that correctly implements producer consumer problem using the
concept of inter thread communication.
WEEK – VIII
1. Write a Java program that reads a file name from the user, then displays information about
whether the file exists, whether the file is readable, whether the file is writable, the type of
file and the length of the file in bytes.
2. Write a Java program that reads a file and displays the file on the screen, with a line number
before each line.
3. Write a Java program that displays the number of characters, lines and words in a text file.
WEEK – IX
1. Develop an applet that displays a simple message.
2. Develop an applet that receives an integer in one text field, and computes its factorial Value
and returns it in another text field, when the button named “Compute” is clicked.
3. Write a Java program that works as a simple calculator. Use to buttons for the digits and
for the +, -,*, % operations. Add a text field to display the result.
4. Write a Java program to print a chessboard pattern.
5. Write a Java program to print JAVA IS SIMPLE in different styles and fonts.
WEEK – X
1. Write a Java program that computes the payment of a loan based on the amount of the
loan, the interest rate and the number of months. It takes one parameter from the browser:
Monthly rate; if true, the interest rate is per month; otherwise the interest rate is annual.
2. Write a Java Program for handling mouse Events.
WEEK – XI
1. Write a java program that simulates a traffic light. The program lets the user select one of
three lights: red, yellow, or green. When a radio button is selected, the light is turned on,
and only one light can be on at a time No light is on when the program starts.
2. Write a Java program that allows the user to draw lines, rectangles and ovals.
WEEK – XII
1. Write a simple program that can read a host name and convert it to an IP address.
2. Write an online dictionary application, users are able to search online dictionary and get
the meaning of the words. The online dictionary should maintain its indexing in order to
enhance the performance and scalability.
3. Write a Socket-based Java server program that responds to client messages as follows:
When it receives a message from a client, it simply converts the message into all
uppercase letters and sends back same to the client. Write both client and server programs
demonstrating this.
WEEK-XIII
1. Write a Java program to read a file that contains DNA sequences of arbitrary length one
per line, sort the sequences in descending order with respect to the number of 'TATA'
subsequences present and finally write the sequences in sorted order into another file.
2. Write an interactive program that can update and delete employee via an employee
database using the PreparedStatement class.
3. Write an interactive banking system that can handle daily banking operations such as
deposit, withdraw, and money transfer using JDBC. The system should be reliable, which
means it must support transaction for each operation.