sads

Thursday, 29 December 2016

Quad Euq

public class Quad1
    public static void main(String[] args)
    {
     
        int a = 2;
        int b = 6;
        int c = 4;
 
        //Finding out the roots
        double temp1 = Math.sqrt(b * b - 4 * a * c);

        double root1 = (-b +  temp1) / (2*a) ;
        double root2 = (-b -  temp1) / (2*a) ;

        System.out.println("The roots of the Quadratic Equation \"2x2 + 6x + 4 = 0\" are "+root1+" and "+root2);

    }
}
-------------------------------------------------------------------------
import java.util.Scanner;

public class Calc{
   
    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the values of a, b and c");

        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
         double r1 = 0;
        double r2 = 0;
         double discriminant = b * b - 4 * a * c;
         if (discriminant > 0){
             // r = -b / 2 * a;
            r1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            r2 = (-b - Math.sqrt(discriminant)) / (2 * a);

            System.out.println("The equation has two real roots " + r1 + " and " + r2);
        }
 
        if (discriminant == 0){
            System.out.println("The equation has one root " +r1);

            r1 = -b / (2 * a);
            r2 = -b / (2 * a);
         }

        if (discriminant < 0){
            System.out.println("The equation has no real root");

        }
     
       
    }

}
--------------------------------------------------------------------------------------
import java.io.*;

class Qua
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader read=new InputStreamReader (System.in);
        BufferedReader in=new BufferedReader (read);
       
        System.out.println("a=");
        int a=Integer.parseInt(in.readLine());
       
        System.out.println("b=");      
        int b=Integer.parseInt(in.readLine());
       
        System.out.println("c=");      
        int c=Integer.parseInt(in.readLine());
       
        double dis = Math.sqrt(b*b-4*a*c);
        double c1=(-b+dis)/(2*a);
        double c2=(+b+dis)/(2*a);
        System.out.println("Either "+c1+"or"+c2);
    }
}
---------------------------------------------------------------------------
import javax.swing.JOptionPane;
import java.io.*;
class Solution
{
private int a,b,c,desc;
private  double r1,r2;
    public void solve()
{
desc=b*b-4*a*c;
if(desc>0)
{
r1=(-b+Math.sqrt(desc))/(2*a);
            r2=(-b-Math.sqrt(desc))/(2*a);
JOptionPane.showMessageDialog(null,"roots are"+r1+" "+r2,"Message",JOptionPane.INFORMATION_MESSAGE);
}
else if(desc==0)
{
r1=r2=(-b+Math.sqrt(desc))/(2*a);
JOptionPane.showMessageDialog(null,"roots are"+r1+" "+r2,"Message",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"there are no real solutions","Message",JOptionPane.INFORMATION_MESSAGE);
}
}
public void input()
{
String n1,n2,n3;
n1=JOptionPane.showInputDialog("enter a value");
n2=JOptionPane.showInputDialog("enter b value");
n3=JOptionPane.showInputDialog("enter c value");
a=Integer.parseInt(n1);
b=Integer.parseInt(n2);
c=Integer.parseInt(n3);
}
}
class Quadratic
{
public static void main(String[] args)
{
Solution s=new Solution();
s.input();
s.solve();
}
}

Monday, 19 December 2016

Basics of Java

1. Program to Display String Literal

class  Display 
{
 public static void main( String args[ ] )
 {
  System.out.println("Let  us learn JAVA." );
 }
}


2. Program to Explain Different types of Comments in Java Program  
/*
This is a simple Java program.
Call this file "Comments.java".
*/
class Comments
{
 // Your program begins with a call to main().
 public static void main( String args[ ] )
 {
  System.out.println("BIIT Computer Education.");
 }
}

3. Program to Find Area of Circle

class  AreaCircle

 public static void main( String args[ ] )
 {
  double rad;
  final double PI = 3.14159;

   rad = 10.0;

   double area = PI * rad * rad;
  System.out.print( "\n Area of Circle is = " + area );
 }
}


