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


 Augustus Bruno Gordon
 1 years ago
 Views:
Transcription
1 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003
2 Java Programs A Java program contains at least one class definition. public class Hello { public static void main(string[] args) { System.out.println("Hello, world!") ; } } This code defines a class named Hello. The definition of Hello must be in a file Hello.java. The method main is the code that runs when you call java Hello.
3 Identifiers When we give an element in a program a name, we call that name an identifier. In the previous example, Hello was an identifier. In Java, identifiers: Always start with a letter. Can include letters, digits, underscore ( ) and the dollar sign symbol ($). Must be different from any Java reserved words (or keywords). Keywords that we ve seen so far include: public, static, class and void.
4 Case Sensitivity Identifiers and keywords in Java are case sensitive. In other words, capitalization matters. Keywords are always in lowercase. The following identifers are all different: foobar Foobar FooBar FOOBAR Even so, it would probably be a bad idea to use more than one of them in the same program.
5 Whitespace We use the word whitespace to describe blanks, tabs and newline characters. The Java compiler ignores whitespace except when it is used to separate words. E.g.: y=m*x+b;total=total+y; Is the same as: y = m*x + b ; total = total + y ; But which is easier to read?
6 Comments To make our code understandable to those who come after us, we comment sections whose purpose is not immediately obvious. // this comment ends at a newline /* this comment goes until it is explicitly ended with a: */ The Java compiler treats comments as if they were whitespace it ignores them unless they separate words. I.e., foo/*hi, mom!*/bar becomes foo bar, not foobar.
7 Variables A variable is a location in memory where data is stored. Every variable is associated with an identifier. We can assign a value to a variable using the assignment operator =. x = 12 ; The assignment operator means take the value on the righthand side and store it in the memory location on the lefthand side.
8 Types The values a variable can take on and the operations that can be performed on it are determined by its type. Java has five categories of types: Booleans Characters Integers Floatingpoint numbers References to objects
9 Booleans Boolean variables can only take on the values true or false. They are often used to test for conditions in a program. boolean t = true ; boolean f = false ;
10 Characters Character variables can store one character. A character value is a character surrounded by single quotes: char q = Q ; Some special characters are: \n newline \t tab \ single quote \" double quote \\ backslash
11 Integers The integers are the infinite set: Z = {..., 1, 0, 1,... }. Obviously, we can t represent all of the integers in one variable. We have to choose an nbit encoding that can represent 2 n integers. The Java integer types represent both positive and negative integers. An nbit integer x can represent the range: 2 n 1 x < 2 n 1
12 Integers Types The Java integer types are: byte short int long 8 bits 16 bits 32 bits 64 bits E.g.: byte b = 127 ; short s = ; int i = 2 ;
13 Integer Literals An integer value, or literal, can be written in decimal, hex or octal (base8): A hex literal starts with 0x, e.g.: 0x1F (= ) An octal literal starts with just 0, e.g.: 072 (= ) A decimal literal is just a regular number that doesn t start with 0, e.g.: 123 Integer literals are by default of type int. A long literal ends with L.
14 Integer Conversions If an int literal is small enough to fit into a byte or a short, it will be automatically converted. The same is true for long literals and int, byte and short. byte b = 0x7F ; /* 7 bits, OK */ short s = 0x7FFF ; /* 15 bits, OK */ int i = 0x L /* 29 bits, OK */ byte b2 = 0xFF ; /* Error: 255 > 127 */ int i2 = 0x ABCDEFL /* Error: way too big, 57 bits */
15 Integer Conversions: 2 If a literal is too big for its target variable, you must explicitly convert it using a type cast. The number is converted by truncating the extra bits, which is probably not what you want. /* 0x100 = 256 */ byte b = (byte) 0x100 ; /* b now equals 0! */ An int literal can always be assigned to a long variable its value will be the same as if it was assigned to an int variable.
16 FloatingPoint Numbers Floatingpoint numbers are used to represent the reals (R), i.e., numbers that may have fractional parts. The floatingpoint representation uses a form of scientific notation: = = = =
17 FloatingPoint Types A floatingpoint variable uses some of its bits to store the exponent and some of them to store the fractional part (or significand). Thus, floatingpoint numbers are constrained by both magnitude and precision. The Java floatingpoint types are: float double 32 bits 64 bits
18 FloatingPoint Literals Floatingpoint literals are decimal numbers with an optional decimal point You may also include an exponent n, which multiplies the literal by 10 n : 1.234e2 9.9e1 55e0.02e1 A floatingpoint literal is by default of type double. A float literal ends with F.
19 FloatingPoint Conversions The only automatic conversion between floatingpoint types is the assignment of a float value to a double. double d = 1.23F ; /* OK */ float f = 5.99 ; /* Error: cannot assign double to float */ When an integer literal is assigned to a floatingpoint type, it is automatically promoted to floatingpoint, even if that means a loss of precision. float f = 2 ; /* OK, f = 2.0 */ float f2 = L ; /* OK, but f2 = */
20 Objects You may have heard that Java is an objectoriented programming language. What does that mean? An object is a collection of data and operations that manipulate that data. public class Circle { int x ; int y ; double radius ;... }
21 ObjectOriented Programming In objectoriented programming (or OOP), we try to define all of our data as objects, and we define the program as interaction between those objects. public class HappyFace { Circle head ; SemiCircle smile ; } void draw() { head.draw() ; smile.draw() ; }...
22 ObjectOriented Programming: 2 OOP encourages us to think of objects as modules reusable components. If we have Circle, we can use it to make HappyFace. If we have HappyFace, we can use it to make StickMan. public class StickMan { HappyFace head ; Line body ; Line arm1, arm2 ; Line leg1, leg2 ;... }
23 Defining Object Types An object is usually a noun, a thing. To define an object, we first need to define what kind of a thing it is. In Java, we use the keyword class: public class Car { String make ; int model_year ; Color color ; int max_occupants ;... }
24 Defining Object Types: 2 Now that we ve defined a class, we can create instances of the class (i.e., objects). In order to create an instance, we need to tell Java how to initialize the object. We do this using a constructor. public class Car {... public Car(String mk, int yr, Color c, int max) { make = mk ; model_year = yr ; color = c ; max_occupants = max ; } }
25 Defining Objects Now we can use the keyword new to invoke the constructor and create an instance of the class. We can create any kind of a car we need by changing the parameters to the constructor. Car suv = new Car("Ford Explorer", 1999, Color.BLUE, 5) ; Car mini = new Car("Cooper Mini", 2002, Color.RED, 4) ;
26 The Type of an Object An object s type is its class. A Car variable can only take the value of a Car object, or an object that is compatible with Car. (We ll talk about what it means for an object to be compatible next week.) Car suv = new Car("Ford Explorer", 1999, Color.BLUE, 5) ; Car c = suv ; /* OK: suv is a Car */ StickMan s = suv ; /* Error: suv is not a StickMan */
27 References We said earlier that one of the categories of Java types is references to objects. What is a reference? We said that a variable is a location in memory. When we declare an integer variable x and assign it a value 123, the location in memory set aside for x contains the binary representation of 123. x n + 1 n n 1 true e23
28 References: 2 A variable of an object type is a reference variable. When we declare a Car variable suv, the location in memory set aside for suv contains a reference to the location of the data members of suv. suv n + 1 n n e23 ref X "Ford Explorer" 1999 Color.BLUE 5
29 Reference Assignment Assignment means copy the value on the righthand side into the memory location on the lefthand side. If you assign one variable to another, the value is copied and that is that. int x = 0 ; int y = x ; /* x and y both equal 0 */ x = 2 ; /* x now equals 2, y still equals 0 */
30 Reference Assignment: 2 When you assign the value of one reference variable to another, it s the reference that s copied, not the object. This can lead to surprising results. Point p = new Point(0,0) ; Point q = p ; /* p and q now refer to the same object */ p.x = 2 ; /* p.x now equals 2 q.x now equals 2, as well */
31 Reference Assignment: 2 When you copy the reference value, you still have only one copy of the object data. You just have two references to it. p q 0xFFFFFFFF ref ref false x: 2 y: 0 This may seem confusing at first, but there s a simple reason for it: copying data is slow. We avoid it when we can.
32 Strings String is a class that has special support in Java. A string literal is surrounded by double quotes. String hamlet = "To be or not to be" ; String is a reference type, but you don t have to use the new operator to create an instance. You can assign a string literal to a String variable directly, as above.
33 Strings: 2 Character and string values may seem confusingly similar. A char value is a single character, surrounded by single quotes: char c1 = C ; char c2 = \n ; A String value is a sequence of characters surrounded by double quotes. A String may be empty. String s1 = "C" ; String s2 = "" ; String s3 = "LastName\tFirstName\tGrade\n" ; /* Words separated by tabs, closed with a newline. */
34 Arithmetic Java provides five basic arithmetic operators: + addition  subtraction * multiplication / division % remainder There are also unary + and  operators. The operators can be applied to any of the integer or floatingpoint types.
35 Operator Precedence Multiplication, division and remainder have higher precedence than addition and subtraction. All higherprecedence operators are evaluated before any lowerprecedence operators. Operators at the same precedence are evaluated lefttoright. Parentheses can be used to override operator precedence. x+y*z = x+(y*z) a*b+c%d = (a*b)+(c%d) Assignment is the lowestprecedence operator of all. Unary + and  are higherprecedence than multiplication.
36 Integer Arithmetic Integer division is gradeschool division: fractional results round toward zero. 9/2 = 49/2 = 4 Integer division and remainder obey the rule: (x/y)*y + x%y = x 9%2 = 19%2 = 1
37 Overflow and Underflow If the result of an operation is greater than the maximum value (overflow) or less than the minimum value (underflow) of its type, the result wraps around. Integer.MAX VALUE + 1 = Integer.MIN VALUE Integer.MIN VALUE  1 = Integer.MAX VALUE All arithmetic is performed with int and long values. byte and short values are automatically promoted.
38 Typecasting The result of an integer arithmetic operation is always an int or long. This means you have to cast back to a smaller type when you assign a result. byte b = 1 ; byte b2 = b + 1 ; /* Error: can t assign int to byte */ byte b3 = (byte)( b + 1 ) ; /* OK: b3 now equals 2 */ byte b4 = (byte)( Byte.MAX_VALUE + 1 ) ; /* OK: b4 now equals Byte.MIN_VALUE */
39 Floatingpoint Arithmetic Floatingpoint arithmetic works pretty much how you would expect, except you have to be careful about precision. float f = 1.0e8F, g = 1.0e8F ; float x = f + g ; /* x now equals 1.0e8 */ float y = (f  f) + g ; /* y now equals 1.0e8 */ float z = f  (f + g) ; /* z now equals 0 */
40 Special Floatingpoint Values Floatingpoint arithmetic has several special values that behave in interesting ways. +Inf Inf Infinity +0.0 Zero Negative Infinity 0.0 Negative Zero NaN Not a Number
41 Special Floatingpoint Values: 2 Here s how to get and what you get from, these special values: 1/0.0 = Inf 1/0.0 = Inf 1/Inf = 0.0 1/Inf = 0.0 Inf/Inf = NaN 1+Inf = Inf 1%Inf = 1 Inf*0.0 = NaN 1Inf = Inf Inf%1 = NaN
42 Increment and Decrement Adding or subtracting one from a number is a common operation. Java provides a shorthand in the increment (++) and decrement () operators. Increment and decrement can be used as prefix or postfix operators. In prefix form, increment means add one to this variable and use the incremented value in this expression. In postfix form, increment means add one to this variable, and use the previous value in this expression. Aside: C also has increment and decrement. The developers of C++ wanted to indicate it was an incremental improvement on C.
43 Increment and Decrement: Examples int i = 0, j ; i++ ; /* i = 1 */ i ; /* i = 0 */ j = i++ ; /* i=1, j=0 */ j = ++i ; /* i=2, j=2 */ j = i ; /* i=1, j=1 */ j = i ; /* i=0, j=1 */
44 Increment and Decrement: Precedence Increment and decrement are higherprecedence than any operator we ve seen so far. Postfix is higherprecedence than prefix. Highest Lowest = ++,  (postfix) ++,  (prefix), +,  (unary) *, /, % +, 
45 Comparison Operators Java provides the following operators for comparing numbers: > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to!= Not equal to == and!= can be applied to boolean values, but >, <, >= and <= cannot. The result of a comparison operation is a boolean value.
46 Reference Comparison == and!= can be applied to reference variables. They test whether the references are equal, not the data members. Point x = new Point(0,0) ; Point y = new Point(0,0) ; Point z = x ; boolean b = x==y ; /* false, x and y are references to different objects */ boolean b2 = x==z ; /* true, x and z are references to the same object */
47 The instanceof Operator Occasionally we want to investigate the type of an object. The instanceof operator tells us if a reference variable is an instance of a class. Point x = new Point(0,0) ; boolean a = x instanceof Point ; /* true, x is a Point */ boolean b = x instanceof Car ; /* false, x is not a Car */
48 Comparison Operators: Precedence The relational operators have higher precedence than the equality operators. All of the comparison operators have lower precedence than the arithmetic operators. Highest Lowest = ++,  (postfix) ++,  (prefix), +,  (unary) *, /, % +,  <, >, <=, >=, instanceof ==,!=
49 Logical Operators Java provides the following logical operators: & AND OR ˆ XOR! NOT && shortcircuit AND shortcircuit OR The operands of a logical operator are boolean values and the result is also a boolean.
50 AND AND is true if and only if both of its operands are true. a b a&b false false false false true false true false false true true true
51 OR OR is true if one of its operands are true. a b a b false false false false true true true false true true true true
52 XOR XOR is true if and only if exactly one of its operands is true. a b aˆb false false false false true true true false true true true false
53 NOT NOT inverts its operand. a!a false true true false
54 Shortcircuit AND and OR The logical AND and OR operators ( & and ) always evaluate both of their operands. In some cases, this can be wasteful. If the first operand of AND is false, then AND must be false. Shortcircuit AND ( && ) doesn t evaluate the second operand if the first is false. If the first operand of OR is true, then OR must be true. Shortcircuit OR ( ) doesn t evaluate the second operand if the first is true.
55 Shortcircuit Operators: Example boolean a = true, b = false ; int i = 0, j = 1, k = 2 ; boolean c = a (i=j) < k ; /* c = true, i = 0 */ boolean d = a && (i=j) < k ; /* d = true, i = 1 */ boolean e = a && (i=k) < j ; /* e = false, i = 2 */
56 Logical Operators: Precedence AND has higher precedence than OR. The shortcircuit operators have lower precedence than their ordinary equivalents. All of the logical operators have lower precedence than arithmetic and comparison. Highest Arithmetic, etc. <, >, <=, >=, instanceof ==,!= & ˆ && Lowest =
57 Bitwise Operators Java provides the following operators for manipulating bit patterns: & Bitwise AND Bitwise OR ˆ Bitwise XOR << Shift left >> Shift right signed >>> Shift right unsigned &, and ˆ are overloaded operators. They are logical operators when applied to boolean values, and bitwise operators when applied to integers.
58 Bitwise AND Bitwise AND applies the AND operation to every pair of bits in the operands. A 0 is treated as false and a 1 is treated as true xF6 & 0x93 = AND =
59 Bitwise OR Bitwise OR applies the OR operation to every pair of bits in the operands. 0xF6 0x93 = OR =
60 Bitwise XOR Bitwise XOR applies the XOR operation to every pair of bits in the operands. 0xF6 ˆ 0x93 = XOR =
61 Shift left The shift left operator shifts the bits in its first operand as indicated by its second operand. 0xF6 << 2 = <<
62 Shift right signed The way integers are represented in Java, the highestorder bit is the sign of the number. If we shift right the same way we shift left (bringing in zeroes), the sign might changed. The shift right signed operator brings in bits that match the sign. 0xF6 >> 2 = >>
63 Shift right unsigned The shift right unsigned operator ignores the sign bit and brings in zeroes. 0xF6 >>> 2 = >>>
64 Using Bitwise Operators Bitwise AND is good for turning bits off: int i = 0xC7 & 0xFE ; /* i = 0xC6 */ Bitwise OR is good for turning bits on: int i = 0xC7 0x08 ; /* i = 0xCF */ Bitwise XOR is good for flipping bits: int i = 0xC7 0x09 ; /* i = 0xCE */
65 Bitwise Operators: Precedence The precedence of bitwise AND, OR and XOR match their logical equivalents. The precendence of the shift operators is between the arithmetic and comparison operators. Highest Lowest Unary operators *, /, % +,  <<, >>, >>> <, >, <=, >=, instanceof ==,!= Logical operators, assignment
66 String Concatenation The + is also overloaded. When applied to two strings, it concatenates them. I.e., it appends the second to the first. String a = "Flower" ; String b = "Power" ; String c = a + b ; /* c = "FlowerPower" */
67 String Concatenation: 2 All of the basic types are automatically converted to String under the concatenation operator. int i = 2 ; double x = 5.2 ; String s = "i = " + i + " and x = " + x ; /* s = "i = 2 and x = 5.2" */
68 Assignment Operators All of the binary operators have corresponding compound assignment operators. += %= &= = >>= ˆ= *= <<= = /= >>>= x op= y is equivalent to x = x op y. All of the compound assignment operators have the same precedence as assignment.
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
More informationJAVA 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
More informationProgram 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
More informationMore Programming Constructs  Introduction
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
More informationInf2C  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
More informationChapter 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
More informationFundamentals 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
More informationVariables and literals
Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of
More informationSir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )
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.
More informationOperators in java Operator operands.
Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,
More informationOperators. Java Primer Operators1 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
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationCSC 1214: ObjectOriented Programming
CSC 1214: ObjectOriented Programming J. Kizito Makerere University email: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 elearning environment:
More informationValues and Variables 1 / 30
Values and Variables 1 / 30 Values 2 / 30 Computing Computing is any purposeful activity that marries the representation of some dynamic domain with the representation of some dynamic machine that provides
More information2 nd Week Lecture Notes
2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous
More informationNumber Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:
N Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as: a n a a a The value of this number is given by: = a n Ka a a a a a
More informationARG! 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
More informationMODULE 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
More informationFloatingPoint Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3
FloatingPoint Data Representation and Manipulation 198:231 Introduction to Computer Organization Instructor: Nicole Hynes nicole.hynes@rutgers.edu 1 Fixed Point Numbers Fixed point number: integer part
More informationChapter 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
More informationC OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure
C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces
More informationCOMP6700/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,
More informationCSc 10200! Introduction to Computing. Lecture 23 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 23 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
More informationNumber 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,
More informationOperators 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
More informationWhat did we talk about last time? Examples switch statements
Week 4  Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation
More information3. 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:
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
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
More information2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 21 Parts of a C++ Program Parts of a C++ Program // sample C++ program
More informationArithmetic 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:  +
More informationLecture 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
More informationChapter 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
More informationCOMP202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods
COMP202 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,
More informationArithmetic Expressions in C
Arithmetic Expressions in C Arithmetic Expressions consist of numeric literals, arithmetic operators, and numeric variables. They simplify to a single value, when evaluated. Here is an example of an arithmetic
More information1.00 Lecture 4. Promotion
1.00 Lecture 4 Data Types, Operators Reading for next time: Big Java: sections 6.16.4 Promotion increasing capacity Data Type Allowed Promotions double None float double long float,double int long,float,double
More informationLAB 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 commandline argument args[0]
More informationJava 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
More informationfalse, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4
1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new ZhengLiang Lu Java Programming 44
More informationElementary Programming
Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHENWEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal
More informationLecture 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
More informationModule 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
More information4 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.
More informationJava Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.
Java Language Essentials Java is Case Sensitive All Keywords are lower case White space characters are ignored Spaces, tabs, new lines Java statements must end with a semicolon ; Compound statements use
More informationOn a 64bit CPU. Size/Range vary by CPU model and Word size.
On a 64bit 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.
More informationCSCI 2212: Intermediate Programming / C Chapter 15
... /34 CSCI 222: Intermediate Programming / C Chapter 5 Alice E. Fischer October 9 and 2, 25 ... 2/34 Outline Integer Representations Binary Integers Integer Types Bit Operations Applying Bit Operations
More informationFloating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !
Floating point Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties Next time! The machine model Chris Riesbeck, Fall 2011 Checkpoint IEEE Floating point Floating
More informationLearning the Binary System
Learning the Binary System www.brainlubeonline.com/counting_on_binary/ Formated to L A TEX: /25/22 Abstract This is a document on the base2 abstract numerical system, or Binary system. This is a VERY
More informationA First Program  Greeting.cpp
C++ Basics A First Program  Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout
More informationModule 2 Introducing Data Types and Operators
Module 2 Introducing Data Types and Operators Table of Contents CRITICAL SKILL 2.1: The C++ Data Types... 2 Project 21 Talking to Mars... 10 CRITICAL SKILL 2.2: Literals... 12 CRITICAL SKILL 2.3: A Closer
More informationIntroduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language
Chapter 1 Getting Started Introduction To Java Most people are familiar with Java as a language for Internet applications We will study Java as a general purpose programming language The syntax of expressions
More informationSimple Java Program. public class HelloWorld {
Simple Java Program file: HelloWorld.java public class HelloWorld { } public static void main (String[] args) { //our instructions go here System.out.println("Hello, World!"); } Each word of this should
More informationData Types. Data Types. Integer Types. Signed Integers
Data Types Data Types Dr. TGI Fernando 1 2 The fundamental building blocks of any programming language. What is a data type? A data type is a set of values and a set of operations define on these values.
More informationChapter 3. Fundamental Data Types
Chapter 3. Fundamental Data Types ByoungTak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Variable Declaration
More informationSprite an animation manipulation language Language Reference Manual
Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3
More informationCS 101: Computer Programming and Utilization
CS 101: Computer Programming and Utilization JulNov 2017 Umesh Bellur (cs101@cse.iitb.ac.in) Lecture 3: Number Representa.ons Representing Numbers Digital Circuits can store and manipulate 0 s and 1 s.
More information15213 Recitation 2: Floating Point
15213 Recitation 2: Floating Point 1 Introduction This handout will introduce and test your knowledge of the floating point representation of real numbers, as defined by the IEEE standard. This information
More informationOperators. Lecture 3 COP 3014 Spring January 16, 2018
Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special builtin symbols that have functionality, and work on operands operand an input to an operator Arity  how many operands an operator
More informationDeclaration. 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
More informationCS429: Computer Organization and Architecture
CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: September 18, 2017 at 12:48 CS429 Slideset 4: 1 Topics of this Slideset
More informationClientSide Web Technologies. JavaScript Part I
ClientSide Web Technologies JavaScript Part I JavaScript First appeared in 1996 in Netscape Navigator Main purpose was to handle input validation that was currently being done serverside Now a powerful
More informationNumber 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 Rformat Iformat... integer data number text
More information1. 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
More informationCS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.
CS 265 Computer Architecture Wei Lu, Ph.D., P.Eng. 1 Part 1: Data Representation Our goal: revisit and reestablish fundamental of mathematics for the computer architecture course Overview: what are bits
More informationCourse Outline Introduction to CProgramming
ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to CProgramming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,
More informationLecture 2 Tao Wang 1
Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work
More informationFloating 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 floatingpoint numbers Examples: 3.14159265 (π) 2.71828 (e) 0.000000001 or 1.0 10 9 (seconds in
More informationChapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation
Chapter 2 Float Point Arithmetic Topics IEEE Floating Point Standard Fractional Binary Numbers Rounding Floating Point Operations Mathematical properties Real Numbers in Decimal Notation Representation
More informationInternal representation. Bitwise operators
Computer Programming Internal representation. Bitwise operators Marius Minea marius@cs.upt.ro 23 October 2017 Ideal math and C are not the same! In mathematics: integers Z and reals R have unbounded values
More informationChapter 4: Data Representations
Chapter 4: Data Representations Integer Representations o unsigned o signmagnitude o one's complement o two's complement o bias o comparison o sign extension o overflow Character Representations Floating
More informationC++ Programming Basics
C++ Programming Basics Chapter 2 and pp. 634640 Copyright 19982011 Delroy A. Brinkerhoff. All Rights Reserved. CS 1410 Chapter 2 Slide 1 of 25 Program Components Function main P Every C/C++ program has
More informationFile Handling in C. EECS 2031 Fall October 27, 2014
File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically
More informationJava 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,
More informationFloating Point Arithmetic
Floating Point Arithmetic CS 365 FloatingPoint What can be represented in N bits? Unsigned 0 to 2 N 2s Complement 2 N1 to 2 N11 But, what about? very large numbers? 9,349,398,989,787,762,244,859,087,678
More informationUnitII Programming and Problem Solving (BE1/4 CSE2)
UnitII Programming and Problem Solving (BE1/4 CSE2) 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
More informationChapter 2 Data Representations
Computer Engineering Chapter 2 Data Representations Hiroaki Kobayashi 4/21/2008 4/21/2008 1 Agenda in Chapter 2 Translation between binary numbers and decimal numbers Data Representations for Integers
More informationIntroduction to C# Applications
1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decisionmaking
More informationThe 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
More informationEngineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input
More informationIntroduction 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
More informationCourse 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
More informationSystems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties
Systems I Floating Point Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for
More informationComputer Organization & Systems Exam I Example Questions
Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C
More informationOperators & 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.
More informationCOMP202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015
COMP202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon
More informationICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2
ICS 2008 Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2 Data Representations Sizes of C Objects (in Bytes) C Data Type Compaq Alpha Typical 32bit Intel IA32 int 4 4 4 long int 8 4 4
More informationData Representations & Arithmetic Operations
Data Representations & Arithmetic Operations Hiroaki Kobayashi 7/13/2011 7/13/2011 Computer Science 1 Agenda Translation between binary numbers and decimal numbers Data Representations for Integers Negative
More informationLecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University
Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling
More informationBut first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15
Integer Representation Representation of integers: unsigned and signed Sign extension Arithmetic and shifting Casting But first, encode deck of cards. cards in suits How do we encode suits, face cards?
More information10.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
More informationl l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers
198:211 Computer Architecture Topics: Lecture 8 (W5) Fall 2012 Data representation 2.1 and 2.2 of the book Floating point 2.4 of the book Computer Architecture What do computers do? Manipulate stored information
More informationData 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
More informationChapter 3 Structure of a C Program
Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand
More informationIT 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
More informationChapter 2 Bits, Data Types, and Operations
Chapter 2 Bits, Data Types, and Operations How do we represent data in a computer? At the lowest level, a computer is an electronic machine. works by controlling the flow of electrons Easy to recognize
More informationUNIT  I. Introduction to C Programming. BY A. Vijay Bharath
UNIT  I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a generalpurpose programming language. It has been
More informationComputer Programming C++ (wg) CCOs
Computer Programming C++ (wg) CCOs I. The student will analyze the different systems, and languages of the computer. (SM 1.4, 3.1, 3.4, 3.6) II. The student will write, compile, link and run a simple C++
More informationUnit2 (Operators) ANAND KR.SRIVASTAVA
Unit2 (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
More informationThe C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)
The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments
More informationFloating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.
class04.ppt 15213 The course that gives CMU its Zip! Topics Floating Point Jan 22, 2004 IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Floating Point Puzzles For
More information