0% found this document useful (0 votes)
60 views7 pages

Wppgms

The document discusses different Java concepts including method overloading, method overriding, package implementation, interface implementation, and exception handling.

Uploaded by

dilipkumarke
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views7 pages

Wppgms

The document discusses different Java concepts including method overloading, method overriding, package implementation, interface implementation, and exception handling.

Uploaded by

dilipkumarke
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

METHOD OVERLOADING

class shape { int ht,wd,side; long rad; shape(int a) { side=a; } shape(int h,int w) { ht=h; wd=w; } shape(long r) { rad=r; } void show_sd () { System.out.println("Side="+side); } void show_ht() { System.out.println("Height="+ht+" Width="+wd); } void show_rd() { System.out.println("Radius="+rad); } void geo(int a) { int area=a*a; System.out.println("Area of Square="+area); } void geo(int l, int b) { int area=l*b; System.out.println("Area of Rectangle="+area); } void geo(double r)

{ double area=3.14*r*r; System.out.println("Area of Circle="+area); } } class overload { public static void main(String ar[]) { shape s1=new shape(10); shape s2=new shape(20,30); shape s3=new shape(40l); s1.show_sd(); s2.show_ht(); s3.show_rd(); s1.geo(5); s2.geo(4,5); s3.geo(1.75); } }
METHOD OVERRIDING

class circle { double r; circle() { r=10; } void getarea() { double a=3.14*r*r; System.out.println("Area of a circle:"+a); } } class cylinder extends circle { double r,h; cylinder(double rad,double height) {

r=rad; h=height; } void getarea() { double a=3.14*r*r*h; System.out.println("Area of a cylinder:"+a); } } class findarea { public static void main(String args[]) { circle c=new circle(); cylinder l=new cylinder(5,10); circle ref; ref = c; ref.getarea(); ref = l; ref.getarea(); } }

PACKAGE IMPLEMENTATION

package student; import java.io.*; public class s1 { String name; int ro,m1,m2,m3,tot; double avg; public s1(String n,int r) { name=n; ro=r; } public double marks(int a,int b,int c) { m1=a; m2=b;

m3=c; tot=m1+m2+m3; avg=(m1+m2+m3)/3; System.out.println("Name:"+name); System.out.println("Roll No:"+ro); System.out.println("Total Marks:"+tot); System.out.println("Average="+avg); return avg; } } import student.*; import java.io.*; class s2 { public static void main(String args[]) { s1 ob=new s1("hari",101); ob.marks(78,80,86); } }

INTERFACE IMPLEMENTATION

interface shape2d { void getarea(int l,int b); void getvolume(int l,int b); } interface shape3d { void getarea(int l,int b,int h); void getvolume(int i,int b,int h); } class example implements shape2d,shape3d { public void getarea(int l,int b,int h) { System.out.println("Area of Cuboid:"+(l*b*h)); }

public void getvolume(int l,int b,int h) { System.out.println("Volume of Cuboid:"+(2*(l+b+h))); } public void getarea(int l,int b) { System.out.println("Area of Rectangle:"+(l*b)); } public void getvolume(int l,int b) { System.out.println("Volume of Rectangle:"+(2*(l+b))); } } class result { public static void main(String ar[]) { example ob=new example(); ob.getarea(10,5); ob.getvolume(10,5); ob.getarea(2,4,5); ob.getvolume(2,4,5); } }
EXCEPTION HANDLING

import java.lang.Exception; import java.io.*; class myex extends Exception { myex(String message) { super(message); } } class vehicle { public static void main(String args[])throws IOException { int age=0;

DataInputStream in=new DataInputStream(System.in); try { System.out.println("Enter the age"); age=Integer.parseInt(in.readLine()); if((age>18) && (age<50)) System.out.println("Licence Allowed..."); else throw new myex("Not Permitted!!!"); } catch(NumberFormatException e) { System.out.println("Invalid Input!!!"); } catch(myex e) { System.out.println("Age limit Exception!!!"); System.out.println(e.getMessage()); } } }

You might also like