I. Variables and Data Type week 3

Similar documents
Computational Expression

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

Chapter 2: Basic Elements of Java

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

Programming with Java

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Full file at

CEN 414 Java Programming

Introduction to Programming Using Java (98-388)

CS 302: Introduction to Programming

Lecture 3 Tao Wang 1

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

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS110: PROGRAMMING LANGUAGE I

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

CMPT 125: Lecture 3 Data and Expressions

Chapter 2: Data and Expressions

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

Arithmetic and IO. 25 August 2017

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Datatypes, Variables, and Operations

Data Conversion & Scanner Class

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Object-Oriented Programming

Input. Scanner keyboard = new Scanner(System.in); String name;

CS111: PROGRAMMING LANGUAGE II

Introduction to Computer Programming

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

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

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 2: Data and Expressions

Basic Computation. Chapter 2

Lecture Set 2: Starting Java

Building Java Programs

Full file at

Lecture Set 2: Starting Java

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

Building Java Programs

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CSCE 120: Learning To Code

Chapter 2. Elementary Programming

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

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

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

Introduction to Java & Fundamental Data Types

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

CS Programming I: Primitives and Expressions

STUDENT LESSON A7 Simple I/O

Topic 4 Expressions and variables

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Building Java Programs

Primitive Data Types: Intro

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

COMP 202 Java in one week

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Chapter 2 Working with Data Types and Operators

Basic Computation. Chapter 2

VARIABLES & ASSIGNMENTS

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

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

Data types Expressions Variables Assignment. COMP1400 Week 2

ECE 122 Engineering Problem Solving with Java

COMP 202. Java in one week

Introduction to Java Applications; Input/Output and Operators

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Basic Programming Elements

Introduction to Java Applications

Chapter 02: Using Data

2.5 Another Application: Adding Integers

Java Notes. 10th ICSE. Saravanan Ganesh

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Java Coding 3. Over & over again!

JAVA Ch. 4. Variables and Constants Lawrenceville Press

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Algorithms and Programming I. Lecture#12 Spring 2015

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

Lecture 03 & 04 Computer Programming

Object Oriented Programming. Java-Lecture 1

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

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

3. Java - Language Constructs I

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

Java Programming Language. 0 A history

Chapter 2 - Introduction to C Programming

C Programming

C: How to Program. Week /Mar/05

XQ: An XML Query Language Language Reference Manual

Chapter 2: Using Data

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Chapter 2: Data and Expressions

Chapter 1 Introduction to java

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

3 The Building Blocks: Data Types, Literals, and Variables

Outline. Data and Operations. Data Types. Integral Types

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

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

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Transcription:

I. Variables and Data Type week 3 variable: a named memory (i.e. RAM, which is volatile) location used to store/hold data, which can be changed during program execution in algebra: 3x + 5 = 20, x = 5, x is the variable formally, a variable is defined as an object with the following properties: 1. name: a tag to identify the variable in code 2. address: a location in memory where the variable s value is stored 3. type: determines the possible range of values the variable can store, as well as how much space the variable will occupy 4. value: contents of the memory starting at address and as big as determined by the type 5. scope: where in a program a variable s value is visible, i.e. where it can be referenced 6. lifetime: when a program a variable s value is visible, i.e. where it can be referenced variable and constant naming conventions name must begin with a letter, followed by 0 or more letters, numbers, and/or underscore characters the name cannot be a keyword use meaningful names, i.e. X is not a meaningful name 1 st word lower case, then upper case 1 st letter of each following word, i.e. camelcase as always, variable names are CASE SENSITIVE! 1/25/2018 1

Variable Declaration: all variables/constants in Java must be declared this allows the programmer to give the variable a TYPE and a NAME 1/25/2018 2

