Java File
Java File
Submitted To : Submitted by :
1
CSX-331 17103081
Lab 1
Follow the below steps to install java jdk and jre, and to configure the path settings of
java compiler.
1. Download the desired jdk release from the official Oracle website.
2. Follow the installation steps given in the GUI java installer.
3. After finishing installation, type the command java –version in the cmd, it will return
a valid result if the installation is successful, otherwise not.
4. Now, right click on the This PC icon, open properties, go to advanced system settings.
5. There, edit the PATH environment variable and add the path of javac, which can be
copied from the installation directory, which is in Program Files.
Click apply and ok , and we are done with the installation and configuration of path.
Now , we can successfully compile and execute java programs in the system.
2
CSX-331 17103081
3
CSX-331 17103081
4
CSX-331 17103081
5
CSX-331 17103081
Program 2: Write an application that ask the user to enter 2 integers, obtain them from
user and displays a large number followed by the work “is larger”. If the number are
equal, print the message “ These numbers are equal”.
import java.util.*
6
CSX-331 17103081
if(firstNo>secondNo)
System.out.print(firstNo +" is greater");
else if(secondNo>firstNo)
System.out.print(secondNo+ " is larger");
else
System.out.print("Both numbers are equal");
}
}
Output:
7
CSX-331 17103081
Program 3: Write an application in which user passes Command Line arguments and
the output shows the count of the arguments passed on command line and also print
them all.
import java.util.*;
class StringArgsDemo{
public static void main(String args[]){
System.out.println("No of command line arguments passed is "+args.length);
System.out.println("Following arguments were passed");
for(String arg:args){
System.out.println(arg);
}
}
}
8
CSX-331 17103081
9
CSX-331 17103081
14641
.......
.......
import java.util.*;
for(j=0;j<n-i;j++){
System.out.print(' ');
}
for(j=0;j<=i;j++){
value = fact.factorial(i)/(fact.factorial(j)*fact.factorial(i-j));
System.out.print(value+" ");
}
System.out.print("\n");
}
}
}
class Factorial{
10
CSX-331 17103081
1. ******* 2. *** 3. * 4. *
* * * * *** * *
* * * * ***** * *
* * * * * * *
* * * * * * *
* * * * * * *
******* *** * *
import java.util.*;
import java.lang.Math;
11
CSX-331 17103081
shape= scanner.nextInt();
switch(shape){
case 1:
Square square= new Square();
System.out.print("Enter length of side of square: ");
s= scanner.nextInt();
square.printSquare(s);
break;
case 2:
Diamond diamond= new Diamond();
System.out.print("Enter length of side of diamond: ");
s= scanner.nextInt();
diamond.printDiamond(s);
break;
case 3:
Arrow arrow= new Arrow();
System.out.print("Enter size of arrow: ");
s= scanner.nextInt();
arrow.printArrow(s);
break;
12
CSX-331 17103081
case 4:
Circle circle= new Circle();
System.out.print("Enter radius of circle: ");
s= scanner.nextInt();
circle.printCircle(s);
break;
default:
System.out.print("Invalid choice!");
}
}
}
}
class Square{
public void printSquare(int n){
int i,j;
for(i=0;i<n;i++){
if(i==0||i==n-1)
for(j=0;j<n;j++){
System.out.print('*');
}
else{
for(j=0;j<n;j++){
if(j==0||j==n-1)
System.out.print('*');
13
CSX-331 17103081
else
System.out.print(' ');
}
}
System.out.print('\n');
}
}
}
class Diamond{
public void printDiamond(int n){
if(n%2==0){
System.out.println("Please enter an odd number");
return;
}
int mid=n/2;
int i,j;
int cnt=mid;
int cnt2=0;
for(i=0;i<n;i++){
if(i<=mid){
for(j=0;j<n;j++){
if(j==cnt||j==n-1-cnt)
System.out.print('*');
else
System.out.print(' ');
}
cnt--;
}
else{
14
CSX-331 17103081
cnt2++;;
for(j=0;j<n;j++){
if(j==cnt2||j==n-1-cnt2)
System.out.print('*');
else
System.out.print(' ');
}
System.out.print('\n');
}
}
}
class Arrow{
public void printArrow(int n){
int mid=n/2;
int i,j;
for(i=0;i<n;i++){
if(i<mid){
for(j=0;j<mid-i-1;j++){
System.out.print(" ");
}
for(j=0;j<2*i+1;j++){
System.out.print("*");
}
}else{
for(j=0;j<n-2;j++){
if(j==mid-1)
15
CSX-331 17103081
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
class Circle{
public void printCircle(int r){
double dist;
int i,j;
for(i=0;i<=2*r;i++){
for(j=0;j<=2*r;j++){
dist= Math.sqrt((i-r)*(i-r)+(j-r)*(j-r)); //distance of point from
center
if(dist>r-0.5 && dist<r+0.5)
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
}
16
CSX-331 17103081
17
CSX-331 17103081
18
CSX-331 17103081
Lab 2
Program 1: Write an application to that asks the user to enter three integer values(2 <
n < 100) , tand then displays that these values are Pythagorean triplets or not. The
application should try all the combinations of the values.
import java.util.*;
public class Pythagorean {
if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){
System.out.println("Yes it's a pythagorean triplet! ");
}else{
System.out.println("No it's not a pythagorean triplet ");
}
}
}
}
19
CSX-331 17103081
Program 2. Write an application that inputs from the user the radius of the circle as a
floating point and prints the diameter, circumference and area. Use the following
formulas :
Diameter = 2r
Circumferece = 2 * PI * r
Area = PI * r2
Take PI = 3.14159, should be declared as final
import java.util.*;
20
CSX-331 17103081
import java.util.*;
21
CSX-331 17103081
case 1:
if(emps.size()==0){
System.out.println("No employees yet");
break;
}
System.out.println("Enter registration number of employee: ");
int reg= sc.nextInt();
for(i=0;i<emps.size();i++){
if(emps.get(i).getRegNo()==reg)
emps.get(i).getData();
}
break;
case 2:
System.out.print("Enter name, registration no and salary: ");
String name = sc.next();
regNo= sc.nextInt();
int salary =sc.nextInt();
Employee e=new Employee();
e.setData(name,regNo,salary);
emps.add(e);
break;
case 3:
System.out.print("Enter registration no of employee: ");
regNo= sc.nextInt();
Employee e1;
for(i=0;i<emps.size();i++){
if(emps.get(i).getRegNo()==regNo){
{
emps.get(i).getData();
break;
22
CSX-331 17103081
}
}
}
if(i==emps.size()){
System.out.println("Employee with given id doesn't exist");
}
break;
case 4:
for(i=0;i<emps.size();i++){
emps.get(i).getData();
}
break;
default:
System.out.println("Please enter a valid option!");
}
23
CSX-331 17103081
24
CSX-331 17103081
25
CSX-331 17103081
Program 4: Write an application to determine the gross pay for 3 employees. The
company pays strainght for first 40 hours and half after that for overtime. Salary per
hour and number of hours of each employee should be input by user.
class GrossPay{
26
CSX-331 17103081
}
}
Employee class
import java.util.*;
class Emp {
Emp(int n, int s) {
numberOfHours = n;
salaryPerHour = s;
}
int extraHours() {
27
CSX-331 17103081
int getSalary() {
return ((numberOfHours * salaryPerHour) + (extraHours() *
(salaryPerHour/2)));
}
}
Lab 3
Program 1: Write a program to find an element taken as the input by the user in a 2D
array.
import java.util.*;
28
CSX-331 17103081
29
CSX-331 17103081
Output
Program 2: 2. Write a program to implement all the functions of the String class.
import java.util.*;
30
CSX-331 17103081
do {
switch(option) {
case 1:
System.out.println(str.toLowerCase());
break;
case 2:
System.out.println(str.toUpperCase());
31
CSX-331 17103081
break;
case 3:
System.out.println("Enter index ");
int x = in.nextInt();
System.out.println("Enter new character ");
char ch = in.next().charAt(0);
String replace = str.substring(0, x) + ch + str.substring(x+1);
System.out.println(replace);
break;
case 4:
System.out.println("Enter another string: ");
String string = in.next();
if(str.equals(string))
System.out.println("Strings are equal");
else
System.out.println("Strings are not equal");
break;
case 5:
System.out.println("The length of string is : " + str.length());
break;
case 6:
System.out.println("Enter the index position of the character(indexing starts from 0):
");
int index = in.nextInt();
System.out.println(str.charAt(index));
break;
32
CSX-331 17103081
case 7:
System.out.println("Enter the other string to concatenate: ");
string = in.next();
String string3 = str.concat(string);
System.out.println("The final string is: "+ string3);
break;
case 8:
System.out.println("Enter the string to check as a substring: ");
String substring = in.next();
System.out.println("The given string is " );
if(str.contains(substring)) {
System.out.println("contains the substring.");
}
else {
System.out.println("doesn't contain the substring. ");
}
break;
}
}while(option != 0);
}
33
CSX-331 17103081
34
CSX-331 17103081
Program 3: A string is a double string if it has even length and half of the string is
equal to the next half of the string. Write a program to check if the given string is a
double string.
import java.util.*;
35
CSX-331 17103081
Output
Program 4: Write a program in which you have an array of integers. Implement copy
paste which allows you to copy any continuous subsequences of array and paste it int
any position of the array.
import java.util.*;
class CopyAndPaste{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array: ");
int size=sc.nextInt();
int[] arr= new int[size];
int i,j,length;
System.out.println("Enter elements of array: ");
36
CSX-331 17103081
for(i=0;i<size;i++){
arr[i]=sc.nextInt();
}
int sCopy,eCopy,sPaste;
int choice=1;
while(choice==1){
System.out.println("Enter starting and ending index for copying");
sCopy=sc.nextInt();
eCopy=sc.nextInt();
if(sCopy<0 || eCopy>=size){
System.out.println("Error: Indices out of bound ");
}else{
length=eCopy-sCopy+1;
int[] copiedArray= new int[length];
for(i=sCopy,j=0;i<=eCopy;i++,j++){
copiedArray[j]=arr[i];
}
37
CSX-331 17103081
}
}
}
Output
38
CSX-331 17103081
Lab 4
1. Create a package package1 which contains three classes, one is super class and others
are subclasses. Make some of the components private, some of them protected and some
public, show access control using the above description. Then, create another package
p2 in which the class can access public components of package package1. Now import
the classes of package package1 and package2 for the class of the main() method.
package package1;
public int variable1 = 1234; // accessible to other classes and other packages also
protected String variable2 = " Java is Beautiful"; // only accessible to the base
classes of class Problem1
void CallMe() {
System.out.println("Calling a function of the problem1 class\n");
System.out.println("We have called you. And you answered, so nice of you! I
think you are default or public(may be protected if I am your child:)");
}
// this function acts as public within this package and private outside.
}
package package1;
39
CSX-331 17103081
package package1;
package p2;
import package1.*;
class anotherClass {
void CallingProblem1() {
System.out.println("\nThis is the public member of the problem1 class of
package1 : " + package1.problem1.variable4);
}
}
import package1.*;
40
CSX-331 17103081
class checkingPackage {
package package1;
import java.util.*;
class mainClass {
Program 2: Create a class student which takes the roll number of the students, another
class Test extends Student and add up marks of two tests. Now an interface Sports
declares the sports marks and has method putMarks. Implement multiple inheritance
in Class Result using above classes and interface.
41
CSX-331 17103081
Student.java
Scanner sc= new Scanner(System.in);
Test.java
public class Test extends Student{
public int marks1, marks2;
42
CSX-331 17103081
Sports.java
interface Sports{
int sportsMarks=50;
void getSportsMarks();
}
Result.java
public class Result extends Test implements Sports{
43
CSX-331 17103081
Program 3: Write an application which takes a 1D array of the numbers 0<=n<=9 only
as input and displays the number of times n repeated most consecutively. For example :
Input : 1 2 1 1 1 5 5 2 2 3 3 5 5 5 5 4 6
Output : 4
import java.util.*;
public class Problem3 {
int count=0;
int max=0;
int number=arr[0];
for(int i=0;i<n-1;i++){
if(arr[i]==arr[i+1]){
count++;
if(count>max){
max=count;
44
CSX-331 17103081
number=arr[i];
}
}
}
Program 4: Write an application to store all the addition combination of the two dices
in 2D array and display as follows :
1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
45
CSX-331 17103081
6 7 8 9 10 11 12
import java.util.*;
if(i+j==0){
System.out.print(" ");
}
else{
System.out.print(arr[i][j]+ " ");
}
}
System.out.println('\n');
}
}
46
CSX-331 17103081
47
CSX-331 17103081
Lab 5
Program 1: Write an application to show access protection in user defined packages.
Create a package p1 which has three classes : Protection, Derived and SamePackage.
First class contains variables of all the access types. Derived is subclass of Protection but
SamePackage is not. Other package p2 consisting of two class : one deriving
p1.Protection and other not. Import the package considering all the cases of access
protection.
package p1;
package p1;
package p1;
48
CSX-331 17103081
package p2;
import p1.*;
package p2;
import p1.Protection;
import p1.Derived;
import p1.SamePackage;
49
CSX-331 17103081
import p2.first;
import p2.second;
class Problem1 {
50
CSX-331 17103081
Problem 2: Multiple Inheritance in java is when a class implements more than one
interface. Write an application showing Multiple Inheritance and Dynamic
Polymorphism (Method Overriding) using Interface.
51
CSX-331 17103081
myObject.show();
}
}
52
CSX-331 17103081
Problem 3: Define a package named area which contains classes : Square, Rectangle,
Circle, Elipse and Triangle. Write an application in which the main class, FindArea
outside the package area imports the package area and calculate the area of different
shapes. Output of your application should be menu driven.
import area.*;
import java.util.*;
switch(option){
case 1:
System.out.println("Enter side of square");
int side= sc.nextInt();
Square square=new Square(side);
System.out.println("Area: "+ square.squareArea());
break;
case 2:
System.out.println("Enter radius of circle ");
53
CSX-331 17103081
int r= sc.nextInt();
Circle circle= new Circle(r);
System.out.println("Area: "+circle.circleArea());
break;
case 3:
System.out.println("Enter sides of rectangle: ");
int a=sc.nextInt();
int b=sc.nextInt();
case 4:
System.out.println("Enter length of major and minor axes of ellipse: ");
int mj=sc.nextInt();
int mn=sc.nextInt();
mj=mj/2;
mn=mn/2;
Ellipse e= new Ellipse(mj,mn);
System.out.println("Area: "+e.ellipseArea());
break;
default:
System.out.println("Enter a valid option! ");
}
System.out.println("Press 1 to continue, 0 to exit ");
choice=sc.nextInt();
54
CSX-331 17103081
}
}
package area;
package area;
55
CSX-331 17103081
package area;
package area;
56
CSX-331 17103081
return a*a;
}
}
57
CSX-331 17103081
import java.util.Scanner;
import vol.*;
switch(option){
case 1:
System.out.println("Enter radius: ");
int r= sc.nextInt();
Sphere sphere=new Sphere(r);
58
CSX-331 17103081
case 2:
System.out.println("Enter radius and height of cylinder ");
int rs= sc.nextInt();
int ht= sc.nextInt();
Cylinder c= new Cylinder(rs,ht);
System.out.println("Volume of cylinder is "+ c.volume());
break;
case 3:
System.out.println("Enter side of cube: ");
int a=sc.nextInt();
Cube cube= new Cube(a);
System.out.println("Volume of cube is "+cube.volume());
break;
case 4:
System.out.println("Enter radius and height of cone ");
int ra=sc.nextInt();
int he=sc.nextInt();
Cone cone = new Cone(ra,he);
System.out.println("Volume of cone is "+ cone.volume());
break;
default:
System.out.println("Enter a valid option! ");
59
CSX-331 17103081
}
System.out.println("Press 1 to continue, 0 to exit ");
choice=sc.nextInt();
}
}
}
package vol;
package vol;
60
CSX-331 17103081
double v= (4*pi*r*r*r)/3;
return v;
}
}
package vol;
package vol;
61
CSX-331 17103081
}
public double volume(){
return a*a*a;
}
}
package vol;
62
CSX-331 17103081
63
CSX-331 17103081
Lab 6
import java.util.*;
import package1.ExceptionA;
import package1.ExceptionB;
import package1.ExceptionC;
class Problem1 {
int a;
Scanner in = new Scanner(System.in);
int choice=1;
while(choice==1){
System.out.println("Enter a number: ");
a = in.nextInt();
try {
if(a == 1) {
throw new ExceptionA(a);
64
CSX-331 17103081
}
if(a == 2) {
throw new ExceptionB(a);
}
if(a == 3) {
throw new ExceptionC(a);
}
}
catch(ExceptionA eA) {
System.out.println("Caught in A");
}
}
}
package package1;
public ExceptionA(int a) {
detailA = a;
}
65
CSX-331 17103081
}
}
package package1;
public ExceptionB(int a) {
super(a);
detailB = a;
}
package package1;
public ExceptionC(int a) {
super(a);
detailC = a;
66
CSX-331 17103081
Problem 2: Write a program that demonstrates how various exceptions are caught with
catch(Exception exception).
This time, define classes ExceptionA (which inherits from class Exception) and
ExceptionB (which inherits from class ExceptionA). In your program, create try blocks
that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and
IOException. All exceptions should be caught with catch blocks specifying type
Exception.
import java.io.IOException;
import java.util.*;
import package1.ExceptionA;
import package1.ExceptionB;
import package1.ExceptionC;
67
CSX-331 17103081
class Problem2 {
int a;
Scanner in = new Scanner(System.in);
int choice=1;
while(choice==1){
System.out.println("Enter a number: ");
a = in.nextInt();
try {
if(a == 1) {
throw new ExceptionA(a);
}
else if(a == 2) {
throw new ExceptionB(a);
}
else if(a == 3) {
throw new IOException("IO Exception");
}
else{
throw new NullPointerException("Null pointer");
}
}
catch(Exception e) {
System.out.println("Caught by Exception, " + e);
68
CSX-331 17103081
}
}
Program 3: Write a program that shows that the order of catch blocks is important. If
you try a catch a superclass exception type before a subclass type, the compiler should
generate errors.
import java.util.*;
import package1.ExceptionA;
import package1.ExceptionB;
import package1.ExceptionC;
69
CSX-331 17103081
class Problem3 {
int a;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
a = in.nextInt();
try {
if(a == 1) {
throw new ExceptionA(a);
}
if(a == 3) {
throw new ExceptionB(a);
}
if(a == 5) {
throw new ExceptionC(a);
}
} catch(ExceptionA eC) {
System.out.println("Caught " + eC);
}
70
CSX-331 17103081
catch(ExceptionB eB) {
System.out.println("Caught " + eB);
catch(ExceptionC eA) {
System.out.println("Caught " + eA);
}
}
}
import java.util.*;
class Problem4 {
static void someMethod2(int m, int n) throws ArithmeticException {
int a;
try {
71
CSX-331 17103081
a = (m-n)/(m+n);
}
finally {}
}
try {
someMethod(1, -1);
} catch(ArithmeticException mE) {
System.out.println("\nException raised by : " + mE);
System.out.println("\nStack trace is shown below : \n");
mE.printStackTrace();
}
}
}
72
CSX-331 17103081
import java.util.*;
import package5.DatePassedException;
import package5.InvalidDatesException;
class Problem5 {
73
CSX-331 17103081
year = input.nextInt();
month = input.nextInt();
date = input.nextInt();
try {
if(differenceDays < 0) {
throw new InvalidDatesException();
}
System.out.println("You can keep the book for " + (15 - differenceDays) + " more
days.\n");
}
catch(InvalidDatesException eID) {
System.out.println(eID);
}
catch(DatePassedException eDP) {
74
CSX-331 17103081
eDP.generateAndDisplayFine();
}
}
}
package package5;
long differenceDays;
int finePerDay = 50;
int duration = 15;
public DatePassedException(long differenceDays) {
this.differenceDays = differenceDays;
}
package package5;
75
CSX-331 17103081
}
}
76
CSX-331 17103081
Lab 7
Program 1: Write an application of multithreading. Create three different
classes(threads) that inherit Thread class. Each class consists of a for loop that prints
identity of the class with a number series in increasing order. Start all three threads
together. Now run the application 2 or 3 timese and show the output.
77
CSX-331 17103081
import java.util.*;
78
CSX-331 17103081
NewThread(String tname){
name=tname;
t= new Thread(this,name);
System.out.println(t.getName());
t.start();
}
public void run(){
try {
for(int i=0;i<5;i++) {
System.out.println(t.getName() +" :" + i);
Thread.sleep(1000);
}
}catch(InterruptedException e) {
System.out.println(t.getName() + " interrupted");
}
System.out.println(name +" finished");
}
79
CSX-331 17103081
try {
Thread.sleep(10000);
}catch(Exception e) {
System.out.println("Main thread interrupted");
}
System.out.println("Main thread finished");
}
}
Output
80
CSX-331 17103081
81
CSX-331 17103081
try {
Thread.sleep(10000);
}catch(InterruptedException e) {
System.out.println("Main thread interrupted");
}
minP.stop();
maxP.stop();
normP.stop();
Program 4. Inherit a class from Thread and override the run() method. Inside run(),
print a message, and then call sleep(). Repeat this three times, then return from run().
82
CSX-331 17103081
Put a start-up message in the constructor and override finalize() to print a shut down
message. Make a separate thread class that calls System.gc() and
System.runFinalization() inside run(), printing a message as it does so. Make several
thread objects of both types and run them to see what happens.
Program 5. Create two Thread subclasses, one with a run() that starts up, captures the
reference of the second Thread object and then calls wait(). The other class’ run()
should call notifyAll() for the first thread after some number of seconds have passed, so
the first thread can print a message.
Program 6. There are two processes, a producer and a consumer, that share a common
buffer with a limited size. The producer “produces” data and stores it in the buffer, and
the consumer “consumes” the data, removing it from the buffer. Having two processes
that run in parallel, we need to make sure that the producer won’t put new data in the
buffer when the buffer is full nad the consumer won’t try to remove data from the
buffer if the buffer is empty.
83
CSX-331 17103081
Lab 8
package appletdemo;
import java.awt.*;
import java.applet.*;
}
public void stop(){
}
public void destroy(){
}
public void paint(Graphics g){
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
84
CSX-331 17103081
g.drawString(msg,10,30);
}
****
****
****
****
package appletdemo;
import java.applet.*;
import java.awt.*;
85
CSX-331 17103081
}
public void stop(){
}
public void destroy(){
}
public void paint(Graphics g){
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
g.drawString(msg1, 10, 20);
g.drawString(msg2, 10, 40);
g.drawString(msg1, 10, 60);
g.drawString(msg2, 10, 80);
}
}
86
CSX-331 17103081
Program 3: Write an applet program that asks the user to enter two floaing point
numbers obtains the two numbers from the user and draws their sum, product,
difference and division.
Note : Use PARAM tag for user input.
package appletdemo;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
87
CSX-331 17103081
int n1,n2,ans;
t2.addActionListener(this);
add(new Label("First number")); add(t1);
add(new Label("Second number")); add(t2);
add(new Label("Sum :")); add(plus);
add(new Label("Difference :")); add(minus);
add(new Label("Product :")); add(mult);
add(new Label("Difference :")); add(divide);
88
CSX-331 17103081
plus.setText(""+sum);
minus.setText(""+ diff);
mult.setText(""+prod);
divide.setText(""+div);
Program 4: Write an applet in which background is Red and foreground (text color) is
Blue. Also show the message “Background is Red and Foreground is Blue” in the status
window.
package appletdemo;
89
CSX-331 17103081
import java.awt.*;
import java.applet.*;
}
public void stop(){
}
public void destroy(){
}
public void paint(Graphics g){
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
g.drawString(msg,10,30);
}
90
CSX-331 17103081
Program 5: Write an applet program showing URL of code base and document vase in
the applet window. NOTE : Use getCodeBase() and getDocumentBase().
package appletdemo;
import java.applet.Applet;
import java.awt.*;
import java.net.*;
91
CSX-331 17103081
url= getDocumentBase();
msg= "Document Base: "+url.toString();
g.drawString(msg, 10, 60);
92
CSX-331 17103081
Lab 9
Program 1 : Write an applet to print the message of click, enter , exit, press, and release
when respective mouse event happens in the applet and print dragged and moved when
respective mouse motion event happens in the applet.
Note: Implement MouseListener and MouseMotionListener
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
93
CSX-331 17103081
94
CSX-331 17103081
}
}
Program 2 : Write an applet to print the message of click, enter , exit, press, and release
when respective mouse event happens in the applet and print dragged and moved when
respective mouse motion event happens in the applet.
Note: Implement MouseAdapter and MouseMotionAdapter
package myAdapters;
import java.applet.*;
95
CSX-331 17103081
package myAdapters;
import java.awt.event.*;
96
CSX-331 17103081
package myAdapters;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
97
CSX-331 17103081
98
CSX-331 17103081
Program 4: Write an applet printing the message extending KeyAdapter class in Inner
class and Anonymous inner class. Also show the status of key press and release in status
window.
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
99
CSX-331 17103081
}
});
}
public void paint(Graphics g){
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
g.drawString(msg,10,40);
}
}
import java.applet.*;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import myAdapters.*;
100
CSX-331 17103081
showStatus("Key released");
}
public void keyTyped(KeyEvent ke){
msg+= ke.getKeyChar();
repaint();
}
}
101
CSX-331 17103081
Problem 5: Write an applet demonstrating some virtual key codes i.e. Function keys.
Page Up and Page Down and arrow keys. To demonstrate only print the message of the
pressed. Also show the status of key press and release.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
102
CSX-331 17103081
case KeyEvent.VK_F2:
msg += " F2 ";
break;
case KeyEvent.VK_F3:
msg += " F3 ";
break;
case KeyEvent.VK_PAGE_DOWN:
msg += " PageDown ";
break;
case KeyEvent.VK_PAGE_UP:
msg += " PageUp";
break;
case KeyEvent.VK_LEFT:
msg += " LeftArrow ";
break;
case KeyEvent.VK_RIGHT:
msg += " RightArrow";
break;
}
repaint();
}
public void keyReleased(KeyEvent ke) {
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
public void paint(Graphics g) {
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
103
CSX-331 17103081
g.drawString(msg,10,40);
}
}
104