14 Replies - 980 Views - Last Post: 09 March 2013 - 06:38 PM Rate Topic: -----

#1 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Dr Java does not recognize Math class

Posted 08 March 2013 - 12:46 PM

My Dr Java application, for some reason, does not recognize the Math class. My professor told me to uninstall Dr Java and to reinstall it; will this work? Or do I need to update my compiler as well? If so, how should I go about this? Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Dr Java does not recognize Math class

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 12:49 PM

There's only one way to find out...

Have you installed the JDK?
Was This Post Helpful? 0
  • +
  • -

#3 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 12:52 PM

I'm pretty sure my computer came with some version of it, as I downloaded Dr Java and was able to run it without downloading a JDK seperately. Although could this be my issue?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 12:56 PM

Math class is there still day one of Java so couldn't be the problem even if you had an older JDK
May be Dr Java install a JDK during it's installation process

Now when you say that it does not recognize it... what is there error message ? At which line ?
Was This Post Helpful? 0
  • +
  • -

#5 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 01:07 PM

//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
  public static void main (String[] args)
  {
    
    String input;
    int numbers = 5;
    int sum = 0;
    int average = 0;
    int maximum = 0;
    
    for(int counter = 0; counter<numbers; counter++) 
    {
      input = JOptionPane.showInputDialog("Please enter an integer number.");
      int num = Integer.parseInt(input);
      
      sum += num;
      average = (sum/5);
      maximum = Math.max(num);
    
    }
     JOptionPane.showMessageDialog(null,
                                    "The sum of the numbers is " + sum);
     JOptionPane.showMessageDialog(null,
                                   "The average of the numbers is " + average);
    System.exit(0);
  }
}
      
    



Line 23. error message reads: 1 error found:
File: /Users/jack/Java Programs/numberFacts.java [line: 23]
Error: /Users/jack/Java Programs/numberFacts.java:23: cannot find symbol
symbol : method max(int)
location: class Math

holy cow, I'm really botching this.... let's try one more time. Sorry about this.


//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
  public static void main (String[] args)
  {
    
    String input;
    int numbers = 5;
    int sum = 0;
    int average = 0;
    int maximum = 0;
    
    for(int counter = 0; counter<numbers; counter++) 
    {
      input = JOptionPane.showInputDialog("Please enter an integer number.");
      int num = Integer.parseInt(input);
      
      sum += num;
      average = (sum/5);
      maximum = Math.max(num);
    
    }
     JOptionPane.showMessageDialog(null,
                                    "The sum of the numbers is " + sum);
     JOptionPane.showMessageDialog(null,
                                   "The average of the numbers is " + average);
    System.exit(0);
  }
}



line 23 is the error.

This post has been edited by pbl: 08 March 2013 - 01:05 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 01:09 PM

What is the max() of n
The max() method takes two (2) arguments and returns the largest one

The compiler (not Dr. Java) just does not find the max() method with one parameter located in the Math class
Was This Post Helpful? 2
  • +
  • -

#7 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 01:11 PM

Ahh I see. How would I find the max out of 5 inputted values without variables for each value?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 01:22 PM

And by the way to calculate average 4 times for nothing
You need it fater the loop
    int max = Integer.MIN_VALUE;
    for(.... {
      
        max = Math.max(max, num);
     
    }

    //max contains max of all the num
    average = .... should simply be calculated here


This post has been edited by pbl: 08 March 2013 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#9 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 01:36 PM

Here is my new code.

//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
  public static void main (String[] args)
  {
    
    String input;
    int numbers = 5;
    int sum = 0;
    int average = 0;
    int max = Integer.MIN_VALUE;
    
    
    for(int counter = 0; counter<numbers; counter++) 
    {
      input = JOptionPane.showInputDialog("Please enter an integer number.");
      int num = Integer.parseInt(input);
      
      sum += num;
      
      max = Math.max(max, num);
    }

    average = (sum/5);

     JOptionPane.showMessageDialog(null,
                                    "The sum of the numbers is " + sum);
     JOptionPane.showMessageDialog(null,
                                   "The average of the numbers is " + average);
     JOptionPane.showMessageDialog(null,
                                   "The maximum of the numbers is " + max);
    System.exit(0);
  }
}



I am still getting the "cannot find symbol" error message for line 24, I am confused

Also, is there a way to write this code without using the MIN_VALUE? We have not covered this in class yet. Although I'm sure it would not lose me points, I just don't know its application or understand it, and I'd like to be able to complete the assignment without using parts of code I do not understand.
Was This Post Helpful? 0
  • +
  • -

#10 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 02:31 PM

View PostJackB121, on 08 March 2013 - 08:36 PM, said:

Here is my new code.

//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
  public static void main (String[] args)
  {
    
    String input;
    int numbers = 5;
    int sum = 0;
    int average = 0;
    int max = Integer.MIN_VALUE;
    
    
    for(int counter = 0; counter<numbers; counter++) 
    {
      input = JOptionPane.showInputDialog("Please enter an integer number.");
      int num = Integer.parseInt(input);
      
      sum += num;
      
      max = Math.max(max, num);
    }

    average = (sum/5);

     JOptionPane.showMessageDialog(null,
                                    "The sum of the numbers is " + sum);
     JOptionPane.showMessageDialog(null,
                                   "The average of the numbers is " + average);
     JOptionPane.showMessageDialog(null,
                                   "The maximum of the numbers is " + max);
    System.exit(0);
  }
}



