Posts

Showing posts from July, 2018

WAP that demostrates the use of throw keyword in exception handling. main() method will call a(), a() calls b(), b() calls c() and method c(), b() and a() will throw an exception. Implement this hierarchy

/* WAP that demostrates the use of throw keyword in exception handling. main() method will call a(), a() calls b(), b() calls c() and method c(), b() and a() will throw an exception. Implement this hierarchy.*/ import java.io.*; class Tk   {                       void a() throws IOException { throw new IOException("error che bhai"); }       void b() throws IOException       {            a();          System.out.println("b che");       }       void c() {        try         {             b();             System.out.println("c che");               }          catch(Exception  e)       {     ...

WAP that creates a custom exception InvalidAgeException which will be thrown when user enters age below 18.

 WAP that creates a custom exception InvalidAgeException which will be thrown when user enters age below 18. import  java.util.Scanner; class Exc {  public static void main(String []args)    {      Scanner s=new Scanner(System.in);     System.out.println(" enter your age");       int age = s.nextInt();          try               {                 if(18<age)                 throw new ArithmeticException("your age is 18+");                    else                   System.out.println("you age considered") ;                  } catch(ArithmeticException e) {    System.out.println("Exception is="+ e.getMessage()); } ...

exception handle program example in java

/*6.1. Write a program that divides two numbers. Handle all exceptions that can be generated in this program.*/ class  E {    public static void main(String []args)      {         try          {           int a=5;         System.out.println(a/0);         System.out.println("just check karva ");      }       catch(ArithmeticException  e)        {             System.out.println("exceptin is ="  +e.getMessage());       } System.out.println("print thay k nai joy"); } } ************************************* default catch class  Etry {    public static void main(String []args)      {         try          {           int a=...

Example of array declaration

/*Write a program that creates and initializes a four integer element array. Calculate and display the average of its values.*/ import java.util.Scanner; class  K    {    public static void main(String []args)    {    Scanner b=new Scanner(System.in);       int[]   arr= new int[4];       int i;      System.out.println("enter element");         for(i=0; i<4; i++)        {            arr[i]=b.nextInt();      }        int sum=0;   for(i=0; i<4; i++)        {            sum=sum+arr[i];      }    System.out.println("avrage of element"+ (sum/2)); } }                                     ...

Example of abstract class

/*WAP that create abstract class figure. Declare area method in this class. class Rectangle and Circle inherits  class Figure.Write the method in all class that prints the area of respective Figure.*/  abstract class Ab    {    abstract   void area(); } class Rectangle extends Ab    {         int l,b;      void  area()    {       System.out.println("rectengle is="+(l*b));    }  } class circule extends Ab    {         int r;      void  area()    {       System.out.println("circule is="+(3.14*r*r));    }  }    class Abdemo { public static void main()   {      Ab  s1 =new Rectangle();     Ab  s2 =new circule();          s1.area();         s2.area();...

super keyword program IN JAVA

/*2. WAP that illustrate how method can invoke a super class method. There is one supercalss A. class B extends class A and class C extends class B. Each of these classes define a getdescription() method that returns a string. That string includes description of the class plus description of each super class. Inherits each of these class and invoke the gerdescription() method. "*/ class A   {      void  getdescription()    {     System.out.println("abhi ham ginnda he");    }  }     class B extends A { void  getdescription() { super.getdescription(); System.out.println("have class b ma cho");} } class C extends B { void  getdescription() { super.getdescription(); System.out.println("have class c ma cho"); } } class SuperDemo {   public static void main(String  []args)    {      C  a=new C();   ...

Create a class Box and display the area of box using a method area(). Initialize all the variables using Constructor and demostrate Constructor Overloading.

2. Create a class Box and display the area of box using a method area(). Initialize all the variables using Constructor and demostrate Constructor Overloading.                                                                              import java.util.*; class BoxArea { double height; double width; double depth; double area() { return (height*width*depth); } BoxArea() { System.out.println("The New object is created"); } BoxArea(double h,double w,double d) { height=h; width=w; depth=d; } } class BoxAreaDemo { public static void main(String args[]) { double height=0,width=0,depth=0; Scanner in=new Scanner(System.in); BoxArea b1=new BoxArea(); System.out.println(""); System.out.println("Enter Height :"); hei...

Write a Java program that displays area of different Figures(Rectangle,Square,Triangle) using the method overloading.

/*Write a Java program that displays area of different Figures(Rectangle,Square,Triangle) using the method overloading.*/ class M_overloading {     float h,l;     int base,hight;      float area(float l)       {return(l*l);}     float area(float h,float l)      {return(h*l);}     double area(int hight, int base)       {return(0.5*hight*base);} }   class M_overloadingdemo     {        public static void main(String []args)         {            M_overloading a= new M_overloading();           double ans= a.area(2);        System.out.println("area of squer="+ ans);             ans =a.area(2,5);       System.out.println("area of rectangel="+ ans);             ans= a.area...

inheritance program in java

import  java.util.Scanner; class Office   {      Scanner s=new Scanner(System.in);        void getvalue()     {          System.out.println("super class office:-");  System.out.println("enter employee name:-");     String empname=s.nextLine();    System.out.println("enter employee number:-");     int a=s.nextInt();    System.out.println("enter employee salary:-");     int b=s.nextInt();          } }    class Teching extends  Office       { Scanner s=new Scanner(System.in); void setvalue()  { super.getvalue(); System.out.println("subclass Teching="); System.out.println("enter employee designition:-"); String designition=s.nextLine();   } } class NonTeching extends  Office       { Scanner s=new Sca...

Write a java program to calculate gross salary & net salary taking the following data

/*Write a java program to calculate gross salary & net salary taking the following data. Input:empno,empname,basic  Process:  DA=50%of basic, HRA=25%of basic, CCA=Rs240/-, PF=10%of basic, PT=Rs100/-*/ import java.util.*; class Em      {         public static void main(String   []args)        {          float DA,HRA,PF,gr_sal,net_sal;         float CCA=240f,PT=100f;                        Scanner  a= new Scanner(System.in);          System.out.println("enter employee rollno=");          int r=a.nextInt();         System.out.println("enter your basic ");         float f=a.nextFloat();        System.out.println("enter employee name=");         Stri...

Write a program to accept a line and check how many consonants and vowels are there in line

/*Write a program to accept a line and check how many consonants and vowels are there in line*/ import  java.util.Scanner; class Line    {     public static void main(String   args[])       {          int i,count=0,n=0;           Scanner a=new Scanner(System.in);           System.out.println("enter string=");           String s=a.nextLine();            int b=s.length();                                         for(i=0;i<b;i++) {    char c=s.charAt(i);                       if (c=='i' || c=='a' || c=='e' || c=='o'|| c=='u' ) {       count=count+1; } else n++;         ...

Write a program in java to find that given number or string is palindrome or not

/* Write a program to find that given number or string is palindrome or not. */ import java.util.Scanner; class Palindrome      {      public static void main(String  arg[])        { String r="";           Scanner  c = new Scanner(System.in);            System.out.println("enter your string to check your string is palindrome or not ?");            String s=c.nextLine();             int l=s.length();      int i; for (i=l-1; i>=0;i-- ) {    r=r+s.charAt(i);                     }               if (s.equals(r))                    {               System.out.println("your string is palindrome"); ...

Write a program in java using constuctor

Write a program in java using constuctor class Box    {          double length,hight,depth;             Box (double l,double h,double d)                   {                      length=l; hight=h; depth=d;                     }                 double volume()                    {              return(length*hight*depth);      }              } class BoxDemo2       {         public static void main(String  []args)            {             double ans;         ...

Write a program in java that calculate percentage marks of the student if marks of 6 subjects are given(using else-if)

Write a program that calculate percentage marks of the student if marks of 6 subjects are given(using else-if) import java.util.Scanner; class Percentage { int sub1; int sub2; int sub3; int sub4; int sub5; int sub6; String name; String roll_no; float per() { float ans = (sub1 + sub2 + sub3 + sub4 + sub5 + sub6)/6; return ans; } void display() { float ans=per(); System.out.println("Student Name :" +name); System.out.println("Student Roll_no :" +roll_no); System.out.println("Student Percentage :" +ans+"%"); if(ans>85) System.out.println("Congratulations...!!! You having 1st/Distinction class :"); else if(ans>75) System.out.println("You having 2nd class :"); else if(ans>65) System.out.println("You having pass class :"); else System.out.println("Sorry...!!! You can not clear this examination :"); } } class P...

Write a program in java to enter two numbers and perform mathematical operations on them.(switch case)"

Write a program that calculate percentage marks of the student if marks of 6 subjects are given(using else-if) import java.util.Scanner; class Calculator { int addition(int num1,int num2) { return (num1 + num2); } int subtraction(int num1,int num2) { return (num1 - num2); } int multiplication(int num1,int num2) { return (num1 * num2); } int division(int num1,int num2) { if(num2!=0) { return (num1 / num2); } else { System.out.println(" Denominator never be 'zero' !!!");     return 0; } } } class CalculatorDemo { public static void main(String args[]) { int ch,ans,num1,num2; Calculator c1=new Calculator(); Scanner s=new Scanner(System.in); System.out.println("Enter Number 1 : "); num1=s.nextInt(); System.out.println("Enter Number 2 : "); num2=s.nextInt(); System.out.println("...

1. Write a program to convert rupees to dollar. 60 rupees=1 dollar.