PRACTICLE OF JAVA (2)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

ADVANCE INSTITUTE OF

TECHNOLOGY AND MANAGEMENT


Session 2023-2024

JAVA

SUBMITTED BY: MAN MOHAN SUBMITTED TO: MRS. NEHA MAM

NAME: MAN MOHAN SIGN.:

COURSE: B.TECH CSE

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 {

String designation = "PROFESSOR";

String collegeName = "ADVANCED INSTITUTE OF TECHNOLOGY & MANAGEMENT";

String name = "MR. SAJJAN SIR";

void does() {

System.out.println("Teaching");

public class ab extends Professor {

String mainSubject = "JAVA";

public static void main(String args[]) {

ab obj = new ab();

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="";

Checkbox winXP, winVista, solaris,Mac;


public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
winXP=new Checkbox("Windows XP", null, true);

winVista=new Checkbox("Windows Vista");


solaris=new Checkbox("Solaris");
Mac=new Checkbox("Mac");
add(winXP);

add(winVista);
add(solaris);
add(Mac);
winXP.addItemListener(this);

winVista.addItemListener(this);
solaris.addItemListener(this);
Mac.addItemListener(this);
}

public void itemStateChanged(ItemEvent ie)


{
repaint();
}

public void paint(Graphics g)


{
msg="Current State: ";
g.drawString(msg, 6, 80);

msg="Windows XP " + winXP.getState();


g.drawString(msg, 6, 100);
msg="Windows Vista: " + winVista.getState();
g.drawString(msg, 6, 120);
msg="Solaris: " + solaris.getState();

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);

add(new Button("Left"), BorderLayout.WEST);


String msg="The reasonable man adapts" + "himself to the world;\n" +
"the unreasonable one persists in" + "trying to adapt the world to himself.\n" +
"Therefore all progress depends " + "On the unreasonable man.\n\n" +

" -George Bernard Shaw\n\n";


add(new TextArea(msg), BorderLayout.CENTER);
}
}

/*
<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);
}

public void itemStateChanged(ItemEvent ie)


{
repaint();
}

public void paint(Graphics g)


{
msg="Current state: ";
g.drawString(msg, 6, 80);

msg=" Yes: " + yes.getState();


g.drawString(msg, 6, 100);
msg=" No: " + no.getState();
g.drawString(msg, 6, 120);
msg=" Undecided: " + maybe.getState();

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.*;

public class RadioButtonTest extends Applet


{
public void init( )

{
CheckboxGroup chkgrp = new CheckboxGroup ( );

Checkbox chkRed,chkBlue,chkYellow,chkGreen,chkOrange;

chkRed = new Checkbox("Red", chkgrp, false);


chkBlue = new Checkbox("Blue", chkgrp, false);
chkYellow = new Checkbox("Yellow", chkgrp, false);
chkGreen = new Checkbox("Green", chkgrp, true);
chkOrange = new Checkbox("Orange", chkgrp, false);

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:

12.WAP IN JAVA FOR JDBC TO CREATE A DATABASE.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class New_DB {


static final String DB_URL = "jdbc:mysql://localhost/";
static final String USER = "root";
static final String PASS = "oracle";
public static void main(String[] args) {
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
) {
String sql = "CREATE DATABASE WINDOWS";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
} catch (SQLException e) {
e.printStackTrace();
}
}
}

OUTPUT:

13. WAP IN JAVA USING SWING TO ADD TWO NUMBERS.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sum
{
public static void main(String args[])
{
Abc obj=new Abc();
}
}
class Abc extends JFrame implements ActionListener
{
JLabel l1,l2,l3;
JTextField t1,t2;
JButton b;
public Abc()
{
setLayout(new FlowLayout());
l1=new JLabel("First Number:");
t1=new JTextField(20);
l2=new JLabel("Second Number:");
t2=new JTextField(20);
b=new JButton("Add");
l3=new JLabel("Result");
add(l1);
add(t1);
add(l2);
add(t2);
add(b);
add(l3);
b.addActionListener(this);
setVisible(true);
setSize(250,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
int num1=Integer.parseInt(t1.getText());
int num2=Integer.parseInt(t2.getText());
int value=num1+num2;
l3.setText(""+value);
}

OUTPUT:

You might also like