You are on page 1of 27

Java Lab

LIST OF EXPERIMENTS WEEK WISE


Exercise-1 (Unit-I)
Week-1

1. Write a java program to print the following output.


*
**
***
****
*****
2. Write a Java program to reverse a number.
3. Write a Java program to print Fibonacci series.
4. Write a java program to check given number is palindrome or not.
5. Write a java program to check given number is Armstrong or not.

Exercise-2 (Unit-I)
Week-2
1. Write a java program to read the different types of data from the user and display
that data using
Scanner class.
2. Write a java program to read the different types of data from the user and display
that data using
command line arguments.
3. Write a Java program that prompts the user for an integer and then prints out all
prime numbers up to
that integer.

Exercise-3 (Unit-I)
Week-3
1. Raju's parents are celebrating their marriage anniversary. They have arranged a
small party tonight. They
are expecting their colleagues and Raju's friends to attend the party. They have
planned to serve 'Coke'
and 'Badam Milk' to these guests. But they would like to serve 'Badam Milk' to
teenagers and 'Coke' to
adults. Please help them in finding teenagers and adults based on age. Write a Java
program to find out
the adults and teenagers based on their age.
Note: Teenagers are those whose age is between 13 and 19 (both inclusive).
• Step by Step guide
• Read the age as input from the user
• Check the age with the conditions mentioned
Display the appropriate messages (Eg: Adult or Teenager) and also a message
regarding the drink (Eg: Badam Milk or Coke)
2. There is a telecommunication company called “PoweredAir” who have approached
you to build their Interactive Voice Response (IVR) system. You should write a Java
program and be able to provide the following menu (given below): Note: User should
provide an input for each menu display.
Welcome to Powered Air Service. What would you like to do?
1. Know my balance
2. Know my validity date
3. Know number of free calls available
4. More
In case a user types in 4, the following menu should be displayed:
1. Prepaid Bill Request
2. Customer Preferences
3. GPRS activation
4. Special Message Offers
5. Special GPRS Offers
6. 3G Activation
7. Go back to Previous menu
If user types in 7, the first menu should be displayed.
You are free to display your own messages in this IVR

Exercise-4 (Unit-I)
Week-4
1. Write a Java program to read 10 numbers from user and store it in a array. Display
the maximum and minimum number in the array.
2. Write a Java program to search an element in an array using linear search algorithm.
3. Write a Java program to find duplicate elements in an integer array.

Exercise-5 (Unit-I)
Week-5
1. Create a class Rectangle. The class has attributes length and width. It should have
methods that calculate
the perimeter and area of the rectangle. It should have read Attributes method to read
length and width
from user.
Hint: Area of rectangle = length * width
Perimeter of rectangle = 2*(length+width)
2. Design a class "Company" which has as attributes yearOfEstablishment,
annualTurnover, annualSales,etc., Moreover, these details need to be available to the
outside world. Have appropriate methods for displaying these details. You will also
need to calculate the profitability of this company (if annualTurnover/annualSales > 1
then profitability is high; <0.5 then profitability is low; between 0.5 and
1 then profitability is medium).
3. Write a java program that implements method overloading.

Exercise-5 (Unit-I)
Week-6
1. Write a java program to check weather given string is palindrome or not.
2. Write a Java Program that reads a line of integers, and then displays each integer,
and the sum of all the
integers (use StringTokenizer class)
3. Write a Java program for sorting a given list of names in ascending order.
4. Write a Java program that displays the number of characters, lines and words in a
text file.

Exercise-6 (Unit-II)
Week-7
1. Write a java program to implement various types of inheritance.
i. Single ii. Multi Level
iii. Hierarchical iv. hybrid
2. Write a java program to implement runtime polymorphism.

Exercise-8 (Unit-II)
Week-9
1. Write a java program to implement the following
1. Creation of simple package
2. Accessing a package

Exercise-9 (Unit-III)
Week-10
1. Write a java program which accepts with draw amount from the user and throws an
exception “In
Sufficient Funds” when withdraw amount more than available amount.
2. Write a java program to illustrate finally block.

