0% found this document useful (0 votes)
47 views46 pages

Java P

This document contains a laboratory certificate and practical record book for a Java programming practical course. It certifies that a student satisfactorily completed Java programming experiments prescribed by their university. The record book contains 22 Java programs demonstrating various Java concepts like constructors, method overloading, inheritance, exceptions, file I/O, applets, events, GUI components, and a calculator program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
47 views46 pages

Java P

This document contains a laboratory certificate and practical record book for a Java programming practical course. It certifies that a student satisfactorily completed Java programming experiments prescribed by their university. The record book contains 22 Java programs demonstrating various Java concepts like constructors, method overloading, inheritance, exceptions, file I/O, applets, events, GUI components, and a calculator program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 46

JAVA PROGRAMMING PRACTICAL BCA 501P

Higher and Technical Institute, Mizoram


Department of Computer sciences

Laboratory certificate

This is to certify that Mr./Miss K.LALNUNTLUANGA


has satisfactorily completed the course of experiments in the
JAVA Laboratory practical prescribed
by the Mizoram University for the Bachelor of Computer Applications
(BCA) course in the Laboratory of this Institute in the year 2020.

(R.VANLALAWMPUIA) (K.LALMUANPUIA)
Teacher in-charge Head of the Department.

BCA 5th Semester 2020 Practical Record Book


1
JAVA PROGRAMMING PRACTICAL BCA 501P

HIGHER AND TECHNICAL INSTITUTE,


MIZORAM
Department of Computer Sciences
Chanmari-III, Lunglei-796701

SUBJECT:

JAVA PRACTICAL
Course Code: 501(P)

Name : K.LALNUNTLUANGA
Roll No : 1823BCA006
Registration Number : 1800895
Semester : 5th Semester BCA
Course : Bachelor of Computer Sciences

BCA 5th Semester 2020 Practical Record Book


2
JAVA PROGRAMMING PRACTICAL BCA 501P

TABLE OF CONTENT
S.no Program Names Pages Remarks
1. Demonstrating the use of methods of Math class. 4
2. Programs to implement the methods of String class 5
Write a program to demonstrate constructors and
3. 6-7
constructors overloading
Write a program to demonstrate method overloading and
4. 8-9
overriding.
5. To demonstrate abstract class. 10-11
6. To Demonstrate interfaces 12
7. To demonstrate inheritance 13
8. To demonstrate super and this 14-15
9. To demonstrate static variables and methods 16
10. To demonstrate Exceptions 17
To demonstrate FileInputStream and FileOutput Stream
11. 18-19
Classes
To demonstrate the creation of applets and passing
12. 20-21
parameters to applets
13. To demonstrate Mouse and Keyboard events in an applet 22-27
14. To demonstrate the creation of a frame. 28
15. To demonstrate Labels and Buttons with proper events 29-30
16. To demonstrate Checkboxes with proper events 31-32
17. To demonstrate CheckBoxGroups with proper events 33-34
18. To demonstrate Lists and TextFields with proper events 35-38
19. To demonstrate ScrollBars with proper events 39-40
20. To demonstrate MenuBars and Menus. 41-42
21. To demonstrate Dialog boxes. 43
22. To demonstrate Calculator. 44-45

1. Demonstrating the use of methods of Math class.

BCA 5th Semester 2020 Practical Record Book


3
JAVA PROGRAMMING PRACTICAL BCA 501P

public class MathDemo


{
public static void main(String args[])
{
int angle = 45, x=10;
System.out.println("**** Program to display methods of Math Class**** ");
System.out.println("The sin of given angle ==> "+Math.sin(angle));
System.out.println("Demo for Power ==> "+Math.pow(x,2));
System.out.println("Demo for PI ==> "+Math.PI);
System.out.println("Demo for cos "+Math.cos(angle));
System.out.println("Demo for tan ==> "+Math.tan(angle));
System.out.println("Demo for asin ==> "+Math.asin(angle));
System.out.println("Demo for acos ==> "+Math.acos(angle));
System.out.println("Demo for atan ==> "+Math.atan(angle));
System.out.println("Demo for atan2 ==> "+Math.atan2(5,2));
System.out.println("Demo for exponent ==> "+Math.exp(x));
System.out.println("Demo for log ==> "+Math.log(x));
System.out.println("Demo for square root ==> "+Math.sqrt(36));
System.out.println("Demo for ceil of 12.5 ==> "+Math.ceil(12.5));
System.out.println("Demo for floor of 12.5 ==> "+Math.floor(12.5));
System.out.println("Demo for rint of 17.99 ==> "+Math.rint(17.99));
System.out.println("Demo for absolute of -17.99 ==>"+Math.abs(17.99));
System.out.println("Demo for maximum of 45 and 42 ==>"+Math.max(45,42));
System.out.println("Demo for minimum of 45 and 42 ==>"+Math.min(45,42));
System.out.println("Dome for random number ==> "+Math.random());
}
}

