Вы находитесь на странице: 1из 20

/*

* 1. Program to create an object of a class and calculate the area of the room by accessing the member
functions of the parent class.

*/
public class Prog1 {
int length;
int breadth;
public static int area(int length,int breadth)
{
return length*breadth;
}
public static void main(String args[])
{
System.out.println("Calculation of area of Room");
//Now an object of the class is created
Prog1 obj=new Prog1();
System.out.println("The area of this room is:"+obj.area(10, 20));
}
}

Output

run:
Calculation of area of Room
The area of this room is:200
BUILD SUCCESSFUL (total time: 0 seconds)
Prog6:Create an applet
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
public class HelloWorld extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel("Hello World");
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
/*2. Program to demonstrate the use of command line arguments.
*

*/

public class Prog2 {


public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}

Output

run:
admin
computer
Java
Program
BUILD SUCCESSFUL (total time: 0 seconds)
//
import java.io.*;

public class Prog3{


public static void main(String[] args)throws IOException {
try
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer number: ");

String str = bf.readLine();


int i = Integer.parseInt(str);
System.out.println("String: " + str);
System.out.println("Integer: " + i);

}
catch(Exception e){
System.out.println("An exception has occured"+"\n"+"Please enter an integer");

}
}
}

Output

run1:
Enter an integer number:
t
An exception has occured
Please enter an integer
BUILD SUCCESSFUL (total time: 7 seconds)

run2:
Enter an integer number:
12
String: 12
Integer: 12
BUILD SUCCESSFUL (total time: 2 seconds)
/**
*

*/
public class Prog4 {
public static void main(String args[])
{
//We will make the pyramid till the number entered as argument by the user
int firstArg,i,j;

firstArg = Integer.parseInt(args[0]);
for(i=1;i<firstArg;i++)
{
for(j=1;j<=i;j++)
{ System.out.print(j+" ");
}
System.out.println();
}

}
}

Output

run:
1
12
123
1234
BUILD SUCCESSFUL (total time: 0 seconds)
//7. Write a program to copy bytes from one file to another using the file input and output stream
operations for file handling.

import java.io.*;

public class Prog5{


private static void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);

//For Append the file.


// OutputStream out = new FileOutputStream(f2,true);

//For Overwrite the file.


OutputStream out = new FileOutputStream(f2);

byte[] buf = new byte[1024];


int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
switch(args.length){
case 0: System.out.println("File has not mentioned.");
System.exit(0);
case 1: System.out.println("Destination file has not mentioned.");
System.exit(0);
case 2: copyfile(args[0],args[1]);
System.exit(0);
default : System.out.println("Multiple files are not allow.");
System.exit(0);
}
}
}
run1:
Destination file has not mentioned.
BUILD SUCCESSFUL (total time: 0 seconds)

run2:
File copied.
BUILD SUCCESSFUL (total time: 0 seconds)
/*9. Write a program to append data to an existing file.*/

