Achartengine getRange method issue - java

I am having issues with Achartengine's XYSeries getRange(double, double, int) method.
I am new to Java programming so this issue sort of boggles me.
I am not sure how to use it, since I always get errors when I try it! What is the object that goes in front of it to get values from getRange? I.E:
object here.getRange(double, double, int);
and would I set that equal to something too? Thanks!

What is the object that goes in front of it to get values from
getRange?
XYSeries.getRange(double start, double stop, int beforeAfterPoints);
Returns submap of x and y values according to the given start and end
and would I set that equal to something too?
Use it like this :
SortedMap<Double, Double> map = XYSeries.getRange(double start, double stop, int beforeAfterPoints);
to get y for x value
map.get(X_Value);
Links:
Class XYSeries
Viewing Sub Maps
Hope this helped you.

Related

Point Coordinates to x and y cordinates in variables doesnt really work

i just try to program pong with the use of java swing components.
My problem ist, that i need to store the coordinates of my JButton (my Paddle) in variables to be able to manipulate the position of the Button when moving.
I tried doing it that way:
int posP1_x= paddel1.getLocation().getX(); //Error
When compiling it says, that there is a lossy conversion from double to int. (But the retrun value of getX should be int and in the Point-Class the values are also stored as ints). When i try declaring posP1_x as double and print the variables value on the console, it always prints 0.0. But when i print paddel1.getLocation().getX() directly, it works...
double posP1_x= paddel1.getLocation().getX(); //Works
System.out.println(paddel1.getLocation().getX()); //Prints double value eg 110.0
System.out.println(posP1_x); //Prints double value with 0. --> 0.0
What could be the solution to save JButton Coordinates in Variables.
Thank you and have a nice day
Don't use getX(). Use .x. That's it. For example:
// either
int posP1_x= (int) paddel1.getLocation().getX();
// or
int posP1_x= paddel1.getLocation().x;
More importantly, look at the relevant API before posting here. If you'd simply look at the Point API, you'd have your answer.

Changing Doubles To Ints

Im working on a homework assignment for my intro to computer science class and we are are inputting basic commands to get the percentage of people who drink a certain kind of energy drink. We used JOptionPane to make text boxes and you can input the amount of people and the computer has a set percentage to get the output. My problem is i set up my variables as doubles and my answers are very long decimals. I want to convert the answers to Ints so i can get whole numbers. I have tried to do this through casting but i keep getting the error message" EnergyDrink.java:14: error: variable citrusEnergyDrinkers might not have been initialized". What can i do?
This can't be solved without code. The error is not due to any problem with the conversion, but simply as the compiler-error says:
variable citrusEnergyDrinkers might not have been initialized
This means that the variable might not hold a value at the time you attempt to convert it, which results in undefined behaviour, which java-designers didn't allow for a reason.
The problem is as the error-message tells: citrusEnergyDrinkers gets its value inside some try-catch-block or a block that is only run under certain conditions, like if. One way to work around this would be to simply initialize citrusEnergyDrinkers as 0:
double citrusEnergyDrinkers = 0;.
Note though that this might produce incorrect results depending upon what happens when the value isn't set in case the above mentioned block of code isn't entered/breaks off before setting a value.
For the conversion:
Math.round(citrusEnergyDrinkers) is most likely preferable to a simple cast to int, since double most of the time has some imprecision due to the way it's stored in memory and round will actually round the value, while a cast will simply remove the frictional part. For example:
(int) 0.75 //produces 0
Math.round(0.75) //produces 1
You could multiply the double by 100 and then cast to an int:
double d = .77583495;
int perc = (int) Math.round( d );
I prefer to not cast like that, but it works.
Good luck.

Define an array and pass each point 2 values?

