1 2. Program Construction in Java. 01 Java basics.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2: Introduction to C++.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Expressions, Data Conversion, and Input
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
Primitive Variables.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
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.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java-02 Basic Concepts Review concepts and examine how java handles them.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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 and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Multiple variables can be created in one declaration
Introduction to C++ October 2, 2017.
2.1 Parts of a C++ Program.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Introduction to Java, and DrJava
Expressions and Assignment
elementary programming
Chapter 2: Introduction to C++.
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Just Enough Java 17-May-19.
Variables and Constants
Presentation transcript:

1 2. Program Construction in Java

01 Java basics

3 Variables A memory location with a name where the computer stores data. Think of them as named pigeon holes. Only one datum (data item) is held in that location at a time, but the contents can be changed by the user or the program.

4 Variables All variables must be declared before being used i.e named and their type stated. Variable names start with a lower case letter (never a digit) and have no spaces. Use camelCase to distinguish words e.g. totalScore, averageOf3Throws. ‣ other examples are NaCl, McInally and iPod.

5 Primitives The most basic variable types are called primitives: ‣ byte - small integers from -128 to +127, ‣ int - medium integers in the range of about + or , ‣ long - large integers in the range of about +/- 9 million million million,

6 Primitives ‣ double - real numbers (i.e. with fractional parts) from +/- 2 x 10 +/ , e.g or , ‣ char - a single character e.g. 'A', '$' or '9', ‣ boolean - true or false. Two others ( short and float ) exist but will not be used in the exam.

7 Declaring variables A variable gets declared when first references along with its data type e.g. ‣ int count; ‣ char initial; ‣ double salesTaxRate; Note that every statement in Java ends with a semi-colon (;). A variable’s name is called its identifier.

8 Assignment The equals sign (=) in Java is used to change or initialise variables: ‣ char initial = ‘t’; ‣ int count; count = 4; ‣ boolean smoker = false; smoker = true;

9 Casting This is not allowed: ‣ char initial = 47.58; However, this is allowed: ‣ int p = (int) Math.round(3.142); even though Math.round() is a function that gives a long type of answer.

10 Arithmetic operators Addition (operator is +) Subtraction ( - ) Multiplication ( * ) Division ( / ) Modulus ( % ) [This means remainder e.g. 15%6 would be 3, since 15/6 is 2 remainder 3.]

11 Arithmetic operations Operations on ints give int results: ‣ int a = 12; int b = 6; int c = a / b; puts the value 2 into c Note that this is allowed ‣ int count = 4; count = count + 1;

12 Arithmetic operations A byte divided by a byte gives a byte, etc; When faced with an inexact integer division, Java rounds the result down e.g. dividing 10 by 3 produces 3. What would happen here? ‣ int count = 0; int answer = 758/count;

13 Arithmetic promotion When doing arithmetic on unlike types, Java widens the types involved so as to avoid losing information: ‣ double *( long, int or byte ) gives a double, ‣ long *( int or byte ) gives a long, ‣ int * byte gives an int.

14 Precedence %, * and / are done first from left to right. + and – are done later. Round brackets (around any section) means do it first whatever.

15 Functions Some of many examples: ‣ long y = Math.round(x); ‣ double y = Math.sin(x); ‣ double y = Math.abs(x); ‣ double z = Math.pow(x,y);

16 Spaces and cases Java is case sensitive so a variable called taxRate is not the same as taxrate. White space is not significant except inside “quotation marks”.

17 Comment Comments are ignored by the compiler Everything between /* and */ and on a single line after //. Use the /* */ style comments to comment out multiple lines for debugging (spotting problems in your code) purposes.

18 Blocks and indentation Parts of a program are divided into blocks, surrounded by {braces}. There must be an equal number of opening and closing braces in your programs. The other types of bracket, (...) and [...] are also used but for different purposes, and the same rule applies.

19 Blocks and indentation All lines apart from comments and those with braces must end with a semi-colon (;). Always start the contents of a new block with a tab (indent its contents) - otherwise it will be impossible to spot errors later.