Vous êtes sur la page 1sur 21

Ex No : Date :

SIMPLE INHERITANCE

AIM: To Write a Java Program Working With Simple Inheritance.

ALGORITHM: 1. Create a Class Named As C1 With Two Variable l & b. 2. Extends the Class C1 in New Class C2 And Create a Constructor c2(int,int). 3. In c2(int,int) Constructor Multiply the Values. 4. In Main Method Read Two Values And Pass The Values to c2(l,b). 5. Display the Result. 6. Stop the Program.

PROGRAM: import java.io.*; class c1 { int l,b; } class c2 extends c1 { c2(int x, int y) { l=x; b=y; System.out.println("\n The Volume is : "+ (l*b)); } } class inhr { public static void main(String args[]) throws IOException { int len,brth; InputStreamReader r = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(r); System.out.print("\n Enter the Value of Length : "); len=Integer.parseInt(br.readLine()); System.out.print("\n Enter the Value of Berth : "); brth=Integer.parseInt(br.readLine()); c2 c=new c2(len,brth); } } OUTPUT: D: \Java>javac inhr.java D:\Java>java inhr Enter the Value of Length : 20 Enter the Value of Berth : 35 The Volume is : 700 D:\Java>

RESULT:

Thus the Above Simple Inheritance Program has been Executed successfully and Output Verified. Ex No : Date :

MULTIPLE INHERITANCE

AIM: To Write a Java Program Working With Multiple Inheritance.

ALGORITHM: 1. Create a Class Named As stud With Two Methods getno(int) & putno ( ). 2. Extends the Class stud in New Class test And Create a Methods getmrk(float,float) & putmrk( ). 3. Create An Interface sports Declare putwt( ) Method 4. Create Another Class result Extends test And Implements sports. 5. In result Call the Methods And Define the Method putwt( ). 6. In Main Method Read Create Object For result And Call the Methods getno(1234),getmrk(67.6,87.5) & disp(). 7. Display the Result. 8. Stop the Program.

PROGRAM: class stud { int rno; void getno(int n) { rno = n; } void putno() { System.out.println("Roll No : " + rno); } } class test extends stud { float p1,p2; void getmrk(float m1, float m2) { p1 = m1; p2 = m2; } void putmrk() { System.out.println("Marks obtained "); System.out.println("Part1 = " +p1); System.out.println("Part2 = " +p2); } } interface sports { float spwt = 6.0f; void putwt(); } class results extends test implements sports { float tot; public void putwt() { System.out.println("Sports wt = " +spwt); } void disp() {

tot = p1 + p2 + spwt; putno(); putmrk(); putwt(); System.out.println("Total Marks = " +tot); } } class multiple { public static void main(String aa[]) { results std = new results(); System.out.println("Pass the Values As No=1234 MARKS=67.6,87.5 Weight=6.0 "); std.getno(1234); std.getmrk(67.6f,87.5f); std.disp(); } } OUTPUT: D:\Java >javac multiple.java D:\Java >java multiple Pass the Values As No=1234 MARKS=67.6, 87.5 Weight=6.0 Roll No : 1234 Marks obtained Part1 = 67.6 Part2 = 87.5 Sports wt = 6.0 Total Marks = 161.1 D:\Java >

RESULT: Thus the Above Multiple Inheritance Program has been Executed successfully and Output Verified.

Ex No : Date :

MULTILEVEL INHERITANCE

AIM: To Write a Java Program Working With Multilevel Inheritance.

ALGORITHM: 1. Create a Class Named As a With ipo() Method And Get the Value. 2. Extends the Class a in New Class b And Create a Method ftoc( ) And Call the Method ipo( ). 3. Create Another Class c Extends Class b And Create Method disp( ). 4. In disp( ) Call the Method ftoc( ). 5. In Main Method Create An Object For c As c1 And Call the Method disp( ). 6. Display the Result. 7. Stop the Program.

PROGRAM: import java.io.*; class a { int po; void ipo() throws IOException { InputStreamReader r = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(r); po=Integer.parseInt(br.readLine()); } } class b extends a { void ftoc() throws IOException { ipo(); double kil=po*0.4536; System.out.println("\n Kilogram : "+kil); } } class c extends b { void disp() throws IOException { System.out.println("\n -: Pound To Kilogram Conversion :-\n"); System.out.print("\n Enter Pound : "); ftoc(); } } class mulinhr { public static void main(String args[]) throws IOException { c c1=new c(); c1.disp(); } }

OUTPUT: D:\ Java >javac mulinhr.java D:\ Java>java mulinhr -: Pound To Kilogram Conversion :Enter Pound : 5 Kilogram : 2.268 D:\Java>

RESULT: Thus the Above Multilevel Inheritance Program has been Executed Successfully And Output Verified.

Ex No : Date :

USING THIS KEY

AIM: To Write a Java Program Using the Keyword this.

ALGORITHM: 1. Create a Class Named As Rectangle With Two Constructors Over Loading. 2. Rectangle(int,width) & Rectangle(int) And rectarea( ) Method . 3. In rectarea( ) Method Multiply the Values Length & Width And Return area. 4. In Main Method Create An Object For Rectangle a1(10,5) And . 5. Display the Result. 6. Stop the Program.