Exercise-10 (Unit-III)
Week-11
1. Write a java program to create three threads and that displays “good morning”, for
every one second, “hello” for every 2 seconds and “welcome” for every 3 seconds by
using extending Thread class.
2. Write a Java program that creates three threads. First thread displays “OOPS”, the
second thread displays “Through” and the third thread Displays “JAVA” by using
Runnable interface.

Exercise-11 (Unit-IV)
Week-12
1. Implement a Java program that allows the user to draw the following using
Graphics class
i. lines ii. rectangles iii. Ovals iv. Polygons

Exercise -12 (Unit-IV)


Week-13
1. Write java applet program to get image and audio clip? Play audio clip when applet
is visible and stop
when applet is minimized.
2. Write a java applet program to display Human face.
3. Write a java applet that display student information by using passing parameters to
applets concept.

Exercise-13 (Unit-IV)
Week-14
1. Implement a Java program for handling mouse events when the mouse entered,
exited, clicked, pressed,
released, dragged and moved in the client area.
2. Implement a Java program for handling key events when the key board is pressed,
released, typed.

Exercise-14 (Unit-IV)
Week-15
1. Write a java applet that reads two numbers from two separate text fields and
display sum of two
numbers in third text field when button “add” is pressed.
2. Develop a Applet program to accept two numbers from user and output the sum,
difference in the respective text boxes.

Exercise-15 (Unit-V)
Week-16
1. Write a JAVA program to design student registration form using AWT Controls.
The form which
having the following fields and button SAVE
Form Fields are: Name, RNO, Mailid, Gender, Branch ,Address
2. Write a java swing program that reads two numbers from two separate text fields
and display sum of two numbers in third text field when button ”add” is pressed.
Unit-I
Exercise-1
Week-1
1. Aim : Write a java program to print the following output.
*
**
***
****
*****
Algorithm :
Step1: Start
Step 2: Read a n value
Step 2: for x=1 to n do
Step 3: for y=1 to n do
Step 4: for each y print “*”
Step 5: Stop.
Source code:
Output:
*
**
***
****
*****
Conclusion: The program given above is error free.
2. Aim: Write a java program to reverse digits of a number.
Algorithm:
num=1234
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num

Source code:
Output:
4321
Conclusion: The program given above is error free.
3. Aim: Write a java program print Fibonacci series.
Algorithm:

Step 1. Start
Step 2. Declare variables i, a,b , show
Step 3. Initialize the variables, a=0, b=1, and show =0
Step 4. Enter the number of terms of Fibonacci series to be printed
Step 5. Print First two terms of series
Step 6. Use loop for the following steps
show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
Step 7. End

Source code:

Output:
0112358
Conclusion: The program given above is error free.

4. Aim: Write a java program check given number is palindrome or


not.
Algorithm:
Step 1. First, given number (num)'s value is stored in another integer
variable, originalInteger. This is because, we need to compare the
values of reversed number and original number at the end.
Step 2. Then, a while loop is used to loop through num until it is equal to 0.
a. On each iteration, the last digit of num is stored in remainder.
b. Then, remainder is added to reversedInteger such that it is added to the next
place value (multiplication by 10).
c. Then, the last digit is removed from num after division by 10.
Step 3. Finally, reversedInteger and originalInteger are compared. If equal, it is a
palindrome number. If not, it isn't.
Source code:

Output:
121 is a Palindrome
Conclusion: The program given above is error free.

5. Aim: Write a java program check given number is Armstrong or not.


Algorithm:

Step 1. First, given number (number)'s value is stored in another integer


variable, originalNumber. This is because, we need to compare the values of
final number and original number at the end.
Step 2. Then, a while loop is used to loop through originalNumber until it is equal
to 0.
a. On each iteration, the last digit of num is stored in remainder.
b. Then, remainder is powered by 3 (number of digits) using Math.pow() function
and added to result.
c. Then, the last digit is removed from originalNumber after division by 10.
Step 3. Finally, result and number are compared. If equal, it is an armstrong
number. If not, it isn't.

Source code:

Output:
1634 is an Armstrong number.
Conclusion: The program given above is error free.
Exercise-2
Week-2
1. Aim: Write a Java program that prompts the user for an integer and then prints
out all prime numbers up to that integer.
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:For i=1 to n do
Step 4:Set count=0
Step 5:For j=1 to n do step 6
Step 6: if (i%j= =0) count ++
Step 7: If count =2 then display i and repeat step 4
Step 8 :Stop.
Source code :

