0% found this document useful (0 votes)
13 views

Java Practice Exercises

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Practice Exercises

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

4. (CUSTOM EXCEPTION) Write a custom exception InvalidNameFormatException that


will be used to report exceptions when a name is entered with numbers in it. Create a
Java AWT Frame window to accept a name. If the name has characters other than
letters of the alphabet, InvalidNameFormatException is thrown. This exception is to be
caught and a pop-up is to be displayed with the exception message - Invalid name.

5. (INTERFACE & ABSTRACT CLASS) Construct a virtual CoffeeMaker that dispenses


Latte, Cappuccino and Black Coffee. Should have all the required ingredients that are
used for making these coffee types. Use both interface and abstract classes.

6. (INTERFACE & ABSTRACT CLASS) Construct a virtual scientific calculator that


calculates logarithm, exponential, root and factorial operations using interface and
abstract classes.

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.

9. (ENCAPSULATION & PACKAGE) Write a Java program to perform employee payroll


processing using packages. Create a package java.emp, and include class Employee.
Declare the variables name, employee id, category, basic pay (private), HRA (10% of
basic pay), DA (5% of basic pay), Gross Pay, Income Tax (40% of gross pay) and
allowance (2% of basic pay). Calculate the gross salary and the income tax values in
methods. Create another class in another package java.accounts that would print the
employee details, gross salary and income tax.

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

You might also like