Programming with Java

Similar documents
Computational Expression

Full file at

ECE 122 Engineering Problem Solving with Java

Chapter 2 Elementary Programming

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

Elementary Programming

Chapter 2 ELEMENTARY PROGRAMMING

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

Data Conversion & Scanner Class

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

CS111: PROGRAMMING LANGUAGE II

CS110: PROGRAMMING LANGUAGE I

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

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

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

Programming with Java

Introduction to Java & Fundamental Data Types

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

CEN 414 Java Programming

Chapter 2. Elementary Programming

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

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

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

Important Java terminology

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Computational Expression

CS313D: ADVANCED PROGRAMMING LANGUAGE

Section 2.2 Your First Program in Java: Printing a Line of Text

Fundamentals of Programming Data Types & Methods

Peer Instruction 1. Elementary Programming

Lecture Set 4: More About Methods and More About Operators

Section 2: Introduction to Java. Historical note

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

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

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Full file at

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Lecture Set 2: Starting Java

Basic Computation. Chapter 2

Lecture Set 2: Starting Java

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

Operators in java Operator operands.

Lecture Set 4: More About Methods and More About Operators

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basics of Java Programming

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

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

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Chapter 2 Primitive Data Types and Operations

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Chapter. Let's explore some other fundamental programming concepts

Chapter 2: Data and Expressions

IEEE Floating-Point Representation 1

Accelerating Information Technology Innovation

Getting started with Java

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

3. Java - Language Constructs I

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 2 Elementary Programming

Oct Decision Structures cont d

Chapter 2: Data and Expressions

2.8. Decision Making: Equality and Relational Operators

CMPT 125: Lecture 3 Data and Expressions

Java Notes. 10th ICSE. Saravanan Ganesh

Visual C# Instructor s Manual Table of Contents

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

Primitive Data Types: Intro

Chapter 2 Primitive Data Types and Operations. Objectives

2: Basics of Java Programming

Object Oriented Programming. Java-Lecture 1

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

Mr. Monroe s Guide to Mastering Java Syntax

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

Chapter 2: Using Data

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

Object-Oriented Programming

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Introduction to Java Applications

Control Statements: Part 1

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

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Basic Computation. Chapter 2

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

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

Computer Science 145 Midterm 1 Fall 2016

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Programming with Java

Supplementary Test 1

AP Computer Science A

download instant at

Software Practice 1 Basic Grammar

Chapter 3: Operators, Expressions and Type Conversion

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

COMP 202 Java in one week

CMSC131. Introduction to your Introduction to Java. Why Java?

Transcription:

Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018

Objectives q By the end of this lecture you should be able to : ü Know rules of programming language ü Know how to start writing a program in java ü Use the input methods of Scanner class to get data from the keyboard ü Distinguish between the eight built-in primitive types of java ü Create constant values with the keyword final

Outlines Ø A Java program syntax Ø Identifiers and Reserved Words Ø Primitive Data Types Ø Type casting Ø ASCII Code Ø Arithmetic Expressions, Rational & Boolean Operators Ø Increment and Decrement Ø Input statement

A Java program /* Here you describe what your program does. */ public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class

Some notes q Java is a case-sensitive language. There is a difference between small-letters and capital-letters, for example: o o There is a difference between if and IF or If. The first word is a reserved word, while the two others are not. It s a compilation error to write the word public in capital as Public, or Static. q The java file name, and class name should be the same. q Every class must contain at least one method. q Everything in the class has to be between two braces. q The five output statements can be reduced to a single statement: o System.out.println( ( + x +, + y + ) ) ;

Identifiers q Identifiers are the words and symbolic name that a programmer uses in a program. q These can be assigned to : o Name of a variables, o Name of a methods, o Name of a classes, o Name of functions.

Naming rules 1 2 3 4 Characters allowed to be used in names are: 1 All English Capital Letters (Upper Case Letters ) 2 All English Small Letters (Lower Case Letters) 3 Underscore ( _ ) 4 Currency symbols ( $,, ) Digits( 0, 1, 2,,8,9). Digits are not allowed to be used at the beginning of the name, 5 Spaces are also not allowed, Java keywords are not allowed to be used as identifier name.

Some legal and illegal identifier names Book legal System illegal book legal class illegal _book legal public illegal $book legal @car illegal $Book legal Bo-ok illegal X legal?x illegal _B_O_O_K legal boo k illegal bo$k legal Stu%dent illegal book73 legal 73book illegal add2number legal 2NumberAdd illegal MathewClass legal import illegal While legal while illegal For legal for illegal

Some Java Reserved Words or Keywords: Note: 1. All java reserved words are in small letters, But, don t use those words as a name of a variable, method, or a class. 2. For class names, use names with first letter Capital, like: Ex16, Math12, 3. For methods and variables use names like : addnumbers,multnumbers,...

Primitive Data Types q A data type is a classification mechanism whereby it can be identified that what kind of data is stored inside the variable, and what operations it supports. Type Storage byte Allows for Range byte 1 very small integers -128 to 127 short 2 small integers -32,768 to 32,767 int 4 big integers -2,147,483,648 to 2,147,483,647 long 8 very big integers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 real numbers Approx : +/- 1.4 *10-45 To 3.4 * 10 38 double 8 very big real number boolean 1 true or false true or false. Approx: +/- 4.9 * 10-324 To 1.8 * 10 308 char 2 characters UNICODE character set

Default Values of Java's Primitive Types q The following table lists the default values for Java's primitive data types

Primitive Data Types q Examples: ² int x, y, z ; ² int sum = 0; ² float f; ² double d = 1.4; ² char capitalc = 'C'; ² boolean result = true; ² byte b = 100; ² short s = 10000; ² int i = 100000;

