100% found this document useful (2 votes)
258 views36 pages

Java Programming Laboratory Manual For 5 Sem Is and Cs

This document is a laboratory manual for a Java programming lab course taught at Government Polytechnic, Gulbarga during the 2011-2012 academic year. It was authored by Miss Savitha R, a lecturer in the Information Science department. The manual contains 8 programs that demonstrate various Java concepts like defining classes, constructors, inheritance, nested classes, arrays, strings, and vectors. Contact details for feedback are provided at the end.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
258 views36 pages

Java Programming Laboratory Manual For 5 Sem Is and Cs

This document is a laboratory manual for a Java programming lab course taught at Government Polytechnic, Gulbarga during the 2011-2012 academic year. It was authored by Miss Savitha R, a lecturer in the Information Science department. The manual contains 8 programs that demonstrate various Java concepts like defining classes, constructors, inheritance, nested classes, arrays, strings, and vectors. Contact details for feedback are provided at the end.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

JAVA PROGRAMMING LAB

2011-2012

JAVA PROGRAMMING LABORATORY MANUAL FOR 5TH SEM IS AND CS

(2011-2012)

BY

MISS. SAVITHA R LECTURER INFORMATION SCIENCE DEPTATMENT GOVERNMENT POLYTECHNIC GULBARGA


FOR ANY FEEDBACK CONTACT TO

EMAIL: savitharamu@[Link]

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

JAVA PROGRAMMING LAB


PROGRAM 1 /* Write a Java Program to define a class, describe its constructor, overload the Constructors and instantiate its object */ import [Link].*;

2011-2012

class student { String name; int regno; int marks1,marks2,marks3; // null constructor student() { name="raju"; regno=12345; marks1=56; marks2=47; marks3=78; } // parameterized constructor student(String n,int r,int m1,int m2,int m3) { name=n; regno=r; marks1=m1; marks2=m2; marks3=m3; } // copy constructor student(student s) { name=[Link]; regno=[Link]; marks1=s.marks1; marks2=s.marks2; marks3=s.marks3; } void display() { [Link](name + "\t" +regno+ "\t" +marks1+ "\t" +marks2+ "\t" + marks3); } } class studentdemo { public static void main(String arg[]) { student s1=new student();
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 2

JAVA PROGRAMMING LAB


student s2=new student("john",34266,58,96,84); student s3=new student(s1); [Link](); [Link](); [Link](); } }

2011-2012

****************************OUTPUT************************************* c:\jdk1.6.0_26\bin>javac [Link] c:\jdk1.6.0_26\bin>java studentdemo raju john raju 12345 56 34266 58 12345 56 47 96 47 78 84 78

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

JAVA PROGRAMMING LAB


PROGRAM 2

2011-2012

/* Write a Java Program to define a class, define instance methods for setting and Retrieving values of instance variables and instantiate its object.*/ import [Link].*; class emp { String name; int id; String address; void getdata(String name,int id,String address) { [Link]=name; [Link]=id; [Link]=address; } void putdata() { [Link]("Employee details are :"); [Link]("Name :" +name); [Link]("ID :" +id); [Link]("Address :" +address); } } class empdemo { public static void main(String arg[]) { emp e=new emp(); [Link]("smith",76859,"gulbarga"); [Link](); } }

**************************************OUTPUT***************************** c:\jdk1.6.0_26\bin>javac [Link] c:\jdk1.6.0_26\bin>java empdemo Employee details are : Name :smith ID :76859 Address :Gulbarga

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

JAVA PROGRAMMING LAB


PROGRAM 3

2011-2012

/* Write a Java Program to define a class, define instance methods and overload them and use them for dynamic method invocation.*/ import [Link].*; class add { void display(int a,int b) { int c=a+b; [Link]("The sum of " + a + " & " + b + " is " + c); } void display(double a,double b) { double c=a+b; [Link]("The sum of " + a + " & " + b + " is " + c); } } class add_demo { public static void main(String arg[]) { add obj=new add(); [Link](10,20); [Link](10.2,20.2); } } **************************************OUTPUT***************************** c:\jdk1.6.0_26\bin>javac add_demo.java c:\jdk1.6.0_26\bin>java add_demo The sum of 10 & 20 is 30 The sum of 10.2 & 20.2 is 30.4

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

