0% found this document useful (0 votes)
106 views

Java Record

The document describes a Java program to create a frame with a button called "Mydetails". It involves creating a class called mydetails that extends the Frame class and declares labels, text fields, and buttons. Another class called WH and BH is created that implements an interface and is used to set the window properties. The main method creates an object of the mydetails class and calls its methods to add labels, get text input, and display the frame with the "Mydetails" button.

Uploaded by

Padmapriya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Java Record

The document describes a Java program to create a frame with a button called "Mydetails". It involves creating a class called mydetails that extends the Frame class and declares labels, text fields, and buttons. Another class called WH and BH is created that implements an interface and is used to set the window properties. The main method creates an object of the mydetails class and calls its methods to add labels, get text input, and display the frame with the "Mydetails" button.

Uploaded by

Padmapriya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Ex.

No:01
Extraction of a String
Date:21.12.2014

AIM:
To write a Java program to extract a particular of character string and print the extract
string length.

ALGORITHM:
Step -1: Start the process.

Step -2: Create a class reading and in the main() method declare object for
DataInputStream.

Step -3: Declare String and integer data types to get string and values to extract from the
given string by the user.

Step -4: In the try block, use readline() method to get the string,length() to find the
length of user given string.

Step -5: Use substring() method to extract the portion of user given string specified by the
integer value.

Step -6: Display the respective result.

Step -7: Declare the corresponding catch block for the exception.

Step -8: Stop the process.


EXTRACTION OF A STRING

PROGRAM CODING:

import java.io.DataInputStream;

class Reading

public static void main(String args[])

DataInputStream in=new DataInputStream(System.in);

String str1,str2;

int m,n,len;

try

System.out.println("Enter The String:");

str1=in.readLine();

len=str1.length();

System.out.println("Enter The 2 Values To Extract Strings:");

n=Integer.parseInt(in.readLine ());

m=Integer.parseInt(in.readLine ());

str2=str1.substring(n,m);

System.out.println("Extracted Portion of The String:"+str2);

System.out.println("The Length of Extracted String:"+str2.length());

catch(Exception e)

{}

}
OUTPUT:

D:\Java>javac Reading.java

D:\Java>java Reading

Enter the String:

renegade

Enter 2 values to extract the string:

Extracted portion of the string: egad

Length of the extracted string: 4

RESULT:

Thus the program has been executed successfully.


Ex.No:02
Multiple Inheritance
Date:23.12.2014

AIM:

To write a Java program to implement the concept multiple inheritance using


interface.

ALGORITHM:

Step -1: Start the process.

Step -2: Create a class student to accept the roll number and to display.

Step -3: Create a class test, which extends the super class student to set and display the
marks.

Step -4: Create an interface Sports to assign sports score.

Step -5: Create a class result, which extends the super class test and implements interface
Sports.

Step -6: Create a method in order to inherit all the methods of super class and the interface.

Step -7: Under main() method create object for the last derived class in order to access all
the methods.

Step -8: Stop the process.


MULTIPLE INHERITANCE

PROGRAM CODING:

import java.io.*;
class Student
{
int rollnumber;
void getNumber(int n)
{
rollnumber=n;
}
void putNumber()
{
System.out.println("ROLL NO:"+rollnumber);
}
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
System.out.println("MARKS OBTAINED:");
System.out.println("PART1:"+part1);
System.out.println("PART2:"+part2);
}
}
interface Sports
{
float SportsWt=6.08f;
}
//void putWt()
class Results extends Test implements Sports
{
float total;
public void putWt()
{
System.out.println("SPORTS:"+SportsWt);
}
void display()
{
total=part1+part2+SportsWt;
putNumber();
putMarks();
putWt();
System.out.println("TOTAL SCORE:"+total);
}
}
class Hybrid
{
public static void main(String args[])
{
Results s1=new Results();
s1.getNumber(2207);
s1.getMarks(25.57f,33.08f);
s1.display();
}
}
OUTPUT:

D:\Java>javac Hybrid.java

D:\Java>java Hybrid

ROLL NO:2207

MARKS OBTAINED:

PART1:25.57

PART2:33.08

SPORTS:6.08

TOTAL SCORE:64.73

RESULT:

Thus the program has been executed successfully.


Ex.No:03
Creating an Exception
Date:23.12.2014

AIM:

To write a Java program to create an exception called PayoutofBound and throw the
exception.

ALGORITHM:

Step -1: Start the process.

Step -2: Create a class which extends the super class Exception.

Step -3: Declare the class employee and declare String and integer variables.

Step -4: Using the constructor assign values to the variables.

Step -5: Inside the salary() method range of salary is checked by if condition and it
throw the exception.

Step -6: Using main() method to declare object for the employee class and call the
respective method.

Step -7: Use try block to throw the exception.

Step -8: Stop the process.


CREATING AN EXCEPTION
PROGRAM CODING:

class PayoutofBoundException extends Exception


{
int pay;
PayoutofBoundException(int pay)
{
this.pay=pay;
}
}
public class employee
{
String name;
String Salary;
int pay;
employee(String n,int m)
{
name=n;
pay=m;
}
void findSalary() throws PayoutofBoundException
{
if(pay>10000)throw new PayoutofBoundException(pay);
if(pay<5000)
Salary="low";
else
Salary="high";
System.out.println("Name:"+name+"\tSalary="+Salary+"\tPay:"+pay);
System.out.println(" ");
}
public static void main(String args[])
{
employee x,y,z;
x=new employee("Messi",3500);
y=new employee("Neymar",7000);
z=new employee("Suarez",5000);
try
{
x.findSalary();
y.findSalary();
z.findSalary();
}
catch(PayoutofBoundException e)
{
System.out.println("Pay is greater than 10000");
}
}
}
OUTPUT:

D:\Java>javac employee.java

D:\Java>java employee

Name: Messi Salary=low Pay:3500

Name: Neymar Salary=high Pay:7000

Name :Suarez Salary=high Pay:5000

RESULT:

Thus the program has been executed successfully.


Ex.No:04
Multi Threading
Date:30.12.2014

AIM:

To write a Java program to implement the concept of multi threading with the use
of any multiplication tables and design three different properties.

ALGORITHM:

Step -1: Start the program.

Step -2: Create three classes A, B and C such that each class extending the properties
of Thread class.

Step -3: By using for loop multiplication tables 1, 2 and 3 are formed under the classes A, B
and C respectively.

Step -4: Create the class with main() method

Step -5: Declare objects for the class A, B and C.

Step -6: By using the object set thread priority like max, min and start the thread by using
start() method.

Step -7: Stop the process.


MULTI THREADING
PROGRAM CODING:

import java.io.*;
class A extends Thread
{
public void run()
{
System.out.println("\n1st table started:");
for(int i=1;i<=10;i++)
{
System.out.println(i+"*1="+i*1);
}
System.out.println("Exit from 1st table.");
}
}
class B extends Thread
{
public void run()
{
System.out.println("\n2nd table started:");
for(int j=1;j<=10;j++)
{
System.out.println(j+"*2="+j*2);
}
System.out.println("Exit from 2nd table.");
}
}
class C extends Thread
{
public void run()
{
System.out.println("\n3rd table started:");
for(int k=1;k<=10;k++)
{
System.out.println(k+"*3="+k*3);
}
System.out.println("Exit from 3rd table.");
}
}
class Threadpriority
{
public static void main(String args[]) throws Exception
{
A Obj4=new A();
B Obj5=new B();
C Obj6=new C();
Obj4.setPriority(Thread.MAX_PRIORITY);
Obj5.setPriority(Thread.NORM_PRIORITY);
Obj5.setPriority(Thread.MIN_PRIORITY);
Obj4.run();
Obj5.run();
Obj6.run();
}
}
OUTPUT:

D:\Java>javac Threadpriority.java

D:\Java>java Threadpriority

1st table started:

1*1=1

2*1=2

3*1=3

4*1=4

5*1=5

6*1=6

7*1=7

8*1=8

9*1=9

10*1=10

Exit from 1st table.

2nd table started:


1*2=2

2*2=4

3*2=6

4*2=8

5*2=10

6*2=12

7*2=14

8*2=16

9*2=18

10*2=20
Exit from 2nd table.

3rd table started:

1*3=3

2*3=6

3*3=9

4*3=12

5*3=15

6*3=18

7*3=21

8*3=24

9*3=27

10*3=30

Exit from 3rd table.

RESULT:

Thus the program has been executed successfully.


Ex.No:05
Displaying Several Shapes
Date:30.12.2015

AIM:

To write a Java program to drop several shapes in the created windows.

ALGORITHM:

Step -1: Start the program.

Step -2: Import packages like “awt” and “applet”.

Step -3: Create a class shapes by extending the Applet class.

Step -4: Declare the paint() method.

Step -5: Declare two array int x, int y and assign the values to it.

Step -6: Use polygon method and assign values of array into it.

Step -7: By using paint object, call drawLine(), drawRect(), drawOval() and assign
co-ordinate axis to all the shapes.

Step -8: Display the respective result.

Step -9: Stop the process.


DISPLAYING SEVERAL SHAPES

PROGRAM CODING:

import java.awt.*;
import java.applet.*;
/*<applet code="shapes.class"Width=500 height=500></applet>*/
public class shapes extends Applet
{
public void paint(Graphics g)
{
int X[]={50,89,75};
int Y[]={300,400,450};
Polygon p= new Polygon(X,Y,3);
g.drawPolygon(p);
setBackground(Color.pink);
g.drawLine(50,50,100,50);
g.drawRect(250,50,100,150);
g.setColor(Color.yellow);
g.drawRect(250,50,100,150);
g.setColor(Color.red);
g.drawOval(400,50,50,50);
g.setColor(Color.blue);
g.drawOval(500,50,50,50);
}
}
OUTPUT:

D:\Java>javac shapes.java

D:\Java>appletviewer shapes.java

RESULT:

Thus the program has been executed successfully.


Ex.No:06
Creation of Frame
Date:06.01.2015

AIM:

To create a Java program to create a Frame with button is called “Mydetails”.

ALGORITHM:

Step -1: Start the process.

Step -2: Create a class my details by extending Frame class.

Step -3: Declare Labels, TextFields and Buttons and add the constructor using
mydetails.

Step -4: Create class WH and BH by extending class and using interface respecting.

Step -5: Create window for adding Label, getting text for displaying.

Step -6: Use main() method to create object for class and call the respective method.

Step -7: Stop the process.


CREATION OF FRAME

PROGRAM CODING:

import java.awt.*;

import java.awt.event.*;

public class Mydetails extends Frame

Label l1=new Label("Name");

Label l2=new Label("Street");

Label l3=new Label("City");

Label l4=new Label("Pincode");

TextField t1=new TextField(20);

TextField t2=new TextField(35);

TextField t3=new TextField(35);

TextField t4=new TextField(6);

Button Ok=new Button("Mydetails");

String msg;

Mydetails()

setTitle("Mydetails");

add(l1);add(t1);

add(l2);add(t2);

add(l3);add(t3);

add(l4);add(t4);

Ok.addActionListener(new BH());

add(Ok);

setLayout(new FlowLayout());

addWindowListener(new WH());
}

class WH extends WindowAdapter

public void windowclosing(WindowEvent e)

System.exit(0);

class BH implements ActionListener

public void actionPerformed(ActionEvent e)

if(e.getActionCommand().equals("ok"));

msg="Name: "+t1.getText()+" "+"Street: "+t2.getText()+" "+"\nCity: "+t3.getText()+"


"+"\nPincode: "+t4.getText();

repaint();

}}

public void paint(Graphics g)

g.drawString(msg,10,200);

public static void main(String args[])

Mydetails obj=new Mydetails();

obj.setBounds(1,1,150,300);

obj.setVisible(true);

}}
OUTPUT:

D:\Java>javac Mydetails.java

D:\Java>java Mydetails

RESULT:
Thus the program has been executed successfully.
Ex.No:07
Multi Selection List-Box
Date:24.01.2015

AIM:

To write a Java program to demonstrate multiple selection list-box.

ALGORITHM:

Step -1: Start the process.

Step -2: Create a class which extends applet sub default class and implement interface
ActionListener.

Step -3: Declare List, TextField, Button and String in init() method, assign values to the
text field and add.

Step -4: Add the number of items required for the list.

Step -5: Create a Button under the name “Show Selection” and add the button into
ActionListener.

Step -6: Under actionPerformed() method, use for loop to display the items that has
been selected from the list in the text area provided.

Step -7: Stop the process.


MULTI SELECTION LIST-BOX

PROGRAM CODING:

import java.awt.Component;
import java.awt.List;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*<applet code=multiselection.class width=400 height=400></applet>*/
public class multiselection extends Applet implements ActionListener
{
List list1;
TextField text1;
Button button1;
String selection[];
public void init()
{
text1= new TextField(40);
add(text1);
list1=new List(5,true);
list1.add("Android");
list1.add("iOS");
list1.add("Windows");
list1.add("Linux");
list1.add("Unix");
list1.add("item6");
list1.add("item7");
list1.add("item8");
list1.add("item9");
add(list1);
button1=new Button("Show Selection");
button1.addActionListener(this);
add(button1);
}
public void actionPerformed(ActionEvent e)
{
String outString=new String("You Selected:");
if(e.getSource()==button1)
selection=list1.getSelectedItems();
for(int loopindex=0;loopindex<selection.length;loopindex++)
{
outString+=","+selection[loopindex];
}
text1.setText(outString);
}
}
OUTPUT:

D:\Java>javac multiselection.java

D:\Java>appletviewer multiselection.java