OUTPUT:
2. Programs to implement the methods of String class.

BCA 5th Semester 2020 Practical Record Book


4
JAVA PROGRAMMING PRACTICAL BCA 501P

import java.lang.*;
class StringTest
{
public static void main (String[] args) throws java.lang.Exception
{
//Java String toUpperCase() and toLowerCase() method
String s="Sachin";
String str="Java Programming";
System.out.println(s.toUpperCase()); //SACHIN
System.out.println(s.toLowerCase()); //sachin
System.out.print(s);//Sachin(no change in original)
System.out.println(s);// Sachin
System.out.println();
System.out.println(str.substring(8,9));// Sachin
System.out.println("\n");
System.out.println(s.trim());//Sachin
//Java String startsWith() and endsWith() method
System.out.println(s.startsWith("Sa"));//true
System.out.println(s.endsWith("n"));//true
System.out.println(s.length());//6
String s2=s.intern();
System.out.println(s2);//Sachin
String s1="Java is a programming language. Java is a platform. Java is an Island.";
String replaceString=s1.replace("Java","Kava");//Replace all occurance of "Java" to
"Kava"
System.out.println(replaceString);
}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


5
JAVA PROGRAMMING PRACTICAL BCA 501P

3. Write a program to demonstrate constructors and constructors overloading.

class Room
{
int length,breadth;
Room(int x, int y) //constructor method
{
length=x;
breadth=y;
}

Room(int x) //constructor overloading


{
length=breadth=x;
}

int area()
{
return(length * breadth);
}
}

class RoomArea
{
public static void main(String args[])
{
Room r1=new Room(12,15); //Calling constructor
int area1=r1.area();
System.out.println("Area1 value="+area1);
Room r2=new Room(14);
int area2=r2.area();
System.out.println("Area2 Value="+area2);

}
}

BCA 5th Semester 2020 Practical Record Book


6
JAVA PROGRAMMING PRACTICAL BCA 501P

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


7
JAVA PROGRAMMING PRACTICAL BCA 501P

4. Program to demonstrate method overloading and overriding.

class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
class BaseClass
{
public void methodToOverride() //Base class method
{
System.out.println ("I'm the method of BaseClass");
}
}
class DerivedClass extends BaseClass
{
public void methodToOverride() //Derived Class method
{
System.out.println ("I'm the method of DerivedClass");
}
}
class MethodTest
{
public static void main (String[] args)
{
//Method Overloading
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);

BCA 5th Semester 2020 Practical Record Book


8
JAVA PROGRAMMING PRACTICAL BCA 501P

result = Obj .demo(5.5);


System.out.println("O/P : " + result);
//Method Overriding
//BaseClass reference and object
BaseClass obj1 = new BaseClass();
//BaseClass reference but DerivedClass object
BaseClass obj2 = new DerivedClass();
//Calls the method from BaseClass class
obj1.methodToOverride();
//Calls the method from DerivedClass class
obj2.methodToOverride();
}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


9
JAVA PROGRAMMING PRACTICAL BCA 501P

5. Program to demonstrate abstract class.

abstract class Shape


{
double a, b;
Shape(double d1, double d2)
{
a = d1;
b = d2;
}
abstract double area();
}
class Circle extends Shape
{
Circle(double radius)
{
super(radius, 0);
}
double area()
{
return Math.PI * a * a;
}
}
class Rectangle extends Shape
{
Rectangle(double length, double breadth)
{
super(length, breadth);
}
double area()
{
return a * b;
}
}
public class AbstractTest
{
public static void main(String[] args)
{
Shape shape;
shape = new Circle(5);
System.out.println("Area of circle =" + shape.area());

BCA 5th Semester 2020 Practical Record Book


10
JAVA PROGRAMMING PRACTICAL BCA 501P

shape = new Rectangle(5, 10);


System.out.println("Area of rectangle = " + shape.area());
}
}

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


11
JAVA PROGRAMMING PRACTICAL BCA 501P

6. To Demonstrate interfaces.

interface ClassA
{
static final int num1=770;
}

class ClassB
{
int num2=770;
}
class ClassC extends ClassB implements ClassA
{
void add()
{
System.out.println(num1 + num2);
}
}
class InterfaceMultiple
{
public static void main(String args[])
{
ClassC obj1 = new ClassC();
obj1.add();
}}

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


12
JAVA PROGRAMMING PRACTICAL BCA 501P

7. To demonstrate inheritance.
class A
{
void display()
{
System.out.println("This is grandfather class");
}
}
class B extends A
{
void show()
{
System.out.println("This is father class");
}
}
class C extends B
{
void out()
{
System.out.println("This is child class");
}
}
class InheritanceMultilevel
{
public static void main( String args[])
{
C object1=new C();
object1.display();
object1.show();
object1.out();
}}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


13
JAVA PROGRAMMING PRACTICAL BCA 501P

8. To demonstrate super and this.


class View
{
int x;
View(int x)
{
this.x=x;
}

void display()
{
System.out.println("Super x="+x);
}
}

class Sub extends View


{
int y;
Sub (int x, int y)
{
super(x);
this.y=y;
}

void display()
{
System.out.println("Super x="+x);
System.out.println("Super y="+y);
}
}

class OverrideTest
{
public static void main(String args[])
{
Sub s1=new Sub(200,300);
s1.display();
}
}

BCA 5th Semester 2020 Practical Record Book


14
JAVA PROGRAMMING PRACTICAL BCA 501P

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


15
JAVA PROGRAMMING PRACTICAL BCA 501P

9. To demonstrate static variables and methods.

class UseStatic
{
static int a = 4;
static int b;
static void math(int x)
{
System.out.println("x =" +x);
System.out.println("a =" +a);
System.out.println("b =" +b);
}
static
{
System.out.println("Static block initialized");
b = a*8;
}
public static void main(String args[])
{
math(11);
}
}

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


16
JAVA PROGRAMMING PRACTICAL BCA 501P

10. To demonstrate Exceptions.

class ExceptionTest
{
public static void main (String[] args) throws java.lang.Exception
{
int a=20,b=7,c=7,x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("Division by Zero");
}
y=a/(b+c);
System.out.println("y=" +y);
}
}

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


