Java Record
Java Record
Java Record
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 -7: Declare the corresponding catch block for the exception.
PROGRAM CODING:
import java.io.DataInputStream;
class Reading
String str1,str2;
int m,n,len;
try
str1=in.readLine();
len=str1.length();
n=Integer.parseInt(in.readLine ());
m=Integer.parseInt(in.readLine ());
str2=str1.substring(n,m);
catch(Exception e)
{}
}
OUTPUT:
D:\Java>javac Reading.java
D:\Java>java Reading
renegade
RESULT:
AIM:
ALGORITHM:
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 -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.
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:
AIM:
To write a Java program to create an exception called PayoutofBound and throw the
exception.
ALGORITHM:
Step -2: Create a class which extends the super class Exception.
Step -3: Declare the class employee and declare String and integer 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.
D:\Java>javac employee.java
D:\Java>java employee
RESULT:
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 -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 -6: By using the object set thread priority like max, min and start the thread by using
start() method.
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
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
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.
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
RESULT:
AIM:
ALGORITHM:
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.
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:
AIM:
ALGORITHM:
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.
PROGRAM CODING:
import java.awt.*;
import java.awt.event.*;
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());
}
System.exit(0);
if(e.getActionCommand().equals("ok"));
repaint();
}}
g.drawString(msg,10,200);
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:
ALGORITHM:
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.
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 -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.
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;
Button b1;
frame menuWindow;
add(b1);
b1.addActionListener(this);
menuWindow=new frame("menu");
menuWindow.setSize(200,200);
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);
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()
setVisible(false);
});
if(event.getSource()==menuitem1)
if(event.getSource()==menuitem2)
if(event.getSource()==menuitem3)
}}
OUTPUT:
D:\Java>javac menu.java
D:\Java>appletviewer menu.java
RESULT:
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.*;
int mx=0,my=0;
mx=x;
my=y;
m="button pressed";
repaint();
return true;
mx=x;
my=y;
m="button released";
repaint();
return true;
mx=x;
my=y;
m=”mouse moved”;
return true;
mx=x;
my=y;
m="button drag";
repaint();
return true;
g.drawString(m,mx,my);
}
OUTPUT:
D:\Java>javac me.java
D:\Java>appletviewer me.java
RESULT:
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.*;
int X,Y,ch;
addMouseListener(this);
ch=1;
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);
X=me.getX();
Y=me.getY();
repaint();
X=me.getX();
Y=me.getY();
repaint();
X=me.getX();
Y=me.getY();
repaint();
}
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
RandomAccessFile rFile;
try
rFile=new RandomAccessFile("a1.txt","rw");
rFile.seek(rFile.length());
catch(IOException ioe)
System.out.println(ioe);
}
OUTPUT:
D:\Java>javac RandomAccess.java
D:\Java>java RandomAccess.java
D:\Java>java a1.txt
RESULT: