Вы находитесь на странице: 1из 10

WAP to implement the concept of Packages.

// Inside package1 folder

package package1;

public class ClassA {

public void displayA() {

System.out.println("Inside Class A");

}}

// Inside package2 folder

package package2;

public class ClassB {

protected int m = 10;

public void displayB() {

System.out.println("Inside Class B");

System.out.println("m = " + m);

}}

import package1.ClassA;

import package2.ClassB;

class package_prog {

public static void main(String args[]) {

ClassA objA = new ClassA();

ClassB objB = new ClassB();

objA.displayA();

objB.displayB();

}
Output:
WAP to implement the concept of Synchronization.

class Square {

// Function is synchronized

synchronized void printSquare(int n) {

for(int i = n; i <= (n+5); i++) {

System.out.println(i + " square is " + i*i);

Try {

Thread.sleep(100);

catch(Exception e) {

System.out.println("Thread Interrupted");

} } } }

class thread1 extends Thread {

Square sq;

thread1(Square sq) {

this.sq = sq;

public void run() {

sq.printSquare(5);

} }

class thread2 extends Thread {

Square sq;

thread2(Square sq) {

this.sq = sq;

public void run() {

sq.printSquare(20);
} }

public class Sync {

public static void main(String args[]) {

Square obj = new Square();

thread1 t1 = new thread1(obj);

thread2 t2 = new thread2(obj);

t1.start();

t2.start();

}
Output:
WAP to implement event handling using Mouse Listener.

<!DOCTYPE html>

<html>

<head>

<title>Welcome to my applet</title>

</head>

<body>

<center>

<applet code = MouseListenerExample.class width = 400 height = 200></applet>

</center>

</body>

</html>

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class MouseListenerExample extends Applet implements MouseListener {

String l = "";

public void init(){

addMouseListener(this);

public void mouseClicked(MouseEvent e) {

l = "Mouse Clicked";

repaint();

public void mouseEntered(MouseEvent e) {


l = "Mouse Entered";

repaint();

public void mouseExited(MouseEvent e) {

l = "Mouse Exited";

repaint();

public void mousePressed(MouseEvent e) {

l = "Mouse Pressed";

repaint();

public void mouseReleased(MouseEvent e) {

l = "Mouse Released";

repaint();

public void paint(Graphics g){

g.drawString(l,10,100);

}
Output:
WAP to implement Event Handling using Mouse Motion Listener.

<!DOCTYPE html>

<html>

<head>

<title>MyMousemotionlistener</title>

</head>

<body>

<applet code = Mouse.class width = 400 height = 100></applet>

</body>

</html>

import java.awt.event.*;

import java.awt.*;

import java.applet.*;

public class Mouse extends Applet implements MouseMotionListener {

String l="";

public void init(){

addMouseMotionListener(this);

public void mouseDragged(MouseEvent e){

l = "Mouse Dragged";

repaint(); }

public void mouseMoved(MouseEvent e){

l = "Mouse Moved";

repaint(); }

public void paint(Graphics g){

g.drawString(l,10,100);
}

Output: