|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

01-23-2008, 06:02 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 12
|
|
|
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:
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! =(
|
|

01-23-2008, 06:09 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
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:
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 July 13, 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)
|
|

01-23-2008, 06:13 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 12
|
|
|
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?
|
|

01-23-2008, 06:18 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
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 July 13, 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)
|
|

01-23-2008, 06:43 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 12
|
|
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:
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();
}
}
|
|

01-23-2008, 07:09 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
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 July 13, 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)
|
|

01-23-2008, 07:15 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 12
|
|
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:
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.
|
|

01-23-2008, 07:19 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
Originally Posted by bluegreen7hi
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:
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 July 13, 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)
|
|

01-23-2008, 07:46 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 16
|
|
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 
|
|

01-23-2008, 07:49 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
Originally Posted by sandeeprao.techno
...
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 July 13, 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)
|
|

01-24-2008, 04:46 AM
|
 |
Senior Member
|
|
Join Date: Nov 2007
Location: New York
Posts: 143
|
|
|
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?
|
|

01-24-2008, 09:54 AM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 296
|
|
|
Literals
Hello everyone
Originally Posted by sandeeprao.techno
You can also use
System.out.println((a + b + c + d + e) / 5.0);
When I use literals of type double I like to do this:
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. 
__________________
If your ship has not come in yet then build a lighthouse.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|