Student Desk
Download
Report
Transcript Student Desk
Mathematical
Methods
Mrs. Bellacera
In Java…How do I?
• Raise to a power
• Find the square root of a
number
• Find the cosine of an angle
Use Java’s Math Class
Java’s Math Class
These methods are general
purpose, thus they are
declared as public static and
are used without an object.
public class math_stuff()
{ public static void main(String[] args)
{ double answer;
double input=-72.837;
answer= Math.abs(input); }
}
Java’s Math Class
Method Name
Description
abs(x)
Absolute value
pow(x1,x2)
x1 raised to x2
power
Square root
Natural log
E raised to the x
power
Smaller of the
two arguments
Larger of the two
arguments
sqrt(x)
log(x)
exp(x)
min(x,y)
max(x,y)
Returned
Value
Same as
argument
double
double
double
double
Same as
argument
Same as
argument
Java’s Math Class
Method
Name
rint(x)
Description
Returned Value
round(x)
Rounded value
int or long
random()
double
sin(x)
Random number
between 0.0 and 1.0
(including 0.0)
Sine of x
cos(x)
Cosine of x
double
tan(x)
Tangent of x
double
asin(x)
Arcsin of x
double
Closest integer value to double
argument
double
Java’s Math Class
Method
Name
acos(x)
Description
Returned Value
Arccos of x
double
atan(x)
Arctan of x
double
ceil(x)
Smallest integer that is
not less than x
double
floor(x)
Largest integer that is
not greater than x
double
Assignment:
Implement a class which simulates the child’s
game of Magic 8-ball.
Call the class Fortune. A Fortune object
should represent a many-sided fortune-teller.
Include all necessary constructors, accessors,
mutators and other methods.
Make an magic 8-Ball… 10 sides. Each time the
object is “rolled” (or shaken),
a randomly selected fortune is returned.
Create a client class to simulate how the Magic
8 Ball works. Continue until the user has no more
questions to ask.
Sample Output
Please ask a question if you dare:
Will I win one million dollars?
…. Outlook foggy. Ask again later.
Would you like to ask another question? (y/n)
y
Please ask a question if you dare:
Will I get a 5 on the AP exam?
…. Don’t count on it.
Would you like to ask another question? (y/n)
n
Refer to: http://en.wikipedia.org/wiki/Magic_8-Ball
Initial Thoughts
How do you generate a random number between
1 and 10?