PROGRAM: import java.io.*; class Rectangle { int length; int width; Rectangle(int length,int width) { this.length=length; this.width=width; } Rectangle(int length) { this.length=length; this.width=5; } int rectarea() { int area; area=length*width; return area; } } class thiskey { public static void main(String args[]) { Rectangle a1=new Rectangle(10,5); System.out.println(" Passing Value (10,5) : "+a1.rectarea()); Rectangle a2=new Rectangle(20); System.out.println(" Passing Value (20) : "+a2.rectarea()); } }

OUTPUT: D:\Java>javac thiskey.java D:\Java >java thiskey Passing Value (10,5) : 50 Passing Value (20) : 100 D:\Java >

RESULT: Thus the Above Program Using This Keyword has been Executed Successfully And Output Verified.

Ex No : Date :

POLYMORPHISM CONCEPTS

AIM: To Write a Java Program Using the Concept of Polymorphism.

ALGORITHM: 1. Create a Class Named As squr With cal(int) Method. 2. cal(int) Method Square the Values. 3. Create Another Class squra Extends squr And Overrides cal(int) Method By Multiply the Values with 4. 4. In class squra, Another Method cal(int,int) And Calculate l*b. 5. In Main Method Create An Object For squra s1 & s2 And Call the Methods s1.cal(10) And s2.cal(10,5). 6. Display the Result. 7. Stop the Program.

PROGRAM: class squr { int v; void cal(int a) { v=a*a; System.out.println("Area of Square : "+v); } } class squra extends squr { void cal(int a) { super.cal(a); v=4*a; System.out.println("Perimeter of Square : "+v); } void cal(int l,int b) { v=l*b; System.out.println("Area of Rectangle : "+v); } } class overrid { public static void main(String args[]) { squra s1=new squra(); s1.cal(10); squra s2=new squra(); s2.cal(10,5); } }

OUTPUT: D:\Java >javac overrid.java D:\Java >java overrid Area of Square : 100 Perimeter of Square : 40 Area of Rectangle : 50 D:\Java >

RESULT:

Thus the Above Concept of Polymorphism Program has been Executed Successfully And Output Verified.

Ex No : Date :

PACKAGE

AIM: To Write a Java Program Working With Package And Imports the Package Classes To Outside of the Classes.

ALGORITHM: 1. Create a Package Named As student. 2. In student Create a Class Named as vh With Method Called in( ). 3. In in( ) Method Reads the Values. 4. .Create Another Class Named as stud And Imports the Package student Store the Class Outside of the Package. 5. In stud Class Create An Object For vh As v And Call the Method in( ). 6. Display the Values. 7. Stop the Program.

PROGRAM:

FILE: student >vh.java


package student; import java.io.*; public class vh { InputStreamReader r = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(r); public String name,no; public void in() throws IOException { System.out.print("\n Enter the Student Name : "); name = br.readLine(); System.out.print("\n Enter the VHC No : "); no = br.readLine(); } }

FILE: stud.java import student.vh; import java.io.*; public class stud { public static void main(String arg[]) throws IOException { vh v = new vh(); v.in(); System.out.println("\n NAME : "+ v.name); System.out.println("\n VHC No : "+ v.no); } }

OUTPUT:

D:\Java>cd student D:\Java\student>javac vh.java D:\Java\student>cd.. D:\Java>javac stud.java D:\Java>java stud Enter the Student Name : Senthamizh Enter the VHC No : VHC177 NAME : Senthamizh

VHC No : VHC177 D:\Java>

RESULT: Thus the Above Package Program has been Executed Successfully And Output Verified. Ex No : 49 Date : 31.08.11

Creation of jar (java archive) file

*AIM: To Write a Java Program To Create User Specific Creation of jar file System.

ALGORITHM: 1. Start the program 2. Create a Another Class to other class used in java program 3. Use for cration jar (java archive) file 4. In Main Method Create the Object t And Call the Method display( ). 5. Display the Result. 6. Stop the Program.

PROGRAM: import java.io.*; class ill extends Exception { ill() { System.out.println("\n Exception: Illegal Marks Exception "); } } class fai extends Exception { fai() { System.out.println("\n Exception: Fail Marks Exception "); } } class Test { int mark; public Test(int m) { mark = m; } void display() throws ill,fai { if((mark<0) || (mark>100)) throw new ill(); if(mark<40) throw new fai(); System.out.println("\n Mark="+mark); } } public class uspex { public static void main(String args[]) throws IOException { int m; InputStreamReader r = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(r); System.out.println("\n My Exception "); System.out.print("\n Enter Mark : "); m=Integer.parseInt(br.readLine()); Test t = new Test(m); try { t.display(); } catch(ill il) { System.out.println("\n Illegal Mark Exception Caughted");

} catch(fai fa) { System.out.println("\n Fail Mark Exception Caughted"); } } } OUTPUT: D:\Java >javac uspex.java D:\Java >java uspex My Exception Enter Mark : 10 Exception: Fail Marks Exception Fail Mark Exception Caughted D:\Java >java uspex My Exception Enter Mark : 100 Mark=100 D:\Java >java uspex My Exception Enter Mark : -10 Exception: Illegal Marks Exception Illegal Mark Exception Caughted D:\Java >java uspex My Exception Enter Mark : 102 Exception: Illegal Marks Exception Illegal Mark Exception Caughted D:\Java >

RESULT:

Thus the Above User Specific Exception Program has been Executed Successfully and Output Verified.

Vous aimerez peut-être aussi