• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Java help!! (how to define/use math.pi :S)

Status
Not open for further replies.
Okay, I have finished most of my mini assignment from last week but I am unable to figure out how to define and use math.pi in my program. I need to write a function in java that calculates the area of a circle. I just need to know how I define Math.PI... I think.
Anyway, I just spent hours reading and doing some other assignment (not java) and I am in dire need of sleep as I have to wake up for class in say 3.5 hours or so.
Any help will be greatly appreciated. ;P

My current code:

class Function
{
public static void main(String args[])
{
int x = 11;
int y = 5;
int radius = 5;
int circleArea = 0;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
{
System.out.println("Sum of " + "i:" + i + " " + " and j:" + j + " equals " + addElements(i,j));
}

System.out.println( x + " divide by " + y + " has a remainder of " + findRemainder(x,y));
System.out.println();
System.out.println("The area of the circle is" + circleArea);
}

// this function add two numbers
public static int addElements(int a, int b)
{
int total = 0;
total = a + b;
return total;
}

// this function finds the remainder
public static int findRemainder(int c, int d)
{
int remainder = 0;
remainder = c%d;
return remainder;
}

// this function finds the area of a circle
public int findAreaCircle(int radius, Math.PI)
{
int circleArea = 0;
circleArea = (Math.PI*radius*radius);
return circleArea;
}
}
 

Hooker

Member
Code:
[...]

// this function finds the area of a circle 
	public int findAreaCircle(int radius, Math.PI)
	{
		int circleArea = 0;
		circleArea = (Math.PI*radius*radius);
		return circleArea;
	}
}
Why is this here?
 

iapetus

Scary Euro Man
This code reminds me of why I hate just about every Java course out there.

Changes you need to make:

1) Given that you're doing everything with static methods, you need to declare findAreaCircle as static too.

2) You don't need to specify pi in the arguments. The signature for that method should just be:

Code:
public int findAreaCircle(int radius)

3) Why the hell are you defining methods for adding numbers and finding remainders? This is just insane - it gives you no benefit over just doing the maths inline, and makes the code unnecessarily verbose. If this is on instructions from your tutor or whoever, then you have my permission to slap them. Seriously, I'm Java-certified. :p

4) You never call the findAreaCircle method. Shouldn't you be doing that? Perhaps:
Code:
int circleArea = 0;
should become:
Code:
int circleArea = findAreaCircle(radius);

5) Ignore CSSer. You don't need to import classes in java.lang.

6) To answer your original question, you don't need to do anything. Just use Math.PI wherever you want the value of pi. It's a predefined constant value - it's just there for you to use.
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
iapetus said:
5) Ignore CSSer. You don't need to import classes in java.lang.

6) To answer your original question, you don't need to do anything. Just use Math.PI wherever you want the value of pi.
.
 

Nerevar

they call me "Man Gravy".
iapetus said:
5) Ignore CSSer. You don't need to import classes in java.lang.

IMO it's good practice to import everything you use. Not necessary, but good practice. You never know when you're going to need to run your classes in a custom JVM that doesn't include the entire java.lang library natively, and you have to reference rt.jar to include the rest of the core java.lang classes (yes, I've had to do something similar to this before).

Edit: Just realized CSSer told him to import "java.lang.Math.*". If you do this, I will hate you. There is nothing worse than inheriting someone else's code who just does a complete import whatever.* for any class they may possibly ever need.
 

iapetus

Scary Euro Man
Petrarca said:
Do you actually hate Java or just the courses?

Just the courses. I code in Java (among other things) for a living, and I used to teach it, which is what really fills me with righteous indignation against the shit that passes for Java instruction these days.
 

Soulhouf

Member
// this function add two numbers
public static int addElements(int a, int b)
{
int total = 0;
total = a + b;
return total;
}
Why not simply:
Code:
// this function add two numbers
public static int addElements(int a, int b){
     return (a + b);
}
 

iapetus

Scary Euro Man
soulhouf said:
Why not simply:
Code:
// this function add two numbers
public static int addElements(int a, int b){
     return (a + b);
}

