JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 8 Arrays and Strings
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
CIS Computer Programming Logic
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
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.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
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.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
August 6, 2009 Data Types, Variables, and Arrays.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
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.
CS-I MidTerm Review Hao Jiang Computer Science Department Boston College.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CPS120: Introduction to Computer Science Variables and Constants.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Object Oriented Programming Lecture 2: BallWorld.
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 Primer 1: Types, Classes and Operators
Object Oriented Programming
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Type Conversion, Constants, and the String Object
Expressions and Assignment
Java Programming Review 1
Problem 1 Given n, calculate 2n
Presentation transcript:

JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University

Primitive data types and expressions Data Type?? – int, double, boolean, char

Primitive DataTypes in JAVA

Operators Expressions Precedence – * and / ( and %) have higher precedence than (are applied before) the + and - operators; – ! is the highest precedence, followed by && and then ||. – use parentheses to override these rules

Type Conversion Numbers are automatically promoted to a more inclusive type if no information is lost. – , the 1 is promoted to the double value 1.0 Cast: Type name in parentheses – (int) 3.7  3, (double) 3 is 3.0.

Comparisons The following operators compare two values of the same type and produce a boolean value – equal (==), not equal (!=), less than ( ), and greater than or equal (>=).

Other Primitives Java int has 2 32 (many machines have 64-bit words nowadays, but the 32-bit int persists) Double has 64 bit 64-bit integers, with arithmetic operations (long) 16-bit integers, with arithmetic operations (short) 16-bit characters, with arithmetic operations (char) 8-bit integers, with arithmetic operations (byte) 32-bit single-precision real numbers, again with arithmetic operations (float)

Statements controlling the flow of execution, often in curly braces, – Decleratoions: – Assingments – Conditionals – Loops – Calls and returns

Declerations A declaration statement associates a variable name with a type at com- pile time. Java is said to be a strongly typed language, because the Java compiler checks for consistency (for example, it does not permit us to multiply a boolean and a double)

Assignments When we write c = a + b in Java, we are not expressing mathematical equality, but are instead expressing an action: – set the value of the variable c to be the value of a plus the value of b. – The left-hand side of an as- signment statement must be a single variable; the right-hand side can be an arbitrary expression that produces a value of the type.

Conditionals Most computations require different actions for different inputs. One way to express these differences in Java is the if statement: if ( ) { } if ( ) { } else { }

Loops Many computations are inherently repetitive – while ( ) { } Break and continue. – The break statement, which immediately exits the loop – The continue statement, which immediately begins the next iteration of the loop

Shortcut Notations Initializing declarations. – int i = 1; Implicit assignments. – i++ i— – The code ++i and --i are the same except that the expression value is taken after the increment/ decrement, not before. – i/=2  i = i/2

Shortcut Notations (Cont.) Single-statement blocks. – Curly braces can be omitted For notation. – for ( ; ; ) { }

Arrays An array stores a sequence of values that are all of the same type. – indexing for N value indexing 0 to N-1 – a[i] i th value Creating and initializing an array. – Declare the array name and type. – Create the array. Specify the length – Initialize the array values.

Using an array Once we create an array, its size is fixed. A program can refer to the length of an array a[] with the code a.length. – Java does automatic bounds checking – ArrayOutOfBoundsException – Note carefully that an array name refers to the whole array if we assign one array name to another, then both refer to the same array,

Two-dimensional arrays A two-dimensional array in Java is an array of one-dimensional arrays. A two-dimensional array may be ragged (its arrays may all be of differing lengths), but we most often work with (for appropriate parameters M and N) M-by-N two-dimensional arrays that are arrays of M rows, each an array of length N (so it also makes sense to refer to the array as having N columns) two-dimensional array a[][], we use the notation a[i][j]

Two-Dimentional Array double[][] a = new double[M][N]; double[][] a; a = new double[M][N]; for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) a[i][j] = 0.0;

Static methods Static methods are called functions in many programming languages, since they can behave like mathematical functions,

Defining a static method A method encapsulates a computation that is defined as a sequence of statements. A method takes arguments (values of given data types) and computes a return value of some data type that depends upon the arguments (such as a value defined by a mathematical function) or causes a side effect that depends on the arguments (such as printing a value)

External libraries. java.lang.*

Strings A String is a sequence of characters (char values). A literal String is a sequence of characters with in double quotes, such as "Hello, World” not a primitive type.

Concatenation Conversion

Automatic conversion "The square root of 2.0 is " + Math.sqrt(2.0)

QUESTIONS Sorting three numbers OF SAME TYPE

DATA ABSTRACTION So far, we have discussed in detail Java’s primitive data types: more convenient to write programs at a higher level of abstraction Programming in Java is largely based on building data types known as reference types with the familiar Java class This style of programming is known as object- oriented programming, as it revolves around the concept of an object, an entity that holds a data type value.

With primitive  numbers With reference type  on strings, pictures, sounds etc.. An abstract data type (ADT) is a data type whose representation is hidden from the client.

Using abstract data types You do not need to know how a data type is implemented in order to be able to use it,