©2007 · georges merx and ronald j. normanslide 1 chapter 3 the structure and syntax of java

23
©2007 · Georges Merx and Ronald J. Norman Slide 1 Chapter 3 Chapter 3 The Structure The Structure and Syntax of and Syntax of Java Java

Upload: warren-bruce

Post on 16-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 1

Chapter 3Chapter 3

The Structure and The Structure and Syntax of JavaSyntax of Java

Page 2: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 2

AgendaAgenda

• Learning the language

• Principles of object orientation

• Enumerations

Page 3: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 3

Java ResourcesJava Resources

• Extensive resources available in print and on the Internet– Visit www.java.sun.com– Find the Java Reference and Tutorial

pages– Use www.google.com to find specific

information on a topic, e.g.• “java jbutton tutorial”

– Search www.amazon.com for “Java” books

Page 4: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 4

Learning LayoutLearning Layout

Page 5: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 5

Learning ConnectionsLearning Connections

Page 6: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 6

Learning the LanguageLearning the Language

• Java programming– Syntax

• Key words, special characters, variable and method names, …

– Structure• Program navigation• Component organization• Data structures

– Semantics• Code logic ( problem solving)• Algorithms

Page 7: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 7

ApproachApproach

• Code logic is like human logic, only more rigorous– Attention to detail– Small errors confuse the

compiler errors

• Design (planning) required

• Modular approach works best– Low coupling, high cohesion

Page 8: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 8

Other Object-Oriented Other Object-Oriented Programming LanguagesProgramming Languages

• SmallTalk– Rigorously object-oriented

• C++– Extension of C

• C#, Visual Basic– Microsoft .NET languages

• Borland Delphi– Successor to Pascal

• others

Page 9: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 9

Java CompilationJava Compilation

• Source code English-like statements• Compilation conversion to bytecodes/native

code• Bytecodes interpreted/executed by Java runtime

during execution– Runtime is platform-specific

• Platform: processor/OS/Programming env• Java Platform: JVM/Java API/Java Language-IDE

Page 10: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 10

Java Primitive TypesJava Primitive Types

Keyword Description Size/Format

Integers

byte Byte-length integer 8-bit two's complement

short Short integer 16-bit two's complement

int Integer 32-bit two's complement

long Long integer 64-bit two's complement

Real numbers

float Single-precision floating point 32-bit IEEE 754

double Double-precision floating point 64-bit IEEE 754

Other types

char A single character 16-bit Unicode character

boolean A boolean value (true or false) true or falseRef. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Page 11: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 11

Java Data TypesJava Data Types

• Primitive types (8)

• Arrays

• Classes

• Interfaces

• null

Page 12: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 12

Java Syntax (1)Java Syntax (1)

Page 13: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 13

Java Syntax (2)Java Syntax (2)

• Groups of statements blocks delineated by braces ({ and })

Page 14: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 14

Class HierarchyClass Hierarchy

• Classes areorganized– Hierarchy– Interfaces

• Objects followclass organization– Abstraction– Composition– Inheritance

Page 15: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 15

Object InstantiationObject Instantiation

• Constructors are methods that are invoked automatically when creating a new object of this class– May be overloaded

Page 16: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 16

Example Class with ConstructorExample Class with Constructor

Page 17: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 17

PolymorphismPolymorphismpublic interface Publication {

public String getName(); // …

}

public class Book implements Publication { // …

}

public class HardcoverBook extends Book { // …}

public class Library { // instance variables

public static void main (String args []) { Publication myPublication = new HardcoverBook();

/* create an object of Publication type and initialize it using the constructor HardcoverBook */

System.out.println(myPublication.getname()); // remaining code statements } }

Page 18: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 18

EnumerationsEnumerations

• J2SE 5.0 introduced us to a special class type called enumerations. An enumeration is declared using the enum keyword and provides a powerful, flexible organization for constant variables and their manipulation.

• public enum FlowType { SEQUENTIAL, PARALLEL, CONCURRENT }

• FlowType ft = FlowType.PARALLEL;• Can be used inside switch statements

Page 19: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 19

Introduction to Graphical User Introduction to Graphical User Interfaces: EventsInterfaces: Events

Page 20: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 20

Graphical ControlsGraphical Controls

• Buttons, menus, windows, …– Light-weight (Swing),

implemented in Java•JButton, JRadioButton, JTextField, JFrame, …

– Variable look-and-feel: Windows, Unix, Metal, …

– Event-driven• Model-View-Controller pattern

Page 21: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 21

GUI and NetworkingGUI and Networking

• Client– Skinny (HTML/JavaScript, AJAX)

HTTP:GET, PUT– Thin (Applet)

HTTP, URLConnection (GET, PUT), Sockets

– Thick (AWT, Swing)HTTP, Sockets, RMI

• Server: – Web server with servlets (Tomcat) vs.

interpreters – Custom server

Page 22: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 22

J2SE 5.0J2SE 5.0

Page 23: ©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java

©2007 · Georges Merx and Ronald J. Norman Slide 23

Position in ProcessPosition in Process

• In this first partof coverage for the Design phase, wefocus on the design model– Development of the class

hierarchy from the use cases and UML diagrams

(1)