JAVA PROGRAMMING LAB


PROGRAM 4 /* Write a Java Program to demonstrate use of sub class */ import [Link].*; class parent { int m; void get_m(int m) { this.m=m; } void display_m() { [Link]("This is from parent : m = " +m); } } class child extends parent { int n; void get_n(int n) { this.n=n; } void display_n() { [Link]("This is from child : n = " +n); } } class childdemo { public static void main(String arg[]) { child c=new child(); c.get_m(10); c.get_n(20); c.display_m(); c.display_n(); } }

2011-2012

**************************OUTPUT******************************* C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java childdemo This is from parent : m = 10 This is from child : n = 20
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 6

JAVA PROGRAMMING LAB


PROGRAM 5 /* Write a Java Program to demonstrate use of nested class.*/ import [Link].*; class outer { int m=10; class inner { int n=20; void display() { [Link]("m = "+m); [Link]("n = "+n); } } } class nesteddemo { public static void main(String arg[]) { outer outobj=new outer(); [Link] inobj=[Link] inner(); [Link](); } }

2011-2012

****************************OUTPUT*************************************

C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java nesteddemo m = 10 n = 20

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

JAVA PROGRAMMING LAB


PROGRAM 6 /* Write a Java Program to implement array of objects. */ import [Link].*; public class EmployeeTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 3500); staff[1] = new Employee("Carl Cracker", 7500); staff[2] = new Employee("Tony Tester", 3800); for (int i = 0; i < 3; i++) staff[i].print(); } } class Employee { private String name; private double salary; public Employee(String n, double s) { name = n; salary = s; } public void print() { [Link](name + " " + salary); } } ******************OUTPUT**************************** C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java EmployeeTest Harry Hacker 3500.0 Carl Cracker 7500.0 Tony Tester 3800.0

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

JAVA PROGRAMMING LAB

2011-2012

PROGRAM 7 (A) Write a Java program to practice using String class and its methods. import [Link]; class stringdemo { public static void main(String arg[]) { String s1=new String("gpt gulbarga"); String s2="GPT GULBARGA"; [Link](" The string s1 is : " +s1); [Link](" The string s1 is : " +s2); [Link](" Length of the string s1 is : " +[Link]()); [Link](" The first accurence of r is at the position : " +[Link]('r')); [Link](" The String in Upper Case : " +[Link]()); [Link](" The String in Lower Case : " +[Link]()); [Link](" s1 equals to s2 : " +[Link](s2)); [Link](" s1 equals ignore case to s2 : " +[Link](s2)); int result=[Link](s2); [Link]("After compareTo()"); if(result==0) [Link]( s1 + " is equal to "+s2); else if(result>0) [Link]( s1 + " is greather than to "+s2); else [Link]( s1 + " is smaller than to "+s2); [Link](" Character at an index of 6 is :" +[Link](6)); String s3=[Link](4,12); [Link](" Extracted substring is :"+s3); [Link](" After Replacing g with a in s1 : " + [Link]('g','a')); String s4=" This is a book "; [Link](" The string s4 is :"+s4); [Link](" After trim() :"+[Link]()); } } *************************OUTPUT************************************ c:\jdk1.6.0_26\bin>javac [Link] c:\jdk1.6.0_26\bin>java stringdemo The string s1 is : gpt gulbarga The string s1 is : GPT GULBARGA Length of the string s1 is : 12 The first accurence of r is at the position : 9 The String in Upper Case : GPT GULBARGA
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 9

JAVA PROGRAMMING LAB


The String in Lower Case : gpt gulbarga s1 equals to s2 : false s1 equals ignore case to s2 : true After compareTo() gpt gulbarga is greather than to GPT GULBARGA Character at an index of 6 is :l Extracted substring is :gulbarga After Replacing g with a in s1 : apt aulbaraa The string s4 is : This is a book After trim() :This is a book

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

10

JAVA PROGRAMMING LAB


