Java Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Experiment - 1

AIM: Write a Java Program to Display Default Value of all Primitive Data Types.

Resources: Personal Computer with JDK installed

Description: It’s not always necessary to assign a value when a field is declared.
Fields that are declared but not initialized will be set to a reasonable default by the
compiler. Generally speaking, this default will be zero or null, depending on the
data type. Relying on such default values, however, is generally considered bad
programming style.

Algorithm :
1. Start
2. Declare and initialize variables for each primitive data type:
a. byte defaultByte = 0
b. short defaultShort = 0
c. int defaultInt = 0
d. long defaultLong = 0L
e. float defaultFloat = 0.0f
f. double defaultDouble = 0.0
g. char defaultChar = '\u0000'
h. boolean defaultBoolean = false
3. Print a message indicating the default values are being displayed.
4. Display the default values of each primitive data type:
a. Print "Default byte: " followed by the value of defaultByte
b. Print "Default short: " followed by the value of defaultShort
c. Print "Default int: " followed by the value of defaultInt
d. Print "Default long: " followed by the value of defaultLong
e. Print "Default float: " followed by the value of defaultFloat
f. Print "Default double: " followed by the value of defaultDouble
g. Print "Default char: " followed by the value of defaultChar
h. Print "Default boolean: " followed by the value of defaultBoolean
5. End
Flowchart

Program:
class shresth {
static byte b;
static short s;
static int i;
static long l;
static float f;
static double d;
static char c;
static boolean bl;
public static void main(String[] args)
{
System.out.println("The default values of primitive data types are:");
System.out.println("Byte :"+b);
System.out.println("Short :"+s);
System.out.println("Int :"+i);
System.out.println("Long :"+l);
System.out.println("Float :"+f);
System.out.println("Double :"+d);
System.out.println("Char :"+c);
System.out.println("Boolean :"+bl);
}
}
Output :

Outcome:

Data type defines the operations that can be done on the data, the meaning of the data, and the
way values of that type can be stored.
Experiment - 2
AIM: Write a Java Program to Find Roots of a Quadratic Equation.

Resources: Personal Computer with JDK installed

Description: The Quadratic Formula. The quadratic equation have the solutions

Algorithm:
 Start
1. Read coefficients of the quadratic equation: a, b, and c.
2. Calculate the discriminant d using the formula: d = b * b - 4 * a * c.
3. Calculate the value of r using the formula: r = -b / (2 * a).
4. If d is greater than 0, go to step 6. Otherwise, go to step 8.
5. Calculate the two real and distinct roots, r1 and r2, using the formulas:
 r1 = r + sqrt(d) / (2 * a)
 r2 = r - sqrt(d) / (2 * a)
6. Print "Roots are real and distinct:"
 Print "First root: r1"
 Print "Second root: r2"
7. If d is equal to 0, go to step 9. Otherwise, go to step 10.
8. Print "Roots are real and equal:" followed by -r.
9. Set d to -d.
10. Calculate the imaginary part im using the formula: im = sqrt(d) / (2 * a).
11. Print "Roots are imaginary:"
 Print "First root: r + i * im"
 Print "Second root: r - i * im"
 Stop
Flowchart :
Program :
import java.util.*;
class shresth {
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");
}}}

Output :
Outcome:
Data type defines the operations that can be done on the data, the meaning of
the data, and the way values of that type can be stored.

Experiment - 3
AIM: Write a Java Program to Implement Binary Search Mechanism

Resources: Personal Computer with JDK installed

Description: Generally, to find a value in unsorted array, we should look through


elements of an array one by one, until searched value is found. In case of searched
value is absent from array, we go through all elements. In average, complexity of
such an algorithm is proportional to the length of the array. Situation changes
significantly, when array is sorted. If we know it, random access capability can be
utilized very efficiently to find searched value quick. Cost of searching algorithm
reduces to binary logarithm of the array length. For reference, log 2(1 000 000) ≈
20. It means, that in worst case, algorithm makes 20 steps to find a value in sorted
array of a million elements or to say, that it doesn't present it the array.
Note: Elements should be in sorted order in the given array
Algorithm:
1. Start
2. Read the sorted array arr and the target element x to be searched.
3. Initialize two pointers, left and right, where left points to the start of the array
and right
points to the end of the array.
4. While left is less than or equal to right, repeat steps 5-7.
5. Calculate the middle index mid as the average of left and right: mid = (left +
right) / 2.
6. If arr[mid] is equal to x, go to step 8.
7. If arr[mid] is less than x, update left to mid + 1. Otherwise, update right to mid -
1.
8. Print "Element found at index: mid".
9. Stop
Flowchart:

Program :

import java.util.Scanner;
class shresth
{
public static void main(String args[])
{
int n, i, num,first, last, middle;
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 in sorted order:");
for (i = 0; i < n; i++){
a[i] = s.nextInt();}
System.out.println("Enter the search value:");
num = s.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( a[middle] < num ){
first = middle + 1;
}
else if ( a[middle] == num )
{
System.out.println("number found");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last ){
System.out.println( " Number is not found");}
}
}

Output :

Outcome:

Data type defines the operations that can be done on the data, the meaning of the data,
and the way values of that type can be stored.

You might also like