Lesson 02 Data Types and Statements. MIT 12043, 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 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 2 Elementary Programming

Chapter 2 ELEMENTARY PROGRAMMING

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

Chapter 2. Elementary Programming

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:

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.

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

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

Full file at

Computational Expression

Chapter 2: Data and Expressions

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

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

Chapter 2: Data and Expressions

CEN 414 Java Programming

Chapter. Let's explore some other fundamental programming concepts

Basics of Java Programming

Computational Expression

Data Conversion & Scanner Class

JAVA Programming Concepts

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

Fundamentals of Programming Data Types & Methods

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

Chapter 2 Elementary Programming

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

Chapter 2 Primitive Data Types and Operations

Full file at

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

Datatypes, Variables, and Operations

Chapter 2: Data and Expressions

CMPT 125: Lecture 3 Data and Expressions

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

BASIC ELEMENTS OF A COMPUTER PROGRAM

AP Computer Science A

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

COMP 202 Java in one week

Java Notes. 10th ICSE. Saravanan Ganesh

Visual C# Instructor s Manual Table of Contents

Elementary Programming

2: Basics of Java Programming

Chapter 2 Elementary Programming

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

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

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

Basic Computation. Chapter 2

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

Chapter 2: Using Data

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

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

Program Fundamentals

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

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

Lecture Set 2: Starting Java

JAVA Programming Fundamentals

Lecture Set 2: Starting Java

Information Science 1

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

Chapter 6 Primitive types

Primitive Data Types: Intro

3. Java - Language Constructs I

A First Program - Greeting.cpp

Programming Lecture 3

Expressions and Casting

Fundamentals of Programming CS-110. Lecture 2

What did we talk about last time? Examples switch statements

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

COMP 202 Java in one week

Chapter 2: Using Data

More Programming Constructs -- Introduction

CS 106 Introduction to Computer Science I

Basic Computation. Chapter 2

Programming with Java

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

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.

6.096 Introduction to C++ January (IAP) 2009

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

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

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Fundamental of Programming (C)

Declaration and Memory

Chapter 2: Using Data

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS102: Variables and Expressions

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Introduction to Programming EC-105. Lecture 2

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

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

Chapter 2 Primitive Data Types and Operations

Java Programming Fundamentals. Visit for more.

Transcription:

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

Topics Covered Statements Variables Constants 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. o Eg: int, char 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

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.

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;

Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;

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 = 32; 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.

Constants A named constant is an identifier that represents a permanent value final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;

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

Integer Division +, -, *, /, and % 5 / 2 yields an integer 2 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division)

Remainder Operator Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd.

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

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

Escape Sequences for Special Characters Description Escape Sequence Tab \t Linefeed \n Backslash \\ Single Quote \' Double Quote \"

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. A program when given three marks of an exam which calculates and prints the total and the average. 2. A Program when given the Currency Rate of a US Dollar. Calculates and prints the a Sri Lankan Rupee amount into US Dollars. 3. 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 4. Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius 5 ( 9 )( fahrenheit 32)

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

Using Scanner Class

Using Scanner Class

Using Scanner Class

Solution for Restaurant Bill Exam - Answer

Solution for Restaurant Bill Classroom Exam

Exercise 05 A university pays its Academic Staff o o Academic Allowance 39% of Basic Salary 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

End of Lecture