Output:
Prime numbers are
2357
Conclusion: The program given above is error free.

2. Aim :Write a java program to read the different types of data from the user and
display that data using Scanner class and command line arguments.

Description: There are various ways to read input from the keyboard, the
java.util.Scanner class is one of them.The Java Scanner class breaks the input into
tokens using a delimiter that is whitespace bydefault. It provides any methods to read
and parse various primitive values.Java Scanner class is widely used to parse text for
string and primitive types using regular expression.

Algorithm :
Step 1: Declare a variable of different data types
Step 2:Use Scanner class method nextInt() to read value into the
variable
Step 3:Print the value of integer variable.
Source Code:

Output:
Geek
F
40
9876543210
9.9

Command line argument in Java


The command line argument is the argument passed to a program at the time when you run it.
To access the command-line argument inside a java program is quite easy, they are stored as
string in String array passed to the args parameter of main() method.
class A{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
float b=Float.parseFloat(args[1]);
double c=Double.parseDouble(args[2]);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Output:
javac A.java
java A 10 20.5 456.789
10
20.5
456.789
Conclusion: The program given below is error free.
Exercise-3
Week-3
1. Aim: Raju's parents are celebrating their marriage anniversary. They have
arranged a small party tonight. They are expecting their colleagues and Raju's
friends to attend the party. They have planned to serve 'Coke' and 'Badam Milk'
to these guests. But they would like to serve 'Badam Milk' to teenagers and 'Coke'
to adults. Please help them in finding teenagers and adults based on age. Write a
Java program to find out the adults and teenagers based on their age. Note:
Teenagers are those whose age is
between 13 and 19 (both inclusive).
Step by Step guide
a. Read the age as input from the user
b. Check the age with the conditions mentioned
c. Display the appropriate messages (Eg: Adult or Teenager) and also a
message regarding the drink (Eg: Badam Milk or Coke)
Algorithm:
Step 1: Read the age using Scanner class
Step 2:If age>=13 and age<=19 print “Serve Badam Milk to Teenagers”
Step 3: If age>19 print “Serve Coke to Adult”
Step 4: else print “Not Applicable”.
Source Code:
Output:
Enter age: 16
Serve Badam Milk
Conclusion: The given above is error free.
2. Aim: There is a telecommunication company called “Powered Air” who have
approached you to build their Interactive Voice Response (IVR) system. You
should write a Java program and be able to provide the following menu (given
below):
Note: User should provide an input for each menu display.
Welcome to Powered Air Service. What would you like to do?
5. Know my balance
6. Know my validity date
7. Know number of free calls available
8. More
In case a user types in 4, the following menu should be displayed:
a. Prepaid Bill Request
b. Customer Preferences
c. GPRS activation
d. Special Message Offers
e. Special GPRS Offers
f. 3G Activation
g. Go back to Previous menu
If user types in 7 the first menu should be displayed.
You are free to display your own messages in this IVR
Algorithm:
Step 1: Start
Step 2: Read users input.
Step 3: Display as “Welcome to Powered Air Service”
Step 4: Invoke main switch loop.
Step 5: Display the sub menu in nested switch loop.
Step 6: Stop
Source Code:
Output:
Powered Air
1.know my balance
2.know my validity date
3.Know number of free calls available
4.More
Enter your choice
2
Your validity
Do you want to continue(1 yes 2 no)
Y
Powered Air
1.know my balance
2.know my validity date
3.Know number of free calls available
4.More
Enter your choice
4
MORE
1. Prepaid Bill Request
2. Customer Preferences
3. GPRS activation
4. Special Message Offers
5. Special GPRS Offers
6. 3G Activation
7. Go back to Previous menu
7
Back to Mai menu
1.know my balance
2.know my validity date
3.Know number of free calls available
4.More
Conclusion: The given program above is error free.
Exercise-4
Week-4
1. Aim : Write a program to read 10 numbers from user and store it in an array.
Display the maximum and minimum number in the array.
Algorithm:
Step 1 : Start
Step 2: Read a number of elements into the array.
Step 3: Read values into an array using Scanner class
Step 4: Initialize a[0] to max and min
Step 5: For i = 1 to n do do step 6 and step 7
Step 6: compare max and a[i]
Step 7: Compare min and a[i]
Step 8: Display min and max values.
Step 9: Stop.
Source Code:
Output:
Enter no. of elements in the array:5
Enter elements of array:
4
2
3
6
1
Maximum value: 6
Minimum value :1
Conclusion: The program given above is error free.

2. Aim: Write a Java program to search an element in an array using linear


search algorithm.
Algorithm:
Algorithm to search an element in an unsorted array using linear search
Let inputArray is an integer array having N elements and K be the number
to search.
• Using a for loop, we will traverse inputArray from index 0 to N-1.
• For every element inputArray[i], we will compare it with K for
equality. If equal we will print the index of in inputArray.
• If even after full traversal of inputArray, non of the element matches
with K then K is not present in inputArray.
Source Code:
Output:
Enter number of elements

Enter 6 elements

387294

Enter element to search

7 is present at index 2

Conclusion: The given program above is error free.

3. Aim: Write a program in Java to find duplicate elements in an integer array.


Algorithm:
Step 1. Algorithm to find duplicate elements in Array
Step 2. Declare an integer array "inputArray" for storing input array
elements.
Step 3. Declare a Map to store the frequency of elements of
inputArray.
Step 4. Using for-each loop, traverse input array and for each element
check whether element exists in Map or not.
Step 5. If present in map, increment it's count, other wise create a new
entry in Map with count as 1.
Step 6. For each key-value entry in Map, check whether value is > 1,
If true then corresponding key is a duplicate element
otherwise unique element.
Source Code:
Output:
Input Array
17321642
Duplicate Elements
12

Conclusion: The given program above is error free.

Exercise-5
Week-5
1. Aim: Create a class Rectangle. The class has attributes length and width. It should have
methods that calculate the perimeter and area of the rectangle. It should read Attributes method
to read length and width from user.
Hint: Ares of rectangle = length * width
Perimeter of rectangle = 2*(length+width)
Algorithm
Step 1 :Start
Step2: Define a class Rectangle with instance variables length and width.
Step 3: Add perimeter() method to calculate perimeter of a rectangle.
Step 4: Add area() method to calculate area of a rectangle.
Step 5: Create a object of Rectangle class and call the methods
Step 6: Display perimeter and area
Step 7: Stop
Source code:
Output:
Enter length and width
10 20
area= 200
perimeter= 60
Conclusion: The program given above is error free.
2. Aim: Design a class "Company" which has as attributes yearOfEstablishment,
annualTurnover, annual Sales, etc., Moreover, these details need to be available to the outside
world. Have appropriate methods for displaying these details. You will also need to calculate
the profitability of this company (if annual Turnover/annualSales > 1 then profitability is
high;<0.5 then profitability is low; between 0.5 and 1 then profitability is medium).
Algorithm:
Step 1: Start
Step 2: Read the values yearOfEstablishment , annualTurnover and
annualSales.
Step 3: Compute profit = annualTurnover/annualSales.
Step 4: If profit >1 display “profitability is high
If profit >=0.5 &profit <=1) display "profitability is medium"
If not display “profitability is low"
Step 5: Stop.
Output:
enter year of estblishment
1995
enter annualTurnover
4
enter annualSales
5
0.8
Profitability is medium
Conclusion: The program given above is error free
3. Aim: Write a java program that implements method overloading.
Description: If a class has multiple methods having same name but different in parameters,
it is known as Method Overloading.If we have to perform only one operation, having same
name of the methods increases the readability of the program.
There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
Algorithm:
Step 1: Create a class .
Step 2: Add different methods within the class with change in parameter and data
types.
Step 3: Create a object of the class and call the methods.
Step 4: Stop.
Source code:
Output:
22
33
Conclusion: The program given above is error free.

