Idioms and nuances - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Idioms and nuances

Description:

Idioms and nuances. Idiom. What's an idiom? Phrase that cannot be understood by ... Idiom for common. programming tasks. Scanner stdin = new Scanner( System.in ) ... – PowerPoint PPT presentation

Number of Views:523
Avg rating:3.0/5.0
Slides: 38
Provided by: jpco
Category:
Tags: idiom | idioms | nuances

less

Transcript and Presenter's Notes

Title: Idioms and nuances


1
Idioms and nuances
2
Idiom
  • Whats an idiom?
  • Phrase that cannot be understood by looking at
    its parts in isolation
  • Specialized phrase
  • Example
  • currentNumber
  • currentNumber currentNumber 1

3
For loop
  • Purpose
  • Idiom for commonprogramming tasks

4
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • int j 0
  • while ( j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

5
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • int j 0
  • while ( j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

6
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • int j 0
  • while ( j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

7
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • int j 0
  • while ( j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

8
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • while ( int j 0 j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

9
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • while ( int j 0 j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

10
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • while ( int j 0 j lt n )
  • System.out.print( j " " )
  • System.out.println()

11
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • while ( int j 0 j lt n j )
  • System.out.print( j " " )
  • System.out.println()

12
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • for ( int j 0 j lt n j )
  • System.out.print( j " " )
  • System.out.println()

13
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • for ( int j 0 j lt n j )
  • System.out.print( j " " )
  • System.out.println()

14
For loop fancified while loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • for ( int j 0 j lt n j )
  • System.out.print( j " " )
  • System.out.println()

15
For loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • for ( int j 0 j lt n j )
  • System.out.print( j " " )
  • System.out.println()

16
While loop
  • Scanner stdin new Scanner( System.in )
  • System.out.print( "Enter a number " )
  • int n stdin.nextInt()
  • int j 0
  • while ( j lt n )
  • System.out.print( j " " )
  • j
  • System.out.println()

17
ForLoop.java
for (int i 0 i lt 3 i) System.out.println(
"i is " i ) System.out.println("all done")
for (int i 0 i lt 3 i) System.out.println(
"i is " i ) System.out.println("all done")
Variable i is local to the for loop. It does
exist not outside of it
i is 0
i is 0 i is 1
i is 0 i is 1 i is 2
i is 0 i is 1 i is 2 all done
18
IllegalScope.java
  • for (int j 0 i lt n i)
  • System.out.println( j " " )
  • System.out.println( "j is " j )

19
ListRange.java
  • int m stdin.nextInt()
  • int n stdin.nextInt()
  • for (int j m j lt n j )
  • System.out.println( j " " )
  • System.out.println()

Displays the numbers m n
20
SumRange.java
  • int m stdin.nextInt()
  • int n stdin.nextInt()
  • int sum 0
  • for ( int j m j lt n j )
  • sum sum j
  • System.out.println( "Sum from " m " to "
  • n " is " sum )

Displays the sum of m n
21
NestedLoop.java
int m stdin.nextInt() int n
stdin.nextInt() for (int i 0 i lt m i)
System.out.println("i is " i) for (int j
0 j lt n j) System.out.println(" j is "
j)
int m stdin.nextInt() int n
stdin.nextInt() for (int i 0 i lt m i)
System.out.println("i is " i) for (int j
0 j lt n j) System.out.println(" j is "
j)
int m stdin.nextInt() int n
stdin.nextInt() for (int i 0 i lt m i)
System.out.println("i is " i) for (int j
0 j lt n j) System.out.println(" j is "
j)
int m stdin.nextInt() int n
stdin.nextInt() for (int i 0 i lt m i)
System.out.println("i is " i) for (int j
0 j lt n j) System.out.println(" j is "
j)
// suppose m is 3 // suppose n is 2
  • i is 0
  • j is 0
  • j is 1
  • i is 1
  • j is 0
  • j is 1
  • i is 2
  • j is 0
  • j is 1

22
Counts.java
  • int counter1 0 int counter2 0 int counter3
    0
  • int counter4 0 int counter5 0
  • for (int i 0 i lt 5 i)
  • counter1
  • for (int j 0 j lt 10 j)
  • counter2
  • for (int k 0 k lt 2 k)
  • counter3
  • counter4
  • counter5
  • System.out.println(counter1 " " counter2 "
    "
  • counter3 " " counter4 " " counter5)

5
50
100
50
5
23
Nuances
24
Nuance
  • Whats a nuance?
  • A subtle or find distinction
  • The intended message
  • Example
  • I will not be happy if you do that, said the
    parent

25
Shorthand representation
26
Examples
  • Consider
  • String a "excellence
  • String b a
  • What is the representation?

27
Examples
  • Consider
  • String a "excellence
  • String b a
  • What is the representation?

28
Uninitialized versus null
  • Consider
  • String dayOfWeek
  • Scanner inStream
  • What is the representation?

29
Uninitialized versus null
  • Consider
  • String fontName null
  • Scanner fileStream null
  • What is the representation?

30
Quick survey
  • What's the memory representation for the
    following definitions?
  • String word1 null
  • String word2
  • String word3 "luminous"

31
Assignment
  • Consider
  • String word1 "luminous"
  • String word2 "graceful"
  • word1 word2
  • Initial representation

32
Assignment
  • Consider
  • String word1 "luminous"
  • String word2 "graceful"
  • word1 word2
  • After assignment

33
Testing variables for equality
  • Consider
  • String a stdin.next()
  • String b stdin.next()
  • if ( a b )
  • System.out.println( "Same" )
  • else
  • System.out.println( "Different" )

What is the output if the user enters different
words? What about the same words?
34
Testing variables for equality
  • Consider
  • String a stdin.next()
  • String b stdin.next()
  • if ( a b )
  • System.out.println( "Same" )
  • else
  • System.out.println( "Different" )

What is the output if the user enters different
words? What about the same words?
35
Testing objects for equality
  • Consider
  • String a stdin.next()
  • String b stdin.next()
  • if ( a.equals( b ) )
  • System.out.println( "Same" )
  • else
  • System.out.println( "Different" )

What is the output if the user enters different
words? What about the same words?
36
Testing objects for equality
  • Consider
  • String a stdin.next()
  • String b stdin.next()
  • if ( a.equals( b ) )
  • System.out.println( "Same" )
  • else
  • System.out.println( "Different" )

All objects have an equals() method
Responsibility of the classs designer to make
sure its definition is sensible
37
Short-circuit evaluation
  • Consider
  • a b
  • a b
  • Facts
  • If the left operand of is false, then the
    must be false
  • If the left operand of is true, then the
    must be true
  • Java uses the facts to make logical operations
    efficient
  • If the value of an operation determined from the
    left operand, then the right operand is not
    evaluated
  • The operation is short-circuited

38
Short-circuit evaluation
  • Suppose you are interested in knowing whether
    scoreSum divided by nbrScores is greater than
    value
  • The condition can be evaluated only if nbrScores
    is nonzero
  • The following expression correctly represents the
    condition
  • ( nbrScores ! 0 ) ( ( scoreSum / nbrScores )
    gt value )
Write a Comment
User Comments (0)
About PowerShow.com