PRACTICLE OF JAVA (2)
PRACTICLE OF JAVA (2)
PRACTICLE OF JAVA (2)
JAVA
ROLL NO.:CS22031
INDEX:
SR. NAME OF PRACTICLES SIGNATURE
NO.
1. WAP IN JAVA TO ADD TWO NUMBERS.
2. WAP IN JAVA TO IMPLEMENT MULTITHREADING RUNNABLE
INTERFACE.
3. WAP IN JAVA TO IMPLEMENT MULTITHREADING USING BY
EXTENDING THREAD CLASS.
4. WAP IN JAVA TO IMPLEMENT EXCEPTION HANDLING USING
TRY, CATCH, THROW & FINALLY.
5. WAP IN JAVA TO IMPLEMENT THE CONCEPT OF
CONSTRUCTORS.
6. WAP IN JAVA TO IMPLEMENT THE CONCEPT OF
INHERITANCE.
7. WAP IN JAVA APPLET TO IMPLEMENT FLOWLAYOUT.
8. WAP IN JAVA APPLET TO IMPLEMENT BORDERLAYOUT.
9. WAP IN JAVA APPLET TO IMPLEMENT CHECKBOX.
10. WAP IN JAVA APPLET TO IMPLEMENT RADIOBUTTON.
11. WAP IN JAVA APPLET TO IMPLEMENT BUTTON.
12. WAP IN JAVA FOR JDBC TO CREATE A DATABASE.
13. WAP IN JAVA USING SWING TO ADD TWO NUMBERS.
1.WAP IN JAVA TO ADD TWO NUMBERS.
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
int a,b,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Number: ");
a=sc.nextInt();
System.out.println("Enter Second Number: ");
b=sc.nextInt();
sc.close();
sum=a+b;
System.out.println("Sum:"+sum);
}
}
OUTPUT:
2. WAP IN JAVA TO IMPLEMENT MULTITHREADING RUNNABLE INTERFACE.
class MyThread implements Runnable {
String name;
Thread t;
{
t = new Thread(this, "threadname");
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();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT:
3.WAP IN JAVA TO IMPLEMENT MULTITHREADING USING BY EXTENDING
THREAD CLASS.
class MyThread extends Thread {
String name;
Thread t;
{
t = new Thread(this, "threadname");
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 ExtendThread {
public static void main(String args[]) {
new MyThread();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
OUTPUT:
4.WAP IN JAVA TO IMPLEMENT EXCEPTION HANDLING USING TRY, CATCH,
THROW & FINALLY.
import java.lang.Exception;
class MyException extends Exception{
MyException(String message){
super(message);
}
}
class TestMyException{
public static void main(String[] args) {
int x=5,y =1000;
try {
float z=(float)x/(float)y;
if (z<0.01) {
throw new MyException("Number is too small");
}
} catch (MyException e) {
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally{
System.out.println("I am always here");
}
}
}
OUTPUT:
5.WAP IN JAVA TO IMPLEMENT THE CONCEPT OF CONSTRUCTOR.
public class Constructor {
String name;
Constructor(){
this.name = "default constructor in java";
}
public static void main(String[] args) {
Constructor obj = new Constructor();
System.out.println(obj.name);
}
}
OUTPUT:
6.WAP IN JAVA TO IMPLEMENT THE CONCEPT OF INHERITANCE.
class Professor {
void does() {
System.out.println("Teaching");
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.name);
System.out.println(obj.mainSubject);
obj.does();
OUTPUT:
7.WAP IN JAVA APPLET TO IMPLEMENT FLOWLAYOUT.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class FlowLayoutDemo extends Applet implements ItemListener
{
String msg="";
add(winVista);
add(solaris);
add(Mac);
winXP.addItemListener(this);
winVista.addItemListener(this);
solaris.addItemListener(this);
Mac.addItemListener(this);
}
g.drawString(msg, 6, 140);
msg="Mac: " + Mac.getState();
g.drawString(msg, 6, 160);
}
}
/*<applet code="MyApplet.java" width="500" height="500">
</applet>
*/
OUTPUT:
8.WAP IN JAVA APPLET TO
IMPLEMENT BORDERLAYOUT.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class BorderLayoutDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("This is across the top."), BorderLayout.NORTH);
add(new Label("The footer message might go here."), BorderLayout.SOUTH);
add(new Button("Right"), BorderLayout.EAST);
/*
<applet code="BorderLayoutDemo.java" width=400 height=200>
</applet>
*/
OUTPUT:
9.WAP IN JAVA APPLET TO IMPLEMENT CHECKBOX.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CheckboxTest extends Applet implements ItemListener
{
String msg="";
Checkbox yes, no, maybe;
public void init()
{
yes=new Checkbox("Yes");
no=new Checkbox("No");
maybe=new Checkbox("Undecided");
add(yes);
add(no);
add(maybe);
yes.addItemListener(this);
no.addItemListener(this);
maybe.addItemListener(this);
}
g.drawString(msg, 6, 140);
}
}
/*<applet code="CheckboxTest.class" width=500 height= 450>
</applet>
*/
OUTPUT:
10.WAP IN JAVA APPLET TO IMPLEMENT RADIOBUTTON.
import java.awt.*;
import java.applet.*;
{
CheckboxGroup chkgrp = new CheckboxGroup ( );
Checkbox chkRed,chkBlue,chkYellow,chkGreen,chkOrange;
add(chkRed);
add(chkBlue);
add(chkYellow);
add(chkGreen);
add(chkOrange);
}
}
/*<applet code="RadioButtonTest.java" width=500 height=200>
</applet>
*/
OUTPUT:
11.WAP IN JAVA APPLET TO IMPLEMENT BUTTON.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonTest extends Applet implements ActionListener
{
String msg="";
Button yes, no, maybe;
public void init()
{
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("Undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Yes"))
{
msg= "You pressed Yes.";
}
else if(str.equals("No"))
{
msg= "You pressed No";
}
else
{
msg= "You pressed Undecided.";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 6, 100);
}
}
/*<applet code="ButtonTest.class" width=500 height= 450>
</applet>
*/
OUTPUT:
OUTPUT:
OUTPUT: