Java P
Java P
Laboratory certificate
(R.VANLALAWMPUIA) (K.LALMUANPUIA)
Teacher in-charge Head of the Department.
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
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
OUTPUT:
2. Programs to implement the methods of String class.
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:
class Room
{
int length,breadth;
Room(int x, int y) //constructor method
{
length=x;
breadth=y;
}
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);
}
}
OUTPUT:
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);
OUTPUT:
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:
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:
void display()
{
System.out.println("Super x="+x);
}
}
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();
}
}
OUTPUT:
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:
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:
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);
}
}
}
OUTPUT:
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>
OUTPUT:
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();
}
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();
}
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:
Keyboard:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
</applet>
*/
implements KeyListener
int X=20,Y=30;
String msg="KeyEvents--->";
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
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();
showStatus("Key Up");
msg+=k.getKeyChar();
repaint();
g.drawString(msg,X,Y);
OUTPUT:
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:
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>
OUTPUT:
OUTPUT:
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>
OUTPUT:
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>
</Html>
OUTPUT:
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:
ScrollEx2()
{
jf = new JFrame("Scrollbar");
//Creating a Label
Label panelLabel1 = new Label("Handling a Scrollbar drag event", Label.CENTER);
//Creating a JLabel and setting its value to the current position value of horizontal
scrollbar.
frameLabel1 = new Label(i.toString());
jf.add(frameLabel1, BorderLayout.SOUTH);
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());
}
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
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;
}