lecture04

lecture04 - Lecture 4 1.00 Fall 2005 More on Java Data...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4 1.00 – Fall 2005 More on Java Data Types and Control Structures Control Structures: Branch General form Example if (boolean) if (x == y) statement; a = 20; if (x ==z) { b = 10; c = 20; } if (boolean) if ( x == y ) { statement1; a = 10; else b = 20; } statement2; else x = y; if (boolean1) if ( x > 60) statement1; y = 20; else if (x < 30) { else if (booleanN) z += y; statementN; y = 25; } else else statement; y= 40; 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Control example 0 Yes No ) / Print root No Print root Print root2 (0) 2 discriminant < 0 Input a, b and c discriminant = b*b - 4.0*a*c discriminant Yes Print “Sorry, no real root” root = - 0.5 * b / a root = (-b + discriminant) / 2*a root2 = (-b - discriminant 2*a System.exit Solve ax + bx + c= 0 Control example import javax.swing.*; // To support simple input public class Control { // Quadratic formula public static void main(String[] args) { final double TOL= 1E-15; // Constant(use ‘final’) String input; input= JOptionPane.showInputDialog("Enter a"); double a= Double.parseDouble(input); input= JOptionPane.showInputDialog("Enter b"); double b= Double.parseDouble(input); input= JOptionPane.showInputDialog("Enter c"); double c= Double.parseDouble(input); 2
Background image of page 2
Control example double discriminant= b*b - 4.0*a*c; if ( discriminant < 0) System.out.println("Sorry, no real root"); else if (Math.abs(discriminant) <= TOL) { double root= -0.5 * b / a; System.out.println("Root is " + root); } else { // Redefine ‘root’; blocks have own scopes double root=(-b + Math.sqrt(discriminant))/ (2.0*a); double root2=(-b- Math.sqrt(discriminant))/ (2.0*a); System.out.println("Roots" + root + “," + root2); } System.exit(0); } } Control example The previous program has a deliberate, subtle bug Can you see it? Is it likely that you’d find it by testing? Is it likely you’d find it by using the debugger and reading the code? Fix the error by rearranging the order of the if-else clauses 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Control structure: Iteration General form Example while (boolean) while (x > 0) { statement; System.out.println(“x= “ + x); x--; } do do { statement; System.out.println(“x=“ + x); while (boolean); x--; // Always executes stmt at least once } while (x > 0) for (start_expr; end_bool; cont_expr) for ( x= 20; x > 0; x--) statement; System.out.println(“x=“ + x); for loops for (start_expr; end_bool; cont_expr) for (j= 0; j < 20; j++) statement; z += j; is equivalent to: start_expr; j= 0; while (end_bool) { while (j < 20) { statement; z += j; cont_expr; j++; } } 4
Background image of page 4
Example Method-Computing ln(x) The natural logarithm of any number x is approximated by the formula ln(x) = (x-1) – (x-1) 2 /2 + (x-1) 3 /3 -(x-1) 4 /4 + (x-1) 5 /5 + …… Iteration Example: ln (x) import javax.swing.*; public class Iteration { public static void main(String[] args) { String input= JOptionPane.showInputDialog("Enter x (0-2)"); double x= Double.parseDouble(input); // Compute 20 terms of // ln x= (x-1) - (x-1)^2/2 + (x-1)^3/3 - . .. final int ITERATIONS= 20;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

lecture04 - Lecture 4 1.00 Fall 2005 More on Java Data...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online