Advanced Programming Behnam Hatami Fall 2017.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
Java Syntax Primitive data types Operators Control statements.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Advanced Programming in Java
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Java Java Translation Program Structure
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Basic Concepts Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Basic Concepts Mehdi Einali Advanced Programming in Java 1.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Chapter VII: Arrays.
Information and Computer Sciences University of Hawaii, Manoa
Primitive/Reference Types and Value Semantics
Chapter 6: Data Types Lectures # 10.
String class.
Programming Languages and Paradigms
Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Programming Paradigms
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
MSIS 655 Advanced Business Applications Programming
Starting JavaProgramming
Java - Data Types, Variables, and Arrays
Chapter 2: Basic Elements of Java
elementary programming
CS2011 Introduction to Programming I Strings
Fundamental Programming
Peer Instruction 4 Control Loops.
Java: Variables, Input and Arrays
Comparing Python and Java
Problem 1 Given n, calculate 2n
Review of Java Fundamentals
Unit-2 Objects and Classes
Presentation transcript:

Advanced Programming Behnam Hatami Fall 2017

Agenda Review User input Strong type checking Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays

Review Variables Operators Methods Conditions Loops Primitive data types Operators Methods Parameter passing Call by value Conditions If, else, else if Loops while do-while for

User Input Print on console How to read from console? Scanner Example: System.out.println How to read from console? Scanner Example:

Example

Type Checking Java has a strong type-checking mechanism Some assignment is not permitted

Direct Type Conversion byte char The arrows are transitive All other conversions need an explicit cast boolean is not convertible char is a special type short int boolean long float double

Type Conversion Grid

Type Conversion N : the conversion cannot be performed Y : the conversion is performed automatically and implicitly by Java C : the conversion is a narrowing conversion and requires an explicit cast Y* : the conversion is an automatic widening conversion, but that some of the least significant digits of the value may be lost by the conversion

Example floating-point types are approximations of numbers They cannot always hold as many significant digits as the integer types

Floating Point, Some Notes Double.NaN Infinity Negative infinity Formatting a double

Comparison Compare doubles Using == with float or double is an anti-pattern An infinite loop:

Numeric Assignments Numeric Suffix Assignment Overflow Large long to int Lower bits are used No runtime error Large double to integer Brings a max int

Switch statement An alternative to if-else Better structure Before Java 1.7 When the condition is a numeric or an ordinal variable With Java 1.7 Strings are also allowed

switch example

Break Breaks the execution of a loop

Continue Stops the execution of the body of the loop and continues from the beginning of the loop Difference between continue in for and while

Nested Loops How to break or continue from outer loop?

Label

Tip of the Day: Indentation

Tip of the Day: Indentation

Comments Comments are ignored by compiler One-line comment Multiple-line comment Javadoc comments

Comment Example

String A sequence of characters Character Strings String is not a primitive type

String String in C and C++ Some functions char* and char[] \0 at the end of String Some functions strlen, strcpy, … String in java is String in java is a class not equal to char[] Constant strings “salam!” “Hellow World!”

Example

Example(2)

String methods charAt concat  plus (+) operator contains startsWith endsWith indesxOf  first index of sth lastIndexOf replace substring length split

Regular Expression Regular Expression or Regex Regex is a way to describe a set of strings Based on their common characteristics Regex can be used to search, edit, or manipulate text You must learn a specific syntax to create regex http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

Regex Examples

String and Regex

Regex Usage

Immutable String String in java is an immutable class After creating a string, you can not change it If you want to change it, you should create a new string There is no such methods for strings: setCharAt(int) setValue(String) Methods like replace and replaceAll, do not change the value They return a new String

Example What is the output of this code?

Data Hierarchy Bit Byte Character Word

Java Characters A Java character has two bytes Java supports Unicode character set standard ASCII Java uses UTF-16 encoding Other unicode encodings: UTF-8 UTF-16 Other non-unicode encodings Windows-1256

Java Special Characters Some characters are special characters Special characters are shown using backslash Examples: New line: \n Tab : \t Double-quote : \” Single-quote : \’ Backslash : \\

Java Special Characters

Array Collections of related data items related data items of the same type Arrays are fixed-length entities they remain the same length once they are created An array is a group of variables called elements containing values that all have the same type The position number of the element is it’s index Array elements are sequentially located in memory

Array

Samples char ch = array[n]; Create an array of 10 integer elements int[] array = new int[10]; int array[] = new int[10];//equal Create an array of n characters char[] characters = new char[n]; Change value of 5’th element array[5] = 12; Retrieving value of n’th element char ch = array[n];

Example

Array Creation Shortcut char[] array = new char[3]; array[0] = 'a'; array[1] = 's'; array[2] = 't'; The above code can be rewritten as: char[] array = {'a','s','t'}; Other examples: int[] numbers = {1,2,3,5,9,123}; boolean[] b = {true, true, false, true};

Multidimensional Arrays

Unbalanced Multidimensional Array

Passing Arrays to Methods

Multi-Dimensional Array Parameters int determinant(int[][] matrix){…} int [][] matrix = { {1,2}, {3,4}} ; int de = determinant(matrix); void check(int[][] array){…} int [][] unbalanced = { {1,2}, {3,4,5,6,7,8}}; check(unbalanced); boolean f(double[][][] cube){…}

Call by Element Values? No If the method has an array parameter Array elements are not copied on method invocations A reference to the array is passed to the method More about this topic  later

Exercises Write a method for sorting an array of integers Write a method that compares two arrays returns true if elements of the arrays are equal returns false , otherwise Write a method that returns determinant of a matrix Matrix is a two-dimensional array as the method parameter

References Java How to Program (9th Edition) Deitel & Deitel Thinking in Java (Fourth Edition) Bruce Eckel Java cup

Any Question