4. Program to explain Dynamic Intialization of Variable 

class  DynInit
{
 public static void main( String args[ ] )
 {
  double a = 3.0, b = 4.0;

   // c is dynamically initialized
  double c = Math.sqrt(a * a + b * b);

   System.out.println("Hypotenuse is " + c);
 }
}

5. Program to declare variable at end of class

class  DeclareAtEnd
{
 public static void main( String args[ ] )
 {
  msg = "Let's learn JAVA !";
        System.out.println("Message : " + msg);
 }

 static String msg;
}


6.Program to declare static variable at end of class.

class  DeclareAtEnd1
{
 public static void main( String args[ ] )
 {
  DeclareAtEnd1 ob = new DeclareAtEnd1();
  ob.msg = "Let's learn JAVA !";
        System.out.println("Message : " + ob.msg);
 }

 String msg;
}


7. Program to declare and use character variables.

class  CharDemo
{
 public static void main(String args[ ]) 
 {
  char  ch1, ch2;
  ch1 = 88;   // code for X
  ch2 = 'Y';
  System.out.print("ch1 and ch2 : ");
  System.out.println(ch1 + " " + ch2);

   ch1++;    // increment ch1
  System.out.println("ch1 :  " + ch1);
 }
}


8. Program to declare and use Boolean variable 

class BoolTest
{
 public static void main(String args[ ]) 
 {
  boolean b;
  b = false;
  System.out.println("b is " + b);

   b = true;
  System.out.println("b is " + b);

   if(b) 
   System.out.println("This is executed.");

   b = false;
  if(b) 
   System.out.println("This is not executed.");

   System.out.println("10 > 9 is " + (10 > 9));
 }
}



9. Program to use Integer( feature of using underscore is available in jdk 7) 

class J7_Literal2
{
 public static void main( String args[ ] )
 {
  long dec = 1234_5678_9876_5432L;
  int hex = 0xE_23D5_8C_7;
  int bin = 0b1110_0010001111010101_10001100_0111;
  
  System.out.println("Dec number : " + dec);
  System.out.println("Hex number : " + hex);
  System.out.println("Bin number : " + bin);
 }
}

10. Program to explain Lifetime of variable 

class LifeTime1
{
 public static void main(String args[ ]) 
 {
  int i;
  for(i = 1; i <= 5; i++) 
  {
   increment();
  }
 }

 private static void increment()
 {
  int  x = 10;
  x++;
  System.out.println(" " + x );
 }
}



11. Program to create and use Constant 

class Constant1
{
 static final int NUMBER_OF_MONTHS = 12;
 static final double PI = (double) 22 / 7;

  public static void main( String args[] )
 {
  System.out.println("NUMBER_OF_MONTHS : " + NUMBER_OF_MONTHS );
  System.out.println("PI : " + PI );
 }
}



12. Program to create public Constant and use it 

class MyConstant
{
 public static final int NUMBER_OF_MONTHS = 12;
 public static final double PI = (double) 22 / 7;
}

class Constant2
{
 public static void main( String args[ ] )
 {
  System.out.println("NUMBER_OF_MONTHS : " + MyConstant.NUMBER_OF_MONTHS );
  System.out.println("PI : " + MyConstant.PI );
 }
}



13. Program to Read data From keyboard using Scanner Class

class Input1
{
 public static void main( String args[ ] )
 {
  Scanner sc = new Scanner( System.in );
  int n;
  
  System.out.print( "Enter a Number : ");
  n = sc.nextInt();

   System.out.println( "The given number : " + n );
 }
}


14. Program to read data from keyboard using Console class.

import java.io.*;

class Input2
{
 public static void main( String args[ ] )
 {
  Console cn = System.console();
  int n;
  
  System.out.print( "Enter a Number : ");
  n = Integer.parseInt( cn.readLine() );

   System.out.println( "The given number : " + n );
 }
}