Program Elements -- Introduction

Similar documents
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

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

ECE 122 Engineering Problem Solving with Java

Chapter. Let's explore some other fundamental programming concepts

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Full file at

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

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

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

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

Chapter 2: Data and Expressions

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 2: Data and Expressions

CMPT 125: Lecture 3 Data and Expressions

Chapter 2: Data and Expressions

Basics of Java Programming

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

Full file at

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Variables, Constants, and Data Types

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at C How to Program, 6/e Multiple Choice Test Bank

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

2: Basics of Java Programming

COMP 202 Java in one week

Conditionals and Loops

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

AP Computer Science A

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Chapter 2: Using Data

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Data Types and the while Statement

IT 374 C# and Applications/ IT695 C# Data Structures

Lecture Set 2: Starting Java

Computational Expression

Visual C# Instructor s Manual Table of Contents

Lecture Set 2: Starting Java

Introduction to Programming

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2 Elementary Programming

COMP 202. Java in one week

Introduction to Java Applications; Input/Output and Operators

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

What did we talk about last time? Examples switch statements

2.5 Another Application: Adding Integers

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

4 Programming Fundamentals. Introduction to Programming 1 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Chapter 4: Conditionals and Loops

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lecture 3 Tao Wang 1

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

Chapter 2. C++ Basics

Chapter 6 Primitive types

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Data Conversion & Scanner Class

Declaration and Memory

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

Chapter 1 Introduction to Computers and C++ Programming

Program Fundamentals

VLC : Language Reference Manual

Types and Expressions. Chapter 3

CS11 Java. Fall Lecture 1

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Getting started with Java

corgi Language Reference Manual COMS W4115

Java Notes. 10th ICSE. Saravanan Ganesh

CS110: PROGRAMMING LANGUAGE I

UNIT- 3 Introduction to C++

CSCI 123 Introduction to Programming Concepts in C++

Introduction to C# Applications

Language Reference Manual

Section 2: Introduction to Java. Historical note

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

COMP 202 Java in one week

Programming with Java

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

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Transcription:

Program Elements -- Introduction We can now examine the core elements of programming Chapter 3 focuses on: data types variable declaration and use operators and expressions decisions and loops input and output 1

Primitive Data Types A data type is defined by a set of values and the operators you can perform on them Each value stored in memory is associated with a particular data type The Java language has several predefined types, called primitive data types The following reserved words represent eight different primitive types: byte, short, int, long, float, double, boolean, char 2

Integers There are four separate integer primitive data types They differ by the amount of memory used to store them Type Storage Min Value Max Value byte short int long 8 bits 16 bits 32 bits 64 bits -128-32,768-2,147,483,648 < -9 x 10 18 127 32,767 2,147,483,647 > 9 x 10 18 3

Floating Point There are two floating point types: Type Storage Approximate Min Value Approximate Max Value float double 32 bits 64 bits -3.4 x 10 38-1.7 x 10 308 3.4 x 10 38 1.7 x 10 308 The float type stores 7 significant digits The double type stores 15 significant digits 4

Characters A char value stores a single character from the Unicode character set A character set is an ordered list of characters The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages 5

Characters The ASCII character set is still the basis for many other programming languages ASCII is a subset of Unicode, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, a, b, c, period, semi-colon, 0, 1, 2, &,, \, carriage return, tab,... 6

Boolean A boolean value represents a true or false condition They can also be used to represent any two states, such as a light bulb being on or off The reserved words true and false are the only valid values for a boolean type 7

Wrappers For each primitive data type there is a corresponding wrapper class. For example: Primitive Type int double char boolean Wrapper Class Integer Double Character Boolean Wrapper classes are useful in situations where you need an object instead of a primitive type They also contain some useful methods 8

Variables A variable is an identifier that represents a location in memory that holds a particular type of data Variables must be declared before they can be used The syntax of a variable declaration is: data-type variable-name; For example: int total; 9

Variables Multiple variables can be declared on the same line: int total, count, sum; Variables can be initialized (given an initial value) in the declaration: int total = 0, count = 20; float unit_price = 57.25; See Piano_Keys.java 10

Assignment Statements An assignment statement takes the following form: variable-name = expression; The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable See United_States.java The expression can be a single value or a more complicated calculation 11

Constants A constant is similar to a variable except that they keep the same value throughout their existence They are specified using the reserved word final in the declaration For example: final double PI = 3.14159; final int STUDENTS = 25; 12

Constants When appropriate, constants are better than variables because: they prevent inadvertent errors because their value cannot change They are better than literal values because: they make code more readable by giving meaning to a value they facilitate change because the value is only specified in one place 13

Input and Output Java I/O is based on input streams and output streams There are three predefined standard streams: Stream System.in System.out System.err Purpose reading input writing output writing errors Default Device keyboard monitor monitor The print and println methods write to standard output 14

Input and Output The Java API allows you to create many kinds of streams to perform various kinds of I/O To read character strings, we will convert the System.in stream to another kind of stream using: BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); This declaration creates a new stream called stdin We will discuss object creation in more detail later 15