PROGRAM 7 (B) Write a Java program to practice using String Buffer class and its methods. import [Link]; class stringbufferdemo { public static void main(String arg[]) { StringBuffer sb=new StringBuffer("This is my college"); [Link]("This string sb is : " +sb); [Link]("The length of the string sb is : " +[Link]()); [Link]("The capacity of the string sb is : " +[Link]()); [Link]("The character at an index of 6 is : " +[Link](6)); [Link](3,'x'); [Link]("After setting char x at position 3 : " +sb); [Link]("After appending : " +[Link](" in gulbarga ")); [Link]("After inserting : " +[Link](19,"gpt ")); [Link]("After deleting : " +[Link](19,22)); } }

2011-2012

**********************************OUTPUT*********************************

c:\jdk1.6.0_26\bin>javac [Link] c:\jdk1.6.0_26\bin>java stringbufferdemo This string sb is : This is my college The length of the string sb is : 18 The capacity of the string sb is : 34 The character at an index of 6 is : s After setting char x at position 3 : Thix is my college After appending : Thix is my college in gulbarga After inserting : Thix is my college gpt in gulbarga After deleting : Thix is my college in gulbarga

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

11

JAVA PROGRAMMING LAB


PROGRAM 8 Write a Java Program to implement Vector class and its methods.

2011-2012

import [Link].*; import [Link]; import [Link]; class vectordemo { public static void main(String arg[]) { Vector v=new Vector(); [Link]("one"); [Link]("two"); [Link]("three"); [Link]("zero",0); [Link]("oops",3); [Link]("four",5); [Link]("Vector Size :"+[Link]()); [Link]("Vector apacity :"+[Link]()); [Link](" The elements of a vector are :"); Enumeration e=[Link](); while([Link]()) [Link]([Link]() +" "); [Link](); [Link]("The first element is : " +[Link]()); [Link]("The last element is : " +[Link]()); [Link]("The object oops is found at position : "+[Link]("oops")); [Link]("oops"); [Link](1); [Link]("After removing 2 elements "); [Link]("Vector Size :"+[Link]()); [Link]("The elements of vector are :"); for(int i=0;i<[Link]();i++) [Link]([Link](i)+" "); } } **************************OUTPUT****************************** C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java vectordemo Vector Size :6 Vector apacity :10 The elements of a vector are : zero one two oops
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 12

JAVA PROGRAMMING LAB


three four The first element is : zero The last element is : four The object oops is found at position : 3 After removing 2 elements Vector Size :4 The elements of vector are : zero two three four

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

13

JAVA PROGRAMMING LAB


PROGRAM 9 Write a Java Program to implement Wrapper classes and their methods. import [Link].*; class wrapperdemo { public static void main(String args[]) { Float P=new Float(0); Float I=new Float(0); int y=0; try { DataInputStream ds=new DataInputStream([Link]); [Link]("ENTER THE PRINCIPAL AMOUNT"); [Link](); String sp=[Link](); P=[Link](sp); [Link]("ENTER THE INTEREST RATE"); [Link](); String SI=[Link](); I=[Link](SI); [Link]("ENTER THE NUMBER OF YEARS"); [Link](); String sy=[Link](); y=[Link](sy); } catch(Exception e) { [Link]("INPUT OUTPUT ERROR"); [Link](1); } float value=loan([Link](),[Link](),y); [Link]("FINAL VALUE IS:"+value); } static float loan(float P,float I,int y) { int year=1; float sum=P; while(year<=y) { sum=sum+(P*I)/100; year++; } return sum; } }
Dept. of CS & IS, Govt. Polytechnic, Gulbarga

2011-2012

14

JAVA PROGRAMMING LAB

2011-2012

*******************************OUTPUT**************************** C:\jdk1.6.0_26\bin>javac [Link] Note: [Link] uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\jdk1.6.0_26\bin>java wrapperdemo ENTER THE PRINCIPAL AMOUNT 1000 ENTER THE INTEREST RATE 2 ENTER THE NUMBER OF YEARS 1 FINAL VALUE IS:1020.0 E:\jdk1.6.0_26\bin>java wrapperdemo ENTER THE PRINCIPAL AMOUNT 1000 ENTER THE INTEREST RATE 2 ENTER THE NUMBER OF YEARS 2 FINAL VALUE IS:1040.0

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

15

JAVA PROGRAMMING LAB


