Java Lab Manual
Java Lab Manual
Java Lab Manual
LAB MANUAL
JAVA LAB
INDEX
2 Lab Objective 4
7 References 58
j) Lab Objective
Week-5
13 Write a Program To find the product of matrices 38
14 Write a Program To find the sum of integers and then displays each 40
integer, and sum of all the integers using string to kenizer class
Week-6
15 Write a Program That reads on a file and display the information that 42
whether the file exists or not, to display the information about the file
and find the type of file whether readable, writable and the length of
bytes.
16 Write a Program That reads a file and displays the file on the screen 44
within line number before each line
Week-7
17 Write a Program That prints a number of characters ,words,lines in 46
that file
Week-8
19 Write an applet that displays a simple message 49
Week-9
21 Write a Program That works as a simple calculator using Grid layout 52
to arrange buttons for the digits and +,-,* % operations. Add a text
filed to print the result.
Week-11
26 Write a Program That implements the client/server application. The 57
client sends the data to the server, the client receives the data, uses it
to produce a result and then sends the result back to the client. The
client displays the result on the console.
Ex: The data sent by the client is radius of a circle and the result
produced by the server is area of a circle
OOP Concepts:
The object oriented paradigm is built on the foundation laid by the structured programming
concepts. The fundamental change in OOP is that a program is designed around the data being
operated upon rather upon the operations themselves. Data and its functions are encapsulated
into a single entity.OOP facilitates creating reusable code that can eventually save a lot of work.
A feature called polymorphism permits to create multiple definitions for operators and functions.
Another feature called inheritance permits to derive new classes from old ones. OOP introduces
many new ideas and involves a different approach to programming than the procedural
programming.
Object:
Objects are the basic run time entities in an object-oriented system.
thy may represent a person, a place, a bank account, a table of data or any item that the
program has to handle.
Class:
The entire set of data and code of an object can be made of a user defined data type with
the help of a class.
I fact, Objects are variables of the type class.
Once a class has been defined, we can create any number of objects belonging to that class
A class is thus a collection of objects of similar type.
for example: mango, apple, and orange are members of the class fruit.
ex: fruit mango; will create an object mango belonging to the class fruit.
Abstraction :
Abstraction referes to the act of representing essential features without including the
background details or explanations.
since the classes use the concept of data abstraction ,thy are known as abstraction data
type(ADT).
Inheritance :
Inheritance is the process by which objects of one class acquire the properties of
objects of another class. Inheritance supports the concept of hierarchical classification.
for example:
Bird
Attributes:
Feathers
Lay eggs
Attributes: Attributes:
----------- -----------
---------- -----------
Robin Swallow
Penguin Kiwi
Attributes: Attributes:
_________ _________ Attributes: Attributes:
_________ _________
The bird 'robin ' is a part of the class 'flying bird' which is agian a part of the class 'bird'.
The concept of inheritance provide the idea of reusability.
POLYMORPHISM:
Polymorphism is another important oop concept. Polymorphism means the ability to take
more than one form. an operation may exhibit different instances. The behavior depends upon
the types of data used in the operation.
The process of making an operator to exhibit different behaviors in different instance is
known as operator overloading.
Polymorphism plays an important role in allowing objects having different internal structures to
share the same external interface. Polymorphism is extensively used if implementing inheritance.
Shape
Draw()
Java History:
C++
C Java
Text Editor
Javac
Java
progra
m
Output
The way these tools are applied to build and run application programs is create a program. We
need create a source code file using a text editor. The source code is then compiled using the java
compiler javac and executed using the java interpreter java. The java debugger jdb is used to find
errors. A complied java program can be converted into a source code.
AIM:
Name of the Experiment:
1. Write a java program to find the simple interest.
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
JDK is required, With compilers.
Algorithm:
1. Start the program.
3. InterestTopay=principle*time*rate/100;
Test Data:
a) Valid data sets: 25000 int value as principal, 12.5f float value as rate, 2.75 as double
as interestToPay
b) Limiting value sets: Only integer values can be stored in the var. principal.
c) Invalid data sets: If we assign float value in var. principal error will occur.
Results for different data sets:
Principalamount is Rs.25000 interest=Rs.8593.75
Total amount to pay to clear the loan = Rs.33593.75
Viva-Voc Questions :
1) What is a method? And What is OOPS?
2) What is the signature of a method?
3) What is the difference between an instance variable and a class variable?
4) What is an abstract method?
5) What is an abstract class?
6)What is an object reference
AIM:
Name of the Experiment:
ALGORITHM:
Step1:start
Step2:input n,r,s
Step3:s=0
Step4:d=n%10
Step5:s=s+d
Step6:n=n/10
Step7: is n!=0
Step8: repeat step 4 to step 6 till step 7 is true
Step9:when step 7 falls
Step10:print s
Step11:stop
Test Data:
Multiplication of a and b = 48
Division of a and b = 3
AIM:
Name of the Experiment:
3. Write a java program to find the given single digit number using switch case.
Software/Hardware Requirements:
ALGORITHM:
Algorithm:
4. By using switch case ( multi way decision statement) when a match is found, that case is
executed.
Test Data:
AIM:
Name of the Experiment:
ALGORITHM:
Step1: start
Step2: input n,I,f
Step3: f=i=1
Step4: if(i<=n)
Step5: f=f*i
Step6: i=i+1
Step7: repeat from step5 to step6 till steps true
Step8: print f
tep9: stop
Test Data:
AIM:
Name of the Experiment:
5.Write a java program to check whether the first number is a multiple of second number.
Software/Hardware Requirements:
ALGORITHM:
1 Start the program.
4 By using switch case ( multi way decision statement) when a match is found, that case is
executed.
Test Data:
AIM:
Name of the Experiment:
Software/Hardware Requirements:
ALGORITHM:
1. Start the program.
Test Data:
3
1
Required Order is
1
1
2
3
4
5
6
7
8
AIM:
Name of the Experiment:
ALGORITHM:
6. sum=sum+r*r*r formula.
Test Data:
AIM:
Name of the Experiment:
Software/Hardware Requirements:
ALGORITHM:
6. By using if(d==0) “Roots are Equalent” loop rotating the integer value.
Test Data:
Enter value of a:
Enter value of b:
Enter value of c:
Enter value of a:
Enter value of b:
Enter value of c:
Root1 = -0.46887112585072543
Root2 = -8.531128874149275
AIM:
9.Write a java program to generate the Fibonacci series, given number of n values.
Software/Hardware Requirements:
Step1: start
Step3: f=0,f1=1,f2=1
Step4: do
I++
F1=f2
F2=f
F=f1+f2
While (i<=n)
Step5: print f
Step6: stop
Test Data:
0 1 1 2 3
Viva Voce Questions
49) . I use the following to read an int. It does not work. Why?
50). I'm trying to read in a character from a text file using the DataInputStream's
readChar() method. However, when I print it out, I get?’s.
51) . Why do I get garbage results when I use DataInputStream's readInt or readFloat
methods to read in a number from an input string?
52). How do I read data from a file?
53) . How do I write data to a file?
54). How do I append data to a file?
AIM:
Name of the Experiment:
10. Write a java program to find the prime numbers upto given number.
Software/Hardware Requirements:
S/W: JDK1.5(JAVA), Office XP, Windows NT Server with Service Pack
H/W: Pentium IV, Intel Mother Board Processor, 40 GB HDD, 256 MB RAM
ALGORITHM:
Step1: start
Step2: read n value
Step3: for i=1 i<=n
Step4:repeat a b c d e
a)factorial equal to 0
b) for i=1,j<=1 repeat c,d
c)if i percentage j equal to zero
d) fact equal to factorial added with one
e) if factorial equal to2print as prime number
step5: display the prime no till nth num
6: stop
Test Data:
57) . Can I write objects to and read objects from a file or other stream?
AIM:
Name of the Experiment:
write palandrom
else
Test Data:
Enter a string
madam
Given string is palindrome
Enter a string
cse
Given string is not palindrome
AIM:
Name of the Experiment:
12.Write a java program arrange strings into assending order
Software/Hardware Requirements:
7. if a[j]<a[j+1] then
Test Data:
AIM:
Name of the Experiment
13.Write a java program to find the product of the matrices by using two dimensional arrays.
Software/Hardware Requirements:
ALGORITHM:
Step1: start
Step2:read I,j,k,a[3][3],b[3][2],c[3][2]
Step3: read a[3][3] & b[3][2]
Step 4:i=0,j=0,k=0
Step5: if i<3 then i++ else goto 1
Step6: if j<3 then j++ else goto 5
Step7: if k<3 then k++ else goto 6
Step8: c[i][j]=c[i][j]+a[k][j]*b[i][k]
Step9: print a[i][j],b[i][j],c[i][j]
Step 10: stop
Test Data:
9
Enter the elements of the 2nd matrix
1
0
0
0
1
0
0
0
1
The productof the matrices is
123
456
789
AIM:
Name of the Experiment:
15.Write java program display the file is exists and number of bytes in the given file
Software/Hardware Requirements:
else
Test Data:
C:\DOCUME~1\student\Desktop>java p15
number of bytes in this file is:249
viva voce Questions
80) What is the signature of a method?
81) How do I create an instance of a class? 82) . Why do I get the
java.lang.UnsatisfiedLinkError when I run my Java program containing Native Method
invocations?
83) Given a method that doesn't declare any exceptions, can I override that method in a
subclass to throw an exception?
84) What's the difference between a runtime exception and a plain exception-why don't
runtime exceptions have to be declared?
85) Why do methods have to declare the exceptions they can throw?
86) Why does the compiler complain about Interrupted Exception when I try to use
Thread's sleep method?
AIM:
Name of the Experiment:
16.Write java program display the line no before the content of the given file
Software/Hardware Requirements:
ALGORITHM:
3. compute count=0
printf counter,count++
Test Data:
line no:3{
line no:5{
line no:3{
line no:5{
line no:8while(token.hasMoreTokens())
line no:9s=s+Integer.parseInt(token.nextToken());
line no:10System.out.println(s);
line no:11}
line no:12}
line no:13
line no:14?
AIM:
Name of the Experiment:
17.Write java program display the nof lines,words and characters of a given file
Software/Hardware Requirements:
3) compute count=0,word=0,line=1,space=0
line++
else
word++
else
char++
6) print word,line,char
Test Data:
AIM:
Name of the Experiment:
Software/Hardware Requirements:
ALGORITHM:
1) a)Algorithm For Inserting an Item into the Stack s:
Procudure PUSH(S,SIZE,TOP,ITEM)
S Array
SIZE Stack size
TOP Stack Pointer
ITEM value in a cell
Step1:{Check for stack overflow}
If TOP>=SIZE then
Printf(‘Stack overflow’)
Return
Step2:{Increment pointer top}
TOP=TOP+1
Step 3:{Insert ITEM at top of the Stack}
S[TOP]=ITEM
Return
Test Data:
OR
AIM:
Name of the Experiment:
19.Write a java applet to display simple message
Software/Hardware Requirements:
AIM:
Name of the Experiment:
20.Write an applet that computes the payment of a loan 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 per annual.
Software/Hardware Requirements:
S/W: JDK1.5(JAVA), Office XP, Windows NT Server with Service Pack
H/W: Pentium IV, Intel Mother Board Processor, 40 GB HDD, 256 MB RAM
ALGORITHM:
1 start
2 take applet
3 add(new Label("select months/years "));
4 actionPerformed(ActionEvent e)
5 else
6 stop
Integer.parseInt(ta.getText())*Integer.parseInt(tm1.getText())*Integer.parseInt(tr.getText())/1200
Test Data:
Valid Data Set: 24 months
Invalid Data Set: Not applicable
Limiting Data Sets: Integer Range (-32768 to 32767)
Results for different Data Sets
AIM:
Name of the Experiment:
21.write a java program that works as a simple calculator
Software/Hardware Requirements:
AIM:
Name of the Experiment:
22.Write a java program to implement the APPLET PACKAGES, draw Mouse event handler
programs.
Software/Hardware Requirements:
ALGORITHM:
1. Start the program.
4. Mouse moments, mouse Clicked, mouse Pressed, mouse Released, mouse Entered, mouse
Exited, mouse Dragged events args.
Test Data:
AIM:
Name of the Experiment:
Software/Hardware Requirements:
ALGORITHM:
1 Start
2 take class
3 for singal wait
4 true 0r false
5 println new InterThread(job)
6 stop
Test Data:
put: 0
got: 0
put: 1
got: 1
put: 2
got: 2
put: 3
got: 3
put: 4
got: 4
AIM:
Name of the Experiment:
25.Write a java program to implement the APPLET PACKAGES, draw Lines, Rectangles,
Rounded Rectangles, filled Polygons programs.
Software/Hardware Requirements:
ALGORITHM:
Test Data:
132) What is size of class having no variable & 1 function which returns int
AIM:
Name of the Experiment:
Software/Hardware Requirements:
Test Data:
Valid Data Set: hai
enter socket no:”127.0.0”,21
149) What are enumerators? and what are in-line functions? giv ans with eg.
REFERENCES:
BOOKS:
WEBSITES:
1. www.java.com
2. www.java.sun.com
3. www.freewarejava.com
4. www.javalobby.org