Data Conversion & Scanner Class

Similar documents
ECE 122 Engineering Problem Solving with Java

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter. Let's explore some other fundamental programming concepts

Computational Expression

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions

A variable is a name for a location in memory A variable must be declared

2: Basics of Java Programming

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Full file at

Computational Expression

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Programming with Java

CS313D: ADVANCED PROGRAMMING LANGUAGE

More Programming Constructs -- Introduction

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CMPT 125: Lecture 3 Data and Expressions

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

H212 Introduction to Software Systems Honors

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Declaration and Memory

Chapter 2. Elementary Programming

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

ing execution. That way, new results can be computed each time the Class The Scanner

Chapter 2: Data and Expressions

CS110: PROGRAMMING LANGUAGE I

Reading Input from Text File

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Java Basic Introduction, Classes and Objects SEEM

Chapter 2 ELEMENTARY PROGRAMMING

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Primitive Data Types: Intro

Visual C# Instructor s Manual Table of Contents

Full file at

CS 106 Introduction to Computer Science I

CEN 414 Java Programming

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CS111: PROGRAMMING LANGUAGE II

Java Primer 1: Types, Classes and Operators

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Peer Instruction 1. Elementary Programming

A+ Computer Science -

Object Oriented Programming. Java-Lecture 1

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Operators & Expressions

Operators in java Operator operands.

COMP 202 Java in one week

AP Computer Science A

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Programming in C++ 5. Integral data types

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Formatting Output & Enumerated Types & Wrapper Classes

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Lecture 3: Variables and assignment

We now start exploring some key elements of the Java programming language and ways of performing I/O

Java Basic Introduction, Classes and Objects SEEM

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Programming for Engineers Iteration

Elementary Programming

AP Computer Science Unit 1. Programs

Program Fundamentals

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Example: Tax year 2000

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Basic Programming Elements

Lecture Set 4: More About Methods and More About Operators

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Operators. Java operators are classified into three categories:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Program Elements -- Introduction

Chapter 2: Using Data

COMP Primitive and Class Types. Yi Hong May 14, 2015

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Information Science 1

Operators and Expressions

Chapter 2 Elementary Programming

CSC 1214: Object-Oriented Programming

Packages & Random and Math Classes

JAVA OPERATORS GENERAL

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

COMP 202. Java in one week

Lecture Set 4: More About Methods and More About Operators

I. Variables and Data Type week 3

Fundamentals of Programming Data Types & Methods

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Chapter 3: Operators, Expressions and Type Conversion

Transcription:

Data Conversion & Scanner Class Quick review of last lecture August 29, 2007 ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Numeric Primitive Data Storing Information The difference between the various numeric primitive types is their size, and therefore the values they can store: Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value -128-32,768-2,147,483,648 < -9 x 10 18 Max Value 127 32,767 2,147,483,647 > 9 x 10 18 +/- 3.4 x 10 38 with 7 significant digits +/- 1.7 x 10 308 with 15 significant digits 9278 9279 9280 9281 9282 9283 9284 9285 9286 10011010 Each memory cell stores a set number of bits (usually 8 bits, or one byte) Large values are stored in consecutive memory locations Storing a short Storing a double 9278 9279 9280 9281 9282 9283 9284 9285 9286 short (16 bits = 2 bytes) 9278 9279 9280 9281 9282 9283 9284 9285 9286 double (64 bits = 8 bytes) 1

Operator Precedence Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable Other material from Sec 2.4 First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value) Increment and Decrement The increment and decrement operators use only one operand The increment operator (++) adds one to its operand The decrement operator (--) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; Increment and Decrement The increment and decrement operators can be applied in postfix form: or prefix form: count++ ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care 2

Assignment Operators Often we perform an operation on a variable, and then store the result back into that variable Assignment Operators There are many assignment operators in Java, including the following: Java provides assignment operators to simplify that process For example, the statement is equivalent to num += count; Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y num = num + count; Assignment Operators The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-min) % num; is equivalent to result = result / ((total-min) % num); Assignment Operators The behavior of some assignment operators depends on the types of the operands If the operands to the += operator are strings, the assignment operator performs string concatenation The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding operator (+) Chapter 2 Sections 2.5 & 2.6 2.5 Data Conversion 3

Widening Conversions Narrowing Conversions Conversion Techniques 1) Assignment conversion Value of one type is assigned to a variable of another type during which the value is converted to the new type. 2) Promotion Occurs automatically when certain operators need to modify their operands. 3) Casting (a.k.a. type casting) Specified explicitly by the programmer Assignment conversion float money; int dollars; dollars=5; money = dollars; // OK, money is now equal to 5.0 dollars= money; //Compile error (automatic) promotion (automatic) promotion float sum, result; int count; sum= 12.0; count=5; // the number 5 is first promoted to a string and then // the two strings are concatenated System.out.println( Five is equal to + 5); result = sum/count; // count promoted to float // before the division 4

Type Casting The programmer explicitly asks the compiler to change the type of a variable or a temporary result before the next operation will take place. Without the cast Java typically will refuse to compile the program Type Casting float money; int dollars; dollars=5; money = dollars; // OK, money is now equal to 5.0 dollars= (int) money; //Compile error OK Type Casting + Promotion Type Casting + Promotion float result; int total, count; total= 12; count=5; result = (float) total / count; // result = 2.4 // 1. total is cast to float // 2. count is promoted to float // 3. the division is performed float result; int total, count; total= 12; count=5; result = (float) (total / count); // result = 2.0 // 1. total and count a divided using integer division // 2. the intermediary result is cast to a float // 3. this float value is assigned to result Interactive Programs Programs generally need input on which to operate Casting Example The Scanner class provides convenient methods for reading input values of various types A Scanner object can be set up to read input from various sources, including the user typing values on the keyboard Keyboard input is represented by the System.in object 5

Reading Input The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); The new operator creates the Scanner object Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextline(); In order to use the Scanner object you must put this line at the top of your Java program import java.util.scanner; Reading Input The Scanner class is part of the java.util class library, and must be imported into a program to be used See Echo.java (page 91) The nextline method reads all of the input until the end of the line is found The details of object creation and class libraries are discussed further in Chapter 3 Input Tokens Unless specified otherwise, white space is used to separate the elements (called tokens) of the input Scanner Class White space includes space characters, tabs, new line characters The next method of the Scanner class reads the next input token and returns it as a string Methods such as nextint and nextdouble read data of particular types See GasMileage.java (page 92) More Scanner Examples THE END 6