Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error=0.000001; double x0.

Similar presentations


Presentation on theme: "1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error=0.000001; double x0."— Presentation transcript:

1 1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error=0.000001; double x0 = 3.0; double c = 5.0; double x1 = x0 - (x0 * x0 * x0 - c) / (3 * x0 * x0); while(Math.abs(x1-x0)>error){ x0=x1; x1 = x0 - (x0 * x0 * x0 - c) / (3 * x0 * x0); } System.out.println(x1); }

2 2 Newton-Raphson /* Solve f(x) = x*x-c = 0 f'(x) = 2x */ public class newton{ public static void main(String args[]){ double error=0.000001; double x0 = 3.0; double c = 5.0; double x1 = x0 - (x0 * x0 - c) / (2 * x0); while(Math.abs(x1-x0)>error){ System.out.println(x1); x0=x1; x1 = x0 - (x0 * x0 - c) / (2 * x0); } System.out.println(x1); }

3 3 Exception JVM can generate an exception in response to some internal error. Your program cannot handle this. Standard exceptions (divide by zero, array-index out of bounds) You can manually generate an exception by using the throw statement

4 4 Exception try block contains program statements that you want to monitor. If an exception occurs within try block it is thrown. Your code can catch this exception using catch and handle it in some rational manner. Use the keyword throw to manually throw an exception. In some cases, an exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed upon exiting from a try block is put in a finally block.

5 5 exceptions // Let JVM handle the error. class NotHandled { public static void main(String args[]) { int nums[] = new int[4]; System.out.println("Before exception is generated."); // generate an index out-of-bounds exception nums[7] = 10; } Before exception is generated. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at ExcDemo1.main(java.lang.String[]) (Unknown Source) at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0) at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)

6 6 exceptions // Demonstrate exception handling. class ExcDemo1 { public static void main(String args[]) { int nums[] = new int[4]; try { System.out.println("Before exception is generated."); // Generate an index out-of-bounds exception. nums[7] = 10; System.out.println("this won't be displayed"); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("Index out-of-bounds!"); //System.out.println(exc); } System.out.println("After catch statement."); } Before exception is generated. Index out-of-bounds! After catch statement.

7 7 exceptions /* An exception can be generated by one method and caught by another. */ class ExcTest { // Generate an exception. static void genException() { int nums[] = new int[4]; System.out.println("Before exception is generated."); // generate an index out-of-bounds exception nums[7] = 10; System.out.println("this won't be displayed"); } class ExcDemo2 { public static void main(String args[]) { try { ExcTest.genException(); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("Index out-of-bounds!"); } System.out.println("After catch statement."); }

8 8 exceptions // Handle error gracefully and continue. class ExcDemo3 { public static void main(String args[]) { int numer[] = { 4, 8, 16, 32, 64, 128 }; int denom[] = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i<numer.length; i++) { try { System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (ArithmeticException exc) { // catch the exception System.out.println("Can't divide by Zero!"); } 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16

9 9 exceptions // Use multiple catch statements. class ExcDemo4 { public static void main(String args[]) { // Here, numer is longer than denom. int numer[] = { 4, 8, 16, 32, 64, 128, 256, 512 }; int denom[] = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i<numer.length; i++) { try { System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (ArithmeticException exc) { // catch the exception System.out.println("Can't divide by Zero!"); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("No matching element found."); } 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16 No matching element found.


Download ppt "1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error=0.000001; double x0."

Similar presentations


Ads by Google