# Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Size: px
Start display at page:

Download "Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University"

Transcription

1 Numerical Data CS 180 Sunil Prabhakar Department of Computer Science Purdue University

2 Problem Write a program to compute the area and perimeter of a circle given its radius. Requires that we perform operations on numbers Strings or other standard classes are not appropriate for this purpose. Instead, we will use a special type of data 2

3 Primitive Data Types As all matter is fundamentally composed of atoms, all objects are fundamentally composed of primitive data types. Primitive types are the building blocks of all data used in Java. Primitive data types are neither classes nor objects. They are so simple, that we don't need the overhead of objects Each type can be processed using only specific operators 3

4 Primitive Data Types Numeric e.g., 2, 3, , for storing and operating on integer and real valued data Character ' ۻ,' 'ש' ' ', e.g., 'a', 'अ', ' ', 'ᡚ', 'ᢆ', ' ', ' ', ' ', for capturing characters for (almost) all world languages Boolean only two allowed valued: true, false logic data type This week we will study Numeric data. 4

5 Area and Perimeter import java.util.scanner; public class CircleCalculator { public static void main (String[] args){ double radius, area, circumference; Not a class Scanner scanner = new Scanner(System.in); System.out.println("Enter radius"); radius = scanner.nextdouble(); } } circumference = 2.0 * 3.14 * radius; area = 3.14 * radius * radius; System.out.println("Given Radius: " + radius + "\n" + "Area: " + area + "\n" + "Circumference: " + circumference); 5

6 Important Points Note the use of = do not confuse this with the = symbol from mathematics circumference = 2 * 3.14 * radius; computes the product of 2, 3.14, and the numeric value stored in radius, and copies this value into circumference This is an assignment statement. Causes the value stored in circumference to change. 6

7 Variables Data items such as area are called variables. since we can change their values during program execution. A variable has three properties: A memory location to store the value, The type of data stored in the memory location, and The name used to refer to the memory location. When the declaration double area; is made, memory space is allocated to store a real number value area is a reference for this space. 7

8 Assignment Statements We set the value of a variable using an assignment statement. Do not confuse with equality in Algebra! double a, b, c; a = 3.0; b = 2.0 * 2.3; c = a * b; Compute the value of the right (of =) and copy the result into the variable on the left. a = 2 * a; Use the current value of a to compute result and copy the result back into a. Can also initialize when declaring double a = 5.9, b = 34; 8

9 Arithmetic Operators double x, y, z; x = 5.0; y = 2.5; Multiplication Addition Subtraction Division z = x * y; z = x + y; z = x - y; z = x / y; z = 12.5 z = 7.5 z = 2.5 z = 2.0 Unary negation z = -y; z =

10 Examples of expressions double tempc, tempf; tempf = tempc * 9.0/ ; double x, y, z; z = x * x + y * y / x; z = x*x+y*y/x; z = x * x + y * y / x; Whitespaces make no difference. All these expressions are identical to the compiler. 10

11 Arithmetic Expressions How is the following expression evaluated? Answer: x is added to 3*y. We determine the order of evaluation by following precedence rules. Evaluation is in order of precedence. double x, y, z;... z = x + 3 * y; Recall PEMDAS Operators at same level are evaluated left to right for most operators 11

12 Precedence Rules Priority Group Operator Rule High Subexpression ( ) Starting with innermost () Low Unary operators Multiplicative operators Additive operators -, + Left to right. *, /, % Left to right. +, - Left to right. 12

13 Precedence Examples x + 4*y - x/z + 2/x =? x + (4*y) - (x/z) + (2/x) (x + y * (4 - x) / z + 2 / -x ) =? (x + ((y * (4-x)) / z) + (2 / (-x))) To be safe, use parentheses! 13

14 Numeric Data Types The type double that we saw allows us to store a very wide range of real number values: X to x bytes are used to store each double variable How? (please wait till this week s Recitation) Sometimes, we don t need such a large range. can use the type float instead only 4 bytes, but smaller range x to x

