0% found this document useful (0 votes)
7 views27 pages

Java Program 2023

The document contains various Java programming examples demonstrating keyboard input, control statements, loops, classes and objects, method overloading, arrays, string manipulation, inheritance, packages, threading, exception handling, file I/O, and event handling. Each example includes code snippets and sample outputs illustrating the functionality of different Java concepts. The document serves as a comprehensive guide for beginners to understand fundamental Java programming techniques.

Uploaded by

mukunonti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views27 pages

Java Program 2023

The document contains various Java programming examples demonstrating keyboard input, control statements, loops, classes and objects, method overloading, arrays, string manipulation, inheritance, packages, threading, exception handling, file I/O, and event handling. Each example includes code snippets and sample outputs illustrating the functionality of different Java concepts. The document serves as a comprehensive guide for beginners to understand fundamental Java programming techniques.

Uploaded by

mukunonti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Keyboard input in java

import [Link].*;
public class input
{
public static void main(String arg[])throws IOException
{
int p,n;
float r,si;
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter principle");
p=[Link]([Link]());
[Link]("Enter years");
n=[Link]([Link]());
[Link]("Enter Interest");
r=[Link]([Link]());
si=(p*n*r)/100;
[Link](si);
[Link]("the interest is "+si);
}}
/*Enter principle
300
Enter years
5
Enter Interest
7
105.0
the interest is 105.0*/
//Control Statements
//if-else
import [Link].*;
public class ifelse
{
public static void main(String[] args)throws IOException
{ int num;
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter the number");
num=[Link]([Link]());
if(num%2==0)
[Link]("The number is even");
else
[Link]("The number is odd");
}}
/*Enter the number
5
The number is odd*/
//For loop
import [Link].*;
public class forloop
{
public static void main(String[]args)
{ int i,add=0;
[Link]("FORLOOP");
[Link]("*******");
for(i=2;i<=9;i++)
{ [Link](i);
add=add+i;
}
[Link]("add is "+add);
}}
/*FORLOOP
*******
2
3
4
5
6
7
8
9
add is 44*/
//Do-While
import [Link].*;
public class dowhileloop
{
public static void main(String[]args)throws IOException
{
[Link]("DOWHILELOOP");
[Link]("***********");
int i=1;
do
{ [Link](i);
i=i+1;
}
while(i<=5);
}
}
/*DOWHILELOOP
***********
1
2
3
4
5*/
//While loop
import [Link].*;
public class whileloop
{
public static void main(String[]args)
{ [Link]("WHILELOOP");
[Link]("---------");
int a=2;
while(a<=5)
{ [Link]("Hello world");
a++;
}
}
}
/*WHILELOOP
---------
Hello world
Hello world
Hello world
Hello world*/
// Classes and Objects
class recangle
{
int length,width;
recangle(int x,int y)
{ length=x;
width=y;
}
int rectarea()
{ return(length*width); }
}
class Rectangle
{
public static void main(String args[])throws Exception
{ recangle rect1=new recangle(15,10);
int area1=[Link]();
[Link]("Area="+area1);
}
}
/*Classes and Objects
Area=150*/
//Method Overloading
import [Link].*;
class over
{
public void disp(char c)
{ [Link](c); }
public void disp(char c,int num)
{ [Link](c+" "+num); }
public void disp(double b)
{ [Link](b); }
}
public class overloading
{
public static void main(String args[])
{
Over sample=new over();
[Link]('a');
[Link]('a',10);
[Link](10.5);
}}
/*METHOD OVERLOADING
a
a 10
10.5*/
//Single dimensional array
import [Link].*;
import [Link].*;
class array
{
public static void main(String args[])throws Exception
{
Scanner s = new Scanner([Link]);
String name;
int marks[] = new int[3];
int total=0;
float avg;
[Link]("Enter name");
name=[Link]();
[Link]("Enter marks one by one");
for(int i=0;i<3;i++)
{
marks[i]=[Link]();
total=total+marks[i];
}
avg=total/3;
[Link]("Name:"+name);
[Link]("Total:"+total);
[Link]("Average:"+avg);
}
}

