Basic Java Data Types, Control Structures
Basic Java Data Types, Control Structures
00 Lecture 3
Basic Java Data Types, Control Structures
These are not objects These are defined (almost) identically on every machine on which Java runs, unlike other programming languages Java is a strongly typed language:
Every variable must have a declared type
-128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 -9,223,372,036,854,775,808L to 9,223,372,036,854,775,807L +/- 3.4E+38F (6-7 significant digits) +/- 1.8E+308 (15 significant digits) ISO Unicode character set true or false
Arithmetic Operators
Table in precedence order, highest precedence at top
Meaning increment decrement unary + ( x = +a) unary ( x = -a) multiplication division modulo addition subtraction
Left to right
Left to right
Logical Operators
Produce results of type boolean Comparisons use 8 operators:
Equal Less than Greater than Logical and == < > && Not equal Less than or equal Greater than or equal Logical or != <= >= ||
Example:
double c= 0.0, b= 3.0; if (c != 0.0 && b/c > 5.0) System.out.println(Boo); // Never use == with float/double (this is bad example) // Short circuit evaluation: quit after false subexpression
Assignment Operators
Assignment is not the same as equality
= is not the same as ==
Exercises
Get the percent grad students in 1.00:
int students= 240; int grads= 35; _________________________;
Write expression to test if int x greater than double y and x less than y2 and x not equal x2:
_________________________; if (_____________________; // Declare x, y // Write logical expression
Exercises
Get the percent grad students in 1.00:
int students= 240; int grads= 35; double pctGrad= grads/(double) students;
Write expression to test if int x greater than double y and x less than y2 and x not equal x2:
int x; double y; if (x > y && x < y*y && x != x*x)
Example
if (boolean) statement1; else statement2; if (boolean1) statement1; else if (booleanN) statementN; else statement;
Control example
Solve ax2 + bx + c= 0
Input a, b and c discriminant = b*b - 4.0*a*c
No
No
root = (-b + discriminant) / 2*a root2 = (-b - discriminant) / 2*a Print root Print root2
Print root
System.exit(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= 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); 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 youd find it by testing? Is it likely youd find it by using the debugger and reading the code?
Control exercises
Exercise 1. Write code in main()
If demand d > supply s, raise price p by a(d-s) If demand = supply, do nothing If demand d < supply s, lower price p by b(d-s)
do statement; while (boolean); // Always executes stmt at least once for (start_expr; end_bool; cont_expr) statement;
10
For loops
for (start_expr; end_bool; cont_expr) statement; for (j= 0; j < 20; j++) z += j;
is equivalent to:
start_expr; while (end_bool) { statement; cont_expr; } j= 0; while (j < 20) { z += j; j++; }
Iteration Example 1: Ln x
import javax.swing.*; public class Iteration { public static void main(String[] args) { String input= JOptionPane.showInputDialog("Ent 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; // Fixed no of iterations double logx= 0.0; double x1= x-1; for (int i= 1; i <= ITERATIONS; i++) { if (i % 2 == 0) // i even logx -= Math.pow(x1, i)/i; else logx += Math.pow(x1, i)/i; } System.out.println("Ln x= " + logx); } }
11
Iteration Example 2: Ln x
import javax.swing.*; // Same series as example 1 public class Iteration2 { public static void main(String[] args) { String input= JOptionPane.showInputDialog("Ent x (0-2)"); double x= Double.parseDouble(input); final double TOLERANCE= 0.00001; // Tol sets no of terms double logx= 0.0; double x1= x-1; int i= 1; double term= 0.0; // Define outside do {} do { term= Math.pow(x1, i)/i; if (i % 2 == 0) // i even logx -= term; else logx += term; i++; } while (Math.abs(term) > TOLERANCE); System.out.println("Ln x= " + logx); System.out.println("Found in " + i + " iterations"); } }
12