If you want to simplify it, why not simply use a + b inline? There's no earthly need for a function that just adds two numbers and returns the value.
 

Soulhouf

Member
iapetus said:
If you want to simplify it, why not simply use a + b inline? There's no earthly need for a function that just adds two numbers and returns the value.
Indeed but he seems to be a beginer. I try to help...
 
yeah this is a very simple program but I can remember my first semester of uni they taught shit really basic like this to get you used to functions.

i hate coding lol.
 

iapetus

Scary Euro Man
phatmike128 said:
yeah this is a very simple program but I can remember my first semester of uni they taught shit really basic like this to get you used to functions.

Firstly, they should be getting you used to objects, not static methods. This is Java, not C.

Secondly, it's perfectly possible to get you used to simple code without making you write methods that just duplicate existing operators.

This example should be a circle class with methods for calculating area, circumference, diameter etc.
 

Soulhouf

Member
iapetus said:
Firstly, they should be getting you used to objects, not static methods. This is Java, not C.

Secondly, it's perfectly possible to get you used to simple code without making you write methods that just duplicate existing operators.

This example should be a circle class with methods for calculating area, circumference, diameter etc.
Agreed.
 

pollo

Banned
You guys are scaring the Op lol. See if he or she will ever post programming questions here again. :lol
 

iapetus

Scary Euro Man
NotMSRP said:
shouldn't int be float or double?
no area of a circle is an integer except for 0

No area of a circle with integer radius is integer except for 0. :p

And yes, it should.

pollo said:
You guys are scaring the Op lol. See if he or she will ever post programming questions here again.

We've answered the question. We're now discussing why his teacher should be tarred and feathered.
 
Thanks for the help guys. :)
Just awoke from my 3.5 hours of sleep... will read up on the solutions later (must rush to my morning class now).

PS: I am not scared off by everyone's deep, seemingly philosophical, discussion of Java coding. It actually makes me more eager to ask questions. :p
 

iapetus

Scary Euro Man
Nerevar said:
IMO it's good practice to import everything you use. Not necessary, but good practice. You never know when you're going to need to run your classes in a custom JVM that doesn't include the entire java.lang library natively, and you have to reference rt.jar to include the rest of the core java.lang classes (yes, I've had to do something similar to this before).

I disagree, in that you shouldn't be trying to plan around all potential non-standard implementations you might ever theoretically have to use. Plan for things that support the standards you're working with properly, and deal with exceptions to that as they come up.

Nerevar said:
Edit: Just realized CSSer told him to import "java.lang.Math.*". If you do this, I will hate you. There is nothing worse than inheriting someone else's code who just does a complete import whatever.* for any class they may possibly ever need.

Actually, I missed that as well. It may actually make this suggestion less bad, as I believe that then makes it part of the syntactic sugar recent versions of Java added to help avoid people using the constant interface antipattern.

So what you could do is:

import java.lang.Math.PI;

[...]
area = PI * radius * radius
[...]

You'll need to be using a JDK1.5 or greater compiler, though.

Sun themselves would advise against it in this case:

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes.

(Their emphasis, not mine...)

This use doesn't meet either of those requirements - there's no temptation here to declare a local copy of PI, and there's certainly no temptation to abuse inheritance because this is a shitty class full of static methods and clearly there's no intention of the course introducing inheritance until the last lesson.
 

Nerevar

they call me "Man Gravy".
iapetus said:
Actually, I missed that as well. It may actually make this suggestion less bad, as I believe that then makes it part of the syntactic sugar recent versions of Java added to help avoid people using the constant interface antipattern.

So what you could do is:

import java.lang.Math.PI;

[...]
area = PI * radius * radius
[...]

You'll need to be using a JDK1.5 or greater compiler, though.

Sun themselves would advise against it in this case:



(Their emphasis, not mine...)

This use doesn't meet either of those requirements - there's no temptation here to declare a local copy of PI, and there's certainly no temptation to abuse inheritance because this is a shitty class full of static methods and clearly there's no intention of the course introducing inheritance until the last lesson.

