Download presentation
Presentation is loading. Please wait.
1
Some problems for your consideration
2
If What output will be produced by the following code: int extra = 2;
if (extra < 0) System.out.println( "small" ); else if (extra == 0) System.out.println( "medium" ); else System.out.println( "large" );
3
If What output will be produced by the following code:
int extra = -37; if (extra < 0) System.out.println( "small" ); else if (extra == 0) System.out.println( "medium" ); else System.out.println( "large" );
4
If What output will be produced by the following code: int extra = 0;
if (extra < 0) System.out.println( "small" ); else if (extra == 0) System.out.println( "medium" ); else System.out.println( "large" );
5
Switch What is the output produced by the following code:
char letter = 'B'; switch (letter) { case 'A' : case 'a' : System.out.println( "Some kind of A." ); case 'B' : case 'b' : System.out.println( "Some kind of B." ); break; default : System.out.println( "Something else." ); }
6
Switch What is the output produced by the following code:
int key = 1; switch (key+1) { case 1 : System.out.println( "apples" ); break; case 2 : System.out.println( "oranges" ); case 3 : System.out.println( "peaches" ); case 4 : System.out.println( "plums" ); default : System.out.println( "fruitless" ); } When key is 3? When key is 5?
7
Boolean Let count be 0 and limit be 10 (the values for x and y are unknown). State whether each of the following is either true or false: (count==0) && (limit<20) count==0 && limit<20 (limit>20) || (count<5) !(count==12) (count==1) && (x<y) (count<10) || (x<y)
8
Depreciation The straight-line method for computing the yearly depreciation D for an item is given by: where P is the purchase price, S is the salvage value, and Y is the number of years the item is used. Write a program that takes as input the purchase price of an item, the expected number of years of service, and the expected salvage value. The program should then output the yearly depreciation for the item.
9
Babylonian algorithm for square root of n
Steps: Make a guess at the answer (you can pick n/2 as your initial guess). Compute r = n / guess. Set guess = (guess+r) / 2. Go back to step 2 for as many iterations as necessary. The more you repeat steps 2 and 3, the closer guess will be to the square root of n. Write a program that inputs n, iterates through the algorithm 5 times, and outputs the answer as a double.
10
Newton-raphson method
11
Newton-Raphson method* *http://en.wikipedia.org/wiki/Newton%27s_method
“In numerical analysis, Newton's method (also known as the Newton–Raphson method), named after Isaac Newton and Joseph Raphson, is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function.”
12
Newton-Raphson method
The Newton-Raphson method in one variable: Given a function ƒ(x) and its derivative ƒ'(x), we begin with a first guess x0 for a root of the function. Provided the function is reasonably well-behaved, a better approximation x1 is: Geometrically, x1 is the intersection with the x-axis of a line tangent to f at f(x0).
13
Newton-Raphson method
Geometrically, x1 is the intersection with the x-axis of a line tangent to f at f(x0). The process is repeated until a sufficiently accurate value is reached:
14
Newton-Raphson method
Let f(x) be sin(x). (You will need to know f’(x). If you don’t know that, go to the Intershop and search for derivative of sin.) Write a Java program that calculates the first 5 steps of the Newton-Raphson method. Prompt the user for the start (x0). Output the result, x5, as well as sin(x5). Test with x0=1 and x0=2. What do you get different answers for x5?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.