Lab Exercise-Basic Java
Lab Exercise-Basic Java
1. Try out the Listing 1.1 to 1.3 in order to understand the procedure for creating an applet
and an application.
2. Store a string in the form of an array and find out whether the string is a palindrome or
not.
3. Write a program which tells whether a number is even or odd. Take a range from 1 - 50.
4. Display the output which is given below:
*
**
***
5. Write a program which sorts an array of type integer.
6. Write a program which prints out the square of odd numbers, the even numbers are
skipped. Use a range between 1 - 20.
7. Write a program to display fibonacci series which is given here: 1,1,2,3,5,8,13
Page 1 of 12
LAB EXERCISE - II
1. Create a class called Employee that will have the following attributes:
Employee Number
Name
Address
Date of Birth
Hiredate
The class must have the following methods also:
i) A constructor that accepts nothing and initializes all numeric attributes with 0
and String with null
ii) A constructor that accepts all attribute values
iii) A method called display() that displays all attributes
iv) A method called changeAddress(String address) that changes the address of
an employee
Create two subclass of Employee class called Wagers and Salaried. The Wagers
should have additional attributes as days worked and salary per day. The Salaried
should have additional attributes as basic salary, da, hra, pf, and other deductions.
Both of these classes should have display() methods of their own that displays all
details of an employee and the type of employee (Salaried or Wager).
i) Test the methods of Employee class.
ii) Test the display() method of all three classes.
iii) Create an object of Wagers class and another object of Employee class. Type
cast the object of Wagers class to an object of Employee class.
iv) Create two objects of a class that have identical information and use ==
operator to check if it gives you the right result.
2. Write a program that accepts numeric command line arguments and adds all of them.
For example, if the argument list is:
100 100 134 150 300 200
The output should be:
984
3. Print all the methods and constructors of Button class.
4. Study the case study given below and create the broad outline of the classes in it. Human
Resource Department of ABC Ltd. handles nearly 10 applications per employee everyday.
Errors in filling forms translate into lost-time and money. They want to automate the task
of employee filling the forms correctly without any errors so that they can be further
processed and added to the database. The employee has to fill all the related forms (not
necessarily in any sequence). We will curtail the number of forms to be filled up (for the
purpose of cutting down the problem domain). There are two forms: ABC Ltd Provident
Fund Scheme and Employee Information Sheet.
Compiled by Logic Option Pvt. Ltd.
Page 2 of 12
Page 3 of 12
Page 4 of 12
Page 5 of 12
LAB EXERCISE IV
1. Draw a rectangle in an applet filled with red color. The dimensions and starting point of
the rectangle should be accepted from the HTML file. If no dimensions are given, draw
the rectangle having a width of 250 and height of 150 at (10,10) position.
2. Create an applet having the background color as pink. The applet should display an image
in its original size and the same image in half of its original size.
3. Add a sound file to the applet created in Q2. The sound file should be played in a loop
and should stop once the user stops the applet and restart when the user starts the applet
again.
4. Use Java2D to create applets that:
i) display this symbol.
ii) defines a stroke that is 15 pixels wide, having a round shape at the end of the
stroke and round shape again at the join and draw the same symbol again.
iii) draws the same symbol having different dash phases.
iv) fills the inner half circles with a diagonal gradient fill and the outer circle with
horizontal gradient fill. Use different fill colors.
Page 6 of 12
LAB EXERCISE V
1. Create an application which accepts two strings as command line arguments. It checks for
the number of command line arguments. If they are less or more it throws an exception
object giving an appropriate message.
2. Create an application that has integer variables a, b and c and result as float. Store some
values in them and apply the formula result = a / (b-c). Catch the probable exception.
3. Create an application that accepts some numbers, maximum five, from the command line.
It stores them in an array called marks and then adds 2% marks more to each element. It
then displays the average of the marks obtained. Zero/es can also be entered as command
line argument as a student may not have appeared in an exam/s. Catch the probable
exception/s.
4. Create an application that contain two classes - Temp and Caller. The Temp class has a
function disp() which displays any message. The Caller class has the main() method
which has only two statement Temp obj = null , and the next statements as obj.disp().
What is the exception being raised and how would you handle it, first by using trycatch
block and then using throws clause. What happens if you do not use throws clause in the
second case?
Page 7 of 12
LAB EXERCISE VI
1. Create an applet in which the text Java Platform 2 moves from right to left. The
moment entire text has moved past the left hand side of the screen, it should reappear
from the right side. First use overriding of update() method to remove flickering.
2. Create any animation of you choice. Add a sound file to the background. (Use any audio
and picture files).
3. Create an animation in which a ball moves back and forth in the following way:
Page 8 of 12
3. Create a panel and attach card layout to it. Create 2 panels more each of which is
containing a question( true/false question whose answer is submitted by using a check
box and multiple choice question whose answer is submitted by using a radio button).
Display these panels one by one programmatically. (After completing the next session
apply event handling to traverse through these panels during runtime)
4. Design the GUI interface to design the application for Case Study in session 2.Your
interface should look like
Page 9 of 12
Page 10 of 12
i) The menu options Close, Save, and Save As should disabled in the beginning and
should be enabled only when New or Open is clicked.
ii) From the View menu option, respective dialog boxes should be invoked as shown
in the figure. For doing this you must define only one subclass of Dialog class and
use it in your program to display all three types of dialog boxes.
Page 11 of 12
LAB EXERCISE IX
1. Create a file stream which writes to a file called parts.dat . This file contains the details of
a part like the partcode - int, qty_avail - int, rate - float. After writing to the file it displays
the contents of the file.
2. Take any file of your file system or find out whether it contains some bytes. If it is not
empty, read and display its contents using the buffered streams.
3. Accept a directory name as a string from the user and validate whether the string is a
directory or not. If it is a directory, then display the list of files and directories under it
distinguishing the files and directories by giving a remark along with their names.
4. Create a random file called salesman.txt that stores the, salesmanID - int and the
compensation - float, given to a particular salesman. Display the record of a particular
salesman.
5. Create a class called Orders that has the following elements:
Data Members
order_No - char
invoice_Number - int
order_Description - char
order_Quantity - float
Member Functions
getDetails()
showDetails()
Serialize this class, add some order details and store in a file called orders.txt . After
that print the order details.
6. Continuing with case study in session 8 handle the event for button Save Form. For
Employee Information sheet save the form objects in employee.txt file and clear the
screen. For Provident Fund save the form objects in pf.txt file and clear the screen.
Additional Exercise
7. Provide button Admission List in SInterface.java which when clicked on displays
details of the student (that is selected applicants) with their Student roll number,
CourseName and optional subject.
8. Modify class CreateOkDialog so that the along with the message to be displayed the
window name can also be passed at run time.
Page 12 of 12