Escape Sequences See Echo.java An escape sequence is a special sequence of characters preceded by a backslash (\) They indicate some special purpose, such as: Escape Sequence \t \n \" \' \\ Meaning tab new line double quote single quote backslash 16

Buffers As you type, the characters are stored in an input buffer When you press enter, the program begins processing the data Similarly, output information is temporarily stored in an output buffer The output buffer can be explicitly flushed (sent to the screen) using the flush method See Python.java 17

Numeric Input Converting a string that holds an integer into the integer value can be done with a method in the Integer wrapper class: value = Integer.parseInt (my_string); A value can be read and converted in one line: num = Integer.parseInt (stdin.readline()); See Addition.java and Addition2.java 18

Expressions An expression is a combination of operators and operands The arithmetic operators include addition (+), subtraction (-), multiplication (*), and division (/) Operands can be literal values, variables, or other sources of data The programmer determines what is done with the result of an expression (stored, printed, etc.) 19

Division If the operands of the / operator are both integers, the result is an integer (the fractional part is truncated) If one or more operands to the / operator are floating point values, the result is a floating point value The remainder operator (%) returns the integer remainder after dividing the first operand by the second The operands to the % operator must be integers See Division.java The remainder result takes the sign of the numerator 20

Division Expression Result 17 / 5 17.0 / 5 17 / 5.0 3 3.4 3.4 9 / 12 9.0 / 12.0 0 0.75 6 % 2 14 % 5-14 % 5 0 4-4 21

Operator Precedence The order in which operands are evaluated in an expression is determined by a well-defined precedence hierarchy Operators at the same level of precedence are evaluated according to their associativity (right to left or left to right) Parentheses can be used to force precedence Appendix D contains a complete operator precedence chart for all Java operators 22

Operator Precedence Multiplication, division, and remainder have a higher precedence than addition and subtraction Both groups associate left to right Expression: 5 + 12 / 5-10 % 3 Order of evaluation: 3 1 4 2 Result: 6 23

Operator Precedence Expression Result 2 + 3 * 4 / 2 8 3 * 13 + 2 (3 * 13) + 2 3 * (13 + 2) 41 41 45 4 * (11-6) * (-8 + 10) 40 (5 * (4-1)) / 2 7 24

The if Statement The Java if statement has the following syntax: if (condition) statement; If the boolean condition is true, the statement is executed; if it is false, the statement is skipped This provides basic decision making capabilities 25

The if Statement condition false true statement 26

Boolean Expressions The condition of an if statement must evaluate to a true or false result Java has several equality and relational operators: Operator Meaning ==!= < <= > <= equal to not equal to less than less than or equal to greater than greater than or equal to See Temperature.java 27

Block Statements Several statements can be grouped together into a block statement Blocks are delimited by braces A block statement can be used wherever a statement is called for in the Java syntax See Temperature2.java 28

The if-else Statement An else clause can be added to an if statement to make it an if-else statement: if (condition) else statement1; statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed See Temperature3.java and Right_Triangle.java 29

The if-else Statement condition false true statement1 statement2 30

Nested if Statements The body of an if statement or else clause can be another if statement These are called nested if statements See Football_Choice.java Note: an else clause is matched to the last unmatched if (no matter what the indentation implies) 31

The while Statement A while statement has the following syntax: while (condition) statement; If the condition is true, the statement is executed; then the condition is evaluated again The statement is executed over and over until the condition becomes false 32

The while Statement condition false true statement 33

The while Statement If the condition of a while statement is false initially, the statement is never executed Therefore, we say that a while statement executes zero or more times See Counter.java, Factors.java, and Powers_of_Two.java 34

Infinite Loops The body of a while loop must eventually make the condition false If not, it is an infinite loop, which will execute until the user interrupts the program This is a common type of logical error -- always double check that your loops will terminate normally See Forever.java 35

Program Development The creation of software involves four basic activities: establishing the requirements creating a design implementing the code testing the implementation The development process is much more involved that this, but these basic steps are a good starting point 36

Requirements Requirements specify the tasks a program must accomplish (what to do, not how to do it) They often address the user interface An initial set of requirements are often provided, but usually must be critiqued, modified, and expanded It is often difficult to establish detailed, unambiguous, complete requirements Careful attention to the requirements can save significant time and money in the overall project 37

Design A program follows an algorithm, which is a step-by-step process for solving a problem The design specifies the algorithms and data needed In object-oriented development, it establishes the classes, objects, and methods that are required The details of a method may be expressed in pseudocode, which is code-like, but does not necessarily follow any specific syntax 38

Implementation Implementation is the process of translating a design into source code Most novice programmers think that writing code is the heart of software development, but it actually should be the least creative Almost all important decisions are made during requirements analysis and design Implementation should focus on coding details, including style guidelines and documentation 39

Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process of discovering the cause of a problem and fixing it Programmers often erroneously think that there is "only one more bug" to fix Tests should focus on design details as well as overall requirements 40

Program Development See Average.java Follow the process of requirements analysis, design, implementation, and testing There are always multiple ways to design and implement a program Any design has advantages and disadvantages; there are always trade-offs See Average2.java 41