Creating constants q There will be values in program that cannot change during program execution such as: o o o The maximum score in an exam(100). The number of hours in a day(24). The mathematical value of pi. q Constants are declared much like variables in java except that they are preceded by the keyword final. Then that value is fixed and can not later be changed. ² final double PI = 3.14159; ² final int hours= 24; ² hours = 12; // will not compile

Type casting q It s the process of converting type of data from one type to another type. q Example: converting double to int and int to double.

Type casting Example: converting char to int and int to char.

ASCII Code q In computer world, characters can be represented by numbers ASCII, stands for: American Standard Code for Information Interchange. They ranged between 0 to 255. A à 65 B à 66 C à 67.... Z à 90 a à 97 b à 98 c à 99.... z à 122 0 à 48 1 à 49 2 à 50. 9 à 57 à 32

Braces, Parentheses, and Brackets }, braces, used for enclosing body of classes, body of methods, body of loops, body of switch, body of if else statements, q { q ( ), parentheses, used for enclosing method parameters, constructor parameters, arithmetic operations,. q [ ], brackets used for indicating size of arrays

Arithmetic Expressions: q Expression: is a collections of literals (constant), variables, operators, and parentheses: q Literals: q Variables: 2, -3, 3.29,.. x, book, car, a, q Operators: +, -, %, *, / q Parentheses ( ) q Expression Example: o 1- (x+10) 1 and 10 are literals, x is a variable, - and + are operators, ( ) are parentheses. o ((book+2)*5) 2 and 5 are literals, book is a variable, + and * are operators, ( ) are parentheses.

Arithmetic Operators: Ø / * % Ø + - q There are Two Types of Operations: o o Integer operations: Integer and integer 10/3 = 3 floating-point operations: floating-point and floating-point 10.0/3.0=3.3333 floating-point and integer 10.0/3 =3.3333 Integer and floating-point 10 /3.0=3.3333

Arithmetic Operators example

Relational Operators Math Symbol Java Notation Description = == Equal to!= Not equal to < < Less than <= Less than or equal > > Greater than >= Greater than or equal ² They are also known as Comparison operators. ² The Comparison operators give a boolean result of true or false. ² Examples: 3 > 1 true 3 == 8 false 3 >= 2 true 3!= 8 true 3 >= 3 true 3 <= 1 false

Boolean Operators: à OR && à AND X Y X Y X Y X && Y true true true true true true true false true true false false false true true false true false false false false false false false Ex: int x =3; (x>2) &&(x<10) = T && T = T Ex: int x =3; (x<2) (x>7) = F F = F Ex: int x =1; (x>2) &&(x<=6) = F && T = F Ex: int x =2; (x>3) (x< 5) = F T = T

Precedence Rule: Level 1 parentheses ( ) Level 2 % / * Level 3 + - Level 4 From left to right, for same level operations Ex: 3. + 2* 3.5 +4 = 3.+7.+4 = 10.+4 = 14.0 Ex: (3. + 2)* (3.5 *4) = 5.*14. = 70.0 Ex: (3+2)*3.5+4 = 5*3.5+4 = 17.5+4 = 21.5 Ex: 3.0+2.*(3.5+4) = 3.0+ 2.* 7.5 = 3.0+ 15. = 18.0 Ex: 3.0+2.*3.5+4 = 3.0+7.0+4 = 10+4 = 14.0 Ex: (10%3+4)-(4%3) = (1+4)-(1) = 5 1 = 4

Increment and Decrement q Postfix version (var++, var--): use value of var in expression, then increment or decrement. Ø ++ increment by 1 Ø -- decrement by 1 Ø y = x++ ; means assign the value of x to y, then increment x by 1 ; It is true for x--. q Example: Ø count++; is meaning count = count + 1; Ø count--; is meaning count = count - 1;

Increment and Decrement q Prefix version (++var, --var): use value of var in expression, then increment or decrement. When used in prefix mode, it increments the operand and evaluates to the incremented value of that operand. Ø ++ increment by 1 Ø -- decrement by 1 Ø y = ++x ; means increment x by 1, then assign this new value of x to y ; It is true for --x.

Increment

Decrement

Increment & Decrement

Increment (prefix & postfix)

Input in Java: Scanner class q Scanner class : Is a special class, which makes it easy for user to write a program that obtains information that is typed in at the keyboard. q Firstly, import Scanner package (import java.util.scanner) q Secondly, Create a Scanner object Ø Scanner keyboard = new Scanner(System.in); q The Scanner class has several input methods include: o nextint(), is used to input the value of an integer, o nextdouble(), is used to input the value of an double o next(), is used to input a string (word), o nextline(), is used to input a sentence o nextboolean(), return boolean data type

Example import java.util.scanner; public class add2numbers { public static void main( String[] args ) { Scanner input = new Scanner( System.in ); int x; int y; int sum; System.out.print( "Enter first integer: " ); x = input.nextint(); System.out.print( "Enter second integer: " ); y = input.nextint(); sum = x + y; System.out.printf( "The Resul is = "+ sum ); } } I / O Enter first integer: 2 Enter second integer: 4 The Resul is = 6

Example Q/ Write a Java program for determining average of four numbers? import java.util.scanner; public class Average4numbers { public static void main (String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter First Number is"); double a = input.nextdouble(); System.out.print("Enter Second Number"); double b = input.nextdouble(); System.out.print("Enter Third Number"); double c = input.nextdouble(); System.out.print("Enter Fourth Number"); double d = input.nextdouble(); double average; average = (a+b+c+d)/4 ; System.out.println("The Average is = average); } } I / O Enter First Number is 50 Enter Second Number is 60 Enter Third Number is 70 Enter Fourth Number is 80 The Average is = 65.0