Math Functions

advertisement
TITLE – LP4-2.1: Java 2
AIM – How to use math functions in Java?
OBJECTIVES – Students will be able to:
1) Utilize 3 math functions in Java.
DO NOW –
What is the value of the variables in the following statements?
int dollars = 1.56 + 3.56;
String aRose = "A rose is a rose is a rose";
String whatRose = aRose.substring(11);
String moreRose = aRose.substring(3, 6);
STEPS –
1. Do Now (2 mins.)
2. Go over Do Now (3 mins.)
 dollars is 5
 aRose is "A rose is a rose is a rose"
 whatRose is "a rose is a rose"
 moreRose is "rose"
3. Mathematical Functions (10 mins.)
 What is a function?
 A function is a routine that computes some value for you, given some information.
 functionValue = functionName(argumentList);
 functionName is the name of the function and argumentList is a list of values
(arguments, separated by commas) provided to the function so it can do its work. In
this assignment statement, functionName uses the values in argumentList to
compute a result and assign that result to the variable we have named
functionValue. We must insure the variable functionValue has the same type as the
value computed by functionName.
 The methods that support mathematical functions are implemented in the Java class
named Math, so the format is Math.functionName.
 Three functions:
1) Math.abs(argument) returns the absolute value of argument. The argument
can be either integer or double.
Example
Result
Math.abs(7)
Math.abs(-11)
Math.abs(-3.14)
Math.abs(72.1)
7
11
3.14
72.1
2) Math.sqrt(argument) returns the square root of a number. The argument must
be a non-negative double number.
Example
Math.sqrt(4.0)
Math.sqrt(36.0)
Math.sqrt(72.1)
Result
2.0
6.0
8.491
3) Math.pow(argument1, argument2) raises argument1 to the argument2th power.
Both arguments must be double numbers.
Example
Result
Math.pow(4.0, 2.0) 16.0
Math.pow(-3.0, 3.0) -27.0
Math.pow(10.0, 4.0) 10000.0
You can raise numbers to powers such as 3.16.

For the more mathematically inclined reader, you should know that there are
many more Java functions available for your use. You might want to look into
using them. There are trigonometric functions and inverse trig functions,
functions to convert from radians to degrees and vice versa, functions to find
extreme values, functions for rounding, logarithm and inverse logarithm functions
and built-in values for pi and e.
Where do you look up for Math functions for Java?
http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html
4. Group Work (30 mins.)
You will be creating a "Savings" Java program. It will have I/O later. Start with the
following:
/*
* Name:
Savings Project
* Author:
Your Name
* Synopsis: Program will calculate savings depending on amount
*
saved and number of weeks.
*
*/
import java.util.Scanner;
public class Savings
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
// declare and initialize variables
String yourName;
double deposit = ;
int weeks = ;
double total = ;
total = deposit * weeks;
}
}
Print out yourName, amount deposited, total, and text to describe each
item. Since banks pay you "interest" on your deposits, do the following:



Define a variable interest to store the yearly savings interest rate. Interest rates are decimal
numbers, so use the double type for this.
Add code to allow the user to input this interest rate.
Modify the code to use interest in computing total. The code for that computation is (get
ready - it’s messy looking):
total = 5200 * (deposit * (Math.pow((1 + interest / 5200), weeks) - 1) / interest);
Make sure you type this all on one line – as often happens, the word processor has made it
look like it is on two. As we said, this is a pretty messy expression, but it’s good practice in
using parentheses and a mathematical function (pow). The number ‘5200’ is used here to
convert the interest from a yearly value to a weekly value.
TONIGHT’S HOMEWORK – HW: Finish your "Savings" program.
DO NOW –
What is the value of the variables in the
following statements?
int dollars = 1.56 + 3.56;
String aRose = "A rose is a rose is a rose";
String whatRose = aRose.substring(11);
String moreRose = aRose.substring(3, 6);
HW –
Finish your "Savings" program.
Functions in Java
 What is a function?
- A function is a routine that computes
some value for you, given some
information
- functionValue =
functionName(argumentList);
Three Math Functions
1) Math.abs(argument) returns the absolute
value of argument. The argument can be either
integer or double.
Example
Math.abs(7)
Math.abs(-11)
Math.abs(-3.14)
Math.abs(72.1)
Result
7
11
3.14
72.1
2) Math.sqrt(argument) returns the square root
of a number. The argument must be a nonnegativedouble number.
Example
Result
Math.sqrt(4.0)
Math.sqrt(36.0)
Math.sqrt(72.1)
2.0
6.0
8.491
3) Math.pow(argument1, argument2) raises
argument1 to the argument2th power. Both
arguments must be double numbers.
Example
Result
Math.pow(4.0, 2.0)
Math.pow(-3.0, 3.0)
Math.pow(10.0, 4.0)
16.0
-27.0
10000.0
You can raise numbers to powers such as
3.16.
Group Work –
You will be creating a "Savings" Java program. It will
have I/O later. Start with the following:
/*
* Name:
Savings Project
* Author:
Your Name
* Synopsis: Program will
* calculate savings depending on
* amount saved and number of weeks.
*
*/
import java.util.Scanner;
public class Savings
{
public static void main(String[] args)
{
// declare and initialize variables
String yourName;
double deposit = ;
int weeks = ;
double total = ;
total = deposit * weeks;
}
}
Print out yourName, amount deposited, total,
and text to describe each item.
Other Things to Try –
Since banks pay you "interest" on your
deposits, do the following:
 Define a variable interest to store the yearly
savings interest rate. Interest rates are decimal
numbers, so use the double type for this.
 Add code to allow the user to input this interest
rate.
 Modify the code to use interest in computing total.
The code for that computation is (get ready - it’s
messy looking):
total = 5200 * (deposit * (Math.pow((1 + interest /
5200), weeks) - 1) / interest);
Make sure you type this all on one line – as often
happens, the word processor has made it look like
it is on two. As we said, this is a pretty messy
expression, but it’s good practice in using
parentheses and a mathematical function (pow).
The number ‘5200’ is used here to convert the
interest from a yearly value to a weekly value.
Download