1151CS302 - Java Programming Lab
1151CS302 - Java Programming Lab
LAB MANUAL
3. Inheritance
4. Package creation.
9. JDBC-To connect Oracle/MS Access for Table creation and Data Manipulation.
G. Learning Resources
i. Text Books
1. H.M.Deitel and P.J.Deitel –“Java How to Program” Pearson Prentice Hall 6th Edition, 2011.
1. docs.oracle.com/javaee/6/tutorial/doc/girgm.html
2. www.webreference.com/programming/java.html
3. www.apl.jhu.edu/~hall/java/Documentation.html
INDEX
2 Constructors
4 Package creation
5
Interfaces
6 Threading
a) Creation of thread in Java applications
b) Multithreading
Aim:
To write a simple java application program for employee details for understanding reference to
an instance of a class (object), methods.
Algorithm
Step1: Start the program.
Step2: Declare all data members in employee class.
Step3: Define all member functions in employee class.
Step4: Create object for employee class.
Step5: call all the member functions and data members using object.
Step6: Display the result.
Step7: Stop the program.
Program
importjava.lang.*;
classemp
String name;
int id;
String address;
this.name=name;
this.id=id;
this.address=address;
voidputdata()
{
classempdemo
e.getdata("smith",76859,"gulbarga");
e.putdata();
OUTPUT
Name :smith
ID :76859
Address :Gulbarga
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 1(b) Handling String Function
Aim:
To write a simple java application program for student details for handling String functions.
Algorithm
Step1: Start the program.
Step2. Import the lang package.
Step3. Get input string.
Step4. And do the String manipulation operation such as modification, search and etc., using
appropriate methods.
Step5. Display the results.
Step6. Stop the program.
Program
importjava.lang.String;
classstringdemo
int result=s1.compareTo(s2);
System.out.println("After compareTo()");
if(result==0)
System.out.println( s1 + " is equal to "+s2);
else if(result>0)
else
String s3=s1.substring(4,12);
OUTPUT
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 2 Constructor
Aim:
To write a simple java application program for the following concept in Constructor over Loading.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program
importjava.lang.*;
class student
String name;
intregno;
int marks1,marks2,marks3;
// null constructor
student()
name="raju";
regno=12345;
marks1=56;
marks2=47;
marks3=78;
// parameterized constructor
student(String n,intr,int m1,int m2,int m3)
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
// copy constructor
student(student s)
name=s.name;
regno=s.regno;
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
void display()
}}
classstudentdemo
{
student s1=new student();
s1.display();
s2.display();
s3.display();
OUTPUT
raju 12345 56 47 78
john 34266 58 96 84
raju 12345 56 47 78
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp_No: 3 Inheritance
Aim:
To write a simple java application program for the following concept in Inheritance with overloading and
overriding methods.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program
Method overriding
importjava.lang.*;
class A
{
void display()
{
System.out.println("This is from class A ");
}
}
class B extends A
{
void display()
{
System.out.println("This is from class B ");
}
}
class AB
{
public static void main(String arg[])
{
B obj=new B();
obj.display();
}
}
OUTPUT
Method Overloading
classDisplayOverloading
System.out.println(c);
class Sample
obj.disp('a');
obj.disp('a',10);
}
Output
a 10
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 4 Package Creation
Aim
To write a simple java application program for Create User Defined Packages in Java.
Procedure
Step1. Create new folder as a calculator in existing folder
Step2: Open the notepad.
Step3. Type the package program in notepad and save <classname>.java
Step4. Then type the main program in the notepad and save <mainclassname>.java
Step5. Compile the package program : javac foldername/classname.java
Step6. Compile the source progam using javac <mainclassname.java>
Step7. Run the program using java <mainclassname>
Program
package forest;
import java.util.*;
public class Tiger
{
public void getDetails(String nickName, int weight)
{
System.out.println("Tiger nick name is " + nickName);
System.out.println("Tiger weight is " + weight);
}
}
Target directory
import forest.Tiger;
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 5 Interfaces
Aim:
To write java program for developing user-defined interfaces and implementation.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program
import java.util.*;
interface Fly
{
void goForward();
void goDown();
}
class Duck implements Fly
{
public void goForward()
{
System.out.println("Duck going forward");
}
}
public class Finch implements Fly {
OUTPUT
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 6 Threading
a.) Creation of thread in Java applications
Aim:
To write a java program for developing a chatting application(Client and Server) using Thread.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program
import java.util.*;
class MyThread implements Runnable {
String name;
Thread t;
MyThread String thread){
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThread {
public static void main(String args[]) {
new MyThread("One");
new MyThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT
b. Multithreading
program
import java.util.*;
class Count extends Thread
{
Count()
{
super("my extending thread");
System.out.println("my thread created" + this);
start();
}
public void run()
{
try
{
for (int i=0 ;i<10;i++)
{
System.out.println("Printing the count " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("my thread interrupted");
}
System.out.println("My thread run is over" );
}
}
class ExtendingExample
{
public static void main(String args[])
{
Count cnt = new Count();
try
{
while(cnt.isAlive())
{
System.out.println("Main thread will be alive till the child thread is live");
Thread.sleep(1500);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread's run is over" );
}
}
OUTPUT
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 7 Exception Handling Mechanism in Java
Aim
To write java program for Handling pre-defined exceptions and User-defined Exception.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using java <mainclassname>
Program
Pre-defined exception
class PrefinedException
{
public static void main(String arg[])
{
try{
int a1[]=new int[5];
int a,b,i,N;
double c;
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter the value of N");
N=Integer.parseInt(d.readLine());
System.out.println("Enter the array elements");
for(i=0;i<N;i++)
a1[i]=Integer.parseInt(d.readLine());
a=a1[0];
b=a1[1];
c=a/b;
System.out.println("c="+c);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error is "+e);
}
catch(ArithmeticException e)
{
System.out.println("Error is "+e);
}
catch(NumberFormatException e)
{
System.out.println("Error is "+e);
}
catch(IOException e)
{
System.out.println("Error is "+e);
}
}
}
OUTPUT
D:\raj>javac PrefinedException.java
Note: PrefinedException.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\raj>java PrefinedException
Enter the value of N
3
Enter the array elements
10
11
12
Sum of N elements=33
c=0.9090909090909091
D:\raj>
D:\raj>java PrefinedException
Enter the value of N
6
Enter the array elements
1
2
3
4
5
6
Error is java.lang.ArrayIndexOutOfBoundsException: 5
D:\raj>java PrefinedException
Enter the value of N
3
Enter the array elements
11
0
13
Sum of N elements=24
Error is java.lang.ArithmeticException: Divide by zero
// default constructor
MyException() { }
// parametrized constructor
MyException(String str) { super(str); }
// write main()
public static void main(String[] args)
{
try {
// display the heading for the table
System.out.println("ACCNO" + "\t" + "CUSTOMER" +
"\t" + "BALANCE");
catch (MyException e) {
e.printStackTrace();
}
}
}
OUTPUT
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exp-No: 8 AWT Components
Aim
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using applet viewer or Internet Explorer <mainclassname>
Program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Border()
{
super( "BorderLayout" );
Container c = getContentPane();
c.setLayout( layout );
bord.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
OUTPUT
Conclusion
Thus the java program was successfully complied and executed and result also verified.
Exno: 9 JDBC
Aim :
Write a simple for connect Oracle for Data Manipulation.
Procedure
Step1: Open the notepad.
Step2. Then type the program in the notepad and save in our own folder <mainclassname>.java
Step3. Set the path: set path=” C:\Program Files\Java\jdk1.6.0_20\bin”;
Step4. Set the classpath: set classpath=”C:\Program Files\Java\jdk1.6.0_20\lib”;
Step4. Compile the source progam using javac <mainclassname.java>
Step5. Run the program using applet viewer or Internet Explorer <mainclassname>
Program
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class addcus extends JFrame implements ActionListener
{
JTextField id;
JTextField name;
JTextField addr;
JTextField pno;
JTextField job;
JButton next;
JButton addnew;
JPanel p;
static Result res;
static Connection conn;
static Statement stat;
public addcus()
{
Super("our application");
Container c=getContentPane();
c.setLayout(new GridLayout(8,1));
id=new JTextField(20);
name=new JTextField(20);
addr=new JTextField(25);
pno=new JTextField(10);
job=new JTextField(15);
next=new JButton("Next");
p=new JPanel();
c.add(new JLabel("customer id",JLabel.CENTER));
c.add(id);
c.add(new JLabel("Customer name",JLabel.CENTER));
c.add(name);
c.add(new JLabel("Customer address",JLabel.CENTER));
c.add(addr);
c.add(new JLabel(" phone num",JLabel.CENTER));
c.add(pno);
c.add(new JLabel("Job",JLabel.CENTER));
c.add(job);
c.add(p);
p.add(next);
next.addActionListener(this);
setSize(500,500);
setVisible(true);
addWindowListener(new WIN());
}
public static void mian(string args[])
{
addcus c=new addcus();
try
{
Class.forName("sun.jdbc.odbc.jdbcodbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:data1");
stat=conn.CreateStatement();
res=stat.executeQuery("select *from customer");
res.next();
}
catch(Exception e)
{
System.out.println("Error"+e);
}
c.showRecord(res);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==next)
{
try
{
res.next();
}
catch(Exception ee)
{}
showRecord(res0;
}
}
public void showRecord(ResultSet res)
{
try
{
id.setText(res.getString(1));
name.setText(res.getString(2));
addr.setText(res.getString(3));
pno.setText(res.getString(4));
job.setText(res.getString(5));
}
catch(Exception e)
{}
}
class WIN extends WindowAdapter
{
public void windowClosing(windowEvent w)
{
JOptionPane jop=new JOptionPane();
jop.showMessageDialog(null,"Database","thanks",JOptionPane.QUESTION_MESSAGE);
}
}
}
OUTPUT
Conclusion
Thus the java program was successfully complied and executed and result also verified.