15 Area and Perimeter import java.util.scanner; public class CircleCalculator { public static void main (String[] args){ float radius, area, circumference; Note type } } Scanner scanner = new Scanner(System.in); System.out.println("Enter radius"); radius = scanner.nextfloat(); Note method name circumference = 2.0 * 3.14 * radius; area = 3.14 * radius * radius; System.out.println("Given Radius: " + radius + "\n" + "Area: " + area + "\n" + "Circumference: " + circumference); 15

16 CAUTION: Imprecision It is not possible to exactly represent every possible real valued number in double or float Fixed number of bits float: 4 bytes bits:2 32 (~1 billion) values double: 8 bytes bits: 2 64 (~1 million trillion) values BUT, how many real numbers between just 1.0 and 2.0? INFINITE! floats and doubles may only store an approximation of the actual number!!!! Do not rely on exact values! Examples in Recitation 16

17 Integer data If we are dealing with integer values only, using float or double is unwise: operations are slow maybe using too much space (memory) sometimes there is a (small) error in representation (more shortly) Instead, we have completely separate numeric types for integer data byte, short, int, long differ in size and range 17

18 Numeric Data Types Type Content Size (bytes) Minimum Value Maximum Value byte short int Integer long 8-9, 223, 372, 036, 854, 780, 000 9, 223, 372, 036, 854, 780, 000 float double Real x x x x

19 Operators for Integer types int x, y, z; x = 5; y = 2; Multiplication z = x * y; z = 10 Addition z = x + y; z = 7 Subtraction z = x - y; z = 3 Division z = x / y; z = 2 Truncation! Modulo z = x % y; z = 1 Remainder 19

20 Division Operator It is important to note the behavior of division when the operands are all Integer types (byte, short, int, long) in this case we get integer division (truncation of the decimal part) at least one is of type float or double in this case we get regular division (no truncation). there may be errors due to inherent problem with float and double representations. Division by 0 causes an error. 20

21 Integer vs. Real Division... public static void main (String[] args){ int i, j, k; float f, g, h; } i = 5; j = 2; k = i/j; k = j/i; f = 5; g = 2; h = f/g; k = 2 k = 0 h =

22 Modulo Operator This is simply a remainder operator x % y computes the remainder when x is divided by y. normally only used when both x and y are integer types (byte, short, int, or long) can be used with float and double, but results are not really meaningful 22

23 Type Safety Why so many different types for numeric data? Integer types are more efficient and 100% accurate, BUT don't handle fractional values. All types have a range larger range implies more memory used Can we mix different types in expressions and assignments? Yes, but have to be careful. 23

24 Numeric Type Precision The numeric types can be arranged in order of their ranges as follows: byte < short < int < long < float < double The range of each type is strictly more precise than the range of each type to its left E.g., any byte value can be stored in a long variable Thus, there is no loss in assigning a smaller typed value to a larger typed variable Going the other way causes losses! 24

25 Examples byte b; short s; int i; long l; float f; double d;... byte b; short s; int i; long l; float f; double d;... Each of these assignments is legal -- no data loss. d = f; d = l; d = i; d = s; d = b; f = l; f = i; f = s; f = b; l = i; l = s; l = b; i = s; i = b; s = b; f = d; l = d; i = d; s = d; b = d; l = f; i = f; s = f; b = f; i = l; s = l; b = l; s = i; b = i; b = s; ERROR!! Each of these assignments is illegal -- could result in data loss. 25

26 Type Casting It is possible to explicitly change types (type casting) Necessary when assigning a more precise type to a less precise one (Demotion). d = (double) i; i = (int) d; possible data loss assigning a float or double to an integer type results in truncation (not rounding) i = (int) 3.5; i will store 3, not 3.5 Automatically done when assigning a less precise type to a more precise type (promotion). No data loss 26

27 Expression Types Each numeric expression has a data type. What is the type of i + j? Depends on the type of i and j. If they are both of the same type, then the expression of the same type too Otherwise the operand with the lower type will be automatically promoted to the higher type; the overall expression will be of this higher type too. 27

28 Expression types byte b; short s; int i; long l; float f; double d; l = b + i; l = (long) (f * d); s = (short) f / b; d = ((s/b) + (i*l))/f; 28

29 Literal Numeric Values What is the type of a literal value such as 3 or 3.45? If there is no decimal point, then the type is int To make it a long type append L or l For byte and short -- no special type. If the value is an integer within the range of byte (short), it can be assigned to a byte (short) If it has a decimal point, then its type is double. To make it a float append F or f byte b = 23; short s = 145; int i = -2345; long l = 234L; float f = F; double d = ; 29

30 Problem Write a program that when given the lengths of two sides of a triangle, and the angle between these sides, computes the length of the third side. Recall: c 2 = a 2 + b 2-2ab cosc c = a 2 + b 2-2ab cosc b C a c 30

31 Solution We know how to get the three inputs. But, how do we compute square roots and cosines? Many common functions are available as methods of the Math class defined in the java.lang package. Trigonometric methods require angles to be expressed in Radians (not degrees). Most methods take double arguments and their return type is double 31

32 Sample Math Class Methods Method name Description Input type Output type pow(x, y) Return x y double double log(x) Return natural log of x. double double sqrt(x) Return the square root of x double double sin(a) Return sine of angle a (radians) double double asin(a) Return the arc sine of a (in radians) double double toradians(d) Convert d from degrees to radians. double double exp(x) Return e x double double max(x, y) Return larger of x or y. * * See API for details 32

33 Primitive vs. Class assignment For assignment, the behavior of primitive variables seems to be different from that of class (reference) variables. double i,j; i = 5.0; i = 85.0; Customer cust1, cust2; cust1 = new Customer( ); cust2 = new Customer( ); i = j; cust1 = cust2; 33

34 Primitive Data: Declaration & Assignment double i,j; i = 5.0; j = 8.0; i j Memory is allocated. Values are stored in those locations. 34

35 Primitive Data Assignment double i,j; i = 5.0; i = 85.0; i 85.0 Memory is allocated. The value 5.0 is stored in i. The value 85.0 is stored in i. Old value is lost. 35

36 Object Assignment Customer customer; customer = new Customer( ); customer = new Customer( ); customer The identifier customer is allocated. : Customer : Customer The reference to the first object is stored in customer. The reference to the second object is stored in customer. The old reference is lost. 36

37 Assigning objects Customer cust1, cust2; cust1 = new Customer( ); cust2 = cust1; cust1 cust2 The identifiers are allocated. : Customer The reference to the object is stored in cust1. The reference stored in cust1. is copied to cust2. 37

38 Assigning Primitive Data double i,j; i = 5.0; j = i; i j Memory is allocated. The value stored in i is copied to j. 38

39 Really the same Customer cust1, cust2; cust1 = new Customer( ); cust2 = cust1; cust1 cust2 The value happens to be a reference to an object. : Customer The value stored in cust1 is copied to cust2. Hence reference type vs. primitive type. 39

40 Area and Perimeter (again) import java.util.scanner; public class CircleCalculator { public static void main (String[] args){ double radius, area, circumference; Scanner scanner = new Scanner(System.in); System.out.println("Enter radius"); radius = scanner.nextdouble(); circumference = 2.0 * * radius; area = * radius * radius; } } System.out.println("Given Radius: " + radius + "\n" + "Area: " + area + "\n" + "Circumference: " + circumference); 40

41 Constants Note new keyword Many programs use a constant value that should not be changed during execution. To avoid errors and reduce effort, we can define these once and reuse them. final double PI = ; area = PI * radius * radius; perimeter = 2 * PI * radius; The Math class defines PI and E Convention: all upper case for constants. 41

42 Why use constants? Consistent values No errors due to mistyping Easy to manage If we need to change the precision of PI, we need only change it in one place. Programs are more readable. 42

43 Numeric Types vs. Strings Numeric data types are not strings! There are no quotes used for numeric types What is the difference between 20 and "20"? They are represented very differently by the computer. 20 is represented in binary equivalent of the value 20. "20" is simply two distinct characters. Doing math on numeric types is direct and fast. Numeric values have special formats. We can convert between the two types println() automatically converts numbers to strings 43

44 Parsing strings to numbers Consider the following attempt to read in the radius value. double radius, area, circumference; radius = JOptionPange.showInputDialog(null, "Enter radius"); Not allowed by the compiler: wrong type. To convert we use a special method defined in a special class: double radius, area, circumference; String inputstring; inputstring = JOptionPange.showInputDialog(null, "Enter radius"); radius = Double.parseDouble(inputString); 44

45 Wrapper classes Useful methods and constants for each of the primitive types are defined in corresponding 'wrapper' classes Primitive Type Wrapper class Sample Method Constants byte Byte parsebyte() short Short parseshort() int Integer parseint() long Long parselong() float Float parsefloat() double Double parsedouble() MIN_VALUE MAX_VALUE SIZE See API for details 45

46 CAUTION: + operator Recall the + operator for strings? It is different than the + operator for numeric data. If BOTH operands are numeric data then it is numeric addition Otherwise, it is string concatenation if one is numeric it will be converted to a string! double x=5.0, y=6.0, z; String name = "234.5", str; str = name + x + y; str = x + y + name; z = name + x + y; z = x + y + name; str = " " str = " " ERROR! ERROR! 46

47 CAUTION: overflow & underflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 Why? b went out of bounds and wrapped around! Overflow. Similarly underflow can occur. Pick types wisely! Each has its own range -- be aware of it. Note: compiler can catch some problems. byte b; b = 128; will not compile! 47

48 Shorthand operators When the right hand side of an assignment uses the same operand as the left hand side, we often use a shorthand form for some operators: Operator Example Shorthand For += x+=y; x = x+y; -= x-=y; x = x-y; *= x*=y; x = x*y; /= x/=y; x = x/y; %= x%=y; x = x%y; Note: no spaces 48

### CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

### CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

### 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

### 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

### 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

### Chapter 3: Operators, Expressions and Type Conversion

101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

### Full file at

Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

### 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

### A Java program contains at least one class definition.

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

### CS Programming I: Primitives and Expressions

CS 200 - Programming I: Primitives and Expressions Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code:

### CEN 414 Java Programming

CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

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

Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

### Basics of Java Programming

Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

### Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Class #07: Java Primitives Software Design I (CS 120): M. Allen, 13 Sep. 2018 Two Types of Types So far, we have mainly been dealing with objects, like DrawingGizmo, Window, Triangle, that are: 1. Specified

### Chapter 2: Using Data

Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

### Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity

### Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

CHAPTER 2 Define a method vs. calling a method Line 3 defines a method called main Line 5 calls a method called println, which is defined in the Java library You will learn later how to define your own

### More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

More Things We Can Do With It! More operators and expression types More s 11 October 2007 Ariel Shamir 1 Overview Variables and declaration More operators and expressions String type and getting input

### Chapter 4: Basic C Operators

Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional

### 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

### Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific

### Full file at

Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

### Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Lesson #3 Variables, Operators, and Expressions Variables We already know the three main types of variables in C: int, char, and double. There is also the float type which is similar to double with only

### COMP 202 Java in one week

CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

### CS112 Lecture: Primitive Types, Operators, Strings

CS112 Lecture: Primitive Types, Operators, Strings Last revised 1/24/06 Objectives: 1. To explain the fundamental distinction between primitive types and reference types, and to introduce the Java primitive

### CIS133J. Working with Numbers in Java

CIS133J Working with Numbers in Java Contents: Using variables with integral numbers Using variables with floating point numbers How to declare integral variables How to declare floating point variables

### What 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

### false, 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 Zheng-Liang Lu Java Programming 45

### Primitive Data Types: Intro

Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

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

Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

### Computer Programming CS F111

Computer Programming CS F111 BITS Pilani Dubai Campus NAND KUMAR Basics of C Programming BITS Pilani Dubai Campus Write a program that 1. Asks 5 marks from the user, find the average of the marks and print

### COMP 110 Introduction to Programming. What did we discuss?

COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1 Today Announcements

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

CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

### BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

### Java Classes: Math, Integer A C S L E C T U R E 8

Java Classes: Math, Integer A C S - 1903 L E C T U R E 8 Math class Math class is a utility class You cannot create an instance of Math All references to constants and methods will use the prefix Math.

### Types and Expressions. Chapter 3

Types and Expressions Chapter 3 Chapter Contents 3.1 Introductory Example: Einstein's Equation 3.2 Primitive Types and Reference Types 3.3 Numeric Types and Expressions 3.4 Assignment Expressions 3.5 Java's

### 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,

### Chapter 2: Data and Expressions

Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

### Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

### Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

### COMP 202. Java in one week

COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

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

Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

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

Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

### Chapter 2. Outline. Simple C++ Programs

Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

### Zheng-Liang Lu Java Programming 45 / 79

1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

### Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 ELEMENTARY PROGRAMMING Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Objectives To write Java programs to perform simple

### 2.5 Another Application: Adding Integers

2.5 Another Application: Adding Integers 47 Lines 9 10 represent only one statement. Java allows large statements to be split over many lines. We indent line 10 to indicate that it s a continuation of

### Arithmetic and IO. 25 August 2017

Arithmetic and IO 25 August 2017 Submissions you can submit multiple times to the homework dropbox file name: uppercase first letter, Yourlastname0829.java the system will use the last submission before

### Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types Java is a strongly-typed 1 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and

### Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

### COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few

### CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables

### 3.1. Chapter 3: The cin Object. Expressions and Interactivity

Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 The cin Object Standard input stream object, normally the keyboard,

### 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

### 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

### COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016 More Tutoring Help The Engineering Peer Tutoring Services (EPTS) is hosting free tutoring sessions

### Lecture Set 2: Starting Java

Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

### Midterms Save the Dates!

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Primitive Data Types Arithmetic Operators Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch 4.1-4.2.

### Lecture Set 2: Starting Java

Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

### Bits, Words, and Integers

Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

### LECTURE 3 C++ Basics Part 2

LECTURE 3 C++ Basics Part 2 OVERVIEW Operators Type Conversions OPERATORS Operators are special built-in symbols that have functionality, and work on operands. Operators are actually functions that use

### Course PJL. Arithmetic Operations

Outline Oman College of Management and Technology Course 503200 PJL Handout 5 Arithmetic Operations CS/MIS Department 1 // Fig. 2.9: Addition.java 2 // Addition program that displays the sum of two numbers.

### 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

### Operators. Java operators are classified into three categories:

Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

### Computer System and programming in C

1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

### WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS OPERATORS Review: Data values can appear as literals or be stored in variables/constants Data values can be returned by method calls Operators: special symbols

### Chapter 2: Using Data

Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

### Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

Introduction Syntax Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data Introduction Map Layer Mathematics Algebraic statements are used to perform the basic mathematical operations

### Operators and Expressions

Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

### Chapter 02: Using Data

True / False 1. A variable can hold more than one value at a time. ANSWER: False REFERENCES: 54 2. The int data type is the most commonly used integer type. ANSWER: True REFERENCES: 64 3. Multiplication,

### Declaration and Memory

Declaration and Memory With the declaration int width; the compiler will set aside a 4-byte (32-bit) block of memory (see right) The compiler has a symbol table, which will have an entry such as Identifier

### CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

### Arithmetic 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

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

### CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

### Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

### false, 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 Zheng-Liang Lu Java Programming 44

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

Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

### Engineering 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

### Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

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. 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

### COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: 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

### CS 302: Introduction to Programming

CS 302: Introduction to Programming Lectures 2-3 CS302 Summer 2012 1 Review What is a computer? What is a computer program? Why do we have high-level programming languages? How does a high-level program

### Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

### 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

### Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

### 1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 41 / 68 Example Given the radius

### These are reserved words of the C language. For example int, float, if, else, for, while etc.

Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

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

Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

### Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Chapter 2: Elementary Programming CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Motivations In the preceding chapter, you learned how to

### 3. Java - Language Constructs I

Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

### Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 2 Elementary Programming 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 problems

### Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

### Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Primitive Types Four integer types: byte short int (most common) long Two floating-point types: float double (most common) One character type: char One boolean type: boolean 1 2 Primitive Types, cont.