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

26
Mar 16, 2022 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Topic 2: Fundamental Programming Structures in Java Maj Joel Young [email protected] Maj Joel Young

description

Object-Oriented Programming Topic 2: Fundamental Programming Structures in Java. Maj Joel Young [email protected]. 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) - PowerPoint PPT Presentation

Transcript of Object-Oriented Programming Topic 2: Fundamental Programming Structures in Java

Apr 20, 2023

Air Force Institute of TechnologyElectrical and Computer Engineering

Object-Oriented Programming

Topic 2:

Fundamental Programming Structures in Java

Maj Joel [email protected]

Maj Joel Young

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 2

Object-Oriented Programming

DesignJava 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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 3

Object-Oriented Programming

DesignJava 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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 4

Object-Oriented Programming

DesignJava Identifiers

• Example Declarations

int speed; // integer, defaults to 0int speed = 100; // integer, init to 100long distance = 3000000000L; // “L” needed for a

longfloat delta = 25.67f; // “f” needed for a floatdouble delta = 25.67; // Defaults to doubledouble bigDelta = 67.8E200d; // “d” is optional hereboolean 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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 5

Object-Oriented Programming

DesignJava 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 arraynumbers[0] = 13; // changes both

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 6

Object-Oriented Programming

DesignJava Types

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 7

Object-Oriented Programming

DesignJava Types

• Strings– String comparison

if (greeting == "hello" ) …. // error, compares location only

if ( greeting.equals("hello")) …. // OKstring1.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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 8

Object-Oriented Programming

DesignJava Operators

• Operators/Order of Precedence

Operator Operation Precedence

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 9

Object-Oriented Programming

DesignJava Operators

• Example Usage

int anInt1 = 2, anInt2 = 1, anInt3;double aDbl1;

anInt3 = anInt1 + anInt2 * 4; // anInt3 gets 6

anInt3 = (anInt1 + anInt2) * 4; // anInt3 gets 12anInt3 = ++anInt2 * 2; // anInt3 gets 4anInt2 = 1;anInt3 = anInt2++ * 2; // anInt3 gets 2anInt2 = 1;anInt3 = anInt1 & anInt2; // anInt3 gets 0anInt3 = anInt1 ^ anInt2; // anInt3 gets 3anInt3 = anInt2 << 1; // anInt3 gets 2anInt3 = 1 / 2; // anInt3 gets 0aDbl1 = 1 / 2; // aDbl1 gets 0.0aDbl1 = 1.0 / 2.0; // aDbl1 gets 0.5

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 10

Object-Oriented Programming

DesignJava Statements

• Import Statements (discussed later)

• Class/Interface Declarations (discussed later)

• Assignments

• Conditionals

• Loops

• Miscellaneous (mixed in other discussions)

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 11

Object-Oriented Programming

DesignJava Statements

• Assignments– General Format: variable = expression ;

Where variable is a previously declared identifier andexpression 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;

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 12

Object-Oriented Programming

DesignJava Conditional Constructs

• “if” Statements

– if with code block

if (boolean_expr){

statements}

– if with single statement

if (boolean_expr) statement;

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 13

Object-Oriented Programming

DesignJava Conditional Constructs

• if” Statements (Continued)

– if-else

if (boolean_expr){

statements for true}else{

statements for false}

ifif

statementsstatements

falsetrue

statementsstatements

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 14

Object-Oriented Programming

DesignJava 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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 15

Object-Oriented Programming

DesignJava 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) // truei2 != 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

1b1 && b2 && b3 // false(b1 || b2) && b3 // trueb1 && (i1 == (i3 – i2)) // true

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 16

Object-Oriented Programming

DesignJava 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”); } }}

ifif

statementsstatements

falsetrue

statementsstatements

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 17

Object-Oriented Programming

DesignJava Conditional Constructs

• The Switch Statement

switch (integer_expression){ case int_value_1:

statementsbreak;

case int_value_2:statementsbreak;

… case int_value_n:

statementsbreak;

default:statements

}

ififtrue

statementsstatements

ifif

false

truestatementsstatements

ifif

false

truestatementsstatements

statementsstatements

false

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 18

Object-Oriented Programming

Design

• Don’t forget the “break”

switch (integer_expression){ case int_value_1:

statements// No break!

case int_value_2:statementsbreak;

… case int_value_n:

statementsbreak;

default:statements

}

ififtrue

statementsstatements

ifif

false

truestatementsstatements

ifif

false

truestatementsstatements

statementsstatements

false

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 19

Object-Oriented Programming

DesignJava Conditional Constructs

• Example

int n = 5;switch (n){ case 1:

n = n + 1;break;

case 5:n = n + 2;break;

default:n = n – 1;

}

ififtrue

statementsstatements

ifif

false

truestatementsstatements

statementsstatements

false

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 20

Object-Oriented Programming

DesignJava 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;

}

ififtrue

statementsstatements

ifif

false

truestatementsstatements

statementsstatements

false

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 21

Object-Oriented Programming

DesignJava 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 statements– Includes a update statements for each iteration

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 22

Object-Oriented Programming

DesignJava Looping Constructs

• while loop

while (boolean_expr){

statements}

• do loop

do{

statements}

while (boolean_expr)

ifif

statementsstatements

false

true

ifif

statementsstatements

false

true

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 23

Object-Oriented Programming

DesignJava Looping Constructs

• for loop

for (init_stmnt; bool_expr; update_stmnt){statements

}

ifif

statementsstatements

false

true

initinit

updateupdate

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 24

Object-Oriented Programming

DesignJava Looping Constructs

• for loop

for (init_stmnt; bool_expr; update_stmnt){statements

}

ifif

statementsstatements

false

true

initinit

updateupdate

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 25

Object-Oriented Programming

DesignJava 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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 20, 2023 26

Object-Oriented Programming

DesignHomework

• 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.