Archive for March, 2010
OOPS through JAVA
by hkesavaraju on Mar.26, 2010, under Java
Java week1 :Fibonacci series
by Shivu on Mar.19, 2010, under Uncategorized
b) The Fibonacci sequence is defined by the following rule:
The fist two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a Java program that uses both recursive and non recursive functions to print the nth value in the Fibonacci sequence.
import java.lang.*;
/**
*fibonacci**/
class Fibonacci
{
public int nthfiborec(int n)
{
if (n==1||n=0)
return 1;
else
return (nthfiborec(n-2)+nthfiborec(n-1));
}
public int nthfibo(int no)
{
int fib=0,num1=1,num2=1;
for(int counter = 1; counter <= no-2; counter++)
{
fib=(num1 + num2);
num1=num2;
num2=fib;
}
return fib;
}
}
class Mfibonacci
{
public static void main(String args[])
{
Fibonacci f=new Fibonacci();
BufferedReader Br=new BufferedReader( new InputStreamReader(System.in));
System.out.print("Enter a number:");
int n=Integer.pareseInt(br.readLine());
System.out.println("The"+n+"th term of fibonacci series is "+f.nthfibo(n));
System.out.println("The"+n+"th term of fibonacci series using recursion is "nthfibo(n-1));
}
System.out.println("k="+k);
}
JAVA week 1 : Nature of quadratic equation
by Shivu on Mar.19, 2010, under Uncategorized
a) Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0. Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display a message stating that there are no real solutions.
import java.lang.*;
import java.io.*;
/**
* This class is used to determine the nature of quadratic equation ( ax^2+bx+c=0 )
* This Class requires java.lang and java.io</> packages to work
* @author Shivu for www.btechadda.com
*/
class Quadratic
{
private int CofA,CofB,CofC,Desc;//discriminant
private double Root1,Root2;
/**
* This discriminant is calcculated with the help of co-efficients CofA,CofB,CofC and then if its value is positive then roots are real and distinct
* if desc is equla to 0 then roots are real and equal else if the roots are -ve then roots are imaginary.
* CofA,CofB,Cofc shoud be read ...
* The values have read or not is checked by means CofDefined
*
*/
public void calculate()
{
Desc=CofB*CofB-(4*CofA*CofC);
if (Desc==0)
{
System.out.println("The roots are real and Equal");
Root1=Root2=-CofB/(2*CofA);
System.out.println("The roots are"+Root1+" and "+Root2);
}
else if(Desc>0)
{
System.out.println("The roots are real and distinct");
Root1=((-CofB+Math.sqrt(Desc))/(2*CofA));
Root2=((-CofB-Math.sqrt(Desc))/(2*CofA));
System.out.println("The roots are"+Root1+" and "+Root2);
}
else
{
System.out.println("The roots are Imaginary");
}
}
/**
* This method prompts the user for providing the input from the keyboard
* This method uses Buffered reader class, so it throws and IO exception , exception should be specified in public static void main
* This requires importing the java.io.BufferedReader , .InputSteamReader, Integer
**/
public void read() throws IOException
{
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Co-efficient a:");
CofA=Integer.parseInt(buffer.readLine());
System.out.print("Enter Co-efficient b:");
CofB=Integer.parseInt(buffer.readLine());
System.out.print("Enter Co-efficient c:");
CofC=Integer.parseInt(buffer.readLine());
CofDefined=true;
}
}
class Mquad
{
public static void main(String[] argsf) throws IOException
{
Quadratic q1=new Quadratic();
Quadratic q2=new Quadratic();
q1.read();
q1.nature();
q2.nature();
}
}