PROGRAM 10

2011-2012

Write a Java Program to implement inheritance and demonstrate use of method overriding. import [Link].*; class A { void display() { [Link]("This is from class A "); } } class B extends A { void display() { [Link]("This is from class B "); } } class AB { public static void main(String arg[]) { B obj=new B(); [Link](); } } **********************************OUTPUT***************** C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java AB This is from class B

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

16

JAVA PROGRAMMING LAB


PROGRAM 11

2011-2012

/* Write a Java Program to implement multilevel inheritance by applying various access controls to its data members and methods. */

import [Link]; class Student { private int rollno; private String name; DataInputStream dis=new DataInputStream([Link]); public void getrollno() { try { [Link]("Enter rollno "); rollno=[Link]([Link]()); [Link]("Enter name "); name=[Link](); } catch(Exception e){ } } void putrollno() { [Link]("Roll No ="+rollno); [Link]("Name ="+name); } } class Marks extends Student { protected int m1,m2,m3; void getmarks() { try { [Link]("Enter marks :"); m1=[Link]([Link]()); m2=[Link]([Link]()); m3=[Link]([Link]()); } catch(Exception e) { } } void putmarks() { [Link]("m1="+m1); [Link]("m2="+m2); [Link]("m3="+m3); }
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 17

JAVA PROGRAMMING LAB


} class Result extends Marks { private float total; void compute_display() { total=m1+m2+m3; [Link]("Total marks :" +total); } } class MultilevelDemo { public static void main(String arg[]) { Result r=new Result(); [Link](); [Link](); [Link](); [Link](); r.compute_display(); } } *********************OUTPUT***************************** C:\jdk1.6.0_26\bin>javac [Link] Note: [Link] uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\jdk1.6.0_26\bin>java MultilevelDemo Enter rollno 12345 Enter name Avinash Enter marks : 54 78 46 Roll No =12345 Name =Avinash m1=54 m2=78 m3=46 Total marks :178.0

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

18

JAVA PROGRAMMING LAB


PROGRAM 12 (A) Write a program to demonstrate use of implementing interfaces. import [Link].*; interface Area { final static float pi=3.14F; float compute(float x,float y); } class rectangle implements Area { public float compute(float x,float y) { return(pi*x*y); } } class circle implements Area { public float compute(float x,float x) { return(pi*x*x); } } class interfacedemo { public static void main(String a[]) { rectangle rect=new rectangle(); circle cir=new circle(); Area A; A=rect; [Link]("Area of rectangle="+[Link](10,20)); A=cir; [Link]("Area of circle="+[Link](30,0)); } }

2011-2012

************************OUTPUT************************************* C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java interfacedemo Area of rectangle=628.0 Area of circle=2,827.43

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

19

JAVA PROGRAMMING LAB


PROGRAM 12 (B) Write a program to demonstrate use of extending interfaces. import [Link].*; interface Area { final static float pi=3.14F; double compute(double x,double y); } interface display extends Area { void display_result(double result); } class rectangle implements display { public double compute(double x,double y) { return(pi*x*y); } public void display_result(double result) { [Link]("The Area is :" +result); } } class InterfaceExtendsDemo { public static void main(String a[]) { rectangle rect=new rectangle(); double result=[Link](10.2,12.3); rect.display_result(result); } }

2011-2012

************************************OUTPUT************************

C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java InterfaceExtendsDemo The Area is : 393.9444131612778

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

20

JAVA PROGRAMMING LAB


PROGRAM 13

2011-2012

Write a Java program to implement the concept of importing classes from user defined package and creating packages. /*Source code of package p1 under the directory C:\jdk1.6.0_26\bin>p1\edit [Link] */ package p1; public class Student { int regno; String name; public void getdata(int r,String s) { regno=r; name=s; } public void putdata() { [Link]("regno = " +regno); [Link]("name = " + name); } } /* Source code of the main function under C:\jdk1.6.0_26\bin>edit [Link] */ import p1.*; class StudentTest { public static void main(String arg[]) { student s=new student(); [Link](123,"xyz"); [Link](); } } *********************************OUTPUT******************************** C:\jdk1.6.0_26\bin>javac p1\[Link] C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>java StudentTest regno = 123 name = xyz

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

