Vous êtes sur la page 1sur 15

History of java Java is product of sun microsystem which was introduced as OAK in 1991 later on its name is change

as java in 1995. Features of java 1 simple 2 portable 3 powreful : because of rich library means in java every field have a built in library. 4 secured: because java does not support pointer. Object oriented programming (OOP) Any language which implements following 4 points is called as object oriented programming language. 1. Encapsulation: wrapping of data in a small unit. 2. Abstraction: hiding of non essential deatails. 3. Inheritance: to use the property of one class in to another class. 4. Polymorphism: single function in different name or different parameters. There for c++ and java is a object oriented programming language.

Difference between c++ and java

NOTE:

a) int is a data type which takes 4 byte in java means it save the 4 bytes in memory for reference variable. b) float is also a data type which store the real no.s and save 4 bytes in memory. c)char is a data type which is used for character type operations.

Q1 Write a program of add two no. class add { public static void main(String args[]) { int a=10; int b=20; int c=a+b; System.out.println("Sum of two no is } } Q2 program of addition of two real no. . class sum { public static void main(String args[]) { float a=1.5f; float b=3.2f; float c=a+b; System.out.println("circumference of circle is " + c);

" + c)

} }

Q3 program of Simple interest. class simpleint { public static void main(String args[]) { int p=1001; int r=10; int t=2; float si= (p*r*t)/100f; System.out.println("simple interest is "+si); } }

Q4 Write a program for print the table of 2 using for loop; class xyz { public static void main(String args[]) { for(int i=1;i<=10;i++) { System.out.println(2*i); } } }

Q 5 Write a program for print the table of 2 using while loop;

class xyz { public static void main(String args[]) { int i=1; while(i<=10) { System.out.println(2*i); i++; } } }

Q6 Write a program for print the greater no among the three nos.

class aa { public static void main(String args[]) { int a=3;int b=10;int c=2; if(a>b&&a>c) System.out.println("a is greater"); else if (b>a&&b>c) System.out.println("b is greater"); else System.out.println("c is greater");

} }

Q7 Write a program to check the no is even or odd

class odd { public static void main(String args[]) { int no=10; if( no%2==0) System.out.println("no is even); else System.out.println(" no is odd); } }

Q8 write a program of to display pattern as. * ** *** **** *****

class star { public static void main(String args[]) { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) {

System.out.print("*"); } System.out.print("\n"); } } } Q9 write a program of to display pattern as. 1 12 123 1234 12345 class star2 { public static void main(String args[]) { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) { System.out.print(j); } System.out.print("\n"); } } } Q10 write a program of to display pattern as. ***** **** ***

** * class star2 { public static void main(String args[]) { for(int i=5;i>=1;i--) { for(int j=1;j<=i;j++) { System.out.print(*); } System.out.print("\n"); } } }

Q WRITE A PROGRAM OF SUM OF TWO VARIABLE BY TAKING THE INPUT FROM USER

class input { public static void main(String args[]) { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int c=a+b; System.out.println("sum is "+c); } }

WRITE A PROGRAM TO FIND THE AREA OF RECTANGLE SQUARE AND CIRCLE USING CLASS.

class area { int x,y; public void getdata(int a,int b) { x=a; y=b; } public void areaofrectangle() { int c=x*y; System.out.println("area of rectangle is "+c); } public void areaofsquare() { int s=x*x; System.out.println("area of square is "+s); } public void areaofcircle() { float r=2*3.14f*x; System.out.println("area of circle is "+r); } public static void main(String args[]) { area p=new area(); p.getdata(Integer.parseInt(args[0]),Integer.parseInt(args[1])); p.areaofrectangle(); p.areaofsquare(); p.areaofcircle(); }

Applet Applets are the small program written in java and executed in browser.

Life cycle of applet

1.public void start() it is also used for initili. Whenever user click on refresh. 2 public void paint() it is used for drawing 3 Public void stop() This method is execudet whenever user click on stop button 4 Public void destroy() This method is executed whenever user close the browser.

AWT: abstract windowing toolkit It is a package that contain classes and interfaces for creating GUI (graphical user interface)application in java.

To make the applet Step 1 to create applet open notepade and write

import java.awt.*; import java.applet.Applet; public class MyApplet extends Applet { public void paint(Graphics g) { g.drawString("wellcome in java",100,100); } }

2 save it as Myapplet.java 3.compile it 4 create an HTML (hyper text markup language) file a) open notepad and write <html> <applet code="amit.class"width=300 height=300> </applet> </html>

b) save it with any name followed by .html in bin 5.run as appletviewer anyname.html

Drawing of different shapes in applet import java.awt.*; import java.applet.Applet; public class amit extends Applet { public void paint(Graphics f) { setBackground(Color.yellow); f.drawString("wellcome in java",100,100); f.drawRect(10,10,200,100); f.drawLine(20,20,200,200); f.drawRoundRect(5,5,50,50,20,30); f.drawArc(40,40,300,300,50,60); setForeground(Color.green); } }

Constuctors It is used for initialization of user define data type in various forms. -constructor is a member function. -same name as of a class -executed automatically whenever an object is created(new). -they are executed only once in the life time of an object. -does not have any return type not even void. -there are no destructor in java.

Q.a) example of various form of constructor. class point { int x_co; int y_co; public point() { x_co=0; y_co=0; } public point(int x) { x_co=x; y_co=0; } public point (int x,int y) { x_co=x; y_co=y; } public void showdata() { System.out.println("x_co= "+x_co); System.out.println("y_co= "+y_co); } public static void main(String args[]) { point p=new point(); point p1=new point(10); point p2=new point(1,2); p.showdata(); p1.showdata(); p2.showdata(); } }

Inheritance -passing of properties from one class to another class -properties include member data and member function -the class that gives the properties is called base class and class that receive the properties is called child or subclass. -to inharit the property of base class extends keyword is used for example syntax is ----class<derived class>extends <base class> Benefit -reusability -overriding0 Types -single -multiple -multilable -hybride Hierchical Java does not support multiple inheritance.one child class can have only one base class. Java does not support operator overloading and friend classes & friend function. Function overloading: Defining function again with- -same name - different argument -same class Fuctioin overriding : Defining function again with - -same name -same arguments - different class(base and derive).

From the view of derived class there are three categories of methods in the base class 1. Constructors To call the constructor of base class super keyword is used. Syntax is : Super(<arguments>); Note: only derive class constructor can call constructor of base class in first line. 2. Overriding function To call overriding function of base class syntax is Super.functionname(<arguments>); 3. Remaining /normal Function name(<arguments>);

Qb) example of inheritance -----in this example the base class is point which is written above and derive class is written belove----

class circle extends point { float radius; public circle() { super(); radius=0f; } public circle(int x,int y) { super(x,y); radius=0f; } public circle(float r) {

super (); radius =r; } public circle (int x,int y,float r) { super(x,y); radius=r; } public void showdata() { super.showdata(); System.out.println("radius is "+radius); } public static void main(String args[]) { circle c1=new circle(10,20,30f); c1.showdata(); } }

Vous aimerez peut-être aussi