Exercise - 1:
- Write a JAVA program to display default value of all primitive data type of JAVA.
public class Demoex1 {
static Boolean val1;
static double val2;
static float val3;
static int val4;
static long val5;
static String val6;
public static void main (String [] args) {
System.out.println("Default values:");
System.out.println("Val1 = " + val1);
System.out.println("Val2 = " + val2);
System.out.println("Val3 = " + val3);
System.out.println("Val4 = " + val4);
System.out.println("Val5 = " + val5);
System.out.println("Val6 = " + val6);
}
}
- 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. util.*;
class Quadratic_qua
{
public static void main (String [] args)
{
int a, b, c;
double r1, r2, D;
Scanner s = new Scanner(System.in);
System.out.println("Given quadratic equation:ax^2 + bx + c");
System.out.print("Enter a:");
a = s.nextInt();
System.out.print("Enter b:");
b = s.nextInt();
System.out.print("Enter c:");
c = s.nextInt();
D = b * b - 4 * a * c;
if (D > 0)
{
System.out.println("Roots are real and unequal");
r1 = (- b + Math.sqrt(D))/(2*a);
r2 = (-b - Math.sqrt(D))/(2*a);
System.out.println("First root is:"+r1);
System.out.println("Second root is:"+r2);
}
else if (D == 0)
{
System.out.println("Roots are real and equal");
r1 = (-b+Math.sqrt(D))/(2*a);
System.out.println("Root:"+r1);
}
else
{
System.out.println("Roots are imaginary");
}
}
}
Exercise - 2:
- Write a JAVA program to search for an element in a given list of elements using binary search mechanism.
import java.io.*;
import java.util.Scanner;
class BinarySearchExample {
int binarySearch2a(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
return -1;
}
public static void main(String args[])
{
BinarySearchExample ob = new BinarySearchExample();
int arr[ ]=new int[5];
Scanner s = new Scanner(System.in);
System.out.println("Enter elements in sorted order:");
for (int i = 0; i < 5; i++)
arr[i] = s.nextInt();
System.out.println("Enter the search value:");
int x = s.nextInt();
int result = ob.binarySearch2a(arr, x);
if (result == -1)
System.out.println("Element is not present in array");
else
System.out.println("Element is present at "+ "index " + result);
}
}
- Write a JAVA program to sort for an element in a given list of elements using bubble sort.
import java.util.Scanner;
class Sort{
void Bubsorting(int a[],int n)
{
int temp,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are:");
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}
class bubblesort2b{
public static void main(String args[]){
int n, i,j, temp;
int a[]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of elements:");
n = s.nextInt();
System.out.println("Enter elements:");
for (i = 0; i < n; i++)
a[i] = s.nextInt();
Sort s1=new Sort();
s1.Bubsorting(a,n);
}
}
- Write a JAVA program using StringBuffer to delete, remove character.
class stringbuffer2d{
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("Hello World");
sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some Content");
System.out.println(sb2);
sb2.delete(0, sb2.length());
System.out.println(sb2);
StringBuffer sb3 = new StringBuffer("Hello World");
sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
Exercise -3:
- Write a JAVA program to implement class mechanism. Create a class, methods and invoke them inside main method.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Example3a
{
int a,b,res;
BufferedReader br=new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
public void readNumber()throws IOException
{
System.out.println("enter a value:");
a=Integer.parseInt(br.readLine());
System.out.println("enter b value:");
b=Integer.parseInt(br.readLine());
}
public void calculate()
{
res=a+b;
}
public void display()
{
System.out.println("the result is:"+res);
}
public static void main(String args[])throws IOException
{
Example3a e=new Example3a();
e.readNumber();
e.calculate();
e.display();
}
}
- Write a JAVA program implement method overloading.
class Adder{
int add(int a,int b){return a+b;}
double add(double a, double b){return a+b; }
}
class Example{
public static void main(String[] args){
Adder A=new Adder();
System.out.println(A.add(5,109));
System.out.println(A.add(7.7,9.2));
}
}
- Write a JAVA program to implement constructor.
public class VolumeCube
{
int cube;
static double PIE=3.14;
VolumeCube(int a)
{
cube=a*a*a;
}
public void displayVolume()
{
System.out.println("the volume of the cube is"+cube);
}
public static void main(String args[]){
VolumeCube v=new VolumeCube(2);
v.displayVolume();
}
}
- Write a JAVA program to implement constructor overloading.
import java.io.IOException;
public class Volume4a
{
static double PIE=3.14;
Volume4a(double r,int h)
{
double cone=(PIE*r*r*h)/3;
System.out.println("the volume of cone is "+cone);
}
Volume4a(double l,double b,double h)
{
double cuboid=l*b*h;
System.out.println("the volume of double cuboid is "+cuboid);
}
public static void main(String args[])throws IOException
{
Volume4a v1=new Volume4a(2.5,3);
Volume4a v2=new Volume4a(2.2,3.3,4.4);
}
}
Exercise -4:
- Write a JAVA program to implement Single Inheritance.
import java.util.*;
class Data{
protected int a,b;
public void readData(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value");
a=sc.nextInt();
System.out.println("Enter b value");
b=sc.nextInt();
}
}
class Sum extends Data{
public int c;
public void Add(){
c=a+b;
System.out.println("Sum of 2 no's is"+c);
}
}
public class singleInherit
{
public static void main(String[] args) {
Sum s=new Sum();
s.readData();
s.Add();
}
}
- Write a JAVA program to implement multi level Inheritance
import java.util.*;
class A{
public int c;
public void Add(int a,int b){
c=a+b;
System.out.println("Sum of 2 no's is"+c);
}
}
class B extends A{
public void Diff(int a,int b){
c=a-b;
System.out.println("Difference of 2 no's is"+c);
}
}
class C extends B{
public void Mul(int a,int b){
c=a*b;
System.out.println("Multiplication of 2 no's is"+c);
}
}
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value");
int a=sc.nextInt();
System.out.println("Enter b value");
int b=sc.nextInt();
C s=new C();
s.Diff(a,b);
s.Add(a,b);
s.Mul(a,b);
}
}
- Write a java program for abstract class to find areas of different shapes
import java.util.*;
abstract class Areas
{
abstract void computeArea();
}
class areaCircle extends Areas
{
public double circle,r,pi=3.14;
areaCircle(double radius)
{
this.r=radius;
}
public void computeArea()
{
circle=pi*r*r;
System.out.print("the area of circle is : "+circle);
}
}
class areaTria extends Areas
{
public double Traingle,b,h;
areaTria(double breadth,double height)
{
this.b=breadth;
this.h=height;
}
public void computeArea()
{
Traingle=0.5*b*h;
System.out.print("the area of Traingle is : "+Traingle);
}
}
public class areaDemo1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int ch;
Double s,l,b,h,r;
while(true)
{
System.out.print("\n1 : Area of Triangle");
System.out.print("\n2 : Area of circle");
System.out.print("\n3 : Exit");
System.out.print("\nenter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\nEnter breath,height of Triangle");
b=sc.nextDouble();
h=sc.nextDouble();
areaTria ac=new areaTria(b,h);
ac.computeArea();
break;
case 2:
System.out.print("\nEnter radius of circle");
r=sc.nextDouble();
areaCircle aci=new areaCircle(r);
aci.computeArea();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("enter correct choice");
}
}
}
}
Exercise -5:
- Write a JAVA program give example for super keyword.
class A{
void print(){
System.out.println("A class printing...");
}
}
class B extends A{
void print(){
System.out.println("B class printing...");
}
void work(){
super.print();
print();
}
}
class TestSuper1{
public static void main(String args[]){
B d=new B();
d.work();
}}
- Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?
interface FirstInterface {
public void myMethod();}
interface SecondInterface {
public void myOtherMethod();}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}
class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}}
- Write a JAVA program that implements Runtime polymorphism.
class Animal
{
public void move()
{
System.out.println("Animals can move");
}
}
class Dog extends Animal
{
public void move()
{
System.out.println("Dogs can walk and run");
}
}
public class TestDog
{
public static void main(String args[])
{
Animal a = new Animal();
Animal b = new Dog();
a.move();
b.move();
}
}
Exercise -6:
- Write a JAVA program that describes exception handling mechanism
public class ExceptionHandling
{
public static void main(String arg[])
{
try
{
for(int i = -10;i<10;i++)
{
double d=100/i;
System.out.println(d);
}
}
catch(ArithmeticException ae)
{
System.out.println("Arithmetic Exception is raised.");
}
finally
{
System.out.println("This is Finally block");
}
}
}
- Write a JAVA program Illustrating Multiple catch clauses
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class MultipleCatch
{
public static void main(String arg[])throws IOException
{
int a[]=new int[5];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter element into array");
for(int i=0;i<5;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
try
{
for(int i=0;i<6;i++)
{
System.out.println("element:"+a[i]);
}
}
catch(ArithmeticException ae)
{
System.out.println("Arithmetic Exception is called.");
}
catch(ArrayIndexOutOfBoundsException aioob)
{
System.out.println("ArrayIndexOutOfBounds Exception is called.");
}
finally
{
System.out.println("This is Finally block");
}
}
}
- Write a JAVA program for creation of Java Built-in Exceptions
public class ExceptionHandling
{
public static void main(String arg[])
{
try
{
for(int i = -2;i<5;i++)
{
System.out.println(100/i);
}
}
catch(ArithmeticException ae)
{
System.out.println("Arithmetic Exception is raised.");
}
finally
{
System.out.println("This is Finally block");
}
}
}
- Write a JAVA program for creation of User Defined Exception
class MyException extends Exception
{
public MyException(String s)
{
super(s);
}
}
public class UserDefinedExceptionDemo
{
public static void main(String args[])
{
try
{
throw new MyException("Dhanekula");
}
catch (MyException ex)
{
System.out.println("Caught the exception1");
System.out.println(ex.getMessage());
}
}
}
Exercise -7:
- Write a JAVA program that creates threads by extending Thread class.First thread display “Good Morning “every 1 sec, the second thread displays “Hello “every 2 seconds and the third display “Welcome” every 3 seconds?(Repeat the same by implementing Runnable)
class ThreadDemo1 implements Runnable
{
public void run()
{
System.out.println("good morning");
}
}
class ThreadDemo2 implements Runnable
{
public void run()
{
System.out.println("hello");
}
}
class ThreadDemo implements Runnable
{
public void run()
{
System.out.println("welcome");
}
public static void main(String args[])
{
ThreadDemo1 td1=new ThreadDemo1();
ThreadDemo2 td2=new ThreadDemo2();
ThreadDemo td3=new ThreadDemo();
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
Thread t3=new Thread(td3);
try{
t1.start();
t1.sleep(1000);
t2.start();
t2.sleep(2000);
t3.start();
t3.sleep(3000);
}
catch(InterruptedException ie)
{
System.out.println("not possible");
}
}
}
- Write a program illustrating isAlive and join ()
class TestDaemon extends Thread{
public void run(){
System.out.println("Name: "+Thread.currentThread().getName());
System.out.println("Daemon: "+Thread.currentThread().isDaemon());
}
public static void main(String[] args){
TestDaemon t1=new TestDaemon();
TestDaemon t2=new TestDaemon();
t1.setDaemon(true);
t1.start();
try{
t1.join(); //Waiting for t1 to finish
}
catch(InterruptedException ie){}
System.out.println(t1.isAlive());
t2.start();
}
}
- Write a Program illustrating Daemon Threads
public class TestDaemon1 extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){
System.out.println("daemon thread work");
}
else{ System.out.println("user thread work"); } }
public static void main(String[] args){
TestDaemon1 t1=new TestDaemon1();
TestDaemon1 t2=new TestDaemon1();
t1.setDaemon(true);//now t1 is daemon thread
t1.start();//starting threads
t2.start();
} }
- Write a JAVA program Producer Consumer Problem
import java.util.LinkedList;
public class PCThreadExample
{
public static class PC
{
LinkedList ll = new LinkedList<>();
int capacity = 10;
public void produce() throws InterruptedException
{
int values = 0;
while(true)
{
synchronized (this)
{
while(ll.size() == capacity)
{
System.out.println("Producer going to wait");
wait();
}
System.out.println("Producer Produced ---->"+values);
ll.add(values);
notify();
values++;
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
while (ll.size()==0)
{
System.out.println("consumer going to wait");
wait();
}
int val = ll.removeFirst();
System.out.println("Consumer consumed ---->"+ val);
notify();
Thread.sleep(1000);
}
}
}
}
public static void main(String[] args)throws InterruptedException
{
final PC pc = new PC();
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
t2.start();
t1.start();
t1.join();
t2.join();
}
}
Exercise -8:
- Write a JAVA program that import and use the defined your package in the previous Problem
package p1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Addition
{
//Add two Integers and print the output
public void addNumbers() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter first Number: ");
float firstNumber = Float.parseFloat(br.readLine());
System.out.print("Enter second Number: ");
float secondNumber = Float.parseFloat(br.readLine());
float result = firstNumber + secondNumber;
System.out.println("the sum of "+firstNumber+" and "+secondNumber+" is "+result+".");
}
}
import p1.Addition;
import java.io.IOException;
public class PackageAddition extends Addition
{
public static void main(String a[]) throws IOException
{
PackageAddition a1 = new PackageAddition();
a1.addNumbers();
}
}
No comments:
Post a Comment