# More Programming Constructs -- Introduction

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and another more operators more selection statements more repetition statements 1

2 Internal Data Representation We discussed earlier that every piece of information stored on a computer is represented as binary values What is represented by the following binary string? You can't tell just from the bit string itself. We take specific binary values and apply an interpretation to them 2

3 Representing Integers There are four types of integers in Java, each providing a different bits to store the value Each has a sign bit. If it is 1, the number is negative; if it is 0, the number is positive byte s 7 bits short s 15 bits int s 31 bits long s 63 bits 3

4 Two's Complement Integers are stored in signed two's complement format A positive value is a straightforward binary number A negative value is represented by inverting all of the bits of the corresponding positive value, then adding 1 To "decode" a negative value, invert all of the bits and add 1 Using two's complement makes internal arithmetic processing easier 4

5 Two's Complement The number 25 is represented in 8 bits (byte) as To represent -25, first invert all of the bits then add Note that the sign bit reversed, indicating the number is negative 5

6 Overflow and Underflow Storing numeric values in a fixed storage size can lead to overflow and underflow problems Overflow occurs when a number grows too large to fit in its allocated space Underflow occurs when a number shrinks too small to fit in its allocated space See Overflow.java 6

7 Representing Floating Point Values A decimal (base 10) floating point value can be defined by the following equation sign * mantissa * 10 exponent where sign is either 1 or -1 mantissa is a positive value that represents the significant digits of the number exponent is a value that indicates how the decimal point is shifted relative to the mantissa 7

8 Representing Floating Point Values For example, the number can be represented by -1 * * 10-3 Floating point numbers can be represented in binary the same way, except that the mantissa is a binary number and the base is 2 instead of 10 sign * mantissa * 2 exponent Floating point values are stored by storing each of these components in the space allotted 8

9 Representing Characters As described earlier, characters are represented according to the Unicode Character Set The character set matches a unique number to each character to be represented Storing the character is therefore as simple as storing the binary version of the number that represents it For example, the character 'z' has the Unicode value 122, which is represented in 16 bits as

10 Representing Characters Because they are stored as numbers, Java lets you perform some arithmetic processing on characters For example, because 'A' is stored as Unicode value 65, the statement char ch = 'A' + 5; will store the character 'F' in ch (Unicode value 70) This relationship is occasionally helpful 10

11 Conversions Each data value and variable is associated with a particular data type It is sometimes necessary to convert a value of one data type to another Not all conversions are possible. For example, boolean values cannot be converted to any other type and vice versa Even if a conversion is possible, we need to be careful that information is not lost in the process 11

12 Widening Conversions Widening conversions are generally safe because they go from a smaller data space to a larger one The widening conversions are: From To byte short char int long float short, int, long, float, or double int, long, float, or double int, long, float, or double long, float, or double float or double double 12

13 Narrowing Conversions Narrowing conversions are more dangerous because they usually go from a smaller data space to a larger one The narrowing conversions are: From To byte short char int long float double char byte or char byte or short byte, short, or char byte, short, char, or int byte, short, char, int or long byte, short, char, int, long, or float 13

14 Performing Conversions In Java, conversion between one data type and another can occur three ways Assignment conversion - when a value of one type is assigned to a variable of another type Arithmetic promotion - occurs automatically when operators modify the types of their operands Casting - an operator that forces a value to another type 14

15 Assignment Conversion For example, if money is a float variable and dollars is an int variable (storing 82), then money = dollars; converts the value 82 to 82.0 when it is stored The value in dollars is not actually changed Only widening conversions are permitted through assignment Assignment conversion can also take place when passing parameters (which is a form of assignment) 15

16 Arithmetic Promotion Certain operators require consistent types for their operands For example, if sum is a float variable and count is an int variable, then the statement result = sum / count; internally converts the value in count to a float then performs the division, producing a floating point result The value in count is not changed 16

17 Casting A cast is an operator that is specified by a type name in parentheses It is placed in front of the value to be converted The following example truncates the fractional part of the floating point value in money and stores the integer portion in dollars dollars = (int) money; The value in money is not changed If a conversion is possible, it can be done through a cast 17

18 More Operators We've seen several operators of various types: arithmetic, equality, relational There are many more in Java to make use of: increment and decrement operators logical operators assignement operators the conditional operator 18

19 The Increment and Decrement Operators The increment operator (++) adds one to its integer or floating point operand The decrement operator (--) subtracts one The statement count++; is essentially equivalent to count = count + 1; 19

20 The Increment and Decrement Operators The increment and decrement operators can be applied in prefix (before the variable) or postfix (after the variable) form When used alone in a statement, the prefix and postfix forms are basically equivalent. That is, count++; is equivalent to ++count; 20

21 The Increment and Decrement Operators When used in a larger expression, the prefix and postfix forms have a different effect In both cases the variable is incremented (decremented) But the value used in the larger expression depends on the form Expression count++ ++count count-- --count Operation add 1 add 1 subtract 1 subtract 1 Value of Expression old value new value old value new value 21

22 The Increment and Decrement Operators If count currently contains 45, then total = count++; assigns 45 to total and 46 to count If count currently contains 45, then total = ++count; assigns the value 46 to both total and count 22

23 The Increment and Decrement Operators If sum contains 25, then the statement System.out.println (sum++ + " " + ++sum + " " + sum + " " + sum--); prints the following result: and sum contains 26 after the line is complete 23

24 Logical Operators There are three logical operators in Java: Operator! && Operation Logical NOT Logical AND Logical OR They all take boolean operands and produce boolean results Logical NOT is unary (one operand), but logical AND and OR are binary (two operands) 24

25 Logical NOT The logical NOT is also called logical negation or logical complement If a is true,!a is ; if a is, then!a is true Logical expressions can be shown using truth tables a!a true true 25

26 Logical AND The expression a && b is true if both a and b are true, and otherwise Truth tables show all possible combinations of all terms a b a && b true true true true true 26

27 Logical OR The expression a b is true if a or b or both are true, and otherwise a b a b true true true true true true true 27

28 Logical Operators Conditions in selection statements and loops can use logical operators to form more complex expressions if (total < MAX &&!found) System.out.println ("Processing..."); Logical operators have precedence relationships between themselves and other operators 28

29 Logical Operators Full expressions can be evaluated using truth tables total < MAX found!found total < MAX &&!found true true true true true true true 29

30 Assignment Operators Often we perform an operation on a variable, then store the result back into that variable Java provides assignment operators that simplify that process For example, the statement num += count; is equivalent to num = num + count; 30

31 Assignment Operators There are many such assignment operators, always written as op=, such as: Operator Example Equivalent To += -= *= /= %= x += y x -= y x *= y x /= y x %= y x = x + y x = x - y x = x * y x = x / y x = x % y 31

32 Assignment Operators The right hand side of an assignment operator can be a complete expression The entire right-hand expression is evaluated first, then combined with the additional operation Therefore result /= (total-min) % num; is equivalent to result = result / ((total-min) % num); 32

33 The Conditional Operator Java has a conditional operator that evaluates a boolean condition that determines which of two expressions is evaluated The result of the chosen expression is the result of the entire conditional operator Its syntax is: condition? expression1 : expression2 If the condition is true, expression1 is evaluated; if it is, expression2 is evaluated 33

34 The Conditional Operator It is similar to an if-else statement, except that it is an expression that returns a value For example: larger = (num1 > num2)? num1 : num2; If num1 is greater that num2, then num1 is assigned to larger; otherwise, num2 is assigned to larger The conditional operator is ternary, meaning it requires three operands 34

35 The Conditional Operator Another example: System.out.println ("Your change is " + count + (count == 1)? "Dime" : "Dimes"); If count equals 1, "Dime" is printed, otherwise "Dimes" is printed 35

36 Another Selection Statement The if and the if-else statements are selection statements, allowing us to select which statement to perform next based on some boolean condition Another selection construct, called the switch statement, provides another way to choose the next action The switch statement evaluates an expression, then attempts to match the result to one of a series of values Execution transfers to statement list associated with the first value that matches 36

37 The switch Statement The syntax of the switch statement is: switch (expression) { case value1: statement-list1 case value2: statement-list2 case } 37

38 The switch Statement The expression must evaluate to an integral value, such as an integer or character The break statement is usually used to terminate the statement list of each case, which causes control to jump to the end of the switch statement and continue A default case can be added to the end of the list of cases, and will execute if no other case matches See Vowels.java 38

39 More Repetition Constructs In addition to while loops, Java has two other constructs used to perform repetition: the do statement the for statement Each loop type has its own unique characteristics You must choose which loop type to use in each situation 39

40 The do Statement The do statement has the following syntax: do statement while (condition); The statement is executed until the condition becomes It is similar to a while statement, except that its termination condition is evaluated after the loop body 40

41 The do Statement statement true condition 41

42 The do Statement See Dice.java The key difference between a do loop and a while loop is that the body of the do loop will execute at least once If the condition of a while loop is initially, the body of the loop is never executed Another way to put this is that a while loop will execute zero or more times and a do loop will execute one or more times 42

43 The for Statement The syntax of the for loop is for (intialization; condition; increment) statement; which is equivalent to initialization; while (condition) { statement; increment; } 43

44 The for Statement Like a while loop, the condition of a for statement is tested prior to executing the loop body Therefore, a for loop will execute zero or more times It is well suited for executing a specific number of times, known in advance Note that the initialization portion is only performed once, but the increment portion is executed after each iteration 44

45 The for Statement initialization condition true statement increment 45

46 The for Statement Examples: for (int count=1; count < 75; count++) System.out.println (count); for (int num=5; num <= total; num *= 2) { sum += num; System.out.println (sum); } See Dice2.java 46

47 The for Statement Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore makes an infinite loop If the increment is left out, no increment opertion is performed Both semi-colons are always required 47

48 The break and continue statements The break statement, which we used with switch statements, can also be used inside a loop When the break statement is executed, control jumps to the statement after the loop (the condition is not evaluated again) A similar construct, the continue statement, can also be executed in a loop When the continue statement is executed, control jumps to the end of the loop and the condition is evaluated 48

49 The break and continue Statements They can calso be used to jump to a line in your program with a particular label Jumping from one point in the program to another in an unstructured manner is not good practice Therefore, as a rule of thumb, avoid the break statement except when needed in switch statements, and avoid the continue statement altogether 49

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

### Data Conversion & Scanner Class

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

### Module 2: Computer Arithmetic

Module 2: Computer Arithmetic 1 B O O K : C O M P U T E R O R G A N I Z A T I O N A N D D E S I G N, 3 E D, D A V I D L. P A T T E R S O N A N D J O H N L. H A N N E S S Y, M O R G A N K A U F M A N N

### Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

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

Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition Program Development The creation of software involves four basic activities: establishing

### Chapter 3: Program Statements

Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by

### Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

### Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

### 4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

### Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Operators Representing and storing primitive data types is, of course, essential for any computer language. But, so, too, is the ability to perform operations on data. Java supports a comprehensive set

### Chapter 2: Data and Expressions

Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

### Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

### Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

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

3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

### Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

1 CS257 Computer Science I Kevin Sahr, PhD Lecture 6: Comparing Data and Complex Boolean Expressions Comparing Data 2 When comparing data it's important to understand the nuances of certain data types

### Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 03: Computer Arithmetic Lesson 09: Arithmetic using floating point numbers Objective To understand arithmetic operations in case of floating point numbers 2 Multiplication of Floating Point Numbers

### Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Chapter 7 Expressions and Assignment Statements (updated edition 11) ISBN 0-321-49362-1 Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean

### CT 229 Java Syntax Continued

CT 229 Java Syntax Continued 29/09/2006 CT229 Lab Assignments One Week Extension for Lab Assignment 1. Due Date: Oct 8 th Before submission make sure that the name of each.java file matches the name given

### Chapter 3. Selections

Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

### Chapter 2: Data and Expressions

Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

### Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

### Chapter 2. Elementary Programming

Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

Sir Muhammad Naveed Arslan Ahmed Shaad (1163135 ) Muhammad Bilal ( 1163122 ) www.techo786.wordpress.com CHAPTER: 2 NOTES:- VARIABLES AND OPERATORS The given Questions can also be attempted as Long Questions.

### Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

### CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 Lecture #16: Java conditionals/loops, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Midterms returned now Weird distribution Mean: 35.4 ± 8.4 What

### In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability.

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

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduce the Java programming

### CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

### Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

### JAVA Programming Fundamentals

Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

### Unit-2 (Operators) ANAND KR.SRIVASTAVA

Unit-2 (Operators) ANAND KR.SRIVASTAVA 1 Operators in C ( use of operators in C ) Operators are the symbol, to perform some operation ( calculation, manipulation). Set of Operations are used in completion

### Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

1 Floating Point The World is Not Just Integers Programming languages support numbers with fraction Called floating-point numbers Examples: 3.14159265 (π) 2.71828 (e) 0.000000001 or 1.0 10 9 (seconds in

### Programming in C++ 6. Floating point data types

Programming in C++ 6. Floating point data types! Introduction! Type double! Type float! Changing types! Type promotion & conversion! Casts! Initialization! Assignment operators! Summary 1 Introduction

### 4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

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

CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

### Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Appendix F Number Systems Binary Numbers Decimal notation represents numbers as powers of 10, for example 1729 1 103 7 102 2 101 9 100 decimal = + + + There is no particular reason for the choice of 10,

### Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

### Operators And Expressions

Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication

### Fundamentals of Programming

Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

### Introduction to Computer Science. Homework 1

Introduction to Computer Science Homework. In each circuit below, the rectangles represent the same type of gate. Based on the input and output information given, identify whether the gate involved is

### Arithmetic Operators. Portability: Printing Numbers

Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - +

### Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators, Expressions, Statements and Arrays. Java technology is: A programming

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

### Operators & Expressions

Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

### Chapter 7. Expressions and Assignment Statements

Chapter 7 Expressions and Assignment Statements Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment

### CMPT 125: Lecture 4 Conditionals and Loops

CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed

### The type of all data used in a C (or C++) program must be specified

The type of all data used in a C (or C++) program must be specified A data type is a description of the data being represented That is, a set of possible values and a set of operations on those values

### Inf2C - Computer Systems Lecture 2 Data Representation

Inf2C - Computer Systems Lecture 2 Data Representation Boris Grot School of Informatics University of Edinburgh Last lecture Moore s law Types of computer systems Computer components Computer system stack

### Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

JAVA BASIC OPERATORS http://www.tuto rialspo int.co m/java/java_basic_o perato rs.htm Copyrig ht tutorialspoint.com Java provides a rich set of operators to manipulate variables. We can divide all the

### Number Systems and Computer Arithmetic

Number Systems and Computer Arithmetic Counting to four billion two fingers at a time What do all those bits mean now? bits (011011011100010...01) instruction R-format I-format... integer data number text

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

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

### IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

IT 1204 Section 2.0 Data Representation and Arithmetic 2009, University of Colombo School of Computing 1 What is Analog and Digital The interpretation of an analog signal would correspond to a signal whose

### Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Lecture 2: Variables and Operators AITI Nigeria Summer 2012 University of Lagos. Agenda Variables Types Naming Assignment Data Types Type casting Operators Declaring Variables in Java type name; Variables

### 1.00 Lecture 4. Promotion

1.00 Lecture 4 Data Types, Operators Reading for next time: Big Java: sections 6.1-6.4 Promotion increasing capacity Data Type Allowed Promotions double None float double long float,double int long,float,double

### Floating Point Arithmetic

Floating Point Arithmetic CS 365 Floating-Point What can be represented in N bits? Unsigned 0 to 2 N 2s Complement -2 N-1 to 2 N-1-1 But, what about? very large numbers? 9,349,398,989,787,762,244,859,087,678

### CSC201, SECTION 002, Fall 2000: Homework Assignment #2

1 of 7 11/8/2003 7:34 PM CSC201, SECTION 002, Fall 2000: Homework Assignment #2 DUE DATE Monday, October 2, at the start of class. INSTRUCTIONS FOR PREPARATION Neat, in order, answers easy to find. Staple

### Java Primer 1: Types, Classes and Operators

Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

### Program Fundamentals

Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

### COMP6700/2140 Operators, Expressions, Statements

COMP6700/2140 Operators, Expressions, Statements Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU 3 March 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Operators,

### Lecture 1. Types, Expressions, & Variables

Lecture 1 Types, Expressions, & Variables About Your Instructor Director: GDIAC Game Design Initiative at Cornell Teach game design (and CS 1110 in fall) 8/29/13 Overview, Types & Expressions 2 Helping

### Software II: Principles of Programming Languages. Why Expressions?

Software II: Principles of Programming Languages Lecture 7 Expressions and Assignment Statements Why Expressions? Expressions are the fundamental means of specifying computations in a programming language

### COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

### MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

### On a 64-bit CPU. Size/Range vary by CPU model and Word size.

On a 64-bit CPU. Size/Range vary by CPU model and Word size. unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed There are (usually) no unsigned floats or doubles.

### CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started Handout written by Julie Zelenski, Mehran Sahami, Robert Plummer, and Jerry Cain. After today s lecture, you should run home and read

### ANSI C Programming Simple Programs

ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

### CS101 Lecture 04: Binary Arithmetic

CS101 Lecture 04: Binary Arithmetic Binary Number Addition Two s complement encoding Briefly: real number representation Aaron Stevens (azs@bu.edu) 25 January 2013 What You ll Learn Today Counting in binary

### ESc101 : Fundamental of Computing

ESc101 : Fundamental of Computing I Semester 2008-09 Lecture 9+10 Types Range of numeric types and literals (constants) in JAVA Expressions with values of mixed types 1 I : Range of numeric types and literals

### Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Electrical and Computer Engineering Object-Oriented Topic 2: Fundamental Structures in Java Maj Joel Young Joel.Young@afit.edu 8-Sep-03 Maj Joel Young Java Identifiers Identifiers Used to name local variables

### Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while

### Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

Chapter 3: Arithmetic Properties CHAPTER 3: ARITHMETIC PROPERTIES Date: Lesson: Learning Log Title: Date: Lesson: Learning Log Title: Chapter 3: Arithmetic Properties Date: Lesson: Learning Log Title:

### C Programming

204216 -- C Programming Chapter 3 Processing and Interactive Input Adapted/Assembled for 204216 by Areerat Trongratsameethong A First Book of ANSI C, Fourth Edition Objectives Assignment Mathematical Library

### Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

### C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

C expressions (Reek, Ch. 5) 1 Shift operations Left shift: value > n Two definitions: logical version: discard the n

### Chapter 3 Structured Program Development in C Part II

Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 3.7 The while Iteration Statement An iteration statement (also called

### Type Conversion. and. Statements

and Statements Type conversion changing a value from one type to another Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 2 tj Suppose

### The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

The CPU and Memory How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram: 1 Registers A register is a permanent storage location within

### 10.1. Unit 10. Signed Representation Systems Binary Arithmetic

0. Unit 0 Signed Representation Systems Binary Arithmetic 0.2 BINARY REPRESENTATION SYSTEMS REVIEW 0.3 Interpreting Binary Strings Given a string of s and 0 s, you need to know the representation system

### Chapter 10 - Computer Arithmetic

Chapter 10 - Computer Arithmetic Luis Tarrataca luis.tarrataca@gmail.com CEFET-RJ L. Tarrataca Chapter 10 - Computer Arithmetic 1 / 126 1 Motivation 2 Arithmetic and Logic Unit 3 Integer representation

### Quick Reference Guide

SOFTWARE AND HARDWARE SOLUTIONS FOR THE EMBEDDED WORLD mikroelektronika Development tools - Books - Compilers Quick Reference Quick Reference Guide with EXAMPLES for Pascal language This reference guide

### 2: Basics of Java Programming

2: Basics of Java Programming CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

### Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Starting with a great calculator... Topic 5: Introduction to Programming in Matlab CSSE, UWA! MATLAB is a high level language that allows you to perform calculations on numbers, or arrays of numbers, in

### 1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM 1.1 Introduction Given that digital logic and memory devices are based on two electrical states (on and off), it is natural to use a number

### Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

### Topics. Chapter 5. Equality Operators

Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator

### Chapter 4: Data Representations

Chapter 4: Data Representations Integer Representations o unsigned o sign-magnitude o one's complement o two's complement o bias o comparison o sign extension o overflow Character Representations Floating

### LAB A Translating Data to Binary

LAB A Translating Data to Binary Create a directory for this lab and perform in it the following groups of tasks: LabA1.java 1. Write the Java app LabA1 that takes an int via a command-line argument args[0]

### Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Course Schedule CS 221 Computer Architecture Week 3: Information Representation (2) Fall 2001 W1 Sep 11- Sep 14 Introduction W2 Sep 18- Sep 21 Information Representation (1) (Chapter 3) W3 Sep 25- Sep

### Logic, Words, and Integers

Computer Science 52 Logic, Words, and Integers 1 Words and Data The basic unit of information in a computer is the bit; it is simply a quantity that takes one of two values, 0 or 1. A sequence of k bits

### Floating Point Arithmetic

Floating Point Arithmetic Floating point numbers are frequently used in many applications. Implementation of arithmetic units such as adder, multiplier, etc for Floating point numbers are more complex

### Java I/O and Control Structures

Java I/O and Control Structures CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Some slides in this presentation are adapted from the slides accompanying

### CREATING FLOATING POINT VALUES IN MIL-STD-1750A 32 AND 48 BIT FORMATS: ISSUES AND ALGORITHMS

CREATING FLOATING POINT VALUES IN MIL-STD-1750A 32 AND 48 BIT FORMATS: ISSUES AND ALGORITHMS Jeffrey B. Mitchell L3 Communications, Telemetry & Instrumentation Division Storm Control Systems ABSTRACT Experimentation

### ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand and I will come to you. Please work the exam in pencil and do not separate

### Java I/O and Control Structures Algorithms in everyday life

Introduction Java I/O and Control Structures Algorithms in everyday life CSC 2014 Java Bootcamp Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Source: http://xkcd.com/627/

### Chapter 5. Algorithms. Introduction. Chapter 5 Algorithms. Search algorithms. Linear search. Worked example

Chapter 5 Introduction Algorithms Algorithms are sets of instructions that can be followed to perform a task. They are at the very heart of what computer science is about. When we want a computer to carry

### Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Floating-point Arithmetic Reading: pp. 312-328 Floating-Point Representation Non-scientific floating point numbers: A non-integer can be represented as: 2 4 2 3 2 2 2 1 2 0.2-1 2-2 2-3 2-4 where you sum

### ARG! Language Reference Manual

ARG! Language Reference Manual Ryan Eagan, Mike Goldin, River Keefer, Shivangi Saxena 1. Introduction ARG is a language to be used to make programming a less frustrating experience. It is similar to C