21

JAVA PROGRAMMING LAB


PROGRAM 14 (A)

2011-2012

Write a program to implement the concept of threading by extending Thread Class import [Link]; class A extends Thread { public void run() { [Link]("thread A is sterted:"); for(int i=1;i<=5;i++) { [Link]("\t from thread A:i="+i); } [Link]("exit from thread A:"); } } class B extends Thread { public void run() { [Link]("thread B is sterted:"); for(int j=1;j<=5;j++) { [Link]("\t from thread B:j="+j); } [Link]("exit from thread B:"); } } class C extends Thread { public void run() { [Link]("thread C is sterted:"); for(int k=1;k<=5;k++) { [Link]("\t from thread C:k="+k); } [Link]("exit from thread C:"); } } class Threadtest { public static void main(String arg[]) { new A().start(); new B().start();
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 22

JAVA PROGRAMMING LAB


new C().start(); } }

2011-2012

*********************************OUTPUT********************************** thread A is sterted: thread B is sterted: thread C is sterted: from thread A:i=1 from thread B:j=1 from thread C:k=1 from thread A:i=2 from thread B:j=2 from thread C:k=2 from thread A:i=3 from thread B:j=3 from thread C:k=3 from thread A:i=4 from thread B:j=4 from thread C:k=4 from thread A:i=5 from thread B:j=5 from thread C:k=5 exit from thread A: exit from thread B: exit from thread C:

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

23

JAVA PROGRAMMING LAB


PROGRAM 14 (B)

2011-2012

Write a program to implement the concept of threading by implementing Runnable Interface import [Link]; class X implements Runnable { public void run() { for(int i=1;i<10;i++) { [Link]("\t Thread X:"+i); } [Link]("End of Thread X"); } } class Runnabletest { public static void main(String arg[]) { X R=new X(); Thread T=new Thread(R); [Link](); } }

*********************************OUTPUT********************************** Thread X:1 Thread X:2 Thread X:3 Thread X:4 Thread X:5 Thread X:6 Thread X:7 Thread X:8 Thread X:9 End of Thread X

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

24

JAVA PROGRAMMING LAB


PROGRAM 15 (A)

2011-2012

Write a program to implement the concept of Exception Handling using predefined exception.

import [Link].*; class Exception_handle { public static void main(String argv[]) { int a=10,b=5,c=5,x,y; try { x=a/(b-c); } catch(ArithmeticException e) { [Link]("DIVISION BY ZERO"); } y=a/(b+c); [Link]("y="+y); } }

********************************OUTPUT*********************************** DIVISION BY ZERO y=1

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

25

JAVA PROGRAMMING LAB


PROGRAM 15 (B)

2011-2012

Write a program to implement the concept of Exception Handling by creating user defined exceptions. import [Link]; import [Link].*; import [Link]; import [Link]; class MyException extends Exception { MyException(String message) { super(message); } } class userdef { public static void main(String a[]) { int age; DataInputStream ds=new DataInputStream([Link]); try { [Link]("Enter the age (above 15 abd below 25) :"); age=[Link]([Link]()); if(age<15 || age> 25) { throw new MyException("Number not in range"); } [Link](" the number is :" +age); } catch(MyException e) { [Link]("Caught MyException"); [Link]([Link]()); } catch(Exception e){ [Link](e); } } }

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

26

JAVA PROGRAMMING LAB

2011-2012

*****************************OUTPUT 1******************************** c:\jdk1.6.0_26\bin>java userdef Enter the age (above 15 abd below 25) : 6 Caught MyException Number not in range *****************************OUTPUT 2******************************** c:\jdk1.6.0_26\bin>java userdef Enter the age (above 15 abd below 25) : 20 the number is :20

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

27

JAVA PROGRAMMING LAB


PROGRAM 16 (A) Write a program using Applet to display a message in the Applet. import [Link].*; import [Link]; /* <applet code="[Link]" width=300 height=300> </applet> */ public class Appletdemo extends Applet { public void paint(Graphics g) { String msg="HELLO!, Welcome to my applet "; [Link](msg,80,150); } }

2011-2012

*******************************OUTPUT************************ C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>appletviewer [Link]

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

