Outline for today 12199 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Outline for today 12199

Description:

Operator precedence ... Order of evaluation is left to right with the following precedence ... and - have the same precedence lower than * and / Try some! What ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 20
Provided by: arijits
Category:

less

Transcript and Presenter's Notes

Title: Outline for today 12199


1
Outline for today (1/21/99)
  • Recap and todays buzzwords
  • Storage bins in programs - variables
  • video Guy Steele on Extending the Java
    programming langauge
  • Data types
  • Input and output
  • Simple arithmetic and operators
  • Syntax summary

2
Recap
3
Todays buzzwords
  • Variables
  • storage bins in programs - can usually only
    hold one thing
  • Memory location/address
  • a number referring to the place in memory in
    which a variable is stored
  • Data types
  • A variable needs to have a type, like a number, a
    character
  • Arithmetic Operators
  • A symbol representing arithmetic operations on
    values (e.g., - / )
  • Operator precedence
  • In an arithmetic expression, a rule that governs
    which operators will be applied first.
  • Assignment operator
  • An operator that takes one value and copies it
    into another

4
Variables
  • Storage areas for keeping values for later use
  • Each variable is unique, and must have a unique
    name (no two variables can have the same name for
    now)
  • Each variable (for now) can only have one value
    in it
  • You can change the value in a variable at will
  • (thats why it is a variable - it varies.)

5
Variable names
  • Must start with a alphabet
  • Can have any number of characters and numbers and
    the underscore _
  • Spaces and punctuations are not permitted
  • Which of the following are valid names?
  • abcd Hello_world
  • 1234 hello,world
  • 3abCd5 Hello world

6
Naming tips
  • Never use single-character names like x, y, i
    although they are common in arithmetic
  • Use descriptive names like count, index, price,
    amount
  • If necessary use multi-word names but ensure the
    words can be read easily, e.g.,
  • priceOfBook, amount_of_loan

7
Lets watch a video!
  • Guy Steele, Sun Microsystems
  • One of the people in Sun as part of the
  • Java Development Team
  • Extending the Java Programming Language

8
Data types
  • Every variable must have a unique type
  • the range of values of a variable depends on its
    type
  • Variables retain the same type during the life of
    the program.
  • Type names are reserved words in Java and can not
    be used for anything else

9
Basic Data Types in Java
  • Number types - can have numeric values
  • int integer values e.g., 10 35598
  • long, short, byte (different sizes of integers)
  • float values with decimal points e.g., 45.768
    (about 7 digits)
  • double decimal values upto about 13 decimal
    places
  • Character types - any ASCII character
  • char character data e.g., 'a' 'b' 'c' 'd'
  • Logical types - only logical values (true, false)
  • boolean

10
Giving types to variables
  • Each variable must be declared.
  • Variable declaration specifies the type and name
    of a variable. e.g.
  • float price
  • int numberOfItems
  • boolean paid
  • You can declare multiple variables in one
    statement
  • double loanAmount, interestPaid

11
Getting values in variables
  • Initialization (assign value when declaring)
  • int numberOfItems 25
  • boolean finished false
  • Assignment (assign value later)
  • float amountOfLoan
  • amountOfLoan 35000.00
  • The is called the assignment operator.

12
More on Assignment Operator
  • The assignment operator calculates the right
    side, and puts the result in the variable in the
    left side
  • Examples
  • totalScore lab1Score lab2Score
  • averagePoints totalPoints / numberOfGames
  • total total itemPrice / How? /
  • Added slide on conversions

13
Implicit and Explicit Conversions
  • Sometimes data is converted implicitly (you may
    not realize that there is a type conversion)
  • double totalAmount 100
  • Often data needs to be converted explicitly (you
    say how you want data to be converted)
  • int totalAmount (int)someDoubleVariable

14
Input and Output
  • Getting input from the user is usually hard in
    Java, because you need to consider many possible
    problems beforehand
  • In this class we use a library instead (the ccj
    library from the book)
  • To use this library, you put the following line
    at the top of your code
  • import ccj.

15
Producing output
  • We already know how to output simple text
  • System.out.println("Hello, A201 folks!\n")
  • To output values of variables, you can use
  • System.out.println(averageScore)
  • To mix and match
  • System.out.println("The class average is "
    averageScore)

16
Getting input
  • With the ccj library, its easy to get input.
  • To read an integer, you use the method
    Console.in.readInt(). e.g.
  • System.out.println(Enter total number of
    students )
  • int noStudents Console.in.readInt()
  • Similar for reading doubles.

17
Arithment Operations in Java
  • Addition (), Subtraction (-), Multiplication
    (), Division (/)
  • Individual operations can be blocked using
    parentheses
  • Order of evaluation is left to right with the
    following precedence
  • ( ) have the highest precedence, innermost first
  • and / have the same precedence
  • and - have the same precedence lower than and
    /

18
Try some!
  • What do the following statements do?
  • int result 7 - 3 5
  • int result (7- 3) 5
  • int result 5 (5 - (5 5))
  • int result (5 5) - 5 5
  • int result 10 / 4
  • float result 10 / 4
  • float result 10.0 / 4

19
Syntax summary
  • Variable names
  • start with a letter, only letters and numbers and
    _
  • Types of variables
  • int, short, long, byte, float, double, char,
    boolean
  • Variable declaration and initialization
  • lttypegt ltnamegt ltvaluegt
  • You have to declare a variable before using it.
  • Importing a library
  • import ccj.
Write a Comment
User Comments (0)
About PowerShow.com