RESULT:
Thus the program has been executed successfully.
Ex.No:08
Frame with Multiple Line
Date:27.01.2015

AIM:

To write a Java program to create a frame with three text field for name, age,
qualification and address.

ALGORITHM:

Step -1: Start the process.

Step -2: Declare the Labels, TextFields, TextAreas and Buttons.

Step -3: In the constructor Frame(), add Label, TextField and Button, set title and layout
for the window.

Step -4: Create the class and method to get String variable and display them using
drawString().

Step -5: In the main(), create object for the class Frame and call the method setBound
and setVisible.

Step -6: Execute the process and display the result.

Step -7: Stop the process.


FRAME WITH MULTIPLE LINE

PROGRAM CODING:

import java.awt.*;
import java.awt.event.*;
class Frame1 extends Frame
{
Label l1=new Label("Name");
Label l2=new Label("Age");
Label l3=new Label("Qualification");
Label l4=new Label("Address");
TextField t1=new TextField(15);
TextField t2=new TextField(15);
TextField t3=new TextField(15);
TextArea t4;
Button OK=new Button("OK");
String msg,msg1,msg2,msg3;
Frame1()
{
setTitle("Text Area");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
t4=new TextArea(msg1,5,25);
add(l4);add(t4);
OK.addActionListener(new BH());
add(OK);
setLayout(new FlowLayout());
addWindowListener(new WH());
}
class WH extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
class BH implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("OK"));
msg=t1.getText();
msg1=t2.getText();
msg2=t3.getText();
msg3=t4.getText();
repaint();
}}
public void paint(Graphics g)
{
g.drawString("Name---------------->"+msg,150,220);
g.drawString("Age------------------->"+msg1,150,240);
g.drawString("Qualification------->"+msg2,150,260);
g.drawString("Address------------->"+msg3,150,280);
}
public static void main(String args[])
{
Frame1 c=new Frame1();
c.setBounds(4,4,150,50);
c.setVisible(true);
}}
OUTPUT:

D:\Java>javac Frame1.java

D:\Java>java Frame1

RESULT:
Thus the program has been executed successfully.
Ex.No:09
Menu Box and Pull Down Menu
Date:03.01.2015

AIM:

To write a Java program to create menu bar and drop down menu.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class menu that extends the applet and use the interface ActionListener.
Step -3: Under the init() method, declare Button and Frame along with its size.
Step -4: Declare a class frame that extends the Frame class.
Step -5: Declare variable for Menu, MenuBar, Label and MenuItem.
Step -6: Declare constructor frame() and add all the variable of above said type.
Step -7: Use actionPerformed and windowClosing method and use if loop for setting the
text.
Step -8: Stop the process.
MENU BOX AND PULL DOWN MENU

PROGRAM CODING:

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

/*<applet code="menu.class"width=300 height=200></applet>*/

public class menu extends Applet implements ActionListener

Button b1;

frame menuWindow;

public void init()

b1=new Button("Display the menu Window");

add(b1);

b1.addActionListener(this);

menuWindow=new frame("menu");

menuWindow.setSize(200,200);

public void actionPerformed(ActionEvent event)

if(event.getSource()==b1)

menuWindow.setVisible(true);

}}}
class frame extends Frame implements ActionListener

Menu menu;

MenuBar menubar;

MenuItem menuitem1,menuitem2,menuitem3;

Label label;

frame(String title)

super(title);

label=new Label("Hello from java");

setLayout(new GridLayout(1,1));

add(label);

menubar=new MenuBar();

menu=new Menu("file");

menuitem1=new MenuItem("item1");

menu.add(menuitem1);

menuitem1.addActionListener(this);

menuitem2=new MenuItem("item2");

menu.add(menuitem2);

menuitem2.addActionListener(this);

menuitem3=new MenuItem("item3");

menu.add(menuitem3);

menuitem3.addActionListener(this);

menubar.add(menu);
setMenuBar(menubar);

addWindowListener(new WindowAdapter()

public void WindowClosing(WindowEvent e)

setVisible(false);

});

public void actionPerformed(ActionEvent event)

if(event.getSource()==menuitem1)

label.setText("YOU CHOOSE ITEM 1");

if(event.getSource()==menuitem2)

label.setText("YOU CHOOSE ITEM 2");

if(event.getSource()==menuitem3)

label.setText("YOU CHOOSE ITEM 3");

}}
OUTPUT:

D:\Java>javac menu.java

D:\Java>appletviewer menu.java

RESULT:

Thus the program has been executed successfully.


Ex.No:10
Mouse Event
Date:07.02.2015

AIM:

To write a Java program to create frame which respond to the mouse click for each
extends with move such as move up, down the corresponding message.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class me extends from Applet.
Step -3: Create method like mouseDown(), mouseUp(), mouseMoved() and mouseDrag() in
order click with mouse positions and movement.
Step -4: Declare and initialize data types String and integer to get the mouse position.
Step -5: Use drawstring() method to find the mouse movement and positions.
Step -6: Stop the process.
MOUSE EVENT

PROGRAM CODING:

import java.awt.*;

import java.io.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="me.class"width=800 height=511></applet>*/

public class me extends Applet

String m=" ";

int mx=0,my=0;

public boolean mouseDown(Event e,int x,int y)

mx=x;

my=y;

m="button pressed";

showStatus("Pressing mouse at position:"+x+","+y);

repaint();

return true;

public boolean mouseUp(Event e,int x,int y)

mx=x;

my=y;

m="button released";

showStatus("Mouse released at position:"+x+","+y);

repaint();
return true;

public boolean mouseMoved(Event e,int x,int y)

mx=x;

my=y;

m=”mouse moved”;

showStatus("Moving mouse at: "+x+","+y);

return true;

public boolean mouseDrag(Event e,int x,int y)

mx=x;

my=y;

m="button drag";

showStatus("Dragging the mouse at position: "+x+","+y);

repaint();

return true;

public void paint(Graphics g)

g.drawString(m,mx,my);

}
OUTPUT:

D:\Java>javac me.java

D:\Java>appletviewer me.java

RESULT:

Thus the program has been executed successfully.


Ex.No:11
Drawing Shapes at Mouse Click Positions
Date:10.02.2015

AIM:

To write a Java program to draw a circle, square, ellipse and rectangle at the mouse
click.
ALGORITHM:
Step -1: Start the process.
Step -2: Create a class which extends Applet implements mouseListener.
Step -3: Use switch statement to draw several shapes like circle, square, ellipse, rectangle
Step -4: Use the different mouse events like clicked, entered and drag with respective
co-ordinate axis.
Step -5: Each corresponding shapes are down using mouse events like pressed, dragged,
released and exited.
Step -6: Stop the process.
DRAWING SHAPES AT MOUSE CLICK POSITIONS

PROGRAM CODING:

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class Drawshap extends Applet implements MouseListener

int X,Y,ch;

public void init()

addMouseListener(this);

ch=1;

public void paint(Graphics g)

switch(ch)

case 1:

g.drawOval(X,Y,50,50);

break;

case 2:

g.drawRect(X,Y,60,50);

break;

case 3:

g.drawOval(X,Y,200,250);

break;

case 4:
g.drawRect(X,Y,250,100);

break;

ch++;

if(ch==5)

ch=1;

showStatus(X+","+Y);

public void mouseClicked(MouseEvent me)

X=me.getX();

Y=me.getY();

repaint();

public void mouseEntered(MouseEvent me)

X=me.getX();

Y=me.getY();

repaint();

public void mouseDrag(MouseEvent me)

X=me.getX();

Y=me.getY();

repaint();
}

public void mouseExited(MouseEvent me)

public void mousePressed(MouseEvent me)

public void mouseDragged(MouseEvent me)

public void mouseReleased(MouseEvent me)

/*<applet code="Drawshap.class"width=500 height=500></applet>*/


OUTPUT:

D:\Java>javac Drawshap.java

D:\Java>appletviewer Drawshap.java

RESULT:
Thus the program has been executed successfully.
Ex.No:12
Appending Text to Existing File
Date:14.02.2015

AIM:

To write a Java program to open an existing file and append text to that file.
ALGORITHM:
Step -1: Start the process.
Step -2: Declare a class RandomAccess and declare the main method.
Step -3: Create an object for RandomAccess file as r file.
Step -4: Inside the try block, specify the name of the text file and open the file in read and
write mode.
Step -5: Using the length() function the length of the file and use seek() method to find the
end of the file.
Step -6: writeBytes() method is used to specify the text to copied in the text file.
Step -7: catch() block is used to print the exception.
Step -8: Stop the process.
APPENDING TEXT TO EXISTING FILE

PROGRAM CODING:

import java.io.*;

class RandomAccess

public static void main(String args[])

RandomAccessFile rFile;

try

rFile=new RandomAccessFile("a1.txt","rw");

rFile.seek(rFile.length());

rFile.writeBytes("Java is a Pure Object-Oriented Programming.");

catch(IOException ioe)

System.out.println(ioe);

}
OUTPUT:

D:\Java>javac RandomAccess.java

D:\Java>java RandomAccess.java

D:\Java>java a1.txt

RESULT:

Thus the program has been executed successfully.

You might also like