Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-23-2008, 05:02 AM
Member
 
Join Date: Nov 2007
Posts: 12
Rep Power: 0
bluegreen7hi is on a distinguished road
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:
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! =(
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-23-2008, 05:09 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
Rep Power: 3
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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:
Code:
public static 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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-23-2008, 05:13 AM
Member
 
Join Date: Nov 2007
Posts: 12
Rep Power: 0
bluegreen7hi is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-23-2008, 05:18 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
Rep Power: 3
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-23-2008, 05:43 AM
Member
 
Join Date: Nov 2007
Posts: 12
Rep Power: 0
bluegreen7hi is on a distinguished road
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:
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();

}
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-23-2008, 06:09 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
Rep Power: 3
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-23-2008, 06:15 AM
Member
 
Join Date: Nov 2007
Posts: 12
Rep Power: 0
bluegreen7hi is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-23-2008, 06:19 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
Rep Power: 3
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
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:

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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-23-2008, 06:46 AM
Member
 
Join Date: Jan 2008
Posts: 22
Rep Power: 0
sandeeprao.techno is on a distinguished road
Default
Instead of using

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

You can also use

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

which is also a double value
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-23-2008, 06:49 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
Rep Power: 3
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
Originally Posted by sandeeprao.techno View Post
...

You can also use

System.out.println((a + b + c + d + e) / 5.0);
doh!.....
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-24-2008, 03:46 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-24-2008, 08:54 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 337
Rep Power: 2
tim is on a distinguished road
Default Literals
Hello everyone

Originally Posted by sandeeprao.techno
You can also use
Code:
System.out.println((a + b + c + d + e) / 5.0);
When I use literals of type double I like to do this:
Code:
System.out.println((double)(a + b + c + d + e) / 5d);
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.
__________________
Grudges are heavy. Why hold them when you can let them go?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Look-Up Method Injection and it’s usage in Spring Framework Java Tip Java Tips 0 03-30-2008 10:09 AM
Timer usage in java program sandeeprao.techno New To Java 5 01-24-2008 07:16 PM
How to determine CPU usage using Java????? JavaEmpires New To Java 2 01-10-2008 11:19 PM
Comm API Usage hobbyist New To Java 0 11-16-2007 04:59 PM
Look-Up Method Injection and it’s usage in Spring Framework JavaBean Java Tips 0 09-26-2007 08:40 PM


All times are GMT +2. The time now is 12:12 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org