Sunday, February 12, 2012

DEVELOP A RATIONAL NUMBER CLASS [ java ]


1   DEVELOP  A  RATIONAL  NUMBER  CLASS


AIM:
            To write a java program to develop a rational number class

ALGORITHM:
           
1. Create a class rational and declare its variables and methods.
            2. Use the rational method get the input for the numerator and denominator.
            3. Define a method to convert string to value
            4. Define reduced form method to get the reduced form of the rational number.
            5. In the main function get the input from the user and call the corresponding methods to                  get the result
            6. Finally if there is an exception, display it.


PROGRAM:

import java.io.*;
import java.lang.*;
public class rational
{
            float numerator;
            float denominator;
            public rational(float x,float y)
            {
                        numerator=x;
                        denominator=y;
            }
            public float getvalue()
            {
                        float result=numerator/denominator;
                        return result;
            }
            public String toString()
            {
                        return numerator+"/"+denominator;
            }
            public void reducedform()
            {
                        if(numerator<denominator)
                        {
                                    for(int i=2;i<=numerator;i++)
                                    {
                                                while((numerator%i)==0 && (denominator%i)==0)
                                                {
                                                            numerator=numerator/i;
                                                            denominatro=denominator/i;
                                                }
                                    }
                        }
                        else
                        {
                                    for(int i=2;i<=denominator;i++)
                                    {
                                                while((numerator%i)==0 && (denominator%i)==0)
                                                {
                                                            numerator=numerator/i;
                                                            denominatro=denominator/i;
                                                }
                                    }
                        }
            }
            public static void main(String args[])throws IOException
            {
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("enter the rational number in the form);
                        String str=in.readLine();
                        float num[]=new float[2];
                        try
                        {
                                    String str1[]=new String[2];;
                                    str1=str.split('/");
                                    for(int i=0;i<2;i++)
                                    {
                                                num[i]=Float.parseFloat(str1[i]);
                                    }
                        rational ral=new rational(num[0],num[1]);
                        ral.reducedform();
                        String s="rational number" +ral;
                        System.out.println(s);
            }
            catch(Exception e)
            {
                        System.out.println("wrong insert");
            }
}





OUTPUT:
Enter the rational number in the form
45/15
The  rational number 3.0/1.0




































RESULT:
            Thus the java program to develop rational number class was implemented.

6 comments:

  1. I am an aspiring blogger. I too want to write blogs like yours.
    Can you teach me how to format(add menubar,links,slider etc) my blog like yours please.

    ReplyDelete
  2. Very informative. Now I got an idea about this topic. thanks for the content.
    CCNA Training In Chennai
    CCNA Training Online
    CCNA Course In Coimbatore

    ReplyDelete
  3. Great Post!!! Thanks for sharing this good information with us!!!
    Impact Of It On Business
    Why Big Data

    ReplyDelete