general form: [access] type var_name [= value] ; // indicates optional Examples: int age; int sum = 20; double subtotal = 33.88; String firstname ; // String is a Class in Java, not a primitive type variable types should be selected based upon the values the variable are expected to contain variable type are essentially classes, and the declaring of the variable instantiates an object of that type Java does not assign default values for local variables, only for class and instance variables, which get initialized to 0, or something that closely resembles 0, e.g. strings initialized to null I strongly suggest declaring all variables together, then do assignments at the appropriate spot in the program (note this is different from the book) Variable Assignment: assigning values to variables general syntax: var_name = expression ; (note: assignment works L R ) expression can be o compatible literal value o another variable of??? type o mathematical equation o call to a method that returns a compatible value o combination of one or more items in this list examples: age = 22 ; sum = sum + 1 ; name = Mark ; answer = sum ; // what is name, what is type, what is data value 1/25/2018 3

bad examples: int age = abc ; int age = "22" ; String Name = 22 ; int sum = 22.5 ; String firstname = Mark Thomas ; Note: 22 = age is not a valid assignment (although it is in Algebra) STRONGLY SUGGESTED techniques 1. declare all necessary variables needed for the entire block, grouping likely used variables together, in order 2. perform any necessary variable assignments, in order of need Named Constants a named memory location used to store/hold data, which CANNOT be changed during program execution general syntax: [access] final type var_name = expression ; naming conventions: all Upper Case with _ between words, e.g. TAX_RATE examples: final double TAX_RATE = 0.065 ; final double PI = 3.14159 ; may need to use type declaration characters following the number: D / d : double ** S / s : short (integer) F/f : float L : long (integer) ** default 1/25/2018 4

II. Variables and Arithmetic Operators What is an arithmetic operator? Something that performs operations. What does an operator perform operations on? Operands. Name Symbol # Operands 1. addition + 2 e.g. num = num + 5 ; 2. subtraction - 2 3. multiplication * 2 4. division / 2 5. negation - 1 unary Operator precedence order of arithmetic evaluation 1. parenthesis 2. negation 3. multiplication/division 4. addition/subtraction? = (2 + 3) * (3 + 3) // answer =? = 4 + 6 / 2 // answer =? = 1 + 2 * 3 // answer = To make precedence a bit more complex, if expressions contain operators of equal precedence, the order of operations is left to right.? = 8 / 2 * 3 // answer = 12? = 8 + 40 / 8 * 2 + 4 // answer = 22? = 7 2 4 + 1 // answer = 2 1/25/2018 5

Typical Types of Arithmetic Techniques 1. counting - typically count a single unit/item - typically count by 1 (or 1) - e.g. count = count + 1 ; 2. accumulation - act of accumulating a total value (i.e. a sum) - typically don t accumulate by a single unit/item - e.g. sum = sum + itemprice ; III. Division and Exponentiation Division division in Java is based upon the types of the numbers in the operation if all numbers are type int, the result will be an int, e.g. 8 / 5 yields 1, because 8 and 5 are int types 8.0 / 5.0 yields 1.6, because both types are double 8.0 / 5 yields 1.6, because one of the numbers is a double, so Java converts the smaller (int) to a larger (double), and the result is a double be very careful with this Exponentiation exponentiation in Java uses the Math.pow method syntax: double Math.pow(double a, double b) example: twosquared = Math.pow (2.0, 2.0); 1/25/2018 6

IV. Getting Console Input (BJ 2.3) we have already used the System.out object, with println and print methods to get input, we use the System.in object, but a bit differently first, we need to define a scanner class as follows: Scanner in = new Scanner(System.in); the new statement, called a constructor, construct or builds a new object, in this case, a Scanner object, passing it the System.in object to specify where to scan from the 2 nd part of the above statement, declares a variable named in of type Scanner and assigns the newly constructed object to it next, to actually get the input, you use the in variable with the.nextint() or.nextdouble() method as follows: int number; number = in.nextint(); note, when using a Scanner class, you ll most likely see an error as the class containing the Scanner class (java.util) has not been imported, to fix this, you can either 1. select Source Fix Imports OK 2. manually include the missing class statement: import java.util.scanner; V. Example Program example task: create a program to prompt the user to enter a number (int), take the number and double it, then output results how many inputs? how many outputs? how many variables? see NumberDoubler program exercise in class for practice: convert NumberDoubler to work on double types 1/25/2018 7