Results 1 to 3 of 3

Thread: Convert this VB code to Java?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Resolved Convert this VB code to Java?

    Hello, i've just come from another post over in the maths forum.

    http://www.vbforums.com/showthread.p...54#post2804654

    I'm trying to get a part of my program to get the ratio between 2 numbers, and cancel it down to the lowest possible form.

    the code:
    Code:
       1.
          val_1 = 15
       2.
            val_2 = 120
       3.
           
       4.
            If val_1 < val_2 Then
       5.
              min_val = val_1
       6.
            Else
       7.
              min_val = val_2
       8.
            End If
       9.
           
      10.
            If (val_1 / min_val) = (val_1 \ min_val) And _
      11.
               (val_2 / min_val) = (val_2 \ min_val) Then
      12.
               min_1 = (val_1 / min_val)
      13.
               min_2 = (val_2 / min_val)
      14.
            Else
      15.
              min_1 = val_1
      16.
              min_2 = val_2
      17.
              For i = (min_val \ 2) To 1 Step -1
      18.
                If (val_1 / i) = (val_1 \ i) And _
      19.
                   (val_2 / i) = (val_2 \ i) Then
      20.
                   min_1 = (val_1 / i)
      21.
                   min_2 = (val_2 / i)
      22.
                   Exit For
      23.
                End If
      24.
              Next i
      25.
            End If
      26.
           
      27.
            MsgBox CStr(min_1) & ":" & CStr(min_2)
    works perfectly in VB.

    I've started to convert it to java but i think im going the wrong way about it; as im creating far too many variables because i cant find the syntax for / and \ in java. Wondering if the chaps of VBForums would be able to lend a hand.

    This is what ive got so far:

    Code:
    public class Ratio
    {
      public static void main(String[]args)
      {
        System.out.println("enter number 1");
    	 int a=KBInput.readInt();  //can post this class file if needed
    	 System.out.println("enter number 2");
    	 int b=KBInput.readInt();  //can post this class file if needed
    	 double a1=a;  //a decimal version of 'a' for divisor calculation
    	 double b1=b;  //a decimal version of 'b' for divisor calculation
    	 int minVal;   
    	 double minVal2;  //decimal version of minVal for divisor calculation
    	 if (a<b)
    	 {
    	   minVal=a;  //set smallest value to a
    		minVal2=a; //""
    	 }
    	 else
    	 {
    	   minVal=b;  //set smallest value to b
    		minVal2=b; //""
    	 }
    	 
    	 int adivmin = (a/minVal);			// adivmin set to (a/minVal) this is Integer
    	 double adivmin2 = (a/minVal2);  // adivmin set to (a/minVal2) this is Double
    	 int bdivmin = (b/minVal);			// bdivmin set to (b/minVal) this is Integer
    	 double bdivmin2 = (b1/minVal2); // adivmin set to (b/minVal2) this is Double
    	 int ans1=0, ans2=0;
    	
    	 System.out.println("A divided min val = "+adivmin);
    	 System.out.println("A divided min val = "+adivmin2);
    	 System.out.println("B divided min val = "+bdivmin);
    	 System.out.println("B divided min val = "+bdivmin2);
    	 
    	 if ((adivmin)==(adivmin2)&&(bdivmin)==(bdivmin2))
    	 {
    	   ans1=(a/minVal);
    		ans2=(b/minVal);
    	 }
    	 else
    	 {
    
     ~~~~~~Im stuck here~~~~~~
    	   ans1=a;
    		ans2=b;
    
    
    		for (int x=0; x<=(minVal/2); x++)
    		{
    		  if((a/x)==(a/x)&&(b/x)==(b/x))  
    		  {
    		    System.out.println("DOH");
    		  }
    		}
    	 }
    	 
    	 
    	 System.out.println("Ratio is:   "+ans1+":"+ans2);
      }
    }
    Thanks a bunch for any help you can give me!!!

    Mathew
    Last edited by MathewF; Mar 8th, 2007 at 01:00 PM. Reason: missed something out

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Convert this VB code to Java?

    You want to shorten a fraction? The standard way is to get the greatest common divisor and divide both factors by it.
    To get the GCD, use the Euclidean Algorithm.

    The VB code above sucks, and the translation to Java (which cares about whether a number is an integer or a floating point number) makes it suck more.

    Code:
    class Fraction
    {
      private int num, den;
    
      public int getNum() { return num; }
      public int getDen() { return den; }
    
      private int gcd(int a, int b) {
        while(b != 0) {
          int t = b;
          b = a % b;
          a = t;
        }
        return a;
      }
      private void shorten() {
        int t = gcd(num, den);
        num /= t;
        den /= t;
      }
    
      public Fraction(int n, int d)
      {
        if(d == 0) throw new IllegalArgumentException("Denominator is zero.");
        num = n;
        den = d;
        shorten();
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Resolved Re: Convert this VB code to Java?

    Thanks a lot for taking the time to help me CornedBee. I really appreciate it.

    I didnt realise the code i was trying to convert to java was quite so bad!

    Rating for you
    Last edited by MathewF; Mar 8th, 2007 at 01:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width