Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Similar documents
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

JAVA OPERATORS GENERAL

Java Basic Programming Constructs

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Object-Oriented Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Prof. Navrati Saxena TA: Rochak Sachan

Basics of Java Programming

JAVA Programming Fundamentals

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

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

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CSC 1214: Object-Oriented Programming

Lecture Set 4: More About Methods and More About Operators

SECTION II: LANGUAGE BASICS

Getting started with Java

Lecture Set 4: More About Methods and More About Operators

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

Operators in java Operator operands.

Chapter 3: Operators, Expressions and Type Conversion

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CT 229. Java Syntax 26/09/2006 CT229

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Operators. Java operators are classified into three categories:

Informatics Ingeniería en Electrónica y Automática Industrial

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

CGS 3066: Spring 2015 JavaScript Reference

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

Zheng-Liang Lu Java Programming 45 / 79

Programming. Syntax and Semantics

Chapter 2: Using Data

Java Notes. 10th ICSE. Saravanan Ganesh

Java Programming Language. 0 A history

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

The Java language has a wide variety of modifiers, including the following:

Lexical Considerations

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

More Programming Constructs -- Introduction

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Index COPYRIGHTED MATERIAL

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

CS 106 Introduction to Computer Science I

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Programming in C++ 5. Integral data types

Language Fundamentals Summary

COMP6700/2140 Operators, Expressions, Statements

CMPT 125: Lecture 3 Data and Expressions

Lexical Considerations

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

ISA 563 : Fundamentals of Systems Programming

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

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

Learning the Java Language. 2.1 Object-Oriented Programming

A variable is a name that represents a value. For

DEPARTMENT OF MATHS, MJ COLLEGE

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

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

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Course Outline. Introduction to java

Introduction to Programming Using Java (98-388)

Java Identifiers, Data Types & Variables

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Expressions & Flow Control

CS 231 Data Structures and Algorithms, Fall 2016

LECTURE 3 C++ Basics Part 2

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

4 Programming Fundamentals. Introduction to Programming 1 1

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

1007 Imperative Programming Part II

1 Lexical Considerations

13 th Windsor Regional Secondary School Computer Programming Competition

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Quick Reference Guide

Declaration and Memory

Lexical Structure (Chapter 3, JLS)

Java+- Language Reference Manual

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

Building Java Programs

3. Java - Language Constructs I

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

A flow chart is a graphical or symbolic representation of a process.

Programming with Java

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Computer Components. Software{ User Programs. Operating System. Hardware

Building Java Programs

Full file at

Computational Expression

PROGRAMMING FUNDAMENTALS

1. Introduction to Java for JAS

Transcription:

Electrical and Computer Engineering Object-Oriented Topic 2: Fundamental Structures in Java Maj Joel Young Joel.Young@afit.edu 8-Sep-03 Maj Joel Young

Java Identifiers Identifiers Used to name local variables Names of attributes Names of classes Primitive Data Types Available in Java (size in bytes) byte (1), -128 to 127 short (2), -32768 to 32767 int (4), -2147483648 to 2147483647 long (8), -9223372036854775808 to 9223372036854775807 float (4), -3.4E38 to 3.4E38, 7 digit precision double (8), -1.7E308 to 1.7E308, 17 digits precision char (2), unicode characters boolean (, ), discrete values 2

Java Identifiers Naming Rules Must start with a letter After first letter, can consist of letters, digits (0,1,,9) The underscore _ and the dollar sign $ are considered letters Variables All variables must be declared in Java Can be declared almost anywhere (scope rules apply) Variables have default initialization values Integers: 0 Reals: 0.0 Boolean: False Variables can be initialized in the declaration 3

Java Identifiers Example Declarations int speed; // integer, defaults to 0 int speed = 100; // integer, init to 100 long distance = 3000000000L; // L needed for a long float delta = 25.67f; // f needed for a float double delta = 25.67; // Defaults to double double bigdelta = 67.8E200d; // d is optional here boolean status; // defaults to boolean status = ; Potential Problems (for the C/C++ crew) long double delta = 3.67E204; // No long double in Java unsigned int = 4025890231; // No unsigned ints in Java 4

Java Types Arrays int[] numbers = new int[n] // Array of integers, size is n size can be computed at run time, but can't be changed allocated on heap (thus enabling run time size allocation) invalid array accesses detected at run time (e.g. numbers[6];) numbers.length; // read only variable specifying length of array reference semantics int[] winning_numbers; winning_numbers = numbers; // refer to same array numbers[0] = 13; // changes both 5

Java Types Strings String message = "Error " + errnum; strings are immutable can't be changed, although variables can be changed (and old string left for garbage collection) message = "Next error " + errnum2; use StringBuffer to edit strings StringBuffer buf = new StringBuffer(greeting); buf.setcharat( 4, '?'); greeting = buf.tostring(); 6

Java Types Strings String comparison if (greeting == "hello" ). // error, compares location only if ( greeting.equals("hello")). // OK string1.compareto(string2) // negative if string1 < string 2; // zero when equal, // positive if string1 > string2 string1.substring(2, 6); // return substring between position 2 and 5 7

Java Operators Operators/Order of Precedence Operator () or [ ] or. or ++ or - - + or or ~ * or / or % + or - <<,>> & ^ Parens, array indices, object invocation, increment, decrement Unary plus, unary minus, complement Multiplication, division, modulus Addition, subtraction Bitwise shifts Bitwise AND Bitwise XOR Bitwise OR Operation Precedence 1 2 3 4 5 6 7 8 8

Java Operators Example Usage int anint1 = 2, anint2 = 1, anint3; double adbl1; anint3 = anint1 + anint2 * 4; // anint3 gets 6 anint3 = (anint1 + anint2) * 4; // anint3 gets 12 anint3 = ++anint2 * 2; // anint3 gets 4 anint2 = 1; anint3 = anint2++ * 2; // anint3 gets 2 anint2 = 1; anint3 = anint1 & anint2; // anint3 gets 0 anint3 = anint1 ^ anint2; // anint3 gets 3 anint3 = anint2 << 1; // anint3 gets 2 anint3 = 1 / 2; // anint3 gets 0 adbl1 = 1 / 2; // adbl1 gets 0.0 adbl1 = 1.0 / 2.0; // adbl1 gets 0.5 9

Java Statements Import Statements (discussed later) Class/Interface Declarations (discussed later) Assignments Conditionals Loops Miscellaneous (mixed in other discussions) 10

Java Statements Assignments General Format: variable = expression ; Where variable is a previously declared identifier and expression is a valid combo of identifiers, operators, and method (a.k.a. procedure or function) calls Shortcuts: var *= expr ; // Equivalent to var = var * (expr); var /= expr ; // Equivalent to var = var / (expr); var += expr ; // Equivalent to var = var + (expr); var -= expr ; // Equivalent to var = var (expr); var %= expr ; // Equivalent to var = var % (expr); var++; // Equivalent to var = var + 1; var--; // Equivalent to var = var - 1; 11

Java Conditional Constructs if Statements if with code block if (boolean_expr) if with single statement if (boolean_expr) statement; 12

Java Conditional Constructs if Statements (Continued) if-else if (boolean_expr) for else for 13

Java Conditional Constructs Boolean Expressions Boolean expressions use conditional operators such that they result in a value of or Conditional Operators (Not by order of precedence) Operator == or!= > or < >= or <=! & or && or Operation Equality, not equal Greater than, less than Greater than or equal, less than or equal Unary negation (NOT) Evaluation AND, short circuit AND Evaluation OR, short circuit OR 14

Java Conditional Constructs Boolean Expression Examples int i1 = 1, i2 = 2, i3 = 3, i4 = 0; boolean b1 =, b2 =, b3 = ; i2 > i1 // (i3 i2) > i1 // (i3 i2) >= i1 // (i2 > i1) & (i2 < i3) // (i2 < i1) (i2 > i1) // i2!= i1 // (i1 < i2) ((i1/i4) > 1) // Divide by 0 exception (i1 < i2) ((i1/i4) > 1) // (i1 < i2) (i1++ > 1) //, i1 contains 2 (i1 < i2) (i1++ > 1) //, i1 contains 1 b1 && b2 && b3 // (b1 b2) && b3 // b1 && (i1 == (i3 i2)) // 15

Java Conditional Constructs if-else Statement Example class Example static public void main(string args[]) // A very contrived example int i1 = 1, i2 = 2; System.out.print( Result: ); if (i1 > i2) System.out.println( i1 > i2 ); else System.out.println( i2 >= i1 ); 16

Java Conditional Constructs The Switch Statement switch (integer_expression) case int_value_1: break; case int_value_2: break; case int_value_n: break; default: 17

Don t forget the break switch (integer_expression) case int_value_1: // No break! case int_value_2: break; case int_value_n: break; default: 18

Java Conditional Constructs Example int n = 5; switch (n) case 1: n = n + 1; break; case 5: n = n + 2; break; default: n = n 1; 19

Java Conditional Constructs Example char c = b ; int n = 0; switch (c) case a : n = n + 1; break; case b : n = n + 2; break; default: n = n 1; 20

Java Looping Constructs while loop Exit condition evaluated at top do loop Exit condition evaluated at bottom for loop Exit condition evaluated at top Includes a initialization Includes a update for each iteration 21

Java Looping Constructs while loop while (boolean_expr) do loop do while (boolean_expr) 22

Java Looping Constructs for loop for (init_stmnt; bool_expr; update_stmnt) init init update update 23

Java Looping Constructs for loop for (init_stmnt; bool_expr; update_stmnt) init init update update 24

Java Looping Constructs class Example static public void main(string args[]) int i = 0; System.out.println("while loop"); while (i < 10) System.out.println(i); i++; System.out.println("do loop"); do System.out.println(i); i--; while (i > 0); System.out.println("for loop"); for (i = 0; i < 10; i++) System.out.println(i); // End main // End Example 25

Homework Go to: http://developer.java.sun.com/developer/infodocs/ Explore the Java API Docs and Tutorials Go to: http://java.sun.com/j2se/1.4.2/docs/api/ Explore: Read the docs for the String class Read the docs for the Integer class Read the docs for the System class Enter the code from the Example class (cut and paste from pdf or ppt) and compile and run it. 26