PDA

View Full Version : Java Exps


UrbanTwitch
02-10-2010, 11:16 PM
Hopefully this will be quicker:

Take this code I converted from PHP to Java
import java.lang.Math.*;
import java.util.*;

public class expTable {


public static int total;

public static void main(String[] args) {

for(int i=1;i<100;i++) {
System.out.println("Level " +i+ " - " + experience(i) + " xp\n");
}
}

public static int experience(int e) {
for(int i=1; i<e; i++) {
double form1 = (int)Math.floor(i+300*(int)Math.pow(2,(i/7)));
double form2 = Math.floor(form1/4);
total = (int) form2;
}
return total;

}

}

It's first level is suppose to produce is 83, then 174...

However... it's not doing that.

Look here for more info: http://rsdo.net/RSDOnline/Guides/Experience%20formula.html

What am I doing wrong?

Fou-Lu
02-10-2010, 11:34 PM
You missed the sigma in the formula:

public static int experience(int e) {

int iTotal = 0;
for(int i=1; i<e; i++) {
iTotal += (int)Math.floor(i + 300 * Math.pow(2, (i / 7)));
}
return (int)Math.floor(iTotal / 4);

}


I created a new variable for the total since I didn't know if you wanted to keep adding it up and stacking on top of it. This method should probably just return what it calculates.

UrbanTwitch
02-10-2010, 11:47 PM
Yeah... that sigma... I tried search for sumulation and i was off.

Thanks.

Also... whats the =+ mean... I tried Google. Math operators didn't show for it.

edit: The first number is suppose to be 83 instead of 75. Darn. :\

edit 2: Yeah, I think we need sigma... but what I don't get is how it's done in PHP and I pretty much execute it Java and it's off. >_<

Old Pedant
02-11-2010, 12:30 AM
You didn't find =+ because that's the wrong operator.

Look again at what FouLu wrote.

UrbanTwitch
02-11-2010, 12:33 AM
Whoops I mean += :-P (simple typo )

Old Pedant
02-11-2010, 12:45 AM
If all you want to do is print the table, then I have no idea why you thought you needed the experience() function.


public static void main(String[] args)
{
int points = 0;
for(int level = 1; level < 100; ++level )
{
int diff = Math.floor( level + 300 * Math.pow(2, level/7.00 ) );
points += diff;
System.out.println( "Level " + (level+1) + " = " + Math.floor( points / 4.0 ) );
}
}


That's a direct translation of the Python code, which seemed the cleanest of the ones provided.

If you want experience() as a function, then why not just echo the PHP code???

PHP:
function experience($L) {
$a=0;
for($x=1; $x<$L; $x++) {
$a += floor($x+300*pow(2, ($x/7)));
}
return floor($a/4);
}

Java:
public static int experience(int level) {
int a = 0;
for( int x=1; x < level; x++ ) {
a += Math.floor( x + 300 * Math.pow(2, (x/7.0) ) );
}
return Math.floor( a / 4.0 );
}

Notice that I converted 7==>7.0 and 4==>>4.0 to ensure that floating point arithmetic is done. PHP does everything like that floating point, so it's not needed in PHP.

Fou-Lu
02-11-2010, 12:47 AM
Sigma is just the summation. So because of it, you need to track the total outside of the loop and add to the value.
I got 83 and 174, I didn't run the code but it calculates out:

iTotal += (int)Math.floor(i + 300 * Math.pow(2, (i / 7)));

So, when provided 'e' is:


2^(1/7) = 1.104
* 300 = 331.226
+ 1 = 332.226
(int) cast floor = 332 (though technically I suppose we don't need to floor it)
/ 4 and floored = 83


Following from the above, total starts @ 332
2 ^(2/7) = 1.219
* 300 = 365.704
+ 2 = 367.704
(int) cast floor = 367
ADD TO PREVIOUS LOOP (332) = 699
/ 4 and floored = 174 (techincally, 174.75, but its floored so just 174)


And so forth. Isn't that the correct values? Did I biff the code (I'm too lazy to write it in java to test O.o)

Old Pedant
02-11-2010, 12:50 AM
So when you searched for += did you get a few hundred thousand hits?

Nope? Me neither. Turns out neither google nor bing will let you search for "+=". Even with quotes around them they are treated as query delimiters instead of something to search for.

