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);
  }
}

Exercise - 3 (Class, Objects)

Exercise - 3 (Class, Objects)

a). Write a JAVA program to implement class mechanism. – Create a class, methods and
invoke them inside main method.

class Student
{
  String name="Raju";
  String _class="B.Tech 1st year";
  int rollno=178;
  public void display()
  {
    System.out.println("Name of the student is:"+name);
    System.out.println("Present qualification of the student is:"+_class);
    System.out.println("Roll number of the student is:"+rollno);
  }

}
class Details
{
  public static void main(String args[])
  {
    Student s1=new Student();
    s1.display();
  }
}


b). Write a JAVA program to implement constructor.

import java.util.Scanner;
class Sports_person
{
int height=164;
  int weight=62;
  Sports_person()
  {
                System.out.print("Enter name of the country:");
                Scanner sc=new Scanner(System.in);
                String country=sc.nextLine();
  System.out.println("Height of the sports person is:"+height+"CM");
  System.out.println("Weight of the sports person is:"+weight+"KG");
System.out.println("He/She belongs to the country:"+country);
 
  }
}
class Olimpics
{
  public static void main(String args[])
  {
  Sports_person sp=new Sports_person();
  }

}

Thursday, December 5, 2019

Exercise - 2 (Operations, Expressions, Control-flow, Strings)

Exercise - 2 (Operations, Expressions, Control-flow, Strings)


a). Write a JAVA program to search for an element in a given list of elements using binary
search mechanism.

import java.util.Scanner;
import java.io.*;
class Binarys
{
  public static void main(String args[])
   {
      int c,first,last,middle,n,search,array[];
      boolean status=false;
       Scanner s=new Scanner(System.in);
       System.out.println("Enter number of elements:");
       n=s.nextInt();
       array=new int[n];
        System.out.println("Enter"+n+"integer:");
      for(c=0;c<n;c++) 
        array[c]=s.nextInt();
     for (int i = 0; i < array.length; i++)
         {
            for (int j = i + 1; j < array.length; j++)
            {
                if (array[i] > array[j])
                {
                  int  temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
      }
        System.out.println("Enter value to find:");
        search=s.nextInt();
        first=0;
        last=n-1;
        middle=(first+last)/2;
    for(int i=0;i<n;i++)
     {
        if(first<=last)
         {
             if(array[middle]<search)
                  first=middle+1;
             else if(array[middle]==search)
              {
                  status=true;
              }
           else
            {
                last=middle-1;
            }
                 middle=(first+last)/2;
         }
    }
       if(status==true)
       {
           System.out.println(search+"found at location"+(middle+1));
       }
     else
         System.out.println(search+"is not found in the list");
   }
}
       
b). Write a JAVA program to sort for an element in a given list of elements using bubble sort.

import java.util.Scanner;
public class Bubblesort
{
public static void main(String[] args)
{
        Scanner s=new Scanner(System.in);
        System.out.println("enter the size of the array:");
        int size=s.nextInt();
        System.out.println("enter the values into the array:");
        int arr[]=new int[size];
        for(int i=0;i<size;i++)
        {
            arr[i]=s.nextInt();
        }
        sorting(arr);
    }
    public static void sorting(int arr[])
    {
        int n=arr.length;
        int temp=0;
        for(int i=0;i<n;i++)
        {
          for(int j=1;j<(n-i);j++)
          {
          if(arr[j-1]>arr[j])
          {
            temp=arr[j-1];
            arr[j-1]=arr[j];
            arr[j]=temp;
          }
          }
        }
        System.out.println("the sorted arraty is:");
        for(int i=0;i<arr.length;i++)
        {
          System.out.print(arr[i]+"\t");
        }
    }
}


(c). Write a JAVA program to sort for an element in a given list of elements using merge sort.

import java.util.Scanner;
class merge
{
public static void main(String args[])
{
  Scanner s=new Scanner(System.in);
  System.out.println("enter size of the array:");
  int size=s.nextInt();
  int arr[]=new int[size];
  System.out.println("enter elements into the array:");
  for(int i=0;i<size;i++)
  {
  arr[i]=s.nextInt();
  }
int start=0;
int end=size-1;
sort(arr,start,end);
System.out.println("the sorted array is:");
for(int j=0;j<size;j++)
{
  System.out.print(arr[j]+"\t");
}
}
public static void sort(int arr[],int start,int end)
{
  if(start<end)
  {
  int middle=(start+end)/2;
  sort(arr,start,middle);
  sort(arr,middle+1,end);
  merge(arr,start,middle,end);
  }
}
public static void merge(int arr[],int start,int middle,int end)
{
  int n1 = middle -start + 1;
  int n2 = end - middle;
  int L[] = new int [n1];
  int R[] = new int [n2];
  for (int i=0; i<n1; ++i)
  {
      L[i] = arr[start + i];
    }
    for (int j=0; j<n2; ++j)
    {
      R[j] = arr[middle + 1+ j];
    }
    int i = 0, j = 0;
    int k = start;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
          arr[k] = L[i];
            i++;
        }
      else
      {
        arr[k] = R[j];
        j++;
        }
        k++;
    }
    while (i < n1)
    {
      arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2)
    {
      arr[k] = R[j];
      j++;
      k++;
    }

}
}



(d) Write a JAVA program using StringBufferto delete, remove character.

import java.util.Scanner;
import java.lang.*;
class stringbuffer
{
  public static void main(String args[])
  {
    Scanner s=new Scanner(System.in);
    System.out.println("enter a string you like:");
    String str=s.nextLine();
    StringBuffer b=new StringBuffer(str);
    System.out.println("enter the index you want to delete from:");
    int i1=s.nextInt();
    System.out.println("to:");
    int i2=s.nextInt();
    b.delete(i1,i2);
    System.out.println("after deletion the new buffer is:"+b);
  }
}


Exercise - 1 (Basics)

Exercise - 1 (Basics)

a). Write a JAVA program to display default value of all primitive data type of JAVA

import java.io.*;
class Primitivevalues
{
static boolean B1;
static byte T1;
static int I1;
static float F1;
static double D1;
static char C1;
public static void main(String args[])
{

System.out.println("static  variable  Default values for BYTE:"+T1);
System.out.println("static  variable  Default values for INT:"+I1);
System.out.println("static  variable  Default values for FLOAT:"+F1);
System.out.println("static  variable  Default values for DOUBLE:"+D1);
System.out.println("static  variable  Default values for CHAR:"+C1);
System.out.println("static  variable  Default values for BOOLEAN:"+B1);
}
}

b). Write a java program that display the roots of a quadratic equation ax2+bx=0. Calculate
the discriminate D and basing on value of D, describe the nature of root.



import java.io.*;
import static java.lang.Math.sqrt;
import java.util.*;
class Roots
{

  void m1(int a,int b,int c)
 {
  double value=(Math.pow(b,2.0)-4*a*c);
   double discriminant=(double)Math.sqrt(value);
  if(value>0)
  {
   System.out.println("Roots are real numbers");
   double root1=(discriminant-b)/(2*a);
      double root2=(-b-discriminant)/(2*a);
      System.out.println("FirstRoot"+root1+"\nSecond Root"+root2);
  }
  else if(value==0)
  {
   double root1=(discriminant-b)/(2*a);
   System.out.println("Polynomial has one Root"+root1);
  }
  if(value<0)
  {
   System.out.println("Roots are imaginary numbers");
   System.out.println((-b+"i"+"+"+(-1*value))+"/"+(2*a));
   System.out.println((-b+"i"+"-"+(-1*value))+"/"+(2*a));
  }


 }
 public static void main(String [] args)
  {
   int a,b,c;
   System.out.print("Enter a,b,c values:");
   Scanner br=new Scanner(System.in);
    a=br.nextInt();
    b=br.nextInt();
    c=br.nextInt();
    new Roots().m1(a,b,c);
  }
}

c). Five Bikers Compete in a race such that they drive at a constant speed which may or may
not be the same as the other. To qualify the race, the speed of a racer must be more than the
average speed of all 5 racers. Take as input the speed of each racer and print back the speed
of qualifying racers.

import java.io.*;
import java.util.*;
class Race
{
public static void main(String [] args)
{
int R[]=new int[20];
float avg;
int sum=0;
System.out.println("Enter Speed of 5 members");
Scanner sc=new Scanner(System.in);
for(int j=1;j<=5;j++)
{
R[j]=sc.nextInt();
sum+=R[j];
}
avg=sum/5;
for(int i=1;i<=5;i++)
{
if(avg<R[i])
{
System.out.println("QualifiedRaceris:"+i+"with value="+R[i]);
}
}
}
}


d) Write a case study on public static void main(250 words)

public static void main(String[] args)
Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args). You can only change the name of String array argument, for example you can change args to myStringArgs.
Also String array argument can be written as String... args or String args[].

public

This is the access modifier of the main method. It has to be public so that java runtime can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public. Let’s see what happens if we define the main method as non-public.

public class Test 
{
static void main(String[] args)
{
     System.out.println("Hello World");   
}
}

OUTPUT:-
$ javac Test.java
$ java Test
Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)

static

When java runtime starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present. Let’s see what happens when we remove static from java main method.
public class Test {
public void main(String[] args){
            System.out.println("Hello World");   
}
}
OUTPUT:-
$ javac Test.java
$ java Test
Error: Main method is not static in class Test, please define the main method as:
   public static void main(String[] args)

void

Java programming mandates that every method provide the return type. Java main method doesn’t return anything, that’s why it’s return type is void. This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM. If we try to return something from the main method, it will give compilation error as an unexpected return value. For example, if we have the main method like below.
public class Test {
public static void main(String[] args){    
return 0;
}
}
OUTPUT:-
$ javac Test.java
Test.java:5: error: incompatible types: unexpected return value
     return 0;          
 ^
1 error

main

This is the name of java main method. It’s fixed and when we start a java program, it looks for the main method. For example, if we have a class like below.
public class Test {
public static void mymain(String[] args)
            System.out.println("Hello World");
}
}
OUTPUT:-
$ javac Test.java
$ java Test
Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

String[] args

Java main method accepts a single argument of type String array. This is also called as java command line arguments. Let’s have a look at the example of using java command line arguments.
public class Test {
public static void main(String[] args){
for(String s : args){
            System.out.println(s);
    }  }}
Above is a simple program where we are printing the command line arguments. Let’s see how to pass command line arguments when executing above program.
OUTPUT:-
$ javac Test.java
$ java Test 1 2 3
1
2
3

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS

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