Exercise-5
Week-6
1. Aim: Write a java program to check weather given string is palindrome or not
Description: Strings which are widely used in Java programming, are a sequence of
characters. In Java programming language, strings are treated as objects. The Java platform
provides the String class to create and manipulate strings. StringLength: Returns the number
of characters contained in the string object
Algorithm:
Step 1: Start
Step 2: Read a string
Step 3:Compute string length
Step 4: Reverse the string
Step 5: Compare the two strings
Step 6: If equal print palindrome else not palindrome
Step 7: Stop
Source code:
Output:
Enter a string:
vignan
vignan is not a palindrome
Conclusion: The program given above is error free.

2. Aim :Write a Java program for sorting a given list of names in ascending order.
Description : Java BufferedReader class is used to read the text from a character-based
input stream. It can be used to read data line by line by readLine() method. It makes the
performance fast. It inherits Reader class.
Algorithm :
Step 1: Start
Step 2: Create an object of bufferedreader
Step 3: Enter the list on names.
Step 4:Compare with string tokenizer method.
Step 5: Print the names.
Step 6: Stop.
Output:
Enter The number of Names :3
Enter Name 1: compare
Enter Name 2: program
Enter Name 3: command
Names in Ascending Order
Command
Compare
Program
Conclusion: The program given above is error free
3. Aim : Write a Java Program that reads a line of integers, and then displays each integer,
and the sum of all the integers (use StringTokenizer class)
Description: The java.util.StringTokenizer class allows you to break a string into tokens.
It is simple way to break string. It doesn't provide the facility to differentiate numbers, quoted
strings, identifiers etc. like StreamTokenizer class.
Algorithm :
Step 1: Start
Step 2: Read a line of integer.
Step 3: Use string tokenizer method to split them into tokens.
Step 4: Sum each token
Step 5: Display the sum
Step 6: Stop.
Source code :
Output:
Enter A Line Of Integers: 1 4 23 2
Number of tokens : 4
Tokens are :
1
4
23
2
The Sum Is :30
Conclusion: The program given above is error free

