Monday, December 30, 2019

Exercise – 9 (User Defined Exception)

Exercise – 9 (User Defined Exception)

a). Write a JAVA program for creation of illustrating throw


public class TestThrow1
{  
    void validate()
    {  
try
{
                System.out.println("Inside of the try block");
throw new ArithmeticException("Not  valid,throwing Exception");
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
    }  
   public static void main(String args[])
   {  
      TestThrow1 t=new TestThrow1();
t.validate();
   }  
}  

b). Write a JAVA program for creation of Illustrating finally

class TestFinallyBlock
{  
  public static void main(String args[])
  {  
  try
  {  
   int data=25/5;  
   System.out.println(data);  
   }  
  catch(NullPointerException e)
  {
System.out.println(e);
  }  
  finally
  {
System.out.println("finally block is defaultly  executed");
   }  
  System.out.println("rest of the code...");  
 }  


c). Write a JAVA program for creation of Java Built-in Exceptions



import java.util.Scanner;
class Demo
{
  public static void main(String args[])
{
ArrayIndex_Demo  obj=new ArrayIndex_Demo();
obj.display();
}
}
class ArrayIndex_Demo
        void display()
        {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the element you want to insert:");
        int n=sc.nextInt();
        System.out.print("Enter the index of the array:");
        try
       
            int a[] = new int[5]; 
            int i=sc.nextInt();
            a[i]=n;
       
        catch (ArrayIndexOutOfBoundsException e)
       
            System.out.println("Array Index is Out Of Bounds"); 
        }
        } 

d).Write a JAVA program for creation of User Defined Exception


import java.lang.Exception;
class Myexception extends Exception 
{
private static double bal[]={10000.00,12000.00,5600.50,999.00,1100.55};
Myexception(String str)
{
super(str);
}
public static void main(String args[])
{
try
{
for(int i=0;i<=5;i++)
{
System.out.println(bal[i]);
if(bal[i]<1000)
{
Myexception me=new Myexception("This is user defined Error.");
throw me;
}
}
}
catch(Myexception me)
{
me.printStackTrace();
}
}
}

Monday, December 23, 2019

Exercise – 8 (Runtime Polymorphism)

Exercise – 8 (Runtime Polymorphism) 

a). Write a JAVA program that implements Runtime polymorphism 


import java.util.Scanner;
class One
{
int a,b;
void sum()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter  a and b values:");
a=s.nextInt();
b=s.nextInt();
int sum=a+b;
System.out.println("Sum of two numbers in the class 'One' is:"+sum);

}

}
class Two extends One
{
int a=90,b=67;
int add;
void sum()
{
add=a+b;
System.out.println("Sum of the numbers in the class 'Two' is:"+add);
}

}
class Poly
{
public static void main(String args[])
{
One obj=new One();
obj.sum();
One t=new Two();
t.sum();

}

}


b). Write a Case study on run time polymorphism, inheritance that implements in above problem



Polymorphism:-
            Polymorphism is the process of performing the single task in different ways. Polymorphism is the Greek word . In which  ‘poly‘  means many and   morphs  means  forms. It means  many forms .
Polymorphism is classified into two types they are-
1.      Static  or Compile time Polymorphism
2.      Dynamic  or Run time  Polymorphism
Runtime Polymorphism:-
            Runtime Polymorphism is also called as Dynamic polymorphism. It is the process of calling the Overriding methods in runtime rather than In compile time.
In  this process  the  overriding methods are called with reference of super class.
Explanation:-
            In the above program we use single inheritance two achieve the runtime polymorphism in our example.
We use two classes here. We call the base class with the reference of  the super class.
 We create different objects to the two classes and call the both base   class   as well as super class with reference of super class only.
So, Runtime polymorphism is achieved here.
           
           

Exercise - 7 (Exception)

Exercise - 7 (Exception) 

a).Write a JAVA program that describes exception handling mechanism