/* Enter name
aa
Enter marks one by one
90
90
90
Name:aa
Total:270
Average:90.0 */
//Two dimensional Array
import [Link].*;
import [Link].*;
class twoarray
{
public static void main(String args[])throws Exception
{
Scanner s = new Scanner([Link]);
int a[][] = new int[2][2];
int b[][] = new int[2][2];
int c[][] = new int[2][2];

[Link]("Enter the first matrix");


for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=[Link]();
}
}
[Link]("Enter the second matrix");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
b[i][j]=[Link]();
}
}
[Link]("Sum of the matrix is");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
[Link](c[i][j]+" ");
}
[Link]();
}
}
}
//String and String Buffer class
import [Link].*;
public class buffer
{
public static void main(String args[])throws IOException
{ String str1,str2;
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter the string1");
str1=[Link]();
[Link]("Enter the string2");
str2=[Link]();
[Link]("The lowercase of the string1 is:"+[Link]());
[Link]("The uppercase of the string2 is:"+[Link]());
[Link]("The length of string1 is:"+[Link]());
String s1=[Link]('a','e');
[Link]("Replace 'a' with 'e' in string1:"+s1);
[Link]("Check equality:"+[Link](str2));
String s2=[Link](str2);
[Link]("Concat str1 and str2:"+s2);
StringBuffer str3=new StringBuffer("object oriented");
[Link]("Length of string3"+[Link]());
[Link]("Append a string at end:"+[Link]("language"));
[Link]("Insertion of string:"+[Link](15,"Programming"));
} }
/*Enter the string1
Jamal
Enter the string2
Mohamed
The lowercase of the string1 is:jamal
The uppercase of the string2 is:MOHAMED
The length of string1 is:5
Replace 'a' with 'e' in string1:Jemel
Check equality:false
Concat str1 and str2:JamalMohamed
Length of string315
Append a string at end:object orientedlanguage
Insertion of string:object orientedProgramminglanguage*/
//Vector class
import [Link].*;
import [Link].*;
public class Vectordemo
{
public static void main(String args[])throws IOException
{ Vector v=new Vector(3,2);
[Link]("Initial size:"+[Link]());
[Link]("Initial capacity;"+[Link]());
[Link](new Integer(1));
[Link](new Integer(3));
[Link]("Size after two additions:"+[Link]());
[Link](new Double(6.08));
[Link](new Integer(7));
[Link]("Current Size:"+[Link]());
[Link](new Integer(11));
[Link](new Integer(12));
[Link]("First element:"+(Integer)[Link]());
[Link]("Last element:"+(Integer)[Link]());
if([Link](new Integer(3)))
[Link]("vector contains 3.");
[Link]("The vector elements are “+v);
}
}
/*Initial size:0
Initial capacity;3
Size after two additions:2
Current Size:4
Current Size:5
Current Size:6
First element:1
Last element:11
vector contains 3.
The vector elements are[1, 3, 6.08, 7, 9.4, 11]
//Single Inheritance
import [Link].*;
class base
{ int z;
public void addition(int x,int y)
{ z=x+y;
[Link]("The sum of given numbers is"+z);}
class derive extends base
{
public void multiplication(int x,int y)
{ z=x*y;
[Link]("The product of given numbers are"+z); } }
class single
{
public static void main(String args[])throws Exception
{ int a,b;
DataInputStream d=new DataInputStream([Link]);
[Link]("Enter the first number");
a=[Link]([Link]());
[Link]("Enter the second number");
b=[Link]([Link]());
derive d1=new derive();
[Link](a,b);
[Link](a,b);
} }
/*Enter the first number :20
Enter the second number:05
The sum of given numbers is25
The product of given numbers are100*/
// Method Overriding
import [Link].*;
class Super
{
int x;
Super(int x)
{
this.x=x;
}
void display()
{
[Link]("Super x="+x);
}
}
class Sub extends Super
{
int y;
Sub(int x, int y)
{
super(x);
this.y=y;
}
void display()
{
[Link]("Super x="+x);
[Link]("Sub y="+y);
}
}
class Override
{
public static void main(String args[])
{
Sub s1= new Sub(100,200);
[Link]();
}
}

/* Super x=100
Sub y=200
*/
//Multiple Inheritance
import [Link].*;
interface Area
{
public void area(double base,double length);
}
class Rect implements Area
{
public void area(double base,double length)
{ [Link]();
double r=length*base;
[Link]("The area of Rectangle:"+r);
} }
class Tri implements Area
{
public void area(double base,double height)
{ [Link]();
double t=0.5*height*base;
[Link]("The area of Triangle:"+t);
} }
public class multiple
{
public static void main(String args[])throws Exception
{
DataInputStream d= new DataInputStream([Link]);
[Link]("\t MULTIPLE INHERITANCE\n");
[Link]("Enter the base");
double b=[Link]([Link]());
[Link]("Enter the length");
double l=[Link]([Link]());
[Link]("Enter the height");
double h=[Link]([Link]());
Rect r=new Rect();
[Link](l,b);
Tri t=new Tri();
[Link](h,b);
} }
/* MULTIPLE INHERITANCE
Enter the base
5
Enter the length
6
Enter the height
8

The area of Rectangle:30.0

The area of Triangle:20.0*/


//Package
package jmc;
import [Link].*;
import [Link].*;
public class Student
{ String name;
int rollno,m1,m2,m3,m4,m5,total;
float avg;
public Student()throws Exception
{ Scanner s=new Scanner([Link]);
[Link]("Enter name");
name=[Link]();
[Link]("Enter rollno");
rollno=[Link]();
[Link]("Enter marks");
m1=[Link]();
m2=[Link]();
m3=[Link]();
m4=[Link]();
m5=[Link](); }
public void calc()
{ total=m1+m2+m3+m4+m5;
avg=total/5; }
public void display()
{
[Link]("******** ****");
[Link]("Marklist list");
[Link]("******** ****");
[Link]("\tName:"+name);
[Link]("\tRollNo:"+rollno);
[Link]("\tmark1:"+m1);
[Link]("\tmark2:"+m2);
[Link]("\tmark3:"+m3);
[Link]("\tmark4:"+m4);
[Link]("\tmark5:"+m5);
[Link]("\tTotal:"+total);
[Link]("\tAverage:"+avg); } }
import jmc.*;
public class marklist
{
public static void main(String args[])throws Exception
{ Student s=new Student();
[Link]();
[Link](); } }
/*Enter name
Farisha
Enter rollno
30027
Enter marks
77
88
99
66
89
******** ****
Marklist list
******** ****
Name:Farisha
RollNo:30027
mark1:77
mark2:88
mark3:99
mark4:66
mark5:89 Total:419 Average:83.0*/
//Thread Program
class A extends Thread
{
public void run()
{ for(int i=1;i<=5;i++)
{ [Link]("From Thread A i="+i);
}
[Link]("Exit from A"); } }
class B extends Thread
{
public void run()
{ for(int j=1;j<=5;j++)
{ [Link]("From Thread B j="+j);
if(j==3)stop();
}
[Link]("Exit from B"); } }
class Threaddemo
{
public static void main(String args[])
{ A a=new A();
B b=new B();
[Link]("Start Thread A");
[Link]();
[Link]("Start Thread B");
[Link]();
[Link]("Exit Main thread"); } }

/*Start Thread A
Start Thread B
Exit Main thread
From Thread A i=1
From Thread B j=1
From Thread B j=2
From Thread A K=1
From Thread A i=2
From Thread A i=3
From Thread A i=4
From Thread A i=5
Exit from A
From Thread B j=3
From Thread A K=2
From Thread A K=3
From Thread A K=4
From Thread A K=5
*/
//Exception Handling
import [Link].*;
public class exdemo
{
public static void main(String args[])throws Exception
{
try
{ int a,b,c;
a=[Link](args[0]);
b=[Link](args[1]);
c=a/b;
[Link]("The result is"+c);
}
catch(ArithmeticException e)
{ [Link](e); }
catch(NumberFormatException e)
{ [Link](e); }
catch(ArrayIndexOutOfBoundsException e)
{ [Link](e); }
catch(Exception e)
{ [Link](e); }
finally
{ [Link]("Thank You"); } } }
/*EXCEPTION
[Link]: Index 0 out of bounds for length 0
Thank You
java exdemo 4 2
The result is2
Thank You
java exdemo 4 0
[Link]: / by zero
Thank You
java exdemo 4 a
[Link]: For input string: "a"
Thank You
java exdemo 4
[Link]: Index 1 out of bounds for length 1
Thank You*/
//I/O Streams
import [Link].*;
public class Filecopy
{
public static void main(String args[])throws Exception
{
try
{ DataInputStream d=new DataInputStream([Link]);
String sfile,dfile;
int n=0;
[Link]("Program to copy from one file to other");
[Link]("\nEnter the source file name");
sfile=[Link]();
[Link]("\nEnter the destinationfile name");
dfile=[Link]();
FileReader fr=new FileReader(sfile);
FileWriter fw=new FileWriter(dfile);
while((n=[Link]())!=-1)
{ [Link](n); }
[Link]("file copied");
[Link]();
[Link]();
}catch(Exception e)
{ [Link]("file not found"); } } }
/*Program to copy from one file to other
Enter the source file name : [Link]
Enter the destinationfile name :[Link]
file copied*/
// Event Handler
import [Link].*;
import [Link].*;
class Eventhand extends Frame implements ActionListener
{
TextField t1;
Eventhand()
{
t1 = new TextField ();
[Link] (60, 50, 170, 20);
Button button = new Button ("Show");
[Link] (90, 140, 75, 40);
[Link] (this);
add (button);
add (t1);
setSize (250, 250);
setLayout (null);
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
[Link] ("Hello World");
}
public static void main (String args[])
{
new Eventhand();
}
}

You might also like