Okay, so just search the Java docs for "operators" and you'll find it.

+=
-=
<<=
>>=
*=
/=
and more.

Oh, wth. It's just "shorthand".

a += b
is shorthand for
a = a + b

that's all. Ditto for all the others.

It's present in all C-derived languages. C/C++/Java/PHP/JavaScript/C# and more.

UrbanTwitch
02-11-2010, 01:06 AM
So... I was right all along but it was just a logic error. Much thanks for that detailed explanation! (both of you)

Fou-Lu
02-11-2010, 01:09 AM
You're welcome mate!
Yes, it was a logic error, in particular it was just the += (or as oldpedant mentioned, a = a+b). I take it that it works alright then? I didn't biff?

UrbanTwitch
02-11-2010, 01:13 AM
That and 4.0,7.0 numbers were troublesome. Nonetheless, thanks ;)

guildwars296
02-19-2010, 04:37 PM
import javax.swing.*;

public class CashReg

{

private double item1;
private double item2;


public CashReg (double pric1, double pric2)
{
item1 = pric1;
item2 = pric2;

}
public double subTotal1()
{
double subTotal1 = item1 + item2;
return subTotal1 ;
}
public double vat()
{
double vat = subTotal1 * 14.0/100;
return vat;
}
public double subTotal2;
{
double subTotal2 = subTotal1 + vat;
return subTotal2;
}
public double discount()
{
double discount = 5.0/10 * subTotal2;
return discount;
}
public double amtDue()
{
double amtDue = subTotal2 - disc;
return amtDue;
}
}

import javax.swing.*;
public class TestCashReg

{
public static void main (String[] args)
{
double price1 = Double.parseDouble(JOptionPane.showInputDialog("Enter price 1:" ));
double price2 = Double.parseDouble(JOptionPane.showInputDialog("Enter price 2:" ));
CashReg cash = new CashReg();

double subTotal1 = item1, item2;
double vat = subTotal1, 14.0/100;
double subTotal2 = subTotal1, vat;
double disc = 5.0/10, subTotal2;
double amtDue = subTotal2, disc;



System.out.println("The Subtotal is : " + subTotal1);
System.out.println("The VAT is : " + vat);
System.out.println("The SubTotal2 is : " + subTotal2);
System.out.println("The Discount is : " + disc);
System.out.println("The Amount Due is : " + amtDue);
}

}

I keep on getting errors when ever I do it can youhelp me ot email me t ikrm.shoaib@gmail.com when you get the solution.

Fou-Lu
02-19-2010, 06:03 PM
import javax.swing.*;

public class CashReg

{

private double item1;
private double item2;


public CashReg (double pric1, double pric2)
{
item1 = pric1;
item2 = pric2;

}
public double subTotal1()
{
double subTotal1 = item1 + item2;
return subTotal1 ;
}
public double vat()
{
double vat = subTotal1 * 14.0/100;
return vat;
}
public double subTotal2;
{
double subTotal2 = subTotal1 + vat;
return subTotal2;
}
public double discount()
{
double discount = 5.0/10 * subTotal2;
return discount;
}
public double amtDue()
{
double amtDue = subTotal2 - disc;
return amtDue;
}
}

import javax.swing.*;
public class TestCashReg

{
public static void main (String[] args)
{
double price1 = Double.parseDouble(JOptionPane.showInputDialog("Enter price 1:" ));
double price2 = Double.parseDouble(JOptionPane.showInputDialog("Enter price 2:" ));
CashReg cash = new CashReg();

double subTotal1 = item1, item2;
double vat = subTotal1, 14.0/100;
double subTotal2 = subTotal1, vat;
double disc = 5.0/10, subTotal2;
double amtDue = subTotal2, disc;



System.out.println("The Subtotal is : " + subTotal1);
System.out.println("The VAT is : " + vat);
System.out.println("The SubTotal2 is : " + subTotal2);
System.out.println("The Discount is : " + disc);
System.out.println("The Amount Due is : " + amtDue);
}

}

I keep on getting errors when ever I do it can youhelp me ot email me t ikrm.shoaib@gmail.com when you get the solution.

This is in no way relevant to this thread.
Start by making a new thread describing what the problem is and any errors if you are receiving them (which I can see you are based on you're calls against CashReg() and main method handling), and please wrap the code within Code here tags.