02 Variables1November 15 02 Variables CE00858-1: Fundamental Programming Techniques.

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
03 Data types1June Data types CE : Fundamental Programming Techniques.
CMT Programming Software Applications
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Hello, world! Dissect HelloWorld.java Compile it Run it.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 2. Program Construction in Java. 01 Java basics.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
BASIC ELEMENTS OF A COMPUTER PROGRAM
2.5 Another Java Application: Adding Integers
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
OPERATORS (1) CSC 111.
Lecture 3 Expressions Richard Gesick.
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
MSIS 655 Advanced Business Applications Programming
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Expressions and Assignment Statements
Expressions and Assignment
elementary programming
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

02 Variables1November Variables CE : Fundamental Programming Techniques

02 Variables2November 15 Objectives In this session, we will: look at integers and doubles declare and initialise variables in Java applications perform arithmetic operations on numbers output values discuss downloading the JDK and Netbeans

02 Variables3November 15 Handling data most programs need to handle data to solve a problem, we need to think about: what data is used what results are required how we convert the data into the results program must store data in memory and access it later memory is like boxes that can contain values: 20a3.141fred

02 Variables4November 15 Numbers most programs use numbers may be whole numbers or decimal point numbers whole numbers: integer days in a week legs on a dog pages in a book decimal point numbers: double distance between Stafford and Bristol in kilometres average exam mark for a group of students person’s reaction time in seconds

02 Variables5November 15 Declaring variables values stored in variables may change as program runs variables have: a name a type a value must declare variable before use int count; double average; countaverage

02 Variables6November 15 Variable names all names are case sensitive rules for variable names: may be any length must start with a letter _ or $ must not contain spaces, tabs or special characters must not be keywords (main, void, public, etc) conventions for variable names: first letter should be lowercase each new word starts with a capital should be meaningful

02 Variables7November 15 Initialisation giving a variable an initial value good practice to initialise variables error raised if variable doesn't have a value before it is used int count = 10; double average = 2.5; 10 count 2.5 average

02 Variables8November 15 Assignment storing data in an existing variable evaluates right hand side of equals sign and overwrites contents of variable on left hand side count = 46; average = 9.75; 46 count 9.75 average

02 Variables9November 15 Arithmetic operators Java supports the standard arithmetic operators: all expressions must be given in full OperatorMeaning *multiply /divide %remainder +add -subtract energy = mass * speedOfLight * speedOfLight; //e = mc 2

02 Variables10November 15 Operator precedence operators have precedence which determines the order in which they are evaluated: operators with same order of precedence are evaluated from left to right brackets can override the order of operation OperatorPrecedence * / %carried out before + and - + -carried out after *, / and % average = (a + b) / 2;

02 Variables11November 15 Displaying variables contents of variables can be output using calls to System.out.print and System.out.println can output more than one thing using concatenator, +: System.out.println("Count is: " + count + " items"); System.out.print("Answer is: " + ans);

02 Variables12November 15 Analysis of calculations need to consider what data: type value where it comes from input by user hard-coded in program calculated by program what operations calculations required output needed

02 Variables13November 15 Calculation example – Rectangle problem: calculate and output area of rectangle given width of 10 and height of 20 analysis : what data? width: integer set to 10 height: integer set to 20 area: integer calculated in program what operations are performed? area = width * height output area

02 Variables14November 15 //calculate and output area of rectangle public class Rectangle { public static void main (String[] args) { int width = 10; int height = 20; //calculate area int area = width * height; //output area System.out.println ("The area is " + area); } Rectangle.java

02 Variables15November 15 Integers and doubles double variable can store integer values: integer variable cannot store double values: double average; int count = 7; average = count; double average; int count = 7; count = average; integer is promoted to a double potential loss of accuracy fails to compile

02 Variables16November 15 Division if both operands are integers and result is integer, integer division takes place: if both results are integer and result is double, integer division takes place and result is promoted to double: if either result is double, result must be double and real division takes place: double ave = ( ) / 4.0; //ave = 2.5 double ave = ( ) / 4; //ave = 2.0 int ave = ( ) / 4; //ave = 2

02 Variables17November 15 Java Development Kit & NetBeans we will be using Java Development Kit (JDK) Java SE versions are available for Windows and Linux NetBeans is an integrated development for developing Java application JDK and NetBeans are available from Sun visit the Sun Java website: download instructions are available from the FPT web site

02 Variables18November 15 Summary In this session we have: looked at when we use integers and doubles declared, initialised and used variables in Java applications performed calculations using numbers output results of calculations considered downloading the JDK and Netbeans In the next session we will: look at more calculations input data into programs