Method always returns zero

I'm pretty new to java and I'm trying to create a program that quizzes users on the difference between 2 random frequencies. Everything works except that when I try to get the difference of 2 frequencies the answer is always 0. How do I get it to display the actual difference? Here is the class I wrote to create the tones ans compute the difference:

public class Quiz{

    private PitchPlay one = new PitchPlay();
    private PitchPlay two = new PitchPlay();

    private int frequencyOne;
    private int frequencyTwo;

    private int dif = new Integer(Math.abs(frequencyTwo - frequencyOne));

public int run(){
    frequencyOne = new Integer((int)(Math.random() * 5000 + 50));
    frequencyTwo = new Integer((int)(Math.random() * 5000 + 50));
    return this.dif;
}

public int freqDif(){
    return this.dif;
}

public void playQuiz(){
    one.play(one.note(frequencyOne, 2, 15));//note(frequency, duration, volume)
    two.play(two.note(frequencyTwo, 2, 15));
}
}

And here is the class where the quiz class is used:

public class Action implements ActionListener{
Quiz one = new Quiz();

public void actionPerformed (ActionEvent e){
        if(e.getSource() == playSoundButton)
        {
            if(answerResponse.getText().compareTo("Correct!")  == 0 || answerResponse.getText().compareTo("Play and Listen...") == 0)
            {
                one.run();
                one.playQuiz();
            }
            else
            {
                one.playQuiz();
            }
        }
        if(e.getSource() == submitButton)
        {   
            String responseText = new String(responseField.getText());
            if(responseText !=null && !"".equals(responseText)){
                try{
                    Integer responseNumber = Integer.parseInt(responseText);

            if(responseNumber == one.freqDif())
            {
                    answerResponse.setText("Correct!");
                    answerResponse.setVisible(true);
            }
            else
            {
                answerResponse.setText("Wrong Answer. The difference is " + one.freqDif() + " hertz.");
                answerResponse.setVisible(true);
            }
                }catch(NumberFormatException f){
                    f.printStackTrace(System.out);
                }
            }



        }

    }
}

Answers


private int dif = new Integer(Math.abs(frequencyTwo - frequencyOne));

This is calculated when the variable is created, not when you use the variable. You need to do the calculation after assigning the variables.


There are several issues with your code:

  1. As others have mentioned, the line

    private int dif = new Integer(Math.abs(frequencyTwo - frequencyOne));
    

    creates a variable and assigns a value calculated from two other variables. The values of the other variables at the time of creation are used. You need to recalculate every time the values of those two variables changes. This does not happen automatically for you.

  2. new Integer(...) creates an Integer object. When you subsequently assign this object to an int variable, the value is taken out of the object. This adds some unnecessary memory and run-time over head to your code. Instead, you should assign directly to your variables. For example,

    frequencyOne = (int)(Math.random() * 5000 + 50);
    

Need Your Help

Is it possible to create an Android Service that listens for hardware key presses?

android listener keyevent

I'd like to run an Android background service that will act as a keylistener from the home screen or when the phone is asleep. Is this possible?

Missing Clickonce prerequisites in Visual Studio 2013

visual-studio-2013 clickonce

I have installed Visual Basic PowerPacks 10.0. It appears as a Clickonce Prerequisite in Visual Studio 2010 but in Visual Studio 2013 Professional It has the yellow triangle next to it and an erro...