r19 JNTUK JAVA LAB ; Exercise-1

r19-JNTUK JAVA LAB Exercise-1
JAVA LAB: Exercise-1
a) Displaying default value of all primitive data types

AIM: To write a JAVA program to display default values
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);
   }
}

Output

Default values.....
Val1 = false
Val2 = 0.0
Val3 = 0.0
Val4 = 0
Val5 = 0
Val6 = null                         
b)Roots of a quadratic equation

AIM: To write a java program that display the roots of a quadratic equation ax2+bx=0.
Calculate the discriminate D and basing on value of D, describe the nature of root.

SOURCE-CODE:

import java.util.*;

class quadraticdemo

{

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

}

}

}


OUT-PUT:

Given quadratic equation:ax^2 + bx + c

Enter a:2

Enter b:3

Enter c:1

Roots are real and unequal

First root is:-0.5

Second root is:-1.0



c) Bike Race:

AIM: 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.


SOURCE-CODE:

import java.util.*;
class racedemo

{

public static void main(String[] args)

{

float s1,s2,s3,s4,s5,average;

Scanner s = new Scanner(System.in);

System.out.println("Enter speed of first racer:");

s1 = s.nextFloat();

System.out.println("Enter speed of second racer:");

s2 = s.nextFloat();

System.out.println("Enter speed of third racer:");

s3 = s.nextFloat();

System.out.println("Enter speed of fourth racer:");

s4 = s.nextFloat();

System.out.println("Enter speed of fifth racer:");

s5 = s.nextFloat();

average=(s1+s2+s3+s4+s5)/5;

if(s1>average)

System.out.println("First racer is qualify racer:");

else if(s2>average)

System.out.println("Second racer is qualify racer:");

else if(s3>average)

System.out.println("Third racer is qualify racer:");

else if(s4>average)

System.out.println("Fourth racer is qualify racer:");

else if(s5>average)

System.out.println("Fifth racer is qualify racer:");

}

}


OUT-PUT:


Enter speed of first racer:
4.5

Enter speed of second racer:
6.7

Enter speed of third racer:
3.8

Enter speed of fourth racer:
5.3

Enter speed of fifth racer:
4.9

Second racer is qualify racer:




Comments

Post a Comment