import java.io.*;
class prog10
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("out.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
// Applet class that does addition of two integer numbers
// import class graphics and javax.swing package
import java.awt.Graphics;
import javax.swing.*;
public class AddApplet extends JApplet
{
int sum;
int x;
int y;

public void init()


{
String num1;
String num2;

// read first number from the keyboard

num1 = JOptionPane.showInputDialog("Enter a whole number");

// read seond number from the ketboard

num2 = JOptionPane.showInputDialog("Enter a whole number");

x= Integer.parseInt(num1); // comvert the string to an integer


y= Integer.parseInt(num2); // convert the string to an integer

sum = x + y;

}
public void paint(Graphics g)
{
//writes the output in the applet starting at position 30, 30
g.drawString("the sum ot x (="+x+")+y(="+y+")="+sum, 30,30);
}
}
The program below demonstrates this with a generic string drawing applet. The applet parameter
"Message" is the string to be drawn.
import java.applet.*;
import java.awt.*;

public class DrawStringApplet extends Applet {

private String defaultMessage = "Hello!";

public void paint(Graphics g) {

String inputFromPage = this.getParameter("Message");


if (inputFromPage == null) inputFromPage = defaultMessage;
g.drawString(inputFromPage, 50, 25);

You also need an HTML file that references your applet. The following simple HTML file will do:
<HTML>
<HEAD>
<TITLE> Draw String </TITLE>
</HEAD>

<BODY>
This is the applet:<P>
<APPLET code="DrawStringApplet" width="300" height="50">
<PARAM name="Message" value="Howdy, there!">
This page will be very boring if your
browser doesn't understand Java.
</APPLET>
</BODY>
</HTML

Output
Howdy, there!
5. Write a program to demonstrate the concept of inheritance in java for the following problem. Create
a class Room and use constructor to initialize its length and breadth and create a function to return its
area. Extend this class and define height in the derived class, also a function to calculate volume.
Calculate the area and volume of room by accessing these functions from the base class.
class Prog7_0 {
int length;
int width;
int area(int length ,int breadth)
{
return length*breadth;
}
}
public class Prog7 extends Prog7_0
{
int height;
Prog7()
{

}
Prog7(int length,int width,int height)
{
this.length=super.length;
this.width=super.width;
this.height = height;

}
int volume(int length,int width,int height)
{
return super.area(length,width)*height;
}
public static void main(String args[] )
{
Prog7 obj=new Prog7();
System.out.println("Required volume is:"+obj.volume(10,
70,20));
System.out.println("Area is:"+obj.area(10,70));
}
}

Output
run:
Required volume is:14000
Area is:700
BUILD SUCCESSFUL (total time: 1 second)
8. Write a program for concatenation and buffering of files in java using file handling concept.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Prog8 {


public static void concatenate(String fileName) {
RandomAccessFile file = null;
String line = null;

try {
file = new RandomAccessFile(fileName, "r");
while ((line = file.readLine()) != null) {
System.out.println(line);
}
return;
} catch (FileNotFoundException fnf) {
System.err.println("File: " + fileName + " not found.");
} catch (Exception e) {
System.err.println(e.toString());
} finally {
if (file != null) {
try {
file.close();
} catch (IOException io) {
}
}
}

public static void main(String[] args) {


for (int i = 0; i < args.length; i++)
Prog8.concatenate(args[i]);
}
}

run:
1
Hello Java
BUILD SUCCESSFUL (total time: 0 seconds)
import java.io.DataInputStream;
import java.io.*;
import java.util.*;
class token1{
public void ktf(){
try{
System.out.println();
System.out.println("Enter the string you want to write into the
file");
System.out.println();
BufferedReader dis=new BufferedReader(new
InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(dis.readLine());
String s=st.nextToken();
BufferedWriter out=new BufferedWriter(new
FileWriter("C:\\fil.txt"));
int y=0;
while(st.hasMoreTokens()||(y==0))
{
if(st.hasMoreTokens())
y=0;
else
y=1;
out.write(s+" ");
if(y==0)
s=st.nextToken();
else
break;
}
out.close();
}catch(Exception e){}
}
}
class token2{
public void fts(){
int y=1;
try{
System.out.println();
System.out.println("Reading data from the file");
System.out.println();
DataInputStream dis = new DataInputStream(new
FileInputStream("C:\\fil.txt"));
StringTokenizer st=new StringTokenizer(dis.readLine());
String s=st.nextToken();
while(st.hasMoreTokens()||(y==1)){
if(st.hasMoreTokens())
y=1;
else
y=0;
System.out.print(s+" ");
if(y==1)
{
s=st.nextToken();
}
else
break;
}
dis.close();
}catch(Exception e){}
}

}
class token{
public static void main(String arg[]){
token1 t1=new token1();
t1.ktf();
token2 t2=new token2();
t2.fts();

}
}

run:

Enter the string you want to write into the file

lab

Reading data from the file

alpha BUILD SUCCESSFUL (total time: 4 seconds)


import java.io.DataInputStream;
import java.io.*;
import java.util.Arrays;
class employee{
public static void main(String arg[]) {
int i=0;
String[][] record=new String[100][10];
int[][] doj=new int[100][3];
int[] salary=new int[100];
int[] empno=new int[100];
try{
System.out.println("************* Welcome To Employee management
system*************");
System.out.println();
System.out.println();
System.out.println();

DataInputStream obj=new DataInputStream(System.in);


System.out.println("Enter the employee no");
empno[0]=Integer.parseInt(obj.readLine());
System.out.println("Enter the employee name");
BufferedReader fr=new BufferedReader(new
InputStreamReader(System.in));
record[0][0]=fr.readLine();

System.out.println("Enter the Date of birth ");


BufferedReader fwr=new BufferedReader(new
InputStreamReader(System.in));
record[0][1]=fwr.readLine();

System.out.println("Enter the Department");


BufferedReader ffr=new BufferedReader(new
InputStreamReader(System.in));

record[0][2]=ffr.readLine();
System.out.println("Enter the Designation");
BufferedReader df=new BufferedReader(new
InputStreamReader(System.in));
record[0][3]=df.readLine();
DataInputStream obj1=new DataInputStream(System.in);
System.out.println("Enter the salary");
salary[0]=Integer.parseInt(obj1.readLine());
System.out.println("Enter the Date of Joining");
DataInputStream obj2=new DataInputStream(System.in);
System.out.print("day: ");

doj[0][0]=Integer.parseInt(obj2.readLine());

DataInputStream obj3=new DataInputStream(System.in);


System.out.print("month: ");

doj[0][1]=Integer.parseInt(obj3.readLine());

DataInputStream obj24=new DataInputStream(System.in);


System.out.print("year: ");

doj[0][2]=Integer.parseInt(obj24.readLine());
System.out.println("Enter 1 to add reccord of another employee");
System.out.println("Enter 2 to list emloyees on the basis of salry");
System.out.println("Enter 3 to list emloyees on the basis of DOJ");
System.out.println("Enter 4 to exit");
DataInputStream obj84=new DataInputStream(System.in);
int choice=Integer.parseInt(obj84.readLine());
while(choice==1)
{
System.out.println();
System.out.println();

i++;
try{
System.out.println("Enter the employee record");
DataInputStream obj9=new DataInputStream(System.in);
System.out.println();
System.out.println();

System.out.println("Enter the employee no");


empno[i]=Integer.parseInt(obj9.readLine());
System.out.println("Enter the employee name");
BufferedReader yf=new BufferedReader(new
InputStreamReader(System.in));
record[i][0]=yf.readLine();

System.out.println("Enter the Date of birth (mm-dd-yy)");


BufferedReader pf=new BufferedReader(new
InputStreamReader(System.in));
record[i][1]=pf.readLine();

System.out.println("Enter the Department");


BufferedReader rf=new BufferedReader(new
InputStreamReader(System.in));
record[i][2]=rf.readLine();
System.out.println("Enter the Designation");
BufferedReader qrf=new BufferedReader(new
InputStreamReader(System.in));
record[i][3]=qrf.readLine();

DataInputStream obj10=new DataInputStream(System.in);


System.out.println("Enter the salary");
salary[i]=Integer.parseInt(obj10.readLine());
System.out.println("Enter the Date of Joining(mm-dd-yy)");
System.out.print("day: ");

DataInputStream obj21=new DataInputStream(System.in);


doj[i][0]=Integer.parseInt(obj21.readLine());
System.out.print("month: ");

DataInputStream obj31=new DataInputStream(System.in);


doj[i][1]=Integer.parseInt(obj31.readLine());
System.out.print("year: ");

DataInputStream obj41=new DataInputStream(System.in);


doj[i][2]=Integer.parseInt(obj41.readLine());
System.out.println("Enter 1 to add reccord of another employee");
System.out.println("Enter 2 to list emloyees on the basis of
salary");
System.out.println("Enter 3 to list emloyees on the basis of DOJ");
System.out.println("Enter 4 to exit");
DataInputStream obj43=new DataInputStream(System.in);
choice=Integer.parseInt(obj43.readLine());
}
catch(Exception d){System.out.println("nnnn");}
}
if(choice==2)
{
int[][] salary1=new int[100][2];
for(int b=0;b<=i;b++)
{

salary1[b][0]=salary[b];
salary1[b][1]=b;

}
for(int w=0;w<=i;w++)
for(int s=w+1;s<=i;s++)
{
if(salary1[w][0]>=salary1[s][0])
{
int[][] temp= new int[1][2];
temp[0][0]=salary1[w][0];
temp[0][1]=salary1[w][1];
salary1[w][0]=salary1[s][0];
salary1[w][1]=salary1[s][1];
salary1[s][0]=temp[0][0];
salary1[s][1]=temp[0][1];
}
}
for(int f=0;f<=i;f++){

int r=salary1[f][1];
System.out.println();
System.out.println();

System.out.println("Employee no :"+ empno[r]);


System.out.println("name :"+ record[r][0]);
System.out.println("Department :"+ record[r][2]);
System.out.println("Designation :"+ record[r][3]);
System.out.println("DOB :"+ record[r][1]);
System.out.println("DOJ :"+ doj[r][0]+"-"+doj[r][1]+"-"+doj[r][2]);
System.out.println("salary :"+ salary[r]);
System.out.println();
System.out.println();

}
}
if(choice==3){
int[][] doj1=new int[100][4];
for(int h=0;h<=i;h++){

doj1[h][0]=doj[h][0];

doj1[h][1]=doj[h][1];
doj1[h][2]=doj[h][2];

doj1[h][3]=h;

for(int z=0;z<=i;z++){

for(int t=z+1;t<=i;t++){

if(doj1[z][2]>doj1[t][2])
{
int[][] temp=new int[1][4];
temp[0][0]=doj1[z][0];
temp[0][1]=doj1[z][1];
temp[0][2]=doj1[z][2];
temp[0][3]=doj1[z][3];

doj1[z][0]=doj1[t][0];
doj1[z][1]=doj1[t][1];
doj1[z][2]=doj1[t][2];
doj1[z][3]=doj1[t][3];

doj1[t][0]=temp[0][0];
doj1[t][1]=temp[0][1];
doj1[t][2]=temp[0][2];
doj1[t][3]=temp[0][3];

}
else if(doj1[z][2]==doj1[t][2]){
if(doj1[z][1]>doj1[t][1]){
int[][] temp=new int[1][3];
temp[0][0]=doj1[z][0];
temp[0][1]=doj1[z][1];
temp[0][2]=doj1[z][2];
temp[0][3]=doj1[z][3];

doj1[z][0]=doj1[t][0];
doj1[z][1]=doj1[t][1];
doj1[z][2]=doj1[t][2];
doj1[z][3]=doj1[t][3];

doj1[t][0]=temp[0][0];
doj1[t][1]=temp[0][1];
doj1[t][2]=temp[0][2];
doj1[t][3]=temp[0][3];

else if(doj[z][1]==doj[t][1]){
if(doj1[z][0]>doj1[t][0]){
int[][] temp=new int[1][3];
temp[0][0]=doj1[z][0];
temp[0][1]=doj1[z][1];
temp[0][2]=doj1[z][2];
temp[0][3]=doj1[z][3];

doj1[z][0]=doj1[t][0];
doj1[z][1]=doj1[t][1];
doj1[z][2]=doj1[t][2];
doj1[z][3]=doj1[t][3];

doj1[t][0]=temp[0][0];
doj1[t][1]=temp[0][1];
doj1[t][2]=temp[0][2];
doj1[t][3]=temp[0][3];
}
}

}
}
for(int p=0;p<=i;p++){
int u=doj1[p][3];
System.out.println();
System.out.println();

System.out.println("Employee no :"+ empno[u]);


System.out.println("name :"+ record[u][0]);
System.out.println("Department :"+ record[u][2]);
System.out.println("Designation :"+ record[u][3]);
System.out.println("DOB :"+ record[u][1]);
System.out.println("DOJ :"+ doj[u][0]+"-"+doj[u][1]+"-"+doj[u][2]);
System.out.println();
System.out.println();
System.out.println();

}
}
if (choice==4){
System.exit(0);
}
}
catch(Exception e){}
}
}