import java.util.Scanner;
class Exception_handle
{
public static void main(String args[])
{
Exception ex=new Exception();
}
}
class Exception
{
Exception()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter a and b values:");
int a=s.nextInt();
int b=s.nextInt();
try
{
int c=a/b;
System.out.print(c);
}
catch(ArithmeticException ae)
{
System.out.println("Arthmatic Exception occured");
}
}

}

b).Write a JAVA program Illustrating Multiple catch clauses 



import java.util.Scanner;
class Multiple_catch
{
public static void main(String args[])
{
Handle h=new Handle();

}
}
class Handle
{
Handle()
{
Scanner sc=new Scanner(System.in);
String str="The world is runs throgh software industry";
String str2=null;
System.out.print("Enter the index of 1st string where you want to retrive the charecter:");
int n1=sc.nextInt();
System.out.print("Enter the index of 2st string where you want to retrive the charecter:");
int n2=sc.nextInt();
try
{

char c=str.charAt(n1);
System.out.println("Charecter at the index of "+n1+" 1st string is:"+c);
char ch=str2.charAt(n2);
System.out.println(ch);

}
catch(StringIndexOutOfBoundsException sib)
{
System.out.println("String index out of range");
}
catch(NullPointerException np)
{
System.out.println("Null pointer exception");
}
}

}

Thursday, December 19, 2019

Exercise - 6 (Inheritance - Continued)

Exercise - 6 (Inheritance - Continued)


a). Write a JAVA program give example for “super” keyword.

//Write a program to implement 'super' keyword

class One
{
protected int a;
One(int i)
{
this.a=i;
}
void display()
{
System.out.println("Super class method:a="+a);
}
}
class Two extends One
{
protected int a;
        Two(int i,int j)
  {
  super(i);
    a=j;
  }
  void display()
  {
  super.display();
  System.out.println("Sub class method:a="+a);
 
  }
}
class Super1
{
  public static void main(String args[])
  {
  Two t=new Two(9,6);
  t.display();

}
}

b). Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?

mport java.util.Scanner;
interface Base1
{
final double pie=3.14;
        void area();
}
class Base2
{
protected int r;
public void getdata()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter the radius of the Circle:");
r=s.nextInt();
}
}
class Derived extends Base2 implements Base1
{
double area;
public void area()
{
area=pie*r*r;
System.out.println("Area of the Circle is:"+area);
}
}
class Inheritance
{
public static void main(String args[])
{
Derived d=new Derived();
                d.getdata();
d.area();
}
}

Multiple inheritance is achieved through interfaces

Exercise - 5 (Inheritance)

Exercise - 5 (Inheritance)

a). Write a JAVA program to implement Single Inheritance

import java.util.Scanner;
class Add
{
  protected int a,b,sum;
  void add()
  {
    Scanner s=new Scanner(System.in);
    System.out.println("Enter two values");
    a=s.nextInt();
    b=s.nextInt();
    sum=a+b;
    System.out.println("The sum of the given numbers is:"+sum);
  }
}
class Mul extends Add
{
  int mul;
  void mul()
  {
    mul=a*b;
    System.out.println("The product of the given numbers is:"+mul);
  }
}
class Calculation
{
  public static void main(String args[])
  {
    Mul m=new Mul();
    m.add();
    m.mul(); 
  }
}

b). Write a JAVA program to implement multi level Inheritance

import java.util.Scanner;
class calculator
{
  public static void  main(String args[])
  {
  System.out.println("Enter the values:");
  Scanner s=new Scanner(System.in);
  int a=s.nextInt();
  int b=s.nextInt();
  Product pr=new Product();
  pr.Addition(a,b);
  pr.Substraction();
  pr.Mul();
  }
}
class Add
{
  protected int p,q;
  public void Addition(int x,int y)
  {
    p=x;
    q=y;
  int r=p+q;
  System.out.println("Inside of the class 'Addition' ......");
  System.out.println("The sum is:"+r);
}
}
class Sub extends Add
{
int diff;
public void Substraction()
{

  int diff=p-q;
  System.out.println("Inside of the class Sub.........");
  System.out.println("Difference is:"+diff);
}
}
class Product extends Sub
{
int prod;
public void  Mul()
{
  int prod=p*q;
  System.out.println("Inside of the class Product.......");
  System.out.println("Product is:"+prod);
}
}

