Java Lab
Java Lab
Java Lab
AIM: Write a Java Program to Display Default Value of all Primitive Data Types.
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.
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
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];
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.