Upload
kandarp-tiwari
View
71
Download
3
Embed Size (px)
Citation preview
Program 1 Program: WAP in java to print “Hello World”.
class Hello { public static void main(String args[ ])
{ System.out.println("Hello World");
} }
Program 2
Program: WAP in java to implement programs to define a class and
instantiate its object.
import java.io.*;
class Student { int sid;
String sname,sadr,sfname,smname; BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void input()throws IOException { System.out.println("please input sid,name,fname and mname");
sid=Integer.parseInt(br.readLine()); sname=br.readLine();
sfname=br.readLine(); smname=br.readLine(); }
public void display() {
System.out.println("Student's id="+sid); System.out.println("Student's name="+sname); System.out.println("Student's father name="+sfname);
System.out.println("Student's mother name="+smname); }
} class object {
public static void main(String args[ ])throws IOException {
Student s=new Student(); s.input(); s.display();
} }
Program 3 Program: WAP in java to implement constructor and method overloading.
class demo {
int area(int a) {
return(a*a); } int area(int a,int b)
{ return(a*b);
} double area(double a) {
return(3.14*a*a); }
} class room {
public static void main(String args[]) {
demo d=new demo(); System.out.println("Area of square="+d.area(5)); System.out.println("Area of rectangle="+d.area(6,4));
System.out.println("Area of circle="+d.area(1.25)); }
}
Program 4
Program: WAP in java to implement Command line argument.
class ComLineTest
{ public static void main(String args[ ])
{ int count,i=0; String s;
count=args.length; System.out.println("Number of arguments="+count);
while(i<count) { s=args[i];
i=i+1; System.out.println(i+":"+"java is"+s+"!");
} } }
Program 5
Program: WAP in java to implement String class.
class abc {
public static void main(String args[]) {
int n,i,j; String s=new String("manju"); System.out.println(" Length of String="+s.length());
int l=s.length(); for(i=0;i<l;i++)
{ for(j=0;j<=i;j++) System.out.print(s.charAt(j));
System.out.print("\n"); }
} }
Program 6
Program: WAP in java to implement 2D-Arrays.
import java .io.*;
class element {
public static void main(String args[ ])throws IOException { int a[ ][ ]=new int[3][3];
int i,j; BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input nine elements for matrices"); for(i=0;i<3;i++) for(j=0;j<3;j++)
a[i ][ j]=Integer.parseInt(br.readLine()); System.out.println("the matrices");
for(i=0;i<3;i++) { for(j=0;j<3;j++)
System.out.println(a[i][j]+"\t"); System.out.println("\n"); }
} }
Program 7
Program: WAP in java to implement Multi-level inheritance.
import java.util.*; class multilevel {
static Scanner sc=new Scanner(System.in); static class A {
int a1,a2;
void getdata() {
System.out.println("Enter the value of a1 is:"); a1=sc.nextInt();
System.out.println("Enter the value of a2 is:"); a2=sc.nextInt(); }
void putdata() {
System.out.println("\n The value of a1 is:"+ a1 +" and a2:"+ a2); } }
static class B extends A {
int b1; void add() {
b1=a1+a2; System.out.println("\n The Sum is:"+b1); } }
static class C extends B { int c1; void mul() {
c1=b1*a1;
System.out.println("The product is :"+c1); } }
public static void main(String args[]) {
C obj=new C(); obj.getdata(); obj.add(); obj.mul(); } }
Program 8
Program: WAP in java implement the use of interface.
interface my
{ public void show();
} class demo implements my {
public void show() {
System.out.println("This is interface program"); } }
class sks {
public static void main(String args[]) { demo d=new demo();
d.show(); } }
Program 9
Program: WAP in java implement method overriding.
class One
{ void display()
{ System.out.println("I am class One"); }
} class Two extends One
{ void display() {
System.out.println("I am class Two"); }
} class Overriding {
void display() { System.out.println("I am from class override");
} public static void main(String args[])
{ Overriding obj=new Overriding(); obj.display();
Two t=new Two(); t.display();
} }
Program 10
Program: WAP in java to implement Multithreading.
class A extends Thread {
public void run() {
for(int i=1;i<5;i++) { System.out.println("\t from Thread A:"+i);
} System.out.println("Exit from Thread A");
} } class B extends Thread
{ public void run()
{ for(int j=0;j<5;j++) {
System.out.println("\t from Thread B:"+j); }
System.out.println("Exit from Thread B"); } }
class threadtest {
public static void main(String args[]) { A ob=new A();
B ob1=new B(); ob.start();
ob1.start(); } }
Program 11
Program: WAP in java to implement Exception Handling.
class Error {
public static void main(String args[]) {
int a=10,b=5,c=5,x,y; try {
x=a/b-c; }
catch(Exception e) {} {
System.out.println("Division by zero"); }
y=a/b+c; System.out.println("y="+y); }
}
Program 12
Program: WAP in java to implement User-defined and pre-defined packages.
package package1;
public class A {
public void display() { System.out.println("This is class A");
} }
import package1.A; class Packagetest1 {
public static void main(String args[]) {
A obj=new A(); obj.display(); }
}
Program 13
Program: WAP in java to implement applet using Graphics class.
import java.awt.*;
import java.applet.*; import java.net.*;
public class star extends Applet { public void paint(Graphics g)
{ g.setColor(Color.green);
int x[ ]={80,100,150,120,150,80,0,30,0,50}; int y[ ]={10,40,40,70,120,90,120,70,40,40}; g.drawPolygon(x,y,10);
g.fillPolygon(x,y,10); }
} /*<applet code="star.class"width=500 height=500></applet>*/
Program 14
Program: WAP in java to implement the use of I/O Streams.
import java.io.*;
class First {
public static InputStreamReader input=new InputStreamReader(System.in); public static BufferedReader keyboardInput=new BufferedReader(input); static PrintWriter screenOutput=new PrintWriter(System.out,true);
public static void main(String[] args)throws IOException {
String name; screenOutput.print("What is your name?"); screenOutput.flush();
name=keyboardInput.readLine(); screenOutput.print("\nHello"+name);
screenOutput.println("java is based on independent platform"); } }
Program 15
Program: WAP in java to implement swing program.
import javax.swing.*; class program extends JFrame
{ program()
{ setTitle("first form"); setSize(300,300);
setVisible(true); }
} class form {
public static void main (String args[ ]) {
program p=new program(); } }