CS 1073 Introductory Programming
|
V(r) = (4/3)*pi*r3 Volume of a sphere of radius 3 is: V(3) To get the value, plug 3 into the formula to get: (4/3)*3.14159*27 = 113.0973355
Here the 3 is called the actual parameter (it's the number we actually plug into the formula for volume). Notice that the equals signs above are not assignments, but are mathematical equals -- quite different and yet similar in some ways.
The r in the formula is called the formal parameter (it's the variable that is given the actual parameter's value). It's formal because it doesn't matter what name we use for it, so that we could write:
V(s) = (4/3)*pi*s3, or V(t) = (4/3)*pi*t3, or V(x) = (4/3)*pi*x3
These are all the same function V and any one would work as well as another. (Plugging 3 in for t in the second formula above would give the same result as for the formula involving r.)
public class Sphere {
public static double V(double r) {
return (4.0/3.0)*Math.PI*r*r*r;
}
public static void main(String[] args) {
System.out.println("Volume of a sphere of radius 3 is: " + V(3));
}
}
Everything except the red above are just little Java "love notes" or "magic parts" that have to be that way for it to work, but these additions are mostly the same for each application. Here is the output when this program is run:
Volume of a Sphere of radius 3 is: 113.09733552923255
In the function main, the presence of V(3) is a call to the function V with actual parameter 3. One says that the actual parameter 3 is passed to the function V. It is just as if there were an assignment statement r = 3; at the start of the function V. Then the expression (4.0/3.0)*Math.PI*r*r*r is evaluated (plugging in 3 for r) and the resulting double is returned to the point of call, the V(3). It is as if the value 113.09... just "pops in" to replace the V(3).
The first line of the function V looks like:
public static double V(double r) {
There are two occurrences of double here, both in red. The second one says that the formal parameter r is supposed to be a double, that is, a high-precision real number in the computer. Even though the call was V(3) with an int for the actual parameter, this 3 is converted to the double equal to 3.0 as it is passed to the function V. The first double in red says that the function should return a double, rather than an int, say. The words public static at the beginning we'll talk about later.
// Abs: compute number of combinations
public class Abs {
public static double abs(double x) {
if (x >= 0.0) return x;
else return -x;
}
public static void main(String[] args) {
System.out.println("abs(4.3) = " + abs(4.3));
System.out.println("abs(-4.3) = " + abs(-4.3));
System.out.println("abs(0.0) = " + abs(0.0));
}
}
Here are results of a run.
abs(4.3) = 4.3 abs(-4.3) = 4.3 abs(0.0) = 0.0
System.out.println(Math.min(13, 47)); System.out.println(Math.min(13.8, 47.2));
When run, this produces:
13 13.8
But we can write our own function min, with two int parameters that returns an int:
// Min: test a min function
public class Min {
public static int min(int x, int y) {
if (x < y) return x;
else return y;
}
public static void main(String[] args) {
System.out.println(min(13, 47));
}
}
This produces the same output 13 that we had before, but when we try the function call min(13.8, 47.2), we get an error. This is because the min function that we wrote is expecting two int parameters, and we are giving two doubles. We can give a separate definition of min for the other parameters as below:
// Min: test a min function
public class Min {
public static int min(int x, int y) {
if (x < y) return x;
else return y;
}
public static double min(double x, double y) {
if (x < y) return x;
else return y;
}
public static void main(String[] args) {
System.out.println(min(13, 47));
System.out.println(Math.min(13.8, 47.2));
System.out.println(Math.min(13, 47.2));
}
}
When run, this produces:
13 13.8 13.0
The first call to min uses the first definition (with two int parameters), while the second call uses the second definition (with two double parameters). Normally the third call wouldn't be able to use either definition, but this situation is a special case, since in the third call, the int parameter 13 is first converted to a double for use by the second min definition. In fact, in this special case the second definition will work for all three calls because ints can be converted to doubles. As an exercise, put an extra printing statement inside each of the two min functions above to see which one is being called.
// Pi: test a pi function
public class Pi {
public static double pi() {
return 3.141592653589793;
}
public static void main(String[] args) {
System.out.println(pi());
}
}
Of course,this just prints the value 3.141592653589793. Notice that both the call pi() and the definition use empty parentheses () for the parameters. These parentheses are required, even though there are no parameters. (They help us keep straight that this is a function with no parameters and not just an identifier.).
In real programs, we would want to use the constant Math.PI instead of this function, or else we could define our own constant using:
public static final double PI = 3.141592653589793;
In both of these cases you must not put in parentheses after the PI
// Hello: test a hello function
public class Hello {
public static void hello() {
System.out.println("Hello!!");
}
public static void main(String[] args) {
hello();
}
}
Here the output is just Hello!!. The void in the function definition is what says that nothing is returned. Because it returns nothing, you cannot have a return inside the funtion. When you call the function, it cannot be inside a printing statement or in an expression, but must be by itself.
Sum of squares of integers <= N is: sumOfSquaresFormula(N) = N3/3 + N2/2 + N/6 = N(N + 1)(2N + 1)/6
For example, if N = 4, then the sum is: 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30. The formula gives: 43/3 + 42/2 + 4/6 = 64/3 + 16/2 + 4/6 = (21 + 1/3) + 8 + 2/3 = 30.
This function might seem a bit strange because it always yields an exact integer, even though the individual terms may not be integers. If N is divisible by 6, say N = 12, then each part of the formula is an integer:
sumOfSquaresFormula(12) = 123/3 + 122/2 + 12/6 = 576 + 72 + 2 = 650
Otherwise however, the individual terms won't be integers, as for example when N = 10:
Here is a program that calculates the sum and compares it with the result of the formula:
sumOfSquaresFormula(10) = 103/3 + 102/2 + 10/6 = (333 + 1/3) + 50 + (1 + 2/3) = 385
// SumOfSquares0: compute sum of squares of integers
public class SumOfSquares0 {
public static void main(String[] args) {
int n = 4;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + i*i;
}
System.out.println("Sum of first " + n +
" integers squared = " + sum);
double sumFormula = n*n*n/3.0 + n*n/2.0 + n/6.0;
System.out.println("Sum of first " + n +
" integers squared (using formula) = " + sumFormula);
}
}
Below I have rewritten the above simple formula to use a number of functions. This illustrates functions square, cube, sumOfSquares, and sumOfSquaresFormula, as well as main. This example artificially uses more functions than necessary. Here is the program itself:
// SumOfSquares: compute sum of squares of integers
public class SumOfSquares {
public static final int N = 10;
public static int square(int x) {
return x*x;
}
public static int cube(int x) {
return x*square(x);
}
public static int sumOfSquares(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + square(i);
}
return sum;
}
public static double sumOfSquaresFormula(int n) {
return cube(n)/3.0 + square(n)/2.0 + n/6.0;
}
public static void main(String[] args) {
System.out.println("Sum of first " + N +
" integers squared = " + sumOfSquares(N));
System.out.println("Sum of first " + N +
" integers squared (using formula) = " + sumOfSquaresFormula(N));
}
}
Here are results of runs with N = 10 (as above), and with N = 40, N = 100, N = 500.
Sum of first 10 integers squared = 385 Sum of first 10 integers squared (using formula) = 385.0
Sum of first 40 integers squared = 22140 Sum of first 40 integers squared (using formula) = 22140.0
Sum of first 100 integers squared = 338350 Sum of first 100 integers squared (using formula) = 338350.0
Sum of first 500 integers squared = 41791750 Sum of first 500 integers squared (using formula) = 4.179175E7