How can I simplify this code?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How can I simplify this code?

  1. #1
    Join Date
    Dec 2004
    Posts
    1

    How can I simplify this code?

    Here's some code I am working on for my CompScience class. If any of you more advanced java users can assist me in simpliying this code, it will be much appreciated. Thanks

    import javax.swing.JOptionPane;
    public class lab09b
    {
    public static void main (String args[])
    {
    String str;
    int num = 1, den = 1; //Initializes variables for use.
    Rational2 r3 = new Rational2 (num,den); //Creates new Rational2 object to be used for math operations.
    str = JOptionPane.showInputDialog("Enter Numerator 1:");
    num = Integer.parseInt(str);
    str = JOptionPane.showInputDialog("Enter Denominator 1:");
    den = Integer.parseInt(str);
    Rational1 rA = new Rational1 (num,den); //Creates object to store original input of fraction 1.
    Rational2 r1 = new Rational2 (num,den); //Creates object to store reduced input of fraction 1.
    str = JOptionPane.showInputDialog("Enter Numerator 2:");
    num = Integer.parseInt(str);
    str = JOptionPane.showInputDialog("Enter Denominator 2:");
    den = Integer.parseInt(str);
    Rational1 rB = new Rational1 (num,den); //Creates object to store original input of fraction 2.
    Rational2 r2 = new Rational2 (num,den); //Creates object to store reduced input of fraction 1.

    r3.add(r1,r2);
    JOptionPane.showMessageDialog(null,rA.getOriginal()+"+"+rB.getOriginal()+"="+r3.getRationa l()); //Outputs results of addition
    r3.multiply(r1,r2);
    JOptionPane.showMessageDialog(null,rA.getOriginal()+"*"+rB.getOriginal()+"="+r3.getRationa l()); //Outputs results of multiplication.
    System.exit(0);
    }
    }
    class Rational1
    {
    protected int num; // Entered numerator.
    protected int den; // Entered denominator.

    private int getGCF(int n1,int n2) //Method for reduction.
    {
    int rem = 0;
    int gcf = 0;
    do
    {
    rem = n1 % n2;
    if (rem == 0)
    gcf = n2;
    else
    {
    n1 = n2;
    n2 = rem;
    }
    }
    while (rem != 0);
    return gcf;
    }
    public void reduce() //Modifies fraction to a equivalent simplified rational.
    {
    int gcf = getGCF(num,den);
    num = num / gcf;
    den = den / gcf;
    }
    public Rational1(int n, int d) //Constructs Rational1 without reducing it.
    {
    num = n;
    if (d == 0)
    {
    System.out.println("Illegal denominator; altered to 1");
    den = 1;
    }
    else
    {
    den = d;
    }
    }
    public int getNum() //Returns Numerator.
    {
    return num;
    }
    public int getDen() //Returns Denominator.
    {
    return den;
    }
    public String getOriginal() //Returns Original Fraction.
    {
    return ""+num+"/"+den;
    }
    }


    class Rational2 extends Rational1 //Class used to modify fractions.
    {
    public Rational2(int n, int d) //Constructs Rational2 using superclass constructor.
    {
    super(n,d);
    }
    public String getRational() //Returns the reduced fraction version.
    {
    super.reduce();
    return ""+num+"/"+den;
    }
    public void add(Rational2 r1, Rational2 r2) //Adds two rationals together.
    {

    den = r1.getDen() * r2.getDen();
    num = r1.getNum() * r2.getDen() + r2.getNum() * r1.getDen();
    }
    public void multiply(Rational2 r1, Rational2 r2) //Multiplies two rationals.
    {
    den = r1.getDen() * r2.getDen();
    num = r1.getNum() * r2.getNum();
    }
    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    What do you mean by simplify?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center