17
JAVA PROGRAMMING PRACTICAL BCA 501P

11. To demonstrate FileInputStream and FileOutput Stream Classes.

WRITEBYTES:

import java.io.*;
class WriteBytes
{
public static void main(String args[])
{
byte cities []={'L','U','N','G','L', 'E','I','A','I','Z','A', 'W','L' };
FileOutputStream outfile = null;
try
{
outfile = new FileOutputStream("city.txt");
outfile.write (cities);
outfile.close ();
}
catch (IOException ioe)
{
System.out.println(ioe);
System.exit(-1);
}
}
}

READBYTES:

import java.io.*;
class ReadBytes
{
public static void main(String args[])
{
FileInputStream infile = null;
int b;
try
{
infile = new FileInputStream (args [0]);
while ((b = infile.read ()) != -1)
{
System.out.print ((char)b);
}
infile.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
System.exit(-1);
}
}
}

BCA 5th Semester 2020 Practical Record Book


18
JAVA PROGRAMMING PRACTICAL BCA 501P

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


19
JAVA PROGRAMMING PRACTICAL BCA 501P

12. To demonstrate the creation of applets and passing parameters to applets.

import java.awt.*;
import java.applet.*;
public class HelloJavaParam extends Applet
{
String str;
public void init()
{
str=getParameter("string");
if(str==null)
str="Java";
str="LAL" +str;
}
public void paint(Graphics g)
{
g.drawString(str, 5, 100);
}
}

HTML CODE:

<HTML>
<!Parameterized HTML file>
<HEAD>
<TITLE>Welcome to Java Applets</TITLE>
</HEAD>
<BODY>
<APPLET
CODE=HelloJavaParam.class
WIDTH=200
HEIGHT=200>
<PARAM NAME ="string" VALUE="sangthana">
</APPLET>
</BODY>
</HTML>

BCA 5th Semester 2020 Practical Record Book


20
JAVA PROGRAMMING PRACTICAL BCA 501P

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


21
JAVA PROGRAMMING PRACTICAL BCA 501P

13. To demonstrate Mouse and Keyboard events in applet.

Mouse:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Mouse" width=500 height=500>
</applet>
*/
public class Mouse extends Applet
implements MouseListener,MouseMotionListener
{
int X=0,Y=20;
String msg="MouseEvents";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.black);
setForeground(Color.red);
}
public void mouseEntered(MouseEvent m)
{
setBackground(Color.magenta);
showStatus("Mouse Entered");
repaint();
}
public void mouseExited(MouseEvent m)
{
setBackground(Color.black);
showStatus("Mouse Exited");
repaint();

BCA 5th Semester 2020 Practical Record Book


22
JAVA PROGRAMMING PRACTICAL BCA 501P

}
public void mousePressed(MouseEvent m)
{
X=10;
Y=20;
msg="NEC";
setBackground(Color.green);
repaint();
}
public void mouseReleased(MouseEvent m)
{
X=10;
Y=20;
msg="Engineering";
setBackground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent m)
{
X=m.getX();
Y=m.getY();
msg="College";
setBackground(Color.white);
showStatus("Mouse Moved");
repaint();
}
public void mouseDragged(MouseEvent m)
{
msg="CSE";
setBackground(Color.yellow);
showStatus("Mouse Moved"+m.getX()+" "+m.getY());
repaint();

BCA 5th Semester 2020 Practical Record Book


23
JAVA PROGRAMMING PRACTICAL BCA 501P

}
public void mouseClicked(MouseEvent m)
{
msg="Students";
setBackground(Color.pink);
showStatus("Mouse Clicked");
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


24
JAVA PROGRAMMING PRACTICAL BCA 501P

Keyboard:
import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="Key" width=300 height=400>

</applet>

*/

public class Key extends Applet

implements KeyListener

int X=20,Y=30;

String msg="KeyEvents--->";

public void init()

addKeyListener(this);

requestFocus();

setBackground(Color.green);

setForeground(Color.blue);

public void keyPressed(KeyEvent k)

showStatus("KeyDown");

int key=k.getKeyCode();

switch(key)

BCA 5th Semester 2020 Practical Record Book


25
JAVA PROGRAMMING PRACTICAL BCA 501P

case KeyEvent.VK_UP:

showStatus("Move to Up");

break;

case KeyEvent.VK_DOWN:

showStatus("Move to Down");

break;

case KeyEvent.VK_LEFT:

showStatus("Move to Left");

break;

case KeyEvent.VK_RIGHT:

showStatus("Move to Right");

break;

repaint();

public void keyReleased(KeyEvent k)

showStatus("Key Up");

public void keyTyped(KeyEvent k)

msg+=k.getKeyChar();

repaint();

public void paint(Graphics g)

BCA 5th Semester 2020 Practical Record Book


26
JAVA PROGRAMMING PRACTICAL BCA 501P

g.drawString(msg,X,Y);

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


27
JAVA PROGRAMMING PRACTICAL BCA 501P

14. To demonstrate the creation of a frame.

import java.awt.*;
public class AwtFrame
{
public static void main(String[] args){
Frame frm = new Frame("Java AWT Frame");
Label lbl = new Label("this is java frame",Label.CENTER);
frm.add(lbl);
frm.setSize(400,400);
frm.setVisible(true);
}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


28
JAVA PROGRAMMING PRACTICAL BCA 501P

15. To demonstrate Labels and Buttons with proper events.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AppletButtonLabel extends Applet implements ActionListener
{
public void init ()
{
button1 = new Button ("Button 1");
add (button1);
button1.addActionListener(this);
button2 = new Button ("Button 2");
add(button2);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
System.out.println("Button 1 was pressed");
else
System.out.println("Button 2 was pressed");
}
Button button1,button2;
}

HTML CODE:

<HTML>
<H1>This is Applet</H1>
<CENTER>
<BODY>
<Applet code=AppletButtonLabel.class
Width=500
Height=200>
</Applet>
</BODY>
</HTML>

BCA 5th Semester 2020 Practical Record Book


29
JAVA PROGRAMMING PRACTICAL BCA 501P

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


30
JAVA PROGRAMMING PRACTICAL BCA 501P

16. To demonstrate CheckBoxes with proper events.


import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="HandleCheckboxEvent" width=200 height=200>
</applet>
*/
public class HandleCheckboxEvent extends Applet implements ItemListener{
Checkbox java = null;
Checkbox vb = null;
Checkbox c = null;
public void init(){
//create checkboxes
java = new Checkbox("Java");
vb = new Checkbox("Visual Basic");
c = new Checkbox("C");
add(java);
add(vb);
add(c);
//add item listeners
java.addItemListener(this);
vb.addItemListener(this);
c.addItemListener(this);
}
public void paint(Graphics g){

g.drawString("Java: " + java.getState(),10,80);

BCA 5th Semester 2020 Practical Record Book


31
JAVA PROGRAMMING PRACTICAL BCA 501P

g.drawString("VB: " + vb.getState(), 10, 100);


g.drawString("C: " + c.getState(), 10, 120);
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


32
JAVA PROGRAMMING PRACTICAL BCA 501P

17. To demonstrate CheckBoxGroups with proper events.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Example extends Applet implements ItemListener
{
public void init ()
{
CheckboxGroup colorGroup = new CheckboxGroup();
Checkbox redCheckbox = new Checkbox("red", colorGroup, false);
redCheckbox.addItemListener(this);
add(redCheckbox);
Checkbox blueCheckbox = new Checkbox("blue", colorGroup, false);
blueCheckbox.addItemListener(this);
add(blueCheckbox);
Label LB1;
LB1 = new Label("Result");
add(LB1);
}
public void itemStateChanged (ItemEvent evt)
{
if (evt.getItem().equals("red"))
setBackground (Color.red);
else if (evt.getItem().equals("blue"))
setBackground (Color.blue);
repaint();
}
}

HTML CODE:

<Html>
<h1> This is Applets </h1>
<Center>
<Body>
<Applet Code = Example.class
Width = 500
Height = 200>
</Applet>
</Body>
</Center>
</Html>

BCA 5th Semester 2020 Practical Record Book


33
JAVA PROGRAMMING PRACTICAL BCA 501P

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


34
JAVA PROGRAMMING PRACTICAL BCA 501P

18. To demonstrate Lists and TextFields with proper events.

LISTS:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ExampleList extends Applet implements ActionListener
{
List sub;
public void init()
{
sub = new List(4);
sub.add("Java");
sub.add("Microprocessor");
sub.add("SPM");
sub.add("CG");
add(sub);
sub.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void paint(Graphics g)
{
String msg = "Current subject is ";
msg +=sub.getSelectedItem();
g.drawString(msg, 6, 120);
}
}

HTML CODE:

<Html>
<h1> This is Applets </h1>
<Center>
<Body>
<Applet Code = ExampleList.class
Width = 700
Height = 400>
</Applet>
</Body>
</Center>

BCA 5th Semester 2020 Practical Record Book


35
JAVA PROGRAMMING PRACTICAL BCA 501P

</Html>
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


36
JAVA PROGRAMMING PRACTICAL BCA 501P

TEXTFIELDS:

import java.applet.Applet;
import java.awt.*;
public class TextAndLabel extends Applet
{
public void init()
{
add( new TextField(30));
add (new Label("FirstName",Label.CENTER));
add(new TextField(30));
add(new Label("LastName",Label.CENTER));
}
}

HTML CODE:

<HTML>
<h1> This is Applets </h1>
<Center>
<Body>
<Applet Code = TextField.class
Width = 500
Height = 200>
</Applet>
</Body>
</Center>
</HTML>

OUTPUT:

BCA 5th Semester 2020 Practical Record Book


37
JAVA PROGRAMMING PRACTICAL BCA 501P

BCA 5th Semester 2020 Practical Record Book


38
JAVA PROGRAMMING PRACTICAL BCA 501P

19. To demonstrate ScrollBars with proper events.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ScrollEx2 implements AdjustmentListener


{
Frame jf;
Panel jp, jp2;
Label frameLabel1;

ScrollEx2()
{
jf = new JFrame("Scrollbar");

//Creating the first JPanel and adding two JLabels to it


jp = new Panel();

ImageIcon image = new ImageIcon("nature3.png");

//Creating a Label
Label panelLabel1 = new Label("Handling a Scrollbar drag event", Label.CENTER);

jp = new Panel(new BorderLayout());

//Adding the Label to NORTH of the Panel


jp.add(panelLabel1,BorderLayout.NORTH);

//Creating the horizontal Scrollbar


Scrollbar scrollBHorizontal = new Scrollbar(Scrollbar.HORIZONTAL, 10, 40, 0, 100);

//Creating the vertical Scrollbar


Scrollbar scrollBVertical = new Scrollbar(Scrollbar.VERTICAL, 10, 60, 0, 100);

//Adding the horizontal Scrollbar to SOUTH of Panel


jp.add(scrollBHorizontal,BorderLayout.SOUTH);

//Adding the vetical Scrollbar to EAST of JPanel


jp.add(scrollBVertical, BorderLayout.EAST);

//Getting the current position value of horizontal scrollbar


Integer i = scrollBHorizontal.getValue();

//Creating a JLabel and setting its value to the current position value of horizontal
scrollbar.
frameLabel1 = new Label(i.toString());

//Adding this JLabel to SOUTH of the JFrame

BCA 5th Semester 2020 Practical Record Book


39
JAVA PROGRAMMING PRACTICAL BCA 501P

jf.add(frameLabel1, BorderLayout.SOUTH);

//Adding the first JPanel to the CENTER of JFrame


jf.add(jp,BorderLayout.CENTER);

scrollBHorizontal.addAdjustmentListener(this);
scrollBVertical.addAdjustmentListener(this);

jf.setSize(350,270);
jf.setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
Integer i =e.getValue();
frameLabel1.setText(i.toString());
}

public static void main(String... ar)


{
new ScrollEx2();
}}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


40
JAVA PROGRAMMING PRACTICAL BCA 501P

20. To demonstrate MenuBars and Menus.

import java.applet.*;
import java.awt.*;
/*
<applet code="MenuDemo" height=200 width=200>
</applet>
*/
public class MenuDemo extends Applet
{
public void init( )
{
Menus m = new Menus("Menu Bar Demo");

m.resize(200,200);
m.show();
}
}
class Menus extends Frame
{
Menus(String s)
{
super(s);
MenuBar mbar=new MenuBar();
Menu mn=new Menu("File");
MenuItem mitem1=new MenuItem("Item 1");
MenuItem mitem2=new MenuItem("Item 2");
//define the submenu

Menu submenu=new Menu("Sub Menu");

MenuItem mitem3=new MenuItem("Item 3");


MenuItem mitem4=new MenuItem("Item 4");
submenu.add(mitem3);
submenu.add(mitem4);

MenuItem mitem5=new MenuItem("Exit");


//add items to the main menu
mn.add(mitem1);
mn.add(mitem2);
mn.addSeparator();
mn.add(submenu);
mn.addSeparator();
mn.add(mitem5);

BCA 5th Semester 2020 Practical Record Book


41
JAVA PROGRAMMING PRACTICAL BCA 501P

//add menu to the menu bar


mbar.add(mn);
setMenuBar(mbar);
}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


42
JAVA PROGRAMMING PRACTICAL BCA 501P

21. To demonstrate Dialog boxes.


import java.awt.*;
import java.awt.event.*;
public class DialogExample
{
private static Dialog d;
DialogExample() {

Frame f= new Frame();


d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});

d.add( new Label ("Click button to continue."));


d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


43
JAVA PROGRAMMING PRACTICAL BCA 501P

BCA 5th Semester 2020 Practical Record Book


44
JAVA PROGRAMMING PRACTICAL BCA 501P

22. To demonstrate Calculator.

import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers: \n");
// nextDouble() reads the next double from the keyboard
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
// operator doesn't match any case constant (+, -, *, /)
default:
System.out.printf("Error! operator is not correct");
return;
}

BCA 5th Semester 2020 Practical Record Book


45
JAVA PROGRAMMING PRACTICAL BCA 501P

System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);


}
}
OUTPUT:

BCA 5th Semester 2020 Practical Record Book


46

You might also like