Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Similar documents
Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 2 Elementary Programming

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

ECE 122 Engineering Problem Solving with Java

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

Computational Expression

Chapter 2 ELEMENTARY PROGRAMMING

Data Conversion & Scanner Class

Chapter 2. Elementary Programming

Computational Expression

Full file at

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

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

Chapter. Let's explore some other fundamental programming concepts

Chapter 2 Primitive Data Types and Operations. Objectives

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Basics of Java Programming

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

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

Full file at

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

CMPT 125: Lecture 3 Data and Expressions

Fundamentals of Programming Data Types & Methods

Visual C# Instructor s Manual Table of Contents

Expressions and Casting

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Program Fundamentals

Chapter 2: Data and Expressions

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Basic Computation. Chapter 2

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

AP Computer Science A

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

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

COMP Primitive and Class Types. Yi Hong May 14, 2015

2: Basics of Java Programming

CEN 414 Java Programming

Chapter 2: Using Data

Primitive Data Types: Intro

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

More Programming Constructs -- Introduction

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.

COMP 202 Java in one week

JAVA Programming Fundamentals

Chapter 2: Data and Expressions

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

BASIC ELEMENTS OF A COMPUTER PROGRAM

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Java Fall 2018 Margaret Reid-Miller

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

COMP 202 Java in one week

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Elementary Programming

Java Primer 1: Types, Classes and Operators

Reading Input from Text File

Chapter 2 Primitive Data Types and Operations

Lecture Set 2: Starting Java

Programming with Java

What did we talk about last time? Examples switch statements

Information Science 1

Lecture Set 2: Starting Java

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

Chapter 2: Using Data

Chapter 2: Using Data

Basic Computation. Chapter 2

MIT Database Management Systems Lesson 03: Entity Relationship Diagrams

ESc101 : Fundamental of Computing

Programming Lecture 3

Values and Variables 1 / 30

Important Java terminology

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

1.3b Type Conversion

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Java Notes. 10th ICSE. Saravanan Ganesh

Chapter 2 Elementary Programming

Zheng-Liang Lu Java Programming 45 / 79

JAVA Programming Concepts

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

Chapter 2 Elementary Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

Chapter 2: Basic Elements of C++

Ex: If you use a program to record sales, you will want to remember data:

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Declaration and Memory

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

COMP 202. Java in one week

Programming II (CS300)

CS 106 Introduction to Computer Science I

Fundamental of Programming (C)

Lesson 01 Introduction

Transcription:

Lesson 02 Data Types and Statements MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Topics Covered Statements Variables Data Types Arithmetic Calculations Pre and Post increment operators Taking Input from User

Statements A Statement is the simplest task you can accomplish in Java. int othrs=5; System.out.println("netsalary= "+netsal); You need to put a semi colon ; at the end of a statement.

Variables Variables are locations in memory where values can be stored

Variable Name Variable is a location in memory Each location in memory has a memory address, which is a number o This long number is inconvenient to use when we want to access the memory location We give a human understandable name to refer to this number o e.g. age, quantity The compiler and the interpreter maps this name to the memory address number

Value of a Variable At a given time one value can be stored under the variable quantity

Variable Type You need to specify what type of data is to be stored. e.g. int, char This is because we must instruct how much memory should be reserved by the program to store the value of a variable The amount of memory needed depends on the maximum of the value we need to store in the variable.

Variable Type

Java Data Types Java supports eight primitive data types. These are built into the language itself. Consists of Numeric Types, char type and Boolean type. Remember String is not a primitive data type in Java o String is a class in Java, thus it is handled as a data type derived from a class. In Java we write classes and class can be a data type o Eg: If you write a class called Student you can use it as the Student data type

Data Types Name Range Storage Size byte 2 7 (-128) to 2 7 1 (127) 8-bit signed short 2 15 (-32768) to 2 15 1 (32767) 16-bit signed int 2 31 (-2147483648) to 2 31 1 (2147483647) 32-bit signed long 2 63 to 2 63 1 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807) float Negative range: 32-bit IEEE 754-3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 double Negative range: 64-bit IEEE 754-1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308

Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable;

Declaring Variables public static void main (String args[]) { int count; String title; boolean isasleep;... } Variables are usually defined at the beginning. However this need not always be the case.

Declaring Variables int x, y, z; String firstname, lastname; Multiple variables can be defined under one type

Declaring Variables Once declared the variable need to be initialized o Initialization Specify the value we want to store in the variable int myage; myage = 39; String myname = SaNa"; boolean istired = true; int a = 4, b = 5, c = 6; You can also initialize variables as the declaration is done.

Declaring and Initializing in One Step int x = 1; double d = 1.4; int age=19; int age; age = 19; The above statements are identical

Variable Names int age; float $money; char my_char; long _no; String name7; A Variable Name should start with an Alphabetical letter or $, or _ symbol The other characters can include numbers But you cannot use symbols like @, #, etc

Variable Names int my age; float @money; char 6my_char; long no*; The above names are incorrect. You cannot have spaces and other special symbols.

Variable Names int qty; String firstname; float basicsal, netsal; It s best if you can give suitable (short but meaningful) variable names.

Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2

Arithmetic Expressions 3 4x 5 10( y 5)( a x b c) 4 9( x 9 y x ) is translated to (3+4*x)/5 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

How to Evaluate an Expression Though Java has its own way to evaluate an expression behind the scene, the result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression. 3 + 4 * 4 + 5 * (4 + 3) - 1 3 + 4 * 4 + 5 * 7 1 3 + 16 + 5 * 7 1 3 + 16 + 35 1 19 + 35 1 54-1 53 (1) inside parentheses first (2) multiplication (3) multiplication (4) addition (5) addition (6) subtraction

Taking User Inputs

Using Scanner Class The Scanner class is a class in java.util, which allows the user to read values of various types. The Scanner looks for tokens in the input. A token is a series of characters that ends with what Java calls whitespace. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file.

Using Scanner Class Method int nextint() long nextlong() float nextfloat() double nextdouble() Returns Returns the next token as an int. Returns the next token as a long. Returns the next token as a float. Returns the next token as a double. String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. String nextline() Returns the rest of the current line, excluding any line separator at the end.

Using Scanner Class 01 02 03

Exercise A university pays its Academic Staff o Academic Allowance 39% of Basic Salary o Research Allowance 45% of Basic Salary o Cost of Living Allowance 7,550/=. And deducts UPF 8% of the Basic Salary. Write a Java program to input the Basic Salary. Calculate the above and display them all with Net Salary

Shortcut Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8.0 f = f - 8.0 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8

Numeric Type Conversion Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2;

Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int.

Type Casting Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) range increases byte, short, int, long, float, double

The String Type The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java"; String is actually a predefined class in the Java library just like the System class. The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable.

String Concatenation // Three strings are concatenated String message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character B String s1 = "Supplement" + 'B'; // s1 becomes SupplementB

Exercises 1. Write a Java program that takes three marks of an exam and displays the total and the average. 2. Write a program to input how many notes, coins of denominations of 1000/=, 500/=, 200/=, 100/= 50/=,20/=,10/=,5/=, 2/= and 1/= are available. Print the total amount

End of Lecture