Results 1 to 12 of 12

Thread: Integer Usage

  1. #1
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default Integer Usage

    I'm trying to assign integer values to letters and then use those letters to compute a simple equation. Here's what I have:
    Java Code:
    public class Math {
         private int a = 53;
         private int b = 40;
         private int c = 36;
         private int d = 12;
         private int e = 27;
        
        public Math() {
        
        a = 53;
        b = 40;
        c = 36;
        d = 12;
        e = 27;
       
    }
        public void main(String[] args)
        {
        	System.out.println(a + b + c + d + e);
    	}
    }
    It compiles fine, but then when I try to run it I get this:

    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.


    I have no idea why it won't run! =(

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    You're going to run into a few other problems, but lets get the first one out of the way. The correct signature for main is:
    Java Code:
    public [B]static[/B] void main(String[] args) {....}
    Fix that and then we can explore your future problems with this code. Report back your error(s) and you reasoning why you might be receiving them.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    Yeah, when I use public static void main I get these errors:

    C:\JavaPrograms\Math.java:19: non-static variable e cannot be referenced from a static context
    System.out.println(a + b + c + d + e);

    It's telling me that a non-static variable can't be referenced in the static main method. How would I go about referencing them then?

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    K, now you can do this in one of a multitude of ways, depending on your design intent. Here's two ways.

    1) Set all your fields to static(put the static keyword before each int keyword in your class).
    or
    2) Instantiate a Math object and reference the fields to compute the values(Math m = new Math(); System.out.println(m.a);).

    See how you do with that.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    Okay, I got everything working for that part. Now for this next part I need it to compute the average of the integers and display it in decimal form using casting. I'm really having a hard time grasping the whole concept of casting. I know that if a number will not go into another number evenly, because both numbers are integers, the result has to be an integer, and it is just the answer truncated to the nearest integer in the direction of 0. However, how would I go about converting the answer this program gives of "33" into the actual answer in decimal form?

    Here is the code I have:
    Java Code:
    public class Math {
      static int a = 53;
      static int b = 40;
      static int c = 36;
      static int d = 12;
      static int e = 27;
    
    public static void computeSum()
    {
    
    
    	System.out.println("The Sum Is:");
    	System.out.println(a + b + c + d + e);
    	
    }
    
    public static void computeAverage()
    {
    	System.out.println("The Average Is:");
    	System.out.println((a + b + c + d + e) / 5);
    }
        public static void main(String[] args)
        {
            
    	computeSum();
    	computeAverage();
    
    }
    }

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    blue, please review this tutorial and even this example. Use Google until you're satisfied you've discovered your understanding. Simply put, casting is the forcing of one type to another.

    And for your problem.. in that situation you want to cast the computation in computeAverage() to a double. However, not to confuse you, but this will not work as trying to cast an int - which has already been truncated, will not produce your desired effect. Instead, you might want to change every int you already have into a double(static double a = 53; ). From there, you can down-cast to an int if you need it. Is there a reason you're using int's specifically? You can't up-cast from an int to a double and expect precision.... something to consider.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  7. #7
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    Well, here's what I came up with. For the average, instead of assigning double to each integer, I just divided by (double)5. Here it is:

    Java Code:
    public class Math 
    {
      static int a = 53;
      static int b = 40;	
      static int c = 36;
      static int d = 12;
      static int e = 27;
      
    
    public static void computeSum() 
    {
    	System.out.println("The Sum Is:");
    	System.out.println(a + b + c + d + e);	
    }
    
    public static void computeAverage()			
    {                                   		
    	System.out.println("The Average Is:");
    	System.out.println((a + b + c + d + e) / (double)5);
    }
        public static void main(String[] args)	
        {                                       
        	computeSum();
    		computeAverage();
    	}
    }
    Compiles and runs perfectly, displaying the correct 33.6 as the average. Thanks for all your help.

  8. #8
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Quote Originally Posted by bluegreen7hi View Post
    Well, here's what I came up with. For the average, instead of assigning double to each integer, I just divided by (double)5. Here it is:

    Java Code:
    public class Math 
    {
      static int a = 53;
      static int b = 40;    
      static int c = 36;
      static int d = 12;
      static int e = 27;
      
    
    public static void computeSum() 
    {
        System.out.println("The Sum Is:");
        System.out.println(a + b + c + d + e);    
    }
    
    public static void computeAverage()            
    {                                           
        System.out.println("The Average Is:");
        System.out.println((a + b + c + d + e) / (double)5);
    }
        public static void main(String[] args)    
        {                                       
            computeSum();
            computeAverage();
        }
    }
    Compiles and runs perfectly, displaying the correct 33.6 as the average. Thanks for all your help.
    Lol, yep, that works too. Congratulations, you've just hacked. :) What I said about up and down casting still holds.. which, from what I gather you might already have an idea about. As you've just proven, there's many ways to do one task, especially in Java and the route you take for that task can depend on any number of factors(design, taste, etc.).

    Good job. ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  9. #9
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    Instead of using

    System.out.println((a + b + c + d + e) / (double)5);:confused:

    You can also use

    System.out.println((a + b + c + d + e) / 5.0);:p

    which is also a double value:cool:

  10. #10
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Quote Originally Posted by sandeeprao.techno View Post
    ...

    You can also use

    System.out.println((a + b + c + d + e) / 5.0);:p
    doh!..... :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  11. #11
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    I agree with CaptainMorgan that you should just use all doubles. Say we wanted to make a tester class in which the user can input values for the 5 numbers, so they can get the average. What happens if they input 92.5? You're always better of using doubles in math programs. If you want to make it so that in the tester the numbers appear as ints because it looks nicer, just cast. Like if you were to make a program that finds the original function after the user gives it 2 roots, and you don't want it to look ugly like 1.0x^2 + 2.0x + 3.0, just cast each to an int. This is a good practice in math programs because decimals are very common of course.

  12. #12
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Literals

    Hello everyone

    Quote Originally Posted by sandeeprao.techno
    You can also use
    Java Code:
    System.out.println((a + b + c + d + e) / 5.0);
    When I use literals of type double I like to do this:
    Java Code:
    System.out.println([COLOR="RoyalBlue"](double)[/COLOR](a + b + c + d + e) / 5[COLOR="RoyalBlue"]d[/COLOR]);
    This shows me that the brackets result in a double and the 5 is a double. So no conversions is needed before devision.

    Hope this helped. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. How to determine CPU usage using Java?????
    By JavaEmpires in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:49 AM
  2. Replies: 0
    Last Post: 03-30-2008, 11:09 AM
  3. Timer usage in java program
    By sandeeprao.techno in forum New To Java
    Replies: 5
    Last Post: 01-24-2008, 08:16 PM
  4. Comm API Usage
    By hobbyist in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 05:59 PM
  5. Replies: 0
    Last Post: 09-26-2007, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •