R19 JAVA PROGRAMMING LAB
Exercise - 1 (Basics)
a) Write a JAVA program to display default value of all primitive data type of JAVA
Program:
class DefaultValues
{
byte b;
short s;
int i;
long l;
float f;
double d;
char c;
boolean bl;
String s;
public static void main(String[] args)
{
DefaultValues df=new DefaultValues();
System.out.println("Byte :"+df.b);
System.out.println("Short :"+df.s);
System.out.println("Int :"+df.i);
System.out.println("Long :"+df.l);
System.out.println("Float :"+df.f);
System.out.println("Double :"+df.d);
System.out.println("Char :"+df.c);
System.out.println("Boolean :"+df.bl);
System.out.println("String :"+df.s);
}
}
Output:
b) Write a java program that display the roots of a quadratic equation ax2+bx+c=0. Calculate the discriminate D and basing on value of D, describe the nature of root.
NOTE:
The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Program:
//Java Program to Find Roots of a Quadratic Equation
import java.util.Scanner;
class Quadratic
{
public static void main(String[] args)
{
//double a = 2.3, b = 4, c = 5.6;
double a,b,c;
System.out.println("Enter a,b,c values:");
Scanner scan=new Scanner(System.in);
a=scan.nextFloat();
b=scan.nextFloat();
c=scan.nextFloat();
double root1, root2;
double determinant = b * b - 4 * a * c;
// condition for real and different roots
if(determinant > 0)
{
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f", root1 , root2);
System.out.println("\nRoots are real and different");
}
// Condition for real and equal roots
else if(determinant == 0)
{
root1 = root2 = -b / (2 * a);
System.out.format("root1 = root2 = %.2f;", root1);
System.out.println("\nRoots are real and equal");
}
// If roots are not real
else
{
double realPart = -b / (2 *a);
double imaginaryPart = Math.sqrt(-determinant) / (2 * a);
System.out.format("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi",realPart, imaginaryPart, realPart, imaginaryPart);
System.out.println("\nRoots are imaginary"); //complex and different
}
}
}
Output:
c) 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 a[])
{
int r1,r2,r3,r4,r5,avg;
Scanner s=new Scanner(System.in);
System.out.println("enter the speed of the r1 r2 r3 r4 and r5");
r1=s.nextInt();
r2=s.nextInt();
r3=s.nextInt();
r4=s.nextInt();
r5=s.nextInt();
avg=(r1+r2+r3+r4+r5)/5;
if(r1>avg)
{
System.out.println("first racer is qualified with a race speed "+r1+"\n");
}
else if(r2>avg)
{
System.out.println("second racer is qalified with a race speed "+r2);
}
else if(r3>avg)
{
System.out.println("third racer is qalified with a race speed "+r3);
}
else if(r4>avg)
{
System.out.println("fourth racer is qalified with a race speed "+r4);
}
else if(r5>avg)
{
System.out.println("fifth racer is qalified with a race speed "+r5);
}
else
{
System.out.println("no biker is qualified");
}
}
}
Output:
Good Information!
ReplyDeletePython Online Training In Hyderabad
Python Online Training