I am still getting the "cannot find symbol" error message for line 24, I am confused

Also, is there a way to write this code without using the MIN_VALUE? We have not covered this in class yet. Although I'm sure it would not lose me points, I just don't know its application or understand it, and I'd like to be able to complete the assignment without using parts of code I do not understand.


Uhm, Integer.MIN_VALUE is the field that represents -2^32, and I'm assuming that is not your intention. I think the original poster meant for you to change that, not just copy and paste the code...
Was This Post Helpful? 0
  • +
  • -

#11 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 02:47 PM

No, that was not my intention. I'm just trying to learn and to get this program to run properly. I still do not understand why I am getting an error message where I pointed it out, nor can I get the program to find the maximum of the 5 integer values, any thoughts?
Was This Post Helpful? 0
  • +
  • -

#12 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 02:53 PM

View PostJackB121, on 08 March 2013 - 09:47 PM, said:

No, that was not my intention. I'm just trying to learn and to get this program to run properly. I still do not understand why I am getting an error message where I pointed it out, nor can I get the program to find the maximum of the 5 integer values, any thoughts?

That's not what Math.max() does. http://docs.oracle.c.../lang/Math.html look at that to find what it actually does. If you really want to do that, every time they enter a value, if that value is greater then some int variable, set the int variable to that value.
Was This Post Helpful? 1
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 04:48 PM

View PostJackB121, on 08 March 2013 - 04:47 PM, said:

I still do not understand why I am getting an error message where I pointed it out, nor can I get the program to find the maximum of the 5 integer values, any thoughts?

I don't see anything wrong with your code in post #9. If you're still seeing an error message, post the exact message and stack trace, copied and pasted.

As for your other question, "Also, is there a way to write this code without using the MIN_VALUE?" The only time the MIN_VALUE is needed is for the first comparison to ensure that max' initial value is less than any possible value input by the user. Do you know, can you use an if/else statement? If so, an option to using MIN_VALUE would be to set max = num when counter == 0, then use max = Math.max( num, max ) thereafter.

Keep coding!
Was This Post Helpful? 1
  • +
  • -

#14 JackB121  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 11
  • Joined: 08-March 13

Re: Dr Java does not recognize Math class

Posted 08 March 2013 - 05:03 PM

Yes we covered if-else statements so far, I had a feeling it would include one but I was not sure. thank you so much for your help!
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Dr Java does not recognize Math class

Posted 09 March 2013 - 06:38 PM

To avoid the use of MIN_VALUE, which mean to avoid to iniatilize max to a value that is the smallest of your group you will have to init it once at the beginning and to loop one iteration less in your loop

//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
  public static void main (String[] args)
  {
    
    String input;
    int numbers = 5;
    int sum;
    int average;
    int max;
 
     input = JOptionPane.showInputDialog("Please enter an integer number.");
     int num = Integer.parseInt(input);
      
      sum = num;
      max =num ;     // init max with first number
    
    
    // not we start with counter = 1 instead of 0
    for(int counter = 1; counter<numbers; counter++) 
    {
      input = JOptionPane.showInputDialog("Please enter an integer number.");
      int num = Integer.parseInt(input);
      
      sum += num;
      
      max = Math.max(max, num);
    }

    average = (sum/5);

     JOptionPane.showMessageDialog(null,
                                    "The sum of the numbers is " + sum);
     JOptionPane.showMessageDialog(null,
                                   "The average of the numbers is " + average);
     JOptionPane.showMessageDialog(null,
                                   "The maximum of the numbers is " + max);
    System.exit(0);
  }
}



View Postnatecat, on 08 March 2013 - 04:31 PM, said:

Uhm, Integer.MIN_VALUE is the field that represents -2^32, and I'm assuming that is not your intention. I think the original poster meant for you to change that, not just copy and paste the code...

To find the max() of 5 number within a loop you need to int max to something
Or you could the horribe
//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
  public static void main (String[] args)
  {
    
    String input;
    int numbers = 5;
    int sum = 0;
    int average = 0;
    int max = Integer.MIN_VALUE;
    
    
    for(int counter = 0; counter<numbers; counter++) 
    {
      input = JOptionPane.showInputDialog("Please enter an integer number.");
      int num = Integer.parseInt(input);
      
      sum += num;
      if(counter == 0)
         max = num;
      else
         max = Math.max(max, num);
    }

    average = (sum/5);

     JOptionPane.showMessageDialog(null,
                                    "The sum of the numbers is " + sum);
     JOptionPane.showMessageDialog(null,
                                   "The average of the numbers is " + average);
     JOptionPane.showMessageDialog(null,
                                   "The maximum of the numbers is " + max);
    System.exit(0);
  }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1