28

JAVA PROGRAMMING LAB


PROGRAM 16 (B)

2011-2012

/*Write a program using Applet For configuring Applets by passing parameters.*/ import [Link].*; import [Link]; /* <applet code="[Link]" width=300 height=100> <param name=place value="Gulbarga"> <param name=college value="Govt Polytechnic"> </applet> */ public class AppletParamDemo extends Applet { String p,c; public void init() { p=getParameter("place"); c=getParameter("college"); } public void paint(Graphics g) { [Link](c,80,20); [Link](p,100,40); } } ********************************OUTPUT********************************* C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>appletviewer [Link]

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

29

JAVA PROGRAMMING LAB


PROGRAM 17 (A) Write a Java Program to demonstrate Keyboard event import [Link].*; import [Link].*; import [Link].*; /* <applet code="[Link]" width=300 height=200> </applet> */ public class KeyEvents extends Applet implements KeyListener { String msg =" "; int x=10,y=20; public void init() { addKeyListener(this); requestFocus(); } public void keyPressed(KeyEvent k) { showStatus("key down"); } public void keyReleased(KeyEvent k) { showStatus("key up"); } public void keyTyped(KeyEvent k) { msg +=[Link](); repaint(); } public void paint(Graphics g) { [Link](msg,x,y); } }

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

30

JAVA PROGRAMMING LAB

2011-2012

*******************************OUTPUT********************************* C:\jdk1.6.0_26\bin>javac [Link] C:\jdk1.6.0_26\bin>appletviewer [Link]

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

31

JAVA PROGRAMMING LAB


PROGRAM 17 (B) Write a Java Program to demonstrate Mouse events import [Link].*; import [Link].*; import [Link].*; /* <applet code="[Link]" width=300 height=200> </applet> */

2011-2012

public class MouseEvents extends Applet implements MouseListener,MouseMotionListener { String msg =" "; int x=0,y=0; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent m) { x=10; y=10; msg ="mouse clicked"; repaint(); } public void mouseEntered(MouseEvent m) { x=10; y=10; msg ="mouse Entered"; repaint(); } public void mouseExited(MouseEvent m) { x=10; y=10; msg ="mouse Exited"; repaint(); } public void mousePressed(MouseEvent m) { x=[Link](); y=[Link](); msg ="Down"; repaint();
Dept. of CS & IS, Govt. Polytechnic, Gulbarga 32

JAVA PROGRAMMING LAB


} public void mouseReleased(MouseEvent m) { x=[Link](); y=[Link](); msg ="Up"; repaint(); } public void mouseDragged(MouseEvent m) { x=[Link](); y=[Link](); msg ="*"; showStatus("Draggeg mouse at " +x+ " & "+y); repaint(); } public void mouseMoved(MouseEvent m) { showStatus("Moving mouse at " +[Link]()+ " & "+[Link]()); } public void paint(Graphics g) { [Link](msg,x,y); } }

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

33

JAVA PROGRAMMING LAB


OUTPUT

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

34

JAVA PROGRAMMING LAB


PROGRAM 18 Write programs for using Graphics class - to display basic shapes and fill them - draw different items using basic shapes - set background and foreground colors. import [Link].*; import [Link].*; /* <applet code=[Link] width=800 height-800> </applet>*/ public class Shapes extends Applet { public void paint(Graphics g) { setForefround([Link]); setBackGround([Link]); //drawing squares [Link](10,10,100,10); [Link](10,10,10,10); [Link](10,100,100,100); [Link](100,100,100,10); // Drawing triangle [Link](10,120,100,120); [Link](10,120,50,200); [Link](50,200,100,120); //drawing Rectangle [Link](120,10,220,120); [Link](120,120,220,120); //drawing ellipse and circle [Link](10,220,100,220); [Link]([Link]); [Link](120,250,250,250); //draw a filled arc [Link](350,50,400,100,0,90); //draw a polygon int x[]={400,500,400,500); int y[]={240,240,340,340}; [Link](x,y,4); } }

2011-2012

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

35

JAVA PROGRAMMING LAB

2011-2012

OUTPUT

Dept. of CS & IS, Govt. Polytechnic, Gulbarga

36

You might also like