CS 61B Data Structures and Programming Methodology. June David Sun

Similar documents
CS2141 Software Development using C/C++ C++ Basics

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSC Java Programming, Fall Java Data Types and Control Constructs

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

Introduction to Programming Using Java (98-388)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CIS133J. Working with Numbers in Java

Eng. Mohammed S. Abdualal

Programming for Engineers Iteration

Declaration and Memory

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.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

1 Shyam sir JAVA Notes

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Pace University. Fundamental Concepts of CS121 1

Lecture Set 4: More About Methods and More About Operators

CS111: PROGRAMMING LANGUAGE II

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

MODULE 02: BASIC COMPUTATION IN JAVA

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

PROGRAMMING FUNDAMENTALS

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Computer System and programming in C

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

JVM (java) compiler. A Java program is either a library of static methods (functions) or a data type definition

DM503 Programming B. Peter Schneider-Kamp.

DATA TYPES AND EXPRESSIONS

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

CS201 Some Important Definitions

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Lecture Set 4: More About Methods and More About Operators

COMP 110 Introduction to Programming. What did we discuss?

Chapter 2. Elementary Programming

Java Basic Programming Constructs

CS111: PROGRAMMING LANGUAGE II

CSEN 202 Introduction to Computer Programming

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

CS110: PROGRAMMING LANGUAGE I

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter 2 Elementary Programming

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

The Warhol Language Reference Manual

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 2: Using Data

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Array. Prepared By - Rifat Shahriyar

Zheng-Liang Lu Java Programming 45 / 79

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

Dept. of CSE, IIT KGP

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

CS1 Recitation. Week 2

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

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Important Java terminology

Reviewing all Topics this term

ECE 122 Engineering Problem Solving with Java

APCS Semester #1 Final Exam Practice Problems

Introduction to Computer Science and Object-Oriented Programming

COMP 202 Java in one week

Review of Basic Java

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Simple Java Reference

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Chapter 3: Operators, Expressions and Type Conversion

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Programming with Java

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

COMP 202. Java in one week

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Basics of Java Programming

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

Unit 2: Java in the small

Language Reference Manual simplicity

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

More Programming Constructs -- Introduction

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

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

CEN 414 Java Programming

1 Lexical Considerations

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Transcription:

CS 61B Data Structures and Programming Methodology June 25 2008 David Sun

Announcements Visit 387 Soda to arrange for after hours access to Soda Hall. Computer Science Undergraduate Association event: Thursday June 26, 310 Soda Intro to UNIX at 4 and 6 PM CS61[ABC] Survival Guide, 5 PM

static methods

Today Primitive types Control of flow Arrays

Primitive Types

Type Java is a strongly typed language every variable must have a declared type A Type can be a class (predefined or user created) a primitive type.

Integer types Numbers without fractional parts. Number of bytes Range of values byte 1 128 to 127 short 2 32,768 to 32,786 int 4 2147483648 to 2147483647 long 8 9,223,372,036,854,775,808 to 9223372036854775807 Integer types do not depend on the machine on which you will be running the Java code.

Floating point types Numbers with fractional parts float: 32 bytes, e.g., 43.2f double: 64 bytes, e.g., 23.5 Double.NaN (not a number) To indicate errors (0/0 or square root of a negative number). to check if x is NaN : if (x == Double.NaN) //wrong Double.isNaN(x) //correct

Boolean and Char boolean Two possible values: true or false You cannot convert between integers and boolean values. char a : a character a : a string with a single character content

Differences Between Object and Primitive Types Object type Primitive type Variable contains a Reference Value How defined? Class definition Built into Java How created? new 6, 3.4, true, a How initialized? Constructor Default (usually zero) How used? Methods Operators

Operators Operations on int, long, short and byte types: + - * / If x and y are integers: x/y rounds toward zero (drops the remainder). x%y calculates the remainder of x/y. If either x or y are floating point numbers: x/y is floating point division (rounds off after a certain number of digits). Integer division by 0 raises a run time exception. Floating point division by 0 yields an infinite or NaN result.

More Operators Increment and Decrement operators x = x + 1; x++; x = x 1; x--; More operators in java.lang The Math class: double x = Math.pow(x, a) The Integer class. int x = Integer.parseInt( 1984 ); The Double class: double d = Double.parseDouble( 2.4 );

Boolean Operators && (and) (or)! (not) a b a && b a b!a true true true true false true false false true false true false true true false false false true boolean x = 2 == 4; x = 5.2 <= 5.2; x = 2!= 3 1; x = false == (2 == 0); x is false x is true x is false x is true

Type Conversion char Conversion without loose of precision Loosy conversion byte short int long float double int n = 123456789; float f = n; //f is 1.23456792E8

Type Conversion (cont.) Automatic casting If one operand is double, the other will be autocasted to double If one operand is float, the other will be autocasted to float If one operand is long, the other will be autocasted to long Manual casting double n = 9.997; int x = n; //compile time error int x = (int) n; //x is now 9. int y = (int) Math.round(x); //x is now 10

Flow of control

Conditional Statement if (condition) statements; if (condition) statement1; else statement2; Examples

switch Long chains of if-then-else can be simplified with a switch statement. Example. Case labels must be integers. You cannot test strings. String input = ; switch (input) //ERROR { case A : //ERROR break; }

while Loops while (condition) statement Examples

do-while Loops Similar while loops except the loop body is executed at least once. do { s = keybd.readline(); process(s); } while (s.length() > 0);

for loops Loop controlled by a counter or similar variable that s updated after every iteration. for(initialize; test; update;) statement; initialize; while (test) { statement; next; }

Breaking Control Flow break: breaks from the inner most loop enclosing the break and continues executing at the code after the loop. public void amethod() { while(true) { do_something(); if (condition) break; } do_other_things(); }

Breaking Control Flow return : causes the method to end immediately and return to the calling method. public void amethod() { while(true) { do_something(); if (condition) return; } }

Breaking Control Flow continue : transfers control to the header of the inner most enclosing loop: public void amethod() { while(true) { do_something(); if (condition) continue; do_something(); } }

for and while loops Revisited int i = 0; while (i < 10) { } if (condition(i)) continue; call(i); i++; for (int i = 0; i < 10; i++) { if (condition(i)) continue; call(i); } What s the difference?

More on Loops do { String s = keybd.readline(); process(s); } while (s.length() > 0); String s = keybd.readline(); while (s.length() > 0) { process(s); s = keybd.readline(); } while (true) { String s = keybd.readline(); if (s.length() < 0) break; process(s); }

Arrays

Arrays Data structure that stores a collection of variables of the same type. Individual variables are accessed through an integer index. The variables is always indexed from 0 in increments of one.

Declaring Array Declare an array variable by specifying an array type: char[] c; c = new char[4]; c[0] = 'b'; // Store the character 'b' at //index 0. c[1] = 'l'; c[2] = 'u'; c[3] = 'e'; What if you access element 4? Once an array is created its size remains constant. The length of an array is stored in the field c.length.

Array Example

Array Initializers Shorthand to create an array and supply initial value to the array: int [] smallprimes = { 2, 3, 5, 7, 11, 13}; smallprimes = new int [] {17,19,23, 29,31, 37}; int [] anonymous = [] {17,19,23, 29,31, 37}; smallprimes = anonymous;

Array Copying If you copy one array variable into another, both variables refer to the same array System.arraycopy( ) copies all the values from one array into another.

Command Line Parameters The String[] args of the main method receives the string arguments specified on the command line. Example

Multi Dimensional Arrays Array of references to arrays Used for tables or complex arrangements. Rectangular Arrays: double [][] table = new double[7][7]; int[][] magicsquare = { {16, 3, 2, 13}, {5, 10, 11, 8}, {9, 6, 7, 12}, {4, 15, 14, 1} }

Triangular Arrays Pascal s Triangle Each entry is the sum of the two nearest entries in the row immediate above. Binomial Coefficients: coefficients of the polynomial (x + 1) ^ i E.g. (x + 1) ^ 4 = 1x^4 + 4x^3 + 6x^2 + 4x + 1 Example

Arrays of Objects Java does not create the objects automatically when you create an array of objects. The array contains space for references to the objects. Human[] h = new Human[5]; h[0] = new Human(12, Wendy ); h[1] = new Human(11, David );

Next Class Reading: Head First Java Chapter 10 Next Class: Lists and Recursion JUnit