4. Aim: Write a Java program that displays the number of characters, lines and words
in a text file.
Description: Java FileReader class is used to read data from the file. It returns data in byte
format like FileInputStream class.It is character-oriented class which is used for file handling
in java. InputStreamReader is a bridge from byte streams to character streams. It reads bytes
and decodes them into characters using a specified charset.
Algorithm :
Step 1: Create a object of File reader.
Step 2: Until the end of the file perform the following
Step 3: Read the text until empty line
Step 4: Compare each character with newline char ‟\n‟ to count number
of lines.
Step 5: Compare each character with tab char „\t‟ or space to count
number of
words.
Step 6: Compare first character with NULL char „\0‟ to find the end of
text.
Step 7: Number of characters = length of each line of text.
Step 8: Print number of lines ,words and characters.
Step 9 :Stop
Source code:
Output:
Welcome to java
Dept the CSE
No of lines= 2
No of words= 6
No the character = 27
Conclusion: The program given above is error free.

Unit-II
Exercise-6
Week-7
1. Aim: Write a java program to implement inheritance.
Description: Inheritance is the process of deriving a new class from an old class.
Algorithm :
Step1: create class with class name(super class)
Step2: define variables within that class
Step3: define method
Step4: create sub class for previous class with the help of extends keyword
Step5: define sub class variables
Step6: define sub class methods
Step7: create main class
Step8: define main method within main class
Step9: within the main method create object for sub class
Step10: call super class methods and sub class methods with the help of object.
Source code:
Output:
30
Conclusion: The program given above is error free.
2. Aim: Write a java program to implement runtime polymorphism.
Algorithm :
Step1: create class with class name (super class)
Step2: define variables within that class
Step3: define method
Step4: create subclass(1) for previous class with the help of extends keyword
Step5: define subclass(1) variables
Step6: define subclass(1) methods (method name should be same as super class
method)
Step7: create another subclass(2) for subclass(1) with the help of extends keyword
Step8: define subclass(2) variables
Step9: define subclass(2) methods (method name should be same as super class
method)
Step10: create main class
Step11: define main method within main class
Step12: with in main method create object for super class, subclass(1), andsubclass(2).
Step13:Assigning reference variable for super class
Step14: with the help of that reference variable refer all the super class, sub class(1),
sub
class(2) objects.
Step15: call super class methods and sub class methods with the help of that reference
variable.
Source code:
Output:
I am a generic Animal.
I am a Dog.
I am a Cow.
I am a Snake.
Conclusion: The program given above is error free.
Exercise-7
Week-8
1. Aim: Create an abstract class Media (id, description). Derive classes Book (pagecount)
and CD(playtime). Define parameterized constructors. Create one object of Book and CD
each and display the details.
Description: abstract class is a class which contains instance variables and concrete
methods and at least one abstract method.
Algorithm:
Step1: create abstract class with class name
Step2: define variables within the abstract class
Step3: define abstract method(without body)
Step4: create sub class for abstract class with the help of extends key word
step5: define subclass variables
step6: define subclass methods (method name should be same as super class method)
step7: create main class
step8: define main method within main class
Step9: within the main method create object for sub class
Step10:call sub class method with the help of object.
Source code:
Output:
1 oops 126
2 JAVA 1000
Conclusion: The program given above is error free.
2. Aim: Define an interface, operations which has method area(), volume(). Define a
constant PI having value 3.14. Create class a Cylinder which implements this interface
(member-id, height). Create one object and calculate area and volume.
Description: Interface is a pure abstract class which contains pure abstract methods.
Algorithm :
Step1: create interface
Step2: define variables within the interface
Step3: define abstract method(without body)
Step4: create a class to implement interface by using implements
keyword
step5: implement abstract methods of interface with in this class
step6: create main class
step7: define main method within main class
step8: within the main method create object for sub class
step9: call sub class methods with the help of object.
Source code:
Output:
Enter the radius
5
Enter the height
4
Area of the cylinder = 226.00
volume of the cylinder = 251.20000000000002
Conclusion: The program given above is error free.
Exercise-8
Week-9
1. Aim: Write a java program to implement the following
a) Creation of simple package
b) Accessing a package
Description: It is a group of related classes and interfaces
Algorithm :
Step1: define package
Step2: create class in that package
Step3: import package
Step4: create class
Step5: define main method
Step6: create objects for class
Source code:
Output:
11
5
Conclusion: The program given above is error free.

