Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When I change the list option in the code below, java picks up my input and gives me an answer that is really weird. This is the code:
package tempconverter;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import static java.lang.System.out;
public class TempConverter extends JFrame implements ActionListener {
final static String[] inList = {"From Celsius", "From Fahrenheit", "From Kelvin"};
static JFrame f = new JFrame();
static JTextField enter = new JTextField(3);
static JButton confirm = new JButton("Convert");
static JList choose = new JList(inList);
public static void main(String[] args) {
confirm.addActionListener(new TempConverter());
f.setLayout(new FlowLayout());
f.setSize(300, 60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(enter);
f.add(confirm);
f.add(choose);
f.setVisible(true);
}
#Override
public void actionPerformed (ActionEvent e) {
Object choice = choose.getSelectedValue();
double toConvert = Double.parseDouble(enter.getText());
double inF, inK, inC;
if (choice.equals("From Celsius")) {
inF = toConvert * 1.8 + 32;
inK = toConvert + 273.15;
out.println("In degrees Fahrenheit, " + toConvert + " degrees Celsius would be " + inF + " degrees.");
out.println("In degrees Kelvin, " + toConvert + " degrees Celsius would be " + inK + " degrees.");
}
if (choice.equals("From Fahrenheit")) {
inC = toConvert - 32 / 1.8;
inK = toConvert - 32 / 1.8 - 273.15;
out.println("In degrees Celsius, " + toConvert + " degrees Fahrenheit would be " + inC + " degrees.");
out.println("In degrees Kelvin, " + toConvert + " degrees Fahrenheit would be " + inK + " degrees.");
}
if (choice.equals("From Kelvin")) {
inC = toConvert - 273.15;
inF = inC + 1.8 + 32;
out.println("In degrees Celsius, " + toConvert + " degrees Kelvin would be " + inC + " degrees.");
out.println("In degrees Fahrenheit, " + toConvert + " degrees Kelvin would be " + inF + " degrees.");
}
}
}
How do I make java realise that I changed the list selection?
An example I tested is that I set the selection to "From Fahrenheit" and typed 32, but it gave me 14.222222222222221 degrees Celsius and -258.92777777777775 degrees Kelvin.
It should be +273.15 instead of -273.15, and typo with *. Change mentioned in comment
if (choice.equals("From Fahrenheit")) {
inC = toConvert - 32 / 1.8;
inK = toConvert - 32 / 1.8 + 273.15;
// change here ............^......
out.println("In degrees Celsius, " + toConvert
+ " degrees Fahrenheit would be " + inC + " degrees.");
out.println("In degrees Kelvin, " + toConvert
+ " degrees Fahrenheit would be " + inK + " degrees.");
}
if (choice.equals("From Kelvin")) {
inC = toConvert - 273.15;
inF = inC * 1.8 + 32;
// .......^.... change here
out.println("In degrees Celsius, " + toConvert
+ " degrees Kelvin would be " + inC + " degrees.");
out.println("In degrees Fahrenheit, " + toConvert
+ " degrees Kelvin would be " + inF + " degrees.");
}
First you have wrong calculation here.
inF = inC + 1.8 + 32;
here is working code.
if (choice.equals("From Kelvin")) {
inC = toConvert - 273.15;
inF = (inC * 1.8) + 32;
out.println("In degrees Celsius, " + toConvert + " degrees Kelvin would be " + inC + " degrees.");
out.println("In degrees Fahrenheit, " + toConvert + " degrees Kelvin would be " + inF + " degrees.");
}
if (choice.equals("From Fahrenheit")) {
inC = toConvert - 32 / 1.8;
inK = (toConvert - 32 / 1.8) + 273.15;
out.println("In degrees Celsius, " + toConvert + " degrees Fahrenheit would be " + inC + " degrees.");
out.println("In degrees Kelvin, " + toConvert + " degrees Fahrenheit would be " + inK + " degrees.");
}
Related
I recently tried converting a Turing program to java, but it hasn't worked out as well as I've planned. Here's the turing program.
var rgr, peri, semiperi, area, radcirc, radinsc, angle1, angle2, angle3 :
real
var nextpage : char
var input1, input2, input3 : int
put " "
put "Input side length a; make sure it's a positive integer. (in centimetres)"
get input1
put "Input side length b; make sure it's a positive integer. (in centimetres)"
get input2
put "Input side length c; make sure it's a positive integer. (in centimetres)"
get input3
% Now here is where the cal-goo-lations are performed
peri := input1 + input2 + input3
semiperi := peri / 2
area := semiperi * (semiperi - input1) * (semiperi - input2) * (semiperi - input3)
radcirc := (input1 * input2 * input3) / (4 * area)
radinsc := (2 * area) / peri
angle1 := arccosd ((input2 ** 2 + input3 ** 2 - input1 ** 2) / (2 * input2 * input3))
angle2 := arccosd ((input3 ** 2 + input1 ** 2 - input2 ** 2) / (2 * input3 * input1))
angle3 := arccosd ((input1 ** 2 + input2 ** 2 - input3 ** 2) / (2 * input1 * input2))
put " "
delay (500)
cls
put "PAGE #1: The Basics"
put " "
put "1. This triangle's perimeter is..."
put peri, " cm"
put " "
put "2. This triangle's semi-perimeter is..."
put semiperi, " cm"
put " "
put "3. This triangle's area is..."
put area, " cm squared"
put " "
delay (500)
cls
put "PAGE #2: Circumscribed/Inscribed Circles"
put " "
put "4. The radius of the circumscribed circle is..."
put radcirc, " cm"
put " "
put "5. The radius of the inscribed circle is..."
put radinsc, " cm"
put " "
delay (500)
cls
put "PAGE #3: Angles"
put " "
put "6. The three angles are..."
put "Angle 1's angle is ", angle1:0:1
put "Angle 2's angle is ", angle2:0:1
put "Angle 3's angle is ", angle3:0:1
put " "
put "Thanks for using this program!"
So in the turing program, the angle calculation worked. But, when I tried to convert it to java it wouldn't. Here's my code.
import java.lang.Math;
class JavaTriangleCalg00lations
{
public static void main (String[] args)
{
int posInt1, posInt2, posInt3 ;
double peri, sP, area, circumR, inscrR, angle1, angle2, angle3, dposInt1, dposInt2, dposInt3;
System.out.println("Please put in 3 positive integers.");
System.out.println(" ");
do
{
System.out.println("Enter the 1st integer; positive only.");
posInt1= In.getInt();
} while (posInt1 <= 0);
{
do
{
System.out.println("Enter the 2nd integer; positive only.");
posInt2= In.getInt();
} while (posInt2 <= 0);
}
{
do
{
System.out.println("Enter the 3rd integer; positive only.");
posInt3= In.getInt();
} while (posInt3 <= 0);
System.out.println(" ");
//ayy yo lmao xD
dposInt1 = posInt1;
dposInt2 = posInt2;
dposInt3 = posInt3;
peri = posInt1 + posInt2 + posInt3;
sP = peri / 2;
area = sP * (sP- posInt1) * (sP - posInt2) * (sP - posInt3);
circumR = (posInt1 * posInt2 * posInt3) / (4 * area);
inscrR = (2 * area) / peri;
angle1 = Math.toDegrees(Math.acos((dposInt2 * dposInt2) + (dposInt3 * dposInt3) - (dposInt1 * dposInt1)) / (2 * dposInt2 * dposInt3));
angle2 = Math.toDegrees(Math.acos((dposInt3 * dposInt3) + (dposInt1 * dposInt1) - (dposInt2 * dposInt2)) / (2 * dposInt3 * dposInt1));
angle3 = Math.toDegrees(Math.acos((dposInt1 * dposInt1) + (dposInt2 * dposInt2) - (dposInt3 * dposInt3)) / (2 * dposInt1 * dposInt2));
}
System.out.print("The Perimeter is... ");
System.out.print(peri);
System.out.print("The Semi-Perimeter is... ");
System.out.print(sP);
System.out.print("The Area is... ");
System.out.print(area);
System.out.print("The circumscribed radius is... ");
System.out.print(circumR);
System.out.print("The inscribed radius is... ");
System.out.print(inscrR);
System.out.println(" ");
System.out.println("The three angles are... ");
System.out.println("Angle 1... ");
System.out.println(Math.round(angle1 * 100) / 100);
System.out.println("Angle 2... ");
System.out.println(Math.round(angle2 * 100) / 100);
System.out.println("Angle 3... ");
System.out.println(Math.round(angle3 * 100) / 100);
System.out.println(" ");
System.out.println("Thanks for using this program!");
}
}
And for some reason the numbers were inaccurate. For example; when I entered the integers 6, 8, 10; it said the angles were 90 degrees, 90 degrees, 90 degrees.
To read input from the user through the shell, use the Scanner class.
first step : Important scanner class (goes at top of your program) import java.util.Scanner;
Second step : Instantiate scanner Scanner scanner = new Scanner(System.in);place at line 5 (above those variable declarations)
Third step : Search and replace. Where you see In.getIn() replace with scanner.nextInt() or scanner.nextDouble() Depending on if the variable is of type int or double
Edit: This was not your question. Please add the formulas you are using to calculate perimeter please, thanks!
Nevermind; I realized after hours of pondering that the error. It was careless use of brackets.
The code that worked was...
angle1 = Math.toDegrees(Math.acos((dposInt2 * dposInt2 + dposInt3 * dposInt3 - dposInt1 * dposInt1) / (2 * dposInt2 * dposInt3)));
angle2 = Math.toDegrees(Math.acos((dposInt3 * dposInt3 + dposInt1 * dposInt1 - dposInt2 * dposInt2) / (2 * dposInt3 * dposInt1)));
angle3 = Math.toDegrees(Math.acos((dposInt1 * dposInt1+ dposInt2 * dposInt2 - dposInt3 * dposInt3) / (2 * dposInt1 * dposInt2)));
Compared to...
angle1 = Math.toDegrees(Math.acos((dposInt2 * dposInt2) + (dposInt3 * dposInt3) - (dposInt1 * dposInt1)) / (2 * dposInt2 * dposInt3));
angle2 = Math.toDegrees(Math.acos((dposInt3 * dposInt3) + (dposInt1 * dposInt1) - (dposInt2 * dposInt2)) / (2 * dposInt3 * dposInt1));
angle3 = Math.toDegrees(Math.acos((dposInt1 * dposInt1) + (dposInt2 * dposInt2) - (dposInt3 * dposInt3)) / (2 * dposInt1 * dposInt2));
Using the example inputs I used previously (6, 8, 10), The angles outputed to 36.86989764584401, 53.13010235415598 and 90; which is as close as it'll get to 180 degrees to complete a triangle. Thanks to those who tried to help me!
I'm having an issue with my function call. I'm trying to find the solution to the two-point boundary value problem x' = f(t,x) = x + 0.09 x 2 + cos(10 t) with boundary condition x(0) + x(1) - 3.0 = 0 using the secant and third-order Runge-Kutta methods and for whatever reason, my equation method gives me four errors on a single line, those being that it's not a statement, is missing two semicolons, and is missing an end parenthesis.
public class BoundaryValueProblem
{
public static double f(double t, double x)
{
return x + 0.09x^2 + Math.cos(10t);
}
public static void findZero
{
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
BoundaryValueProblem FZ = new BoundaryValueProblem();
f1 = FZ.f(1.0, 1.0);
f2 = FZ.f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while(abs(x5 + x6 - 3.0) < 1e-5)
{
x4 = x6 - f2 * ((x6 - x5)/(f2 - f1));
fx = FZ.f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}
public void rkm(double x0, double t0, double h)
{
double x1, x2, x3;
int i=0;
double a1 = 0.5;
double a2 = 0.25;
double c0 = 2.0/3.0;
double c1 = 5.0/3.0;
double c2 = -4.0/3.0;
double b21 = -.25;
double b10 = .5;
double b20 = .5;
double stepsize = .025;
System.out.println("Runge-Kutta Method:");
System.out.println("i: \t\t h: \t\t t0: \t\t x0:");
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0 );
for(i = 0; i < 40; i++)
{
x1 = x0 + h * b10 * f(t0, x0);
x2 = x0 + h * (b20 * f(t0, x0) + b21 * f(t0 + a1 * h, x1));
x3 = x0 + h * (c0 * f(t0, x0) + c1 * f(t0 + a1 * h, x1) + c2 * f(t0 + a2 * h, x2));
t0 = t0 + stepsize;
x0 = x3;
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0 );
}
System.out.println();
}
public static void main(String[] args)
{
BoundaryValueProblem FZ1 = new BoundaryValueProblem();
FZ1.findZero();
BoundaryValueProblem RKM1 = new BoundaryValueProblem();
RKM1.rkm(1.0, 0.0, 0.0);
}
}
I've had it work on simpler equations but this one is not working for me. I have the rest of the code mostly correct (I believe), but I don't think it's the cause of the error since I commented it out and the errors persisted, thus I'm not focused on that. Any advice or help would be greatly appreciated.
Below is fixed code. Please remember that java unlike Octave or Matlab requires multiplication operator (*) and doesn't support pow operator (^) - you would need to use Math.pow() instead.
public class BoundaryValueProblem {
public static double f(double t, double x) {
return x + 0.09 * x * x + Math.cos(10 * t);
}
public static void findZero() {
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
f1 = f(1.0, 1.0);
f2 = f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while (Math.abs(x5 + x6 - 3.0) < 1e-5) {
x4 = x6 - f2 * ((x6 - x5) / (f2 - f1));
fx = f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}
public void rkm(double x0, double t0, double h) {
double x1, x2, x3;
int i = 0;
double a1 = 0.5;
double a2 = 0.25;
double c0 = 2.0 / 3.0;
double c1 = 5.0 / 3.0;
double c2 = -4.0 / 3.0;
double b21 = -.25;
double b10 = .5;
double b20 = .5;
double stepsize = .025;
System.out.println("Runge-Kutta Method:");
System.out.println("i: \t\t h: \t\t t0: \t\t x0:");
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0);
for (i = 0; i < 40; i++) {
x1 = x0 + h * b10 * f(t0, x0);
x2 = x0 + h * (b20 * f(t0, x0) + b21 * f(t0 + a1 * h, x1));
x3 = x0 + h * (c0 * f(t0, x0) + c1 * f(t0 + a1 * h, x1) + c2 * f(t0 + a2 * h, x2));
t0 = t0 + stepsize;
x0 = x3;
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0);
}
System.out.println();
}
/**
* #param args
*/
public static void main(String[] args) {
BoundaryValueProblem FZ1 = new BoundaryValueProblem();
FZ1.findZero();
BoundaryValueProblem RKM1 = new BoundaryValueProblem();
RKM1.rkm(1.0, 0.0, 0.0);
}
}
public static void findZero// correct this. Where are parenthesis () for this method
{
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
BoundaryValueProblem FZ = new BoundaryValueProblem();
f1 = FZ.f(1.0, 1.0);
f2 = FZ.f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while(abs(x5 + x6 - 3.0) < 1e-5)
{
x4 = x6 - f2 * ((x6 - x5)/(f2 - f1));
fx = FZ.f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}
There is a method without parentheses: public static void findZero
It should be public static void findZero()
Function findZero should have parenthesis at the end like this:
public static void findZero()
return x + 0.09x^2 + Math.cos(10t);
In Java you have to put math operator between operands so instead of 0.09x you have to write 0.09*x and instead of 10t you have to write 10*t
^ sign is XOR operator not power operator so you have to use Math.pow function (I'm guessing that's what you wanted to do)
In the end this line should look like this:
return x + Math.pow(0.09*x, 2) + Math.cos(10*t);
I'm also getting error with abs function. I don't know if you wrote that function yourself but if you didn't you should use Math.abs function
I have a question regarding java's initializing, and I do not know why java thinks that the double in my program is not initialized, even though I have initialized it. Can someone please explain to me how to resolve this problem? In the code below, java says that double "inC" is not initialized.
package tempconverter;
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.BorderLayout;
public class TempConverter extends JFrame implements ActionListener, ItemListener
{
static String[] choices = { "From Celsius", "From Fahrenheit", "From Kelvin", "From Rankine" };
static JFrame f = new JFrame("Temperature Converter");
static JTextField enter = new JTextField(5);
static JButton confirm = new JButton("Convert");
static JComboBox choose = new JComboBox(choices);
static JFrame tell = new JFrame();
static JLabel one = new JLabel();
static JLabel two = new JLabel();
static JLabel three = new JLabel();
public static void main(String[] args)
{
Font main = new Font("Comic Sans", Font.PLAIN, 12);
confirm.addActionListener(new TempConverter());
confirm.setFont(main);
choose.setFont(main);
enter.setFont(main);
choose.addItemListener(new TempConverter());
f.setBackground(Color.WHITE);
f.setLayout(new FlowLayout());
f.setSize(340, 60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(enter);
f.add(confirm);
f.add(choose);
tell.setLayout(new BorderLayout());
tell.add(one, BorderLayout.NORTH);
tell.add(two, BorderLayout.WEST);
tell.add(three, BorderLayout.SOUTH);
tell.setSize(160, 65);
tell.setLocationRelativeTo(null);
f.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{
double toConvert = Double.parseDouble(enter.getText());
double inF, inK, inC, tempF = 0, tempK, inR, tempR;
double tempC;
if (choose.getSelectedItem().equals("From Celsius"))
{
tempF = toConvert * 1.8 + 32;
tempK = toConvert + 273.15;
tempR = (toConvert + 273.15) * 1.8;
inF = Math.round(tempF * 100.00) / 100.00;
inK = Math.round(tempK * 100.00) / 100.00;
inR = Math.round(tempR * 100.00) / 100.00;
one.setText(toConvert + "°C = " + inF + "°F");
two.setText(toConvert + "°C = " + inK + "°K");
three.setText(toConvert + "°C = " + inR + "°R");
tell.setVisible(true);
}
if (choose.getSelectedItem().equals("From Fahrenheit"))
{
if (toConvert == 32)
{
inC = 0;
inK = 273.15;
inR = toConvert + 459.67;
one.setText(toConvert + "°F = " + inC + "°C");
two.setText(toConvert + "°F = " + inK + "°K");
three.setText(toConvert + "°F = " + inR + "°R");
}
else
tempC = (toConvert - 32) / 1.8;
tempK = (toConvert - 32) / 1.8 + 273.15;
tempR = toConvert + 459.67;
inC = Math.round(tempC * 100.00) / 100.00;
inK = Math.round(tempK * 100.00) / 100.00;
inR = Math.round(tempR * 100.00) / 100.00;
one.setText(toConvert + "°F = " + inC + "°C");
two.setText(toConvert + "°F = " + inK + "°K");
three.setText(toConvert + "°F = " + inR + "°R");
tell.setVisible(true);
}
if (choose.getSelectedItem().equals("From Kelvin"))
{
tempC = toConvert - 273.15;
tempF = tempC * 1.8 + 32;
tempR = toConvert * 1.8;
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempR * 100.00) / 100.00;
one.setText(toConvert + "°K = " + inC + "°C");
two.setText(toConvert + "°K = " + inF + "°F");
three.setText(toConvert + "°K = " + inR + "°R");
tell.setVisible(true);
}
if (choose.getSelectedItem().equals("From Rankine"))
{
if (toConvert == 0)
{
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText("0°R = -273.15°C");
two.setText("0°R = " + inF + "°F");
three.setText("0°R = " + inR + "°K");
tell.setVisible(true);
}
else
tempC = (toConvert - 491.67) * (5 / 9);
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText(toConvert + "°R = " + inC + "°C");
two.setText(toConvert + "°R = " + inF + "°F");
three.setText(toConvert + "°R = " + inR + "°K");
tell.setVisible(true);
}
}
#Override
public void itemStateChanged(ItemEvent e)
{
repaint();
}
}
Java points out that I have not initialized "inC" in the else statement of the "From Fahrenheit" and the "From Rankine" method. However, when I initialize it in the variable declaration, it doesn't change when I try to change it in the if-else statement and the methods. How do I fix this?
I have copied yout example and i get the message at tempC. The reason is a missing } at the else path.
Change :
else
tempC = (toConvert - 491.67) * (5 / 9);
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText(toConvert + "°R = " + inC + "°C");
two.setText(toConvert + "°R = " + inF + "°F");
three.setText(toConvert + "°R = " + inR + "°K");
tell.setVisible(true);
TO:
else {
tempC = (toConvert - 491.67) * (5 / 9);
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText(toConvert + "°R = " + inC + "°C");
two.setText(toConvert + "°R = " + inF + "°F");
three.setText(toConvert + "°R = " + inR + "°K");
tell.setVisible(true);
}
As pointed out above the easiest thing would be to simply assign a value to it when you declare it.
The reason that Java thinks that it has not been initialized is that it might actually not have. You only ever assign it a value in one of the 4 outer most if-statements. If none of these four conditions are true, the variable will never be initialized.
And as Jens has also pointed out, you are missing a { after the "else".
The actual cause of the error is dull: you're missing a brace.
But, let's look at tempK = toConvert * (5 / 9);. This will not do what you want since (5 / 9) will evaluate to 0 as the parentheses mean this is evaluated on its own and in integer arithmetic. To solve this, the easiest thing to do is remove the parentheses:
tempK = toConvert * 5 / 9;
so everything is computed in floating point. (Note that * and / have identical precedence).
You make this error a couple of times in your program.
So I have been looking through tutorials (And questions on this site) and have found nothing to solve my problem. Here is the current code I am trying to implement:
private void pick() {
float[] matModelView = new float[16], matProjView = new float[16];
int[] view = new int[16];
float mouseX = width / 2;
float mouseY = height / 2;
Vector3f start = new Vector3f();
Vector3f end = new Vector3f();
FloatBuffer modelBuffer = compileBuffer(matModelView);
FloatBuffer projBuffer = compileBuffer(matProjView);
FloatBuffer startBuffer = compileBuffer(new float[]{start.x, start.y, start.z, 1});
FloatBuffer endBuffer = compileBuffer(new float[]{end.x, end.y, end.z, 1});
IntBuffer viewBuffer = compileBuffer(view);
glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
glGetInteger(GL_VIEWPORT, viewBuffer);
gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
gluUnProject(mouseX, mouseX, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);
start = new Vector3f(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
end = new Vector3f(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));
picks.add(new Vector3f[]{start, end});
System.out.println("Mouse Coords: " + mouseX + ", " + mouseY);
System.out.println("Position: " + position.x + ", " + position.y + ", " + position.z);
System.out.println("Rotation: " + rotation.x + ", " + rotation.y + ", " + rotation.z);
System.out.println("Near Plane: " + start.x + ", " + start.y + ", " + start.z);
System.out.println("Far Plane: " + end.x + ", " + end.y + ", " + end.z);
}
I have the mouseX and Y set the way I do because my mouse is grabbed. Here is an image and some output for you. (I can't really explain the problem)
Mouse Coords: 400.0, 350.0
Position: 0.0, 0.0, 0.0
Rotation: 0.0, 0.0, 0.0
Near Plane: 0.0, 0.0, -0.1
Far Plane: 0.0, 4.2315264, -99.99771
So, for a rotation of X:0 Y:0 Z:0, the expected output of the Y coord should be the same as the input. It is higher. Here is a picture of that output.
Can anyone give me some kind of hint or explanation on why this would happen?
EDIT: Facepalms violently: I was pushing mouseX in the Y parameter of the second gluUnProject
So I'm doing something very simple. I have two instance variables, newX and newY. These are both doubles initialized to 0.0.
During an update loop I perform this calculation:
long now = System.currentTimeMillis();
double elapsed = (now - mNextTime)/1000.0; // convert to seconds
Log.i("nx", newX + " + " + elapsed + " = " + (newX + elapsed));
newX = newX + elapsed;
newY = newY + elapsed;
Here are a few iterations of the logging statement, this is what I can't explain:
1.3073173811609962E9 + 0.058 = 1.3073173812189963E9
1.3073173812189963E9 + 0.112 = 1.3073173813309963E9
1.3073173813309963E9 + 0.02 = 1.3073173813509963E9
1.3073173813509963E9 + 0.018 = 1.3073173813689961E9
1.3073173813689961E9 + 0.018 = 1.307317381386996E9
1.307317381386996E9 + 0.101 = 1.307317381487996E9
Why does 1.307 + 0.112 = 1.307?? I'm very confused.
Notice the E9. You are looking at very large numbers.
1E9 = 1,000,000,000
so
1.3073173811609962E9 + 0.058 = 1,307,317,381.21899623