I want to define a grid in which I specify an (x,y) coordinate for each point in the grid. So I want to do something like this:
int [][] pt;
for (x=0; x<numX; x=x+1) {
for (y=0; y<numY; y=y+1) {
pt[x][y] = {xval, yval};
}
}
The reason why is because I am mapping the values of an orderly grid to a disorderly grid. The above code of course causes an exception (unexpected token "{").
What is the best way to do what I'm trying to do? Thanks.
Two things:
You havent initialized your array (maybe you did just didnt put in code)
You are trying to put two values into a place where only one can be held.
Initialize your array like this (if you didnt)
int[][] pt = new int[numX][numY];
To store both values in the array you will need to use an object. The java Point class would be an example of something you could use
Point[][] pt = new Point[numX][numY];
for (x=0; x<numX; x=x+1) {
for (y=0; y<numY; y=y+1) {
pt[x][y] = new Point(xval, yval);;
}
}
You basically want to store a fixed number of values inside every array cell?
Then you are limited with 2 major cases:
Use an object
Java doesn't have user defined value types, so you are forced to use full-blown objects on the heap (with little hope that JVM will be very clever and optimize it, but chances are near zero), be it an array, or any other class.
If both of your values are less than 64 bits, you can pack them in built-in primitive type (such as long) using bitwise arithmetic. (You must be very careful here)
ints are 32 bit, so you can pack 2 ints in 1 long.
pt[x][y] = {xval, yval} is illegal, pt[][] is a double dimensional array. It only can store one value. Just like this pt[x][y] = value
You may try java map.

Java typecasting for retrieving integer part of a double as double

I sometimes tend to use (double)(long)(a*b/c) to store the integer part of the result as double. This works well for negative numbers too.
Is there any better way to achieve the same thing as I believe typecasting is a costly operation.
Please note I'm looking for Integer part of the number and not the rounded value.
For eg :
MyObj.setDouble((double)(long)(522.99))
MyObj.getDouble() returns 522.0 and not 523.0
Thanks.
Try Math.rint(double) or Math.round(double). Regardless of performance differences it's at least more clear and concise.
[Edit]
In response to your clarified question - "how do I get the integer part of a double without casting" (despite your title asking about rounding), try this:
public static double integerPart(double d) {
return (d <= 0) ? Math.ceil(d) : Math.floor(d);
}
integerPart(522.99); // => 522d
integerPart(-3.19); // => -3d
Of course, this form is likely no faster than casting since it's using a comparison and a method call.
Performance is not an issue here. But code (double)(long)(a*b/c) is ugly. You actually do not need casting at all if you assign the result to `double variable:
double d = a*b/c; exactly the same as double d = (double)(long)a*b/c;
You actually never need to perform casting when moving from lower to upper types. It is correct for primitives (e.g. int -> double) and for classes (e.g. ArrayList -> List).
What about Math.floor(double) I cant see the difference between integer part and rouding it down.

Strange Java error- float getting set to Infinity

I'm new to Java, and I'm using Processing to make some data visualizations. I'm getting this strange error in my code though, was wondering if anyone could help me out. It seems the Xspacing float keeps getting set to Infinity, however when I print out the expression it gets set to the proper value gets printed...
float Xspacing = (endX-(width*.04) - startX)/ values;
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Result is:
49.0
Infinity
Any help would be appreciated!
Sorry, I wrote this out very quickly and omitted some pretty necessary info:
49.0 IS what is should be. All other types are floats, besides values which is an integer.
The code DOES compile, and println is build into Processing, which is the framework (correct term?) that I'm using. It is basically a function that prints to the console in the Processing GUI.
Xspacing was intended to be data for my class "Graph," however when I define the variable within a public function "drawBasic" everything works fine. Now I am just curious....
Using System.out.println(0 yields the same results. Initial values or variables are:
float startX = 120.00001
float endX = 740.0
int values = 12
width is an integer (although not explicit) that is set to 800
The odd thing seems to be that within a function definition this works fine, its only when I try to define it within the class that it doesn't work...
Your code couldn't be like that because a number *.04 creates a double, and that would mean you'd need to cast the expression into a float.
For your code to compile it would have to be something like
float Xspacing = (float)((endX-(width*.04) - startX)/ values);
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Now, on the result. If your code had, for example:
System.out.println(3/0);
Java would give you a java.lang.ArithmeticException: / by zero
However, if you have
System.out.println(3f/0);
Then Java will give you "Infinity". Why? http://grouper.ieee.org/groups/754/
Try this:
float Xspacing = (endX-(width*.04) - startX)/ values;
println((float)((endX-(width*.04) - startX)/ values));
println(Xspacing);
float Xspacing = (endX-(width*.04) - startX)/ values;
Even assuming the variables are floats that line does not compile, because of the 0.4 double literal.
Also 'println' is not a standalone method, so you must have written your own.
What is your actual code?
you forget a ) and you should've put System.out.println(xspacing);
fyi you can also just type syso and ctrl spacebar and it will print out the print statement for you.

Categories

Resources