UNIT -3
Exercise-9
Week-10
1. Aim: Write a java program which accepts with draw amount from the user and throws
an exception “In Sufficient Funds” when with draw amount more than available amount.
Algorithm :
Step 1 : Import required packages
Step2 : Create a class that extends Exception class
Step3 : Create a constructor and use super.
Step4 : Create another class with main() method
Step5 : Read the required input
Step6 : Create, throw and catch user defined exception
Source code:
Output :
Enter withdraw amount
30000
In sufficient funds
Conclusion: The program given above is error free.
Exercise-10
Week-11
1. Aim: Write a java program to create three threads and that displays “good morning”,
for every one second ,”hello” for every 2 seconds and “welcome” for every 3 seconds by
using extending Thread class.
Algorithm :
Step 1: Create class A which extends Thread class and implement run ()
method with
in that class
Step 2: Define sleep () method to display good morning for every one
second .
Step 3: Create class B which extends Thread class and implement run ()
method with in
that Class
Step 4: Define sleep () method to display hello for every two seconds .
Step 5: Create class C which extends Thread class and implement run ()
method with in
that Class
Step 6: Define sleep () method to display welcome for every three seconds.
Step 7: Create another class with main method
Step 8 : Create objects for each class.
Step 9: Invoke start () method through each object.
Source code:
Output:
GOOD MORNING
WELCOME
HELLO
GOOD MORNING
HELLO
GOOD MORNING
WELCOME
GOOD MORNING
HELLO
GOOD MORNING
WELCOME
HELLO
HELLO
WELCOME
WELCOME
Conclusion: The program given above is error free.

2. Aim: Write a Java program that creates three threads. First thread displays “OOPS”, the
second thread displays “Through” and the third thread Displays “JAVA” by using Runnable
interface.
Algorithm :
Step 1 : Create class A which implements Runnable interface and
implement run ()
method with in that class
Step 2: Define sleep () method to display oops for every one second.
Step 3: Create class B which implements Runnable interface and
implement run ()
method with in that class
Step 4: Define sleep () method to display Through for every two seconds.
Step 5: Create class C which implements Runnable interface and
implement run ()
method with in that class
Step 6 : Define sleep () method to display JAVA for every three seconds.
Step 7: Create another class with main method
Step 8: Create objects for each class.
Step 9: Create Thread class object for every object.
Step 10: Invoke start () method through each object.
Source Code:
Output:
Oops
Through
Java
Conclusion: The above program is error free.
UNIT-4
Exercise-11
Week-12
1. Aim: Implement a Java program that allows the user to draw the following using Graphics
class i. Lines ii. Rectangle iii. Ovals iv. Polygons
Algorithm :
Step1:Import required Packages.
Step2: Extend applet class and Html tags
Step3: Define Paint Method.
Step4: Define Co-ordinates for each Object (Mentioned in the Problem).
Step5: Draw i. lines ii. rectangles iii. Ovals iv. Polygons.
Source code:
Output:

Conclusion: The program given above is error free.


Exercise-12
Week-13
1. Aim: Write a java applet that display student information by using passing parameters
to applets concept.
Algorithm :
Step1: Import required Packages.
Step2:Extend applet class and Define required Html tags
Step3: Declare a class
Step4: Define required parameters in class.
Step5: Define initialization method (init())
Step6: Define start method.
Step7: Pass parameters in start method.
Step8: Define paint method and Initialize values.
Step9: Define coordinates in for the above given parameters
Source code:
Output:

Conclusion: The program given above is error free.

Exercise-13
Week-14
1. Aim: Implement a Java program for handling mouse events when the mouse entered,
exited, clicked, pressed, released, dragged and moved in the client area.
Algorithm :
Step1: Import required Packages.
Step2: Extend applet class and Define required Html tags
Step3: Declare a class
Step4: Define required parameters in class.
Step5: Define initialization method (init()) and add mouse methods
Step6: Provide required mouse methods (2 or more depends upon your problem
&logic)
Step7: Define coordinate
Step8: Define paint method.
Source code:
Output:
Conclusion: The program given above is error free.
2. Aim: Implement a Java program for handling key events when the key board is pressed,
released, typed.
Algorithm :
Step1: Import required Packages.
Step2: Extend applet class and Define required Html tags
Step3: Declare a class
Step4: Define required parameters in class.
Step5: Define initialization method (init()) and add Key methods
Step6: Provide required Key Event methods (2 or more depends upon your
problem&logic)
Step7: Define coordinate
Step8: Define paint method.
Source Code:
Output:
Conclusion: The program given above is error free.
Exercise-14
Week-15
1. Aim: Write a java applet that reads two numbers from two separate text fields and
display sum of two numbers in third text field when button “add” is pressed.
Algorithm :
Step1: Import required Packages.
Step2: Extend applet class and Define required Html tags
Step3: Declare a class
Step4: Define required parameters in class.
Step5: Define initialization method (init())
Step6: Define Required Textfields, Labels and Buttons and Listeners
Step7: Define Action method.
Source code:
Output:
Conclusion: The program given above is error free.
UNIT-5
Exercise-15
Week-16
1. Aim : Write a JAVA program to design student registration form using AWT Controls.
The form which having the following fields and button SAVE Form Fields are:
Name, RNO, Mailid, Gender, Branch ,Address
Algorithm :
1. Import required Packages.
2. Create an applet using applet tag
3. Create a class that extends applet class and implements ActionListener
interface
4. Define initialization method (init())
5. Create required TextFields, TextArea, Labels, Buttons, checkboxes and
choices.
6. Add buttons to the window.
7. Call addActionListener() method through button which invokes
actioPerformed method
when button pressed .
8. Define actioPerformed method ().
9. Define paint () method.
Source Code:
Output:
Conclusion: The above program is error free.
2. Aim: Write a java swing program that reads two numbers from two separate text
fields and display sum of two numbers in third text field when button “add” is pressed
Algorithm :
1. Import required Packages.
2. Create an applet using applet tag
3. Create a class that extends JApplet class and implements ActionListener
interface
4. Define initialization method (init())
5. Create ContentPane and set Layout.
6. Create required JTextFields, JLabels, and JButtons classes.
7. Add textfields,labels and buttons to the ContentPane.
8. Call addActionListener() method through button which invokes
actioPerformed method
when button pressed .
9. Define actioPerformed method ().
10. In actioPerformed method () get the two values form the two textfields
and add those two
values and display sum on the third textfield
Source code:
Output:
Conclusion: The above program is error free.

You might also like