Yeah, I think you mean "static import java.lang.Math.*", and I still don't agree with its usage. The constant interface antipattern is where the user implements an interface simply to gain access to all the constants it includes. This doesn't really fit one of those cases. In this case the user clearly needs access to java.lang.Math.PI (a constant from the Math class) so they should just import java.lang.Math and reference Math.PI in the code itself. This all comes down to preference though, so to each his own.
 

iapetus

Scary Euro Man
NotMSRP said:
?

A=r^2*pi

You tell me a noninteger radius that gives an integer area.

10 = r^2 * PI

Divide both sides by PI:

r^2 = 10/PI

Take the square root of both sides:

r = sqrt(10/PI)

So a circle with radius of sqrt(10/PI) has an area of 10.
 

iapetus

Scary Euro Man
Nerevar said:
Yeah, I think you mean "static import java.lang.Math.*", and I still don't agree with its usage.

I think you mean you think I mean "import static java.lang.Math.*", and I agree with you not agreeing with its usage here, as I said. :p
 

Nerevar

they call me "Man Gravy".
iapetus said:
I think you mean you think I mean "import static java.lang.Math.*", and I agree with you not agreeing with its usage here, as I said. :p

heh, I guess that's why I do all my development in IDE's. Stupid syntax ;)
 

NotMSRP

Member
Yeah, only an irrational number will work because pi is transcendental. So he should use float or double instead of int.
 
I am trying to make a while loop with for statements that detects the input the user types. Essentially this is like a numbers guessing game (depending on what range the number falls under, different statements appears telling the player how far off they are and a subsequent re-enter occurs).
The problem right now is that after it detects the player's first input and prints out the statement, it will just continue printing out that same statement even if I enter something that is supposed to activate another statement. (ie. I entered 100, it says "too low", and then even if I enter the correct answer, it still says "too low")

The code I have is currently like this:
//is done via a method
public void priceTest(int PriceMini)
{
System.out.println("\nEnter price of " + Sandwich + ":");
basePriceGuess = input.nextInt();
while (basePriceGuess != PriceMini){
if (basePriceGuess>PriceMini+10000)
{
System.out.println("You are at least $10000 over!");
System.out.println ("Guess again!");
System.out.println ("Enter price guess:");
int basePriceGuess = input.nextInt();
}
if (basePriceGuess>PriceMini+20000)
{
System.out.println("You are at least $20000 over!");
System.out.println ("Guess again!");
System.out.println ("Enter price guess:");
int basePriceGuess = input.nextInt();
}
if (basePriceGuess>PriceMini+30000)
{
System.out.println("You are at least $30000 over!");
System.out.println ("Guess again!");
System.out.println ("Enter price guess:");
int basePriceGuess = input.nextInt();
}
if (basePriceGuess<PriceMini)
{
System.out.println("too low!! Guess again!");
System.out.println ("Enter price guess:");
int basePriceGuess = input.nextInt();
}
Trials = Trials+1;
///this code is supposed to help count how many tries are taken
}
System.out.println("Why so good? You are within $500 of the price! You am win!");
System.out.println("It took you " + Trials + " tries!");
}
 
...
if (basePriceGuess>PriceMini+10000)
{
System.out.println("You are at least $10000 over!");
System.out.println ("Guess again!");
System.out.println ("Enter price guess:");
int basePriceGuess = input.nextInt();
}
...

You are declaring a variable named basePriceGuess that is local to only the if block. As soon as the code leaves the if block, that variable vanishes into the ether. It is a completely different variable than the basePriceGuess you declared elsewhere.

If you want it to work within your structure, you should put basePriceGuess = input.nextInt() (make sure not to put int in front of it!) just before Trials = Trials + 1. And remove it from all of the if blocks.

Notes: Trials (and other variables) should be lowercase. Uppercase names should be reserved for classes. You can also just say trials++ to add 1 to itself.
Since you are comparing ints, you may want to consider using a switch statement.
 
Status
Not open for further replies.
Top Bottom