600'107 Introduction to Programming in Java - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

600'107 Introduction to Programming in Java

Description:

Rules to determine invalid Vs valid identifiers. Cannot be a reserved word. No ... 3 kinds of primitive data types. Type conversion ... Decrement operators, ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 26
Provided by: jaima
Category:

less

Transcript and Presenter's Notes

Title: 600'107 Introduction to Programming in Java


1
600.107Introduction to Programming in Java
  • Review Session
  • Jai Madhok
  • Email jaimadhok_at_jhu.edu

2
Key Concepts
  • Identifiers- What are they?
  • Rules to determine invalid Vs valid identifiers
  • Cannot be a reserved word
  • No spaces in between
  • No Symbols other than the and _
  • Cannot begin with a digit

3
Data Types
  • 3 kinds of primitive data types
  • Type conversion between data types
  • Implicit Vs Explicit type conversion
  • The Cast operator
  • Syntax (dataType) Expression
  • What happens when an operator has mixed operands?

4
Strings
  • It is an Object
  • In Java, they are enclosed in double quotes
  • What is a null string? Empty string?
  • How does indexing work?
  • Predefined Methods
  • substring(startIndex, stopIndex)
  • charAt(index)
  • indexOf(ch)
  • concat()
  • length()
  • replace()
  • toLowerCase()/toUpperCase()

5
Named Constants
  • Use the word final
  • Identifier is in all Capital Letters
  • Memory location whose content is not allowed to
    change during program execution
  • Ex final int CENTS_PER_DOLLAR100
  • Several Advantages
  • Prevent typographical errors
  • Easy to modify the program in the future
  • If you mistype the name of the location computer
    will warn you as compared to typing in a wrong
    value which the computer will blindly accept
  • Good programming practice

6
Input Statements
  • To put data into variables from an input device
    we first create an input stream object and
    associate it with a standard input device
  • Scanner scan new Scanner (System.in)
  • More about Scanner
  • next()
  • nextInt()
  • nextDouble()
  • nextLine()
  • How do we read in a single character using a
    scanner object?

Know how to use these!
7
Tokenizing Parsing
  • Sometimes we read in input in the form of a
    string and then want to extract numeric data from
    it
  • How do we do this? Tokenize and Parse
  • Syntax
  • StringTokenizer t new StringTokenizer(str,
    delimiters)
  • Say we want an integer
  • int a Integer.parseInt(t.nextToken())
  • Other method Double.parseDouble()
  • Note Parsing and type casting are different
    things

8
File Handling
  • A file is defined as an area in secondary storage
    used to hold information
  • Scanner scanf new Scanner(new FileReader(filename
    1))
  • PrintWriter oFile new PrintWriter(filename2)
  • Warning Closing the file you are writing to is
    more important than you think it is
  • Use same methods for reading and
    writing/formatting files as you have been for
    user input/monitor display

9
Formatting output with printf
  • Syntax
  • System.out.printf(formatString, argumentList)
  • formatString is a string specifying the format
  • argumentList list of arguments containing
    constant values, variables, expressions separated
    by commas
  • --------------------------------------------------
    -----------------------------------
  • Example
  • System.out.printf(There are .2f inches in d
    centimeters n ,cm/2.54, cm)
  • Where cm is a variable of the type int.
  • Note You can also use the DecimalFormat class
    to format your decimal output. Take your pick!

10
More fun with strings
  • compareTo
  • Compare Strings character by character until a
    mismatch is found.
  • What does this method return?
  • What is the basis for deciding what a mismatch
    is and what it isnt?
  • equals
  • Case sensitive method
  • How to solve the case sensitivity issue?
  • What does the method return?

11
Operators- Order of Precedence
  • () .
  • Unary Operators negation, casting, not (!),
    , --
  • Arithmetic Operators I /
  • Arithmetic Operators II -
  • Comparison Operators lt lt gt gt
  • Equivalence Operators !
  • Logical AND
  • Logical OR
  • Ternary Operator ?
  • Assignment Operators , , -, , /,

12
Control Structures
  • Provide alternatives to sequential program
    execution and are used to alter flow of execution
  • Alternatives are
  • (i) Selection if, if/else, switch/case
  • (ii) Repitition for, while, do-while

13
While loops
  • Using loop control variables
  • Counter controlled while loops
  • Sentinel Controlled while loops
  • Flag controlled while loops
  • EOF controlled while loops
  • Do-While Loops ? different
  • from a generic while

T
F
14
For loops
  • for (initial statement loop condition update
    Statement)
  • KNOW THIS LOOP COLD!

Initial Statement
Loop Condition
T
Statements
Update Statement
F
15
Break Statement
  • Exit early from a loop
  • Skip the remaining cases in a switch case
    construct
  • Dont try and use them at random places to make
    your code work, most of the time it wont.

16
Packages in Java
  • util Scanner, StringTokenizer
  • io- BufferedReader, FileReader
  • text- DecimalFormat
  • lang String, Math

17
Miscellaneous things
  • Reading flow in java
  • a a 2
  • A in ASCII is 65
  • ASCII is a subset of Unicode
  • Escape Sequences
  • Compound Operators
  • Increment/Decrement operators, pre/post

18
Documentation Reading
  • Read up some common methods of the following
    classes for use in the assignments and exams.
  • String
  • Scanner
  • StringTokenizer
  • Math
  • Character

19
Exam Format
  • True/False
  • Multiple Choice
  • Code Tracing
  • Code Completion
  • Finding Errors
  • Other random kinds of questions like matching
    exercises are a possibility

20
Exam Tips
  • Read through all questions carefully
  • Write answers legibly, because we have to be able
    to read them to grade them
  • Show all work for partial credit
  • Do not spend too much time on any one particular
    problem if it is causing trouble, come back to it
    later on
  • Read through the chapter summaries in the text
    book and all of Dr.Hs class notes
  • Good Luck!

21
Questions
  • ?

22
Sample Code Tracing 1
  • char let A
  • for (int i1 ilt3i)
  • for(int jIjlt5j)
  • System.out.print(let)
  • System.out.println()
  • let1
  • Predict the output? 6 points
    Difficulty Medium

23
Sample Code Tracing 2
  • n1 6 n210
  • if(n1 gt n2/2)
  • if(true false)
  • System.out.print( one )
  • else
  • System.out.print(two )
  • System.out.print(three )
  • Predict the output? 3 points Difficulty Easy

24
Sample Code Tracing 3
  • int num6
  • while(numlt16)
  • switch(num4)
  • case 1 System.out.println(one) break
  • case 2 System.out.println(two)
  • case 3 System.out.println(three)break
  • case 0 System.out.println(multiple)
  • num3
  • 4-6 points Difficulty Medium

25
Sample Code Tracing 4
  • boolean atrue, bfalse
  • if (a (true !b) )
  • if( b (!a false) )
  • System.out.println(happy)
  • else System.out.println( halloween )
  • System.out.println(goblins)
  • 6 points Difficulty Medium
Write a Comment
User Comments (0)
About PowerShow.com