2-BTECH-CSE:: I SEM
OOPS THROUGH JAVA PROGRAMMING LAB
EXPERIMENT: 1
1(a): aim:- WRITE A JAVA PROGRAM TO DISPLAY DEFAULT VALUE
OF ALL PRIMITIVE DATA TYPE OF JAVA
PROGRAM:
public class DefaultValues {
byte byteVariable;
short shortVariable;
int intVariable;
long longVaraible;
float floatVariable;
double doubleVariable;
boolean boolVariable;
String stringVariable;
public static void main(String args[]){
DefaultValues obj = new DefaultValues();
[Link]("Default values of numeric variables in Java:");
[Link]("byte: "+[Link]);
[Link]("short: "+[Link]);
[Link]("int: "+[Link]);
[Link]("long: "+[Link]);
[Link]("float: "+[Link]);
[Link]("double: "+[Link]);
[Link]("Default values of non-numeric variables in Java:");
[Link]("boolean: "+[Link]);
[Link]("string: "+[Link]);
}
}
OUTPUT:
Default values of numeric variables in Java:
byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
Default values of non-numeric variables in Java:
boolean: false
string: null
1(B) WRITE A JAVA PROGRAM THAT DISPLAY THE ROOTS OF A QUADRATIC EQUATION
AZ2+BX+C=[Link] THE DISCRIMINATE D AND DESCRIBE THE NATURE OF THE ROOT
In algebra, a quadratic equation is an equation that can be reordered in standard form.
The standard form of a quadratic equation is ax2+bx+c=0. It is also known as the
second-degree equation. In this section, first will discuss the quadratic equation after
that we will create Java programs to solve the quadratic equation by using different
approaches.
In the equation ax2+bx+c=0, a, b, and c are unknown values and a cannot be 0. x is an
unknown variable. The formula to find the roots of the quadratic equation is known as
the quadratic formula.
A quadratic equation has two roots and the roots depend on the discriminant. In the
above formula, (√b2-4ac) is called discriminant (d). The value of d may be positive,
negative, or zero.
If d is positive (d>0), the root will be:
If the value of d is positive, both roots are real and different. It means there are two
real solutions.
If d is zero (d=0), the root will be:
If the value of d is zero, both roots are real and the same. It means we get one real
solution.
If d is negative (d<0), the root will be:
If the value of d is negative, both roots are distinct and imaginary or complex. It means
that there are two complex solutions.
PROGRAM;
import [Link];
public class QuadraticEquation
{
public static void main(String []args)
{
Scanner sc=new Scanner([Link]);
double a,b,c; //Quadratic Variables declaration
[Link]("Enter the value of a..");
a=[Link](); //Quadratic Variables Initialization
[Link]("Enter the value of b..");
b=[Link](); //Quadratic Variables Initialization
[Link]("Enter the value of c..");
c=[Link](); //Quadratic Variables Initialization
double d=(b*b)-(4*a*c); //Find the determinant
double D= [Link](d);
double r=2*a;
//Check for Roots
if(D>0)
{
[Link]("Roots are real and unequal");
double root1=(D-b)/r;
double root2=(-D-b)/r;
[Link]("Roots are..");
[Link](root1);
[Link](root2);
}
else if(D==0)
{
[Link]("The roots of the quadratic equation are real and equal.");
double root=(-b)/r;
[Link]("Root is "+root);
}
else
{
[Link]("The roots of the quadratic equation are complex and
different");
[Link]("Roots are ");
[Link]((-b/r)+" +i" + D);
[Link]((-b/r)+" -i" + D);
}
}
}
OUTPUT:
Enter the value of a.. 15
Enter the value of b.. 68
Enter the value of c.. 3
Roots are real and unequal
Roots are..
-0.044555558333472335
-4.488777774999861
OUTPUT:
Enter the value of a.. 7
Enter the value of b.. 7
Enter the value of c.. 7
The roots of the equation are complex and different.
-0.5 + i12.12435565298214
-0.5 - i12.12435565298214