Java Practice Exercises
Java Practice Exercises
1. (AWT) Write a Java AWT program to create the following Frame window. The window
title should be displayed at the top-center, as displayed in the below figure. Below that,
there should be a Yellow button labeled ‘Click here’. Each time the button is clicked, a
random number is generated and displayed in the blue box. The blue box cannot be
edited. There is another Grey button labeled ‘Reset’. When this is pressed, anything
displayed in the blue box is cleared, and nothing is displayed in it.
2. (SWING) Write a Java program to read a comma separated file whose first line contains
the headings. Using Java Swing JTable, print the contents in a tabular manner in a
window.
3. (EXCEPTION) Write a Java program to accept a line from the user (use
Scanner.nextInt() to read each value) and add all the numbers that are present in the
line and present the sum. Use exception handling to handle any non-numeric characters.
There shouldn’t be any exceptions thrown for non-numeric characters, they should be
ignored. Example of an input line: Ram has 2 dogs and 3 cats. Program output should
be 5.
7. (POLYMORPHISM) Write a Java program to create a base class Shape with methods
draw and calculateArea. Create three subclasses Circle, Square and Triangle. Override
the draw method in each subclass to draw the respective shape and override the
calculate area method to calculate and return the area of each shape.
8. (STRING MANIPULATION) Write a Java program that will list the following options:
a. String compare
b. String compare ignoring case
c. String reverse
d. Change to uppercase
e. Change to lowercase
f. END
Each option will be repeated till the end user selects option f. END. Based on the option
selected, the program will prompt the user for input(s), carry out the operation and present the
result to the user.
10. (INNER CLASS) Write a Java program to demonstrate the various types of inner
classes: Member Inner class, Anonymous inner class, local inner class, static nested
class, nested interface.
11. (I/O) Read a text file containing names of friends & relatives, their date of birth (in
dd/mm/yyyy format) in random order. Obtain from end user the ascending or descending
order of birthdates as command-line argument, scan the text file and list the data as per
the preferred order.
12. (APPLET) Write an applet that would paint in red current date and in blue current time.
Embed the applet in an HTML and show.
Reading suggestion:
https://www.javatpoint.com