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
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();
}
}
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 :)