Download presentation
Presentation is loading. Please wait.
1
Relational Expression return Boolean results. operands must be of numerical types. == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to
2
Logical Expression (!a) negation (a || b) or (a && b) and
3
Distributive laws Original Expression: (p && q) || (p && r) (p || q) && (p || r) Equivalent terms p && (q || r) p || (q && r)
4
DeMorgan’s Laws Original Expressions !(p && q) !(p || q) Equivalent Terms (!p) || (!q) (!p) && (!q)
5
Conditional Expression condition ? exp1 : exp2 Example: (x >= 0) ? x : -x (a >= b) ? a : b
6
Postfix/Prefix Operators x (before) expr x(after) Postfix increment x++ 1 1 2 Postfix decrement x— 1 1 0 Prefix increment ++x 1 2 2 Prefix decrement --x 1 0 0
7
Sequence Statements are executed top to bottom. S1; S2; Example: temp = x*x + y*y; d = Math.sqrt(temp); x = 5; y = 11; z = x + y; System.out.println(z);
8
Block a sequence of statements and local variable declarations enclosed by braces { and } { S1; S2;...; Sn; } Example: { double temp, d; temp = x*x + y*y; d = Math.sqrt(temp); }
9
if Statement Execute a block of code (Action) if a condition is true. The block of code is executed at most once. if (condition) { statements; } Example: if (x % 2 == 0) { System.out.println("The number is even."); }
10
If-Else Statement. if (condition) { statement1; } else { statement2; } if (condition1) { statement1; } else if (condition2) { statement2; } else if (condition3) { statement3; } else { statement4; }
11
Example: if.. else if.. else String sDay; if (day == Calendar.SUNDAY) { sDay = "Sunday"; } else if (day == Calendar.MONDAY) { sDay = "Monday"; } else if (day == Calendar.TUESDAY) { sDay = "Tuesday"; } else if (day == Calendar.WEDNESDAY) { sDay = "Wednesday"; } else if (day == Calendar.THURSDAY) { sDay = "Thursday"; } else if (day == Calendar.FRIDAY) { sDay = "Friday"; } else if (day == Calendar.SATURDAY) { sDay = "Saturday"; } else { sDay = "Unknown"; }
12
Switch Statement switch (expression) { case value1: statement1; break; case value2: statement2; break; case value3: statement3; break; default: statement4; }
13
Example: Day3.java String sDay; switch (day) { case Calendar.SUNDAY: sDay = "Sunday"; break; case Calendar.MONDAY: sDay = "Monday"; break; case Calendar.TUESDAY: sDay = "Tuesday"; break; case Calendar.WEDNESDAY: sDay = "Wednesday"; break; case Calendar.THURSDAY: sDay = "Thursday"; break; case Calendar.FRIDAY: sDay = "Friday"; break; case Calendar.SATURDAY: sDay = "Saturday"; break; default: sDay = "Unknown"; }
14
While Loop Repeat a block of code while a condition is true. There is no limit to the number of times that the block can be executed. while (condition) { statements; } When the body of a while statement, or of a for statement consists of a single statement, no braces are necessary.
15
Examples: While Loop int i = 1; while (i <= 20) { System.out.println(i); i++; } //--------------------- int i = 20; while (i >= 0) { System.out.println(i); i--; }
16
For Loop for (initialization; condition; incrementation) { statement(s); }
17
Examples: For Loop for (int i = 1; i <= 20; i++) { System.out.println(i); } //--------------------------------------- for (int i = 20; i >= 0; i--) { System.out.println(i); }
18
Do-While Loop do { statements; } while (condition);
19
Examples: Do-While Loop int i = 1; do { System.out.println(i); i++; } while (i <= 20) //----------------------------- int i = 20; do { System.out.println(i); i--; } while (i >= 0);
20
Break and Continue A break statement in a loop terminates the loop. A continue statement in a loop terminates the current iteration of the loop, and continue with the next iteration of the loop.
21
Examples: Break and Continue int i = 0; while (i <= 20) { i++; if (i % 5 == 0) break; System.out.println(i); } //------------------------ int i = 0; while (i <= 20) { i++; if (i % 5 == 0) continue; System.out.println(i); }
22
Static Methods Some of the methods of a class can be declared static. These methods can be called directly without instances of the class. ClassName.methodName(parameters,...)
23
Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides several static methods for reading particular types of data from standard input. public static String readString() public static String readWord() public static boolean readBoolean() public static char readChar() public static int readInt() public static long readLong() public static float readFloat() public static double readDouble()
24
Example: AmountDue1 import cs1.Keyboard; public class AmountDue1 { public static void main(String[] args) { int qty; double price, amountDue; System.out.print(“Enter quantity purchased: "); qty = Keyboard.readInt(); System.out.print(“Enter the unit price: "); price = Keyboard.readDouble(); amountDue = price * qty; System.out.println(“Amount due: " + amountDue); }
25
Install Keyboard Copy the cs1.jar file Paste it in the C:\jdk1.3\jre\lib\ext directory If the above doesn’t work by itself, then also paste it in C:\Program Files\JavaSoft\JRE\1.3\lib\ext
26
Install Keyboard – Method 2 Create a cs1 subdirectory to the directory containing your java programs Copy Keyboard.java into the cs1 directory Homework directory | +-Java source files | +- cs1 subdirectory | +- Keyboard.java
27
Install Keyboard – Method 3 Copy Keyboard.java into the same directory as your programs.
28
Reading Input –JOptionPane import javax.swing.JOptionPane; public class AmountDue2 { public static void main(String[] args) { int qty; double price, amountDue; String temp; temp = JOptionPane.showInputDialog ("Please enter the quantity purchased: "); qty = Integer.parseInt(temp); temp = JOptionPane.showInputDialog ("Please enter the unit price: "); price = Double.parseDouble(temp); amountDue = price * qty; JOptionPane.showMessageDialog (null,"Amount due: " + amountDue); }
29
Reading Input – java.io import java.io.*; public class AmountDue3 { public static void main(String[] args) throws IOException { int qty; double price, amountDue; BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); System.out.print("Please enter the quantity purchased: "); qty = Integer.parseInt(br.readLine()); System.out.print("Please enter the unit price: "); price = Double.parseDouble(br.readLine()); amountDue = price * qty; System.out.println(“Amount due: " + amountDue); }
30
NumberFormat Class x = 10000.0/3.0; System.out.println(x); // 3333.3333333333335 Printing a number with System.out.print(x) will normally print the maximum number of non-zero digits. The NumberFormat class in the java.text package can format Numbers Currency values Percentage values
31
Number Format import java.text.*; public class FormattedOutput{ public static void main(String[] args) { int qty; double price, amountDue; double x = 10000.0/3.0; System.out.println("Default output is \t" + x); // 3333.3333333333335 NumberFormat num_format = NumberFormat.getNumberInstance(); String s = num_format.format(x); System.out.println("Number format is \t" + s);// 3,333.333 NumberFormat cur_format = NumberFormat.getCurrencyInstance(); s = cur_format.format(x); System.out.println("Currency format is \t" + s);//$3,333.33 NumberFormat per_format = NumberFormat.getPercentInstance(); s = per_format.format(x); System.out.println("Percent format is \t" + s); // 333,333% }
32
Other NumberFormat methods setMaximumIntegerDigits(int digits) setMaximumFractionDigits(int digits) setMinimumIntegerDigits(int digits) setMinimumFractionDigits(int digits)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.