c). Write a java program for abstract class to find areas of different shapes

import java.util.Scanner;
abstract class Areas
{
  public void display()
  {
  System.out.println("Inside of the abstract class....");
  }
  abstract void area();
}
class Rectangle extends Areas
{
  int base=10;
  int height=20;
  public void area()
  {
  int area=base*height;
  System.out.println("Area of the rectangle is:"+area);
  }
}
class Square extends Areas
{
  int base=15;
  public void area()
  {
  int area1=base*base;
  System.out.println("Area of the Square is:"+area1);
  }
}
class Triangle extends Areas
{
  int base=5;
  int height=15;
  public void area()
  {
  int area2=(base*height)/2;
  System.out.println("Area of the Triangle is:"+area2);
  }
}
class Geometry
{
  public static void main(String args[])
  {
  Rectangle r=new Rectangle();
  Square s=new Square();
  Triangle t=new Triangle();
  r.display();
  r.area();
  s.area();
  t.area();
  }
}

Sunday, December 15, 2019

JAVA PROGRAMMING ASSIGNMENT 1 QUESTIONS


SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
JAVA PROGRAMMING ASSIGNMENT 1 QUESTIONS
  1. List and explain Java buzzwords. Which factors are making Java famous language?
  2. Write the problems associated with procedure languages. Elaborate how object oriented languages overcomes the problems of procedural languages.
  3. Explain the architecture of Java Virtual Machine with a neat diagram.
  4. Give the naming conventions in Java.
  5. What are the different primitive data types in java? Give their sizes in bits. How they are different from reference data types?
  6. How to perform type casting in java? Differentiate it from primitive type conversion with an example program.
  7. With an example program explain the concept of classes and nested classes in java.
  8. Write a program that shows an Employee class which contains various methods for accessing employee’s personal information and methods for paying an employee.
  9. Design a class that represents a bank account and construct the methods to

                           i).          Assign initial values
                         ii).          Deposit an amount
                       iii).          Withdraw amount after checking balance
                       iv).          Display the name and balance.
    10. Write about command line arguments. Accept the input from keyboard to display Fibonacci series.



Friday, December 13, 2019

Exercise - 4 (Methods)

Exercise - 4

a). Write a JAVA program to implement constructor overloading.

class Student
{
int height=167;
  int weight=78;
  String course="Intermediate";
  int rollno=225;
  Student()
  {
  System.out.println("Inside of the First Method");
  System.out.println("Height="+height+"CM");
  System.out.println("Weight="+weight+"KG");
  System.out.println("Course with year:"+course);
  System.out.println("Roll NO:"+rollno);
  }
  Student(double marks)
  {
  System.out.println("Inside of the Second Method");
  System.out.println("Marks percentage:"+marks);
  }
}
class Biodata
{
  public static void main(String args[])
  {
  Student s1=new Student();
  Student s2=new Student(92.56);
  }
}

b). Write a JAVA program implement method overloading.


class Details
{
  String name="Vivo-Y87";
  int storage=64;
  String cameratype="Dual Camera";
  public void performance(int ram)
{
System.out.println("Inside of the 1st Method.......");
  System.out.println("Name of the Mobile is:"+name);
  System.out.println("Internal Storage of the Mobile is:"+storage+"GB");
  System.out.println("Ram of the Mobile is:"+ram+"GB");
  System.out.println("Camera type of the Mobile is:"+cameratype);
  }
        public void performance(String os,double version)
        {
System.out.println("Inside of the 2nd Method....");
System.out.println("The os of the mobile is:"+os);
          System.out.println("Version of the mobile is:"+version);
        }
}
public class Mobile
{
public static void main(String args[])
  {
      System.out.println("The Mobile Details are:");
      Details d=new Details();
                  d.performance(8);
                  d.performance("Android",8.10);
  }
}

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS

SIR C R REDDY COLLEGE OF ENGINEERING, ELURU DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS ...