Java Programs


Java Programs For B.Tech in CSE

Basic Introduction

Java is an object-oriented, high-level, class-based programming language designed to have minimum implementation dependencies. The language was developed by Sun Microsystems and released in 1995. Its form borrows a great deal from C and C++, but has fewer low-level facilities when compared to either of them. Obvious among the features that make it special is its platform independence, as programs are executed on any operating system with the help of a Java Virtual Machine (JVM). This feature makes it quite popular for cross-platform applications since Java programs can indeed be written once and run anywhere.

Listed among its significant features are the robustness, safety, and portability of Java. This language provides an integral comprehensive standard library. With appropriate tools available in the JDK, development and testing of the application are possible. It also provides extensive support to multithreading, where two or more threads could execute concurrently for effective CPU utilisation.

It finds applications in various fields, such as Web development, mobile applications—especially Android apps—enterprise systems, and scientific applications, to mention a few. The language is very versatile and has attracted a large, very active community, making Java one of the most popular programming languages in the world.

1.
BASIC PROGRAM:

1.1) Write a Java program to display default value of all primitive data type of JAVA.
1.2) Write a Java program to find the discriminant value D and find out the roots of the quadratic equation of the form ax2+bx+c=0.
1.3) 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. 

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


JAVA DataType

Datatypes available in JAVA

Program

public class Demo {

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

1.2) Write a Java program to find the discriminant value D and find out the roots of the quadratic equation of the form ax2+bx+c=0.

Formula to Calculate Quadratic Equation

Program

import java.util.*;
class Quadratic
{
public static void main(String args[])
{
double a,b,c,d;
double root1,root2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value");
a=sc.nextDouble();
System.out.println("Enter b value");
b=sc.nextDouble();
System.out.println("Enter c value");
c=sc.nextDouble();
d=b*b-4*a*c; if(d>0)
{
root1=(-b+Math.sqrt(d))/(2*a);
root2=(-b-Math.sqrt(d))/(2*a);
System.out.println("Root1="+root1+" Root2="+root2);
System.out.println("Roots are distinct");
}
else if(d==0)
{
root1=root2=(-b)/(2*a);
System.out.println("Root1="+root1+" Root2="+root2);
System.out.println("Roots are equal");
}
else
{
System.out.println("Roots are imaginary");
}
}
}

1.3) 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.

Program

import java.util.*;
class Race
{
public static void main(String args[])
{
double b1,b2,b3,b4,b5,avg;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the speed of 1 st biker");
b1=sc.nextDouble();
System.out.println("Enter the speed of 2 nd biker");
b2=sc.nextDouble();
System.out.println("Enter the speed of 3 rd biker");
b3=sc.nextDouble();
System.out.println("Enter the speed of 4 th biker");
b4=sc.nextDouble();
System.out.println("Enter the speed of 5 th
b5=sc.nextDouble();
avg=(b1+b2+b3+b4+b5)/5;
if(b1>avg)
System.out.println("biker 1 is qualified in a race with "+b1+" speed");
if(b2>avg)
System.out.println("biker 2 is qualified in a race with "+b2+" speed");
if(b3>avg)
System.out.println("biker 3 is qualified in a race with "+b3+" speed");
if(b4>avg)
System.out.println("biker 4 is qualified in a race with "+b4+" speed");
if(b5>avg)
System.out.println("biker 5 is qualified in a race with"+b5+" speed");
}
}

2. CONTROL FLOW STATEMENTS

2.1) Write a Java program to select all the prime numbers within the range of 1 to 100.
2.2) Write a Java program to Find the sum of all even terms in the Fibonacci sequence up to the given range N.
2.3) Write a Java program to check whether a given number is Armstrong or not. 

2.1) Write a Java program to select all the prime numbers within the range of 1 to 100.

Flowchart for Prime Number

Flowchart to find Prime Number

Program

class Prime
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=100;i++)
{
int factor=0; 
for(j=1;j<=i;j++)
{
if(i%j==0)
factor++;
}
if(factor==2)
System.out.print(i+" ");
}
}
}

2.2) Write a Java program to Find the sum of all even terms in the Fibonacci sequence up to the given range N. 

Flowchart to find Fibonacci Sequence

Program

import java.util.*;
class Fibanoci
{
public static void main(String args[])
{
int n,sum=0,a=0,b=1,c;
Scanner sc=new Scanner(System.in);
System.out.println("enter n value");
n=sc.nextInt();
c=a+b;
while(c<=n)
{
if(c%2==0)
sum=sum+c;
a=b;
b=c; c=a+b;
}
System.out.println("sum of all even terms in fibanocii is "+sum);
}
}

2.3) Write a Java program to check whether a given number is Armstrong or not. 

Flowchart to find Armstrong Number 

Program

import java.util.*;
import java.lang.*;
class Armstrong
{
public static void main(String args[])
{
int n,r,temp,count=0,rem;
double sum=0,num;
System.out.println("Enter n value");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
temp=n;
num=n;
while(temp!=0)
{
rem=temp%10;
count++;
temp=temp/10;
}
while(n!=0)
{
r=n%10;
sum=sum+Math.pow(r,count);
n=n/10;
}
if(sum==num)
System.out.println(num+" is a Armstrong number");
else
System.out.println(num+" is not an Armstrong number");
}
}

3. ARRAYS

        3.1) Write a Java program to implement binary search.
        3.2) Write a Java program to sort for an element in a given list of elements using bubble sort.
        3.3) Write a Java program to sort for an element in a given list of elements using merge sort. 

3.1) Write a Java program to implement binary search.

Program

import java.util.*;
class Binary
{
public static void main(String args[])
{
int a[]=new int[20];
int n,key,mid,l,h;
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array");
n=sc.nextInt();
System.out.println("enter the array elements in ascending order");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println("enter key");
key=sc.nextInt();
l=0;
h=n-1;
while(l<=h)
{
mid=(l+h)/2;
if(key==a[mid])
{
System.out.println("Element found at "+(mid+1));
break;
}
else if(key<a[mid])
h=mid-1;
else
l=mid+1;
}
if(l>h)
System.out.println("Element not found");
}
}

3.2) Write a Java program to sort for an element in a given list of elements using bubble sort. 

Program

import java.util.*;
class Bubble
{
  public static void main(String []args)
  {
    int n, i, j, temp;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of integers to sort:");
    n=sc.nextInt();
    int a[] = new int[n];
    System.out.println("Enter the elements:");
    for (i = 0; i < n; i++)
    a[i] = sc.nextInt();
    for (i = 0; i < n - 1 ; i++)
    {
      for (j = 0; j < n - i - 1; j++)
      {
        if (a[j] > a[j+1])
        {
           temp = a[j];
           a[j] = a[j+1];
           a[j+1] = temp;
        }
      }
    }
    System.out.println("After Sorting,the list of integers:");
    for (i = 0; i < n; i++)
    System.out.print(a[i]+" ");
  }
}

3.3) Write a Java program to sort for an element in a given list of elements using merge sort. 

Program

import java.util.*;
class Merge
{
 public static void main(String []args)
  {
    int n, i;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of integers to sort:");
    n=sc.nextInt();
    int a[] = new int[n];
    System.out.println("Enter the elements:");
    for (i = 0; i < n; i++)
    a[i] = sc.nextInt();
    Method m= new Method();
    m.ms(a,0,n-1);
    System.out.println("Array after sorting");
    for(i=0;i<n;i++)
    System.out.print(a[i]+" ");
  }
}
class Method
{
 void ms(int a[],int first,int last)
 {
  int mid;
  if(first<last)
  {
   mid=(first+last)/2;
   ms(a,first,mid);   
   ms(a,mid+1,last);
   merge(a,first,mid,last);
  }
 }
 void merge(int a[],int first,int mid,int last)
 {
    int i,h,j,k;
    int b[]=new int[50];
    h=first;  
    i=first;  
    j=mid+1;  
    while((h<=mid)&&(j<=last))
    {
      if(a[h]<=a[j])   
      {                
        b[i]=a[h];
        h++;
      }
      else                
      {
        b[i]=a[j];
        j++;
      } 
      i++;
    }
    if(h>mid)
    {
     for(k=j;k<=last;k++) 
     {
       b[i]=a[k];
       i++;
     }
    }
    else
    {
     for(k=h;k<=mid;k++)
     {
       b[i]=a[k];
       i++;
     }
    }
    for(k=first;k<=last;k++)
    a[k]=b[k];        
 }
}
}

4. CLASS MECHANISM 

4.1) Write a Java program to display the details of a person. Personal details should be given in one method and the qualification details in another method.
4.2) Write a Java program to implement constructor and constructor overloading.
4.3) Write a Java program to implement method overloading. 

4.1) Write a Java program to display the details of a person. Personal details should be given in one method and the qualification details in another method. 

Program

import java.util.*;
class Person
{
char gender;
String name;
int age;
String quali;
String branch;
void personal()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name");
name=sc.nextLine();
System.out.println("Enter Age");
age=sc.nextInt();
System.out.println("Enter Gender");
gender=sc.next().charAt(0);
}
void qualification()
{
quali="B.tech";
branch="Computer Science And Engineering";
}
void display()
{
System.out.println("Name:"+name);
 System.out.println("Gender:"+gender);
System.out.println("Age:"+age);
System.out.println("Qualification:"+quali);
System.out.println("Branch:"+branch);
}
}
class Details
{
public static void main(String args[])
{
Person p=new Person();
p.personal();
p.qualification();
p.display();
}
}

4.2) Write a Java program to implement constructor and constructor overloading. 

Program

class Test
{
    int x;
    public Test()  // default constructor
               {
                              System.out.println("Default Constructor");
                              x = 1;
               }
               public Test(int x)  // parameterized constructor
               {
                              System.out.println("Parameterized constructor");
                              this.x = x;
               }
               public Test(Test t)  // copy constructor
               {
                              x = t.x;
                              System.out.println("Copy Consturctor");
               }
               void show()
               {
                              System.out.println("X:"+this.x);
               }
               public static void main (String args [])
               {
                              Test t1 = new Test(); // default constructor
                              Test t2 = new Test(13); // parameterized
                              Test t3 = new Test(t2); // copy constructor
                              t1.show (); //  1
                              t2.show (); //  13
                              t3.show (); //  13
               }
}

4.3) Write a Java program to implement method overloading. 

Program

class Shapes
{
void area(float side)
{
System.out.println("Area of Square:"+(side*side));
}
void area(float length,float breadth)
{
System.out.println("Area of Rectangle:"+(length*breadth));
}
void area(double radius)
{
System.out.println("Area of Circle:"+(3.14*radius*radius));
}
void area(double radius,float pi)
{
System.out.println("Area of Sphere:"+(4*pi*radius*radius));
}
}
class Mover
{
public static void main(String args[])
{
Shapes s=new Shapes(); s.area(3.4f);
s.area(6.1f,7.2f);
s.area(4.5);
s.area(6.84,3.14f);
}
}

 

Comments

Popular posts from this blog

Unlocking the Power of Data: An Introduction to Data Warehousing and Data Mining