Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

Syntax the formal rules for combining words and ... Semantics the meaning of the statements; i.e., what the statement ... If a bullfrog had. wings, ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 18
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • Basic Java Syntax
  • and Semantics

2
Terminology
  • Syntax the formal rules for combining words and
    symbols to form legal statements within the
    language.
  • Semantics the meaning of the statements i.e.,
    what the statement does when executed.

3
Literals
  • Literals are items whose values do not change.
  • "Hello World!" is a string literal234 is an
    integer literal-3145 is also an integer
    literal3.14 is a floating point literal5.25E7
    is also a floating point literal

4
Numeric Data Types and Variables
  • We use the type int for integers (4 bytes)
  • We use the type double for floating point numbers
    (8 bytes)
  • A variable is an item whose value can change
    during the execution of the program. A variable
    has
  • A name
  • A type
  • A location in memory
  • A value stored in this location

5
Declaring Variables
  • Before a variable can be used in a program, it
    must be declared. This consists of giving first
    the type of the variable and then its name.
  • double fahrenheitint age, countKeyboardReader
    readerint num20

6
Object Instantiation.
  • When we declare a variable, memory space is
    created for the value of the variable. For a
    variable which is a reference to an object, the
    object is not created simply by declaring the
    variable, only space for the pointer to the
    object.
  • Creating the object itself is called
    instantiating the object. This is done by
    asking for a new object of the given type.

7
Object Instantiation (cont.)
  • KeyboardReader reader
  • reader new KeyboardReader()
  • OrKeyboardReader reader new KeyboardReader()

8
Comments
  • We often need to put comments into the source
    code of a program. A comment has no effect on the
    execution it is there for the benefit of
    someone who reads the program.
  • Examples would be programmers name, purpose of
    the program, explanations to clarify parts of the
    program, etc.
  • Anything following a // on a single line is a
    comment.
  • Anything between / and / , even many lines,
    is a comment.

9
Identifiers in Java
  • Identifiers are used for naming variables,
    methods, objects, etc.
  • Identifiers are strings of
  • Letters
  • Digits
  • Underscores
  • Dollar signs
  • Can not start with digit
  • No spaces
  • Can not be one of Javas Reserved Words

10
Naming Conventions
  • Names should be meaningful
  • Begin names of variables and methods with lower
    case letter.
  • If name is combination of words, run the words
    together and capitalize all but the first.
  • Class names usually begin with capital.

11
Assignment Statements
  • ltvariablegt ltexpressiongt
  • Note single variable on left hand side
  • Expression should yield result which is of type
    appropriate for the variable.
  • Expression created with literals, variables,
    operators, parentheses to form well-formed
    expression
  • Semantics
  • Expression is evaluated using current values of
    variables
  • Resulting value is stored as new value of the
    variable.

12
Common Operators
 
 
 
13
Examples with integer operations
  • Integer division yields the whole number part of
    the result
  • 6/3 yields 2
  • 6/4 yields 1
  • 5/9 yields 0 (hmm)
  • Modulus gives the remainder upon division
  • 6 3 yields 0
  • 6 4 yields 2
  • 5 9 yields 5

14
Examples with precedence
  • 6 4 5 10 5 5
  • 6 4 5 6 20 26
  • (6 4) 5 10 5 50
  • 10 5 6 1 5 6 1 -1 1 -2
  • 10 (5 6 1) 10 (-1 1) 10 (-2) 12
  • 4 11 6 / 2 4 5 / 2 4 2 6

15
Types of Errors
  • Syntax Errors caught by compiler
  • Run-time occur while program is executing
    (maybe divide by 0)
  • Logic errors runs but gives incorrect results.
    celsius (fahrenheit 32.0) (5/9)

16
Testing as you go
  • As you develop more complex programs, a piece of
    advice is to develop small pieces of the program
    at a time and test each before moving on.
  • This avoids having a large piece of code with an
    errror (bug) and trying to locate it within a big
    program.
  • To do this, you can put in extra output
    statements (writer.print) to see values of
    intermediate results, etc.
  • This technique can also be used to find errors.
    Put in statements that print messages and
    variables at various points in the program to
    trace changes.

17
If a bullfrog had wings, he wouldn't ...
Write a Comment
User Comments (0)
About PowerShow.com