Air Force Institute of Technology Electrical and Computer Engineering

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

 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Java Syntax Primitive data types Operators Control statements.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types CSE 115 Spring 2006 April 3 &
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
DAT602 Database Application Development Lecture 5 JAVA Review.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Introduction to Java Java Translation Program Structure
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Reference Learning Java DDC publishing Herst, Yamauchi, Adler COP 3331 Object Oriented Analysis and Design Java Part I.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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: รัฐภูมิ เถื่อนถนอม
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.
Information and Computer Sciences University of Hawaii, Manoa
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Java Variables and Types
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Lecture 2: Data Types, Variables, Operators, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Object-Oriented Programming
Introduction to Programming in Java
Lecture 5 from (Chapter 4, pages 73 to 96)
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2: Basic Elements of Java
Expressions and Assignment
Control Structure Chapter 3.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
Module 2 Variables, Data Types and Arithmetic
Just Enough Java 17-May-19.
Control Structure.
Presentation transcript:

Maj Joel Young Joel.Young@afit.edu Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Topic 2: Fundamental Programming Structures in Java Maj Joel Young Joel.Young@afit.edu 10-Apr-17 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 (true, false), discrete values

Java Identifiers Naming Rules Variables 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

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 “false” boolean status = true; 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

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

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();

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

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

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

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;

Java Conditional Constructs “if” Statements if with code block if (boolean_expr) { statements } if with single statement statement;

Java Conditional Constructs if” Statements (Continued) if-else if (boolean_expr) { statements for true } else statements for false if statements false true

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

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

Java Conditional Constructs if-else” Statement Example if statements false true 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”);

Java Conditional Constructs The Switch Statement switch (integer_expression) { case int_value_1: statements break; case int_value_2: … case int_value_n: default: } if true statements false

Don’t forget the “break” switch (integer_expression) { case int_value_1: statements // No break! case int_value_2: break; … case int_value_n: default: } if true statements false

Java Conditional Constructs Example int n = 5; switch (n) { case 1: n = n + 1; break; case 5: n = n + 2; default: n = n – 1; } if true statements false

Java Conditional Constructs Example char c = ‘b’; int n = 0; switch (c) { case ‘a’: n = n + 1; break; case ‘b’: n = n + 2; default: n = n – 1; } if true statements false

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

Java Looping Constructs while loop while (boolean_expr) { statements } do loop do if statements false true if statements false true

Java Looping Constructs for loop for (init_stmnt; bool_expr; update_stmnt) { statements } if statements false true init update

Java Looping Constructs for loop for (init_stmnt; bool_expr; update_stmnt) { statements } if statements false true init update

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 i--; while (i > 0); System.out.println("for loop"); for (i = 0; i < 10; i++) } // End main } // End Example

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.