Introduction to Computer Science I Scanner, Increment/Decrement, - - PowerPoint PPT Presentation

introduction to computer science i
SMART_READER_LITE
LIVE PREVIEW

Introduction to Computer Science I Scanner, Increment/Decrement, - - PowerPoint PPT Presentation

Introduction to Computer Science I Scanner, Increment/Decrement, Conversion Janyl Jumadinova September 19, 2016 Scanner The Scanner class in the java.util package is a simple text scanner which can parse primitive types and strings We


slide-1
SLIDE 1

Introduction to Computer Science I

Scanner, Increment/Decrement, Conversion

Janyl Jumadinova September 19, 2016

slide-2
SLIDE 2

Scanner

◮ The Scanner class in the java.util package is a simple text

scanner which can parse primitive types and strings

◮ We can use the Scanner class to get the input from the

terminal

◮ We must create an instance of the Scanner as:

Scanner name = new Scanner (System.in) where name is the name you choose for your instance of the Scanner

2/10

slide-3
SLIDE 3

Scanner Methods

◮ next() : get the next word (token) as a String ◮ nextLine() : get a line of input as a String ◮ nextInt() : get an integer ◮ nextDouble() : get a double value 3/10

slide-4
SLIDE 4

Memory Concepts

◮ Variable names such as first, second and sum correspond to

locations in the computer’s memory.

4/10

slide-5
SLIDE 5

Memory Concepts

◮ Variable names such as first, second and sum correspond to

locations in the computer’s memory.

◮ Every variable has:

  • a name,
  • a type,
  • a size,
  • a value

4/10

slide-6
SLIDE 6

Memory Concepts

◮ Variable names such as first, second and sum correspond to

locations in the computer’s memory.

◮ Every variable has:

  • a name,
  • a type,
  • a size,
  • a value

◮ first = input.nextInt(); stores whatever the user types

into the location associated with first.

4/10

slide-7
SLIDE 7

Memory Concepts

◮ Whenever a value is placed in a memory location, it replaces

whatever was there before.

◮ This includes keyboard input (such as

first = input.nextInt()) and assignment statements (such as sum = first + second).

5/10

slide-8
SLIDE 8

Increment and Decrement Operators

◮ ++: adds 1 to any value

count ++ same as count=count+1 prefix form: ++count postfix form: count++

◮ --: subtracts 1 from any value

count -- same as count=count-1 prefix form: --count postfix form: count--

6/10

slide-9
SLIDE 9

Increment and Decrement Operators

◮ ++: adds 1 to any value

count ++ same as count=count+1 prefix form: ++count postfix form: count++

◮ --: subtracts 1 from any value

count -- same as count=count-1 prefix form: --count postfix form: count--

◮ total = count ++; vs. total = ++count; 6/10

slide-10
SLIDE 10

Increment and Decrement Operators

◮ += (-=, *=, ..): combine basic operation with assignment

count += num same as count=count+num

7/10

slide-11
SLIDE 11

Conversion

◮ Widening

  • from byte to short, int, long, float or double
  • from short to int, long, float, double
  • from char to int, long, float, double
  • from int to long, float, double
  • from long to float, double
  • from float to double

8/10

slide-12
SLIDE 12

Conversion

◮ Narrowing - should be avoided!

  • from byte to char
  • from short to byte, char
  • from char to byte, short
  • from int to byte, short, char
  • from long to byte, short, char, int
  • from float to byte, short, char, int, long
  • from double to byte, short, char, int, long, float

9/10

slide-13
SLIDE 13

Conversion

◮ Assignment: grade = ‘A’ ◮ Promotion: total/count, where total is a floating point

value and count is an integer

  • Occurs automatically, count is promoted to a floating point

value

◮ Casting: grade = (int) total

  • Java operator: type name in parentheses
  • Casting converts floating point value total into an integer,

truncating any fractional part.

10/10