Title: Relational Expression
1Relational Expression
equal to ! not equal to lt less than gt
greater than lt less than or equal to gt
greater than or equal to
- return Boolean results.
- operands must be of numerical types.
2Logical Expression
- (!a) negation
- (a b) or
- (a b) and
3Distributive laws
- Original Expression
- (p q) (p r)
- (p q) (p r)
- Equivalent terms
- p (q r)
- p (q r)
4DeMorgans Laws
- Original Expressions
- !(p q)
- !(p q)
- Equivalent Terms
- (!p) (!q)
- (!p) (!q)
5Conditional Expression
- condition ? exp1 exp2
-
- Example
- (x gt 0) ? x -x
- (a gt b) ? a b
6Postfix/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
7Sequence
- Statements are executed top to bottom.
- S1
- S2
- Example
- temp xx yy
- d Math.sqrt(temp)
- x 5
- y 11
- z x y
- System.out.println(z)
8Block
- a sequence of statements and local variable
declarations enclosed by braces and - S1 S2 ... Sn
- Example
- double temp, d temp xx yy d
Math.sqrt(temp)
9if 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.")
-
10If-Else Statement.
- if (condition1)
- statement1
- else if (condition2)
- statement2
- else if (condition3)
- statement3
- else
- statement4
-
- if (condition)
-
- statement1
-
- else
-
- statement2
-
11Example 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"
12Switch Statement
- switch (expression) case value1
statement1 break case value2
statement2 break case value3
statement3 break default statement4
13Example Day3.java
case Calendar.THURSDAY sDay
"Thursday" break case Calendar.FRIDAY
sDay "Friday" break case
Calendar.SATURDAY sDay "Saturday"
break default sDay "Unknown"
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
14While 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.
15Examples While Loop
int i 1 while (i lt 20)
System.out.println(i) i
//--------------------- int i 20 while (i gt
0) System.out.println(i) i--
16For Loop
- for (initialization condition incrementation)
- statement(s)
17Examples For Loop
for (int i 1 i lt 20 i)
System.out.println(i) //--------------------
------------------- for (int i 20 i gt 0
i--) System.out.println(i)
18Do-While Loop
do statements while (condition)
19Examples Do-While Loop
int i 1 do System.out.println(i)
i while (i lt 20) //-----------------------
------ int i 20 do System.out.println(i)
i-- while (i gt 0)
20Break 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.
21Examples Break and Continue
int i 0 while (i lt 20) i if (i
5 0) break System.out.println(i)
//------------------------ int i 0 while (i
lt 20) i if (i 5 0) continue
System.out.println(i)
22Static 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, ...)
23Reading 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()
24Example 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(En
ter the unit price ") price
Keyboard.readDouble() amountDue price
qty System.out.println(Amount due "
amountDue)
25Install Keyboard
- Copy the cs1.jar file
- Paste it in the C\jdk1.3\jre\lib\ext directory
- If the above doesnt work by itself, then also
paste it in C\Program Files\JavaSoft\JRE\1.3\lib\
ext
26Install 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
27Install Keyboard Method 3
- Copy Keyboard.java into the same directory as
your programs.
28Reading 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)
-
29Reading 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) -
-
30NumberFormat 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
-
-
31Number 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 -
-
32Other NumberFormat methods
- setMaximumIntegerDigits(int digits)
- setMaximumFractionDigits(int digits)
- setMinimumIntegerDigits(int digits)
- setMinimumFractionDigits(int digits)