Top Banner
Object Oriented Systems Lecture 01 First Java Programming
23

Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

Dec 27, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

Object Oriented Systems

Lecture 01First Java Programming

Jaeki Song

Page 2: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

2

Objectives

• Learn about programming

• Be introduced to object-oriented programming concepts

• Learn about Java

• Analyze a Java application that uses console output

Page 3: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

3

What is a program?

• A computer program is a set of instructions that enable the computer to solve a problem or perform a task• Display a message on the screen: “How many

hours did you work?”

• A computer’s CPU can only process instructions that are written in machine language• A stream of binary numbers

Page 4: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

4

What is a Program made of?

• Language elements• Keywords (reserved words)

• There are words that have a special meaning in the programming language

• E.g.: public, class, static, etc

• Operators• Symbols or words that perform operation son one or more

operands• E.g.: “=“, “*”, etc

• Punctuation• Most programming languages require the use of punctuation

characters such as the beginning or ending of a statement• E.g.: semicolon in Java – similar to a period in English

Page 5: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

5

What is a Program made of?

• Programmer-defined names• Unlike key words, these are words or names

that are defined by the programmer• E.g.: names of variables

• Syntax• Rules that must be followed when writing a

program

Page 6: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

6

Programming Approaches

• Procedural programming• Divide a problem into smaller sub-problems

• Each sub-problem is analyzed and a solution for the sub-problem is obtained

• Also known as top-down design, stepwise refinement and modular programming

• Object-oriented programming• Object-oriented Design (OOD)

• Identify components called object• Specify the relevant data for each object and possible operations to

be performed on that data• In OOD, the final program is a collection of interacting objects• A programming language that implements OOD is called an OOP

language

Page 7: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

7

Introduction to Object Concepts

• Object-oriented programming differs from traditional procedural programming• Basic concepts

• Objects • Classes• Inheritance• Polymorphism

Page 8: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

8

What is Java?

• Designed in the early of 1990s by Sun Microsystems• Code name “Green”

• Used in consumer devices such as an intelligent television “set-up” boxes

• The language was designed to be simple and architecture-neutral, so that it could be executed on a variety of hardware

• Rewrite the program: architecture-neutral, real-time, reliable, and secure• Applets, now called Java provides animation and

interactivity on the World Wide Web• Web browsers have provided the opportunities to run

Java applets

• The fastest growing language

Page 9: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

9

Java Language

• Standard language used for programming, creating applets, servlets, JavaBeans, and enterprise components

• Java is simple

• Java is object-oriented language

• Java is distributed

• Java is portable

• Java is multithreaded

Page 10: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

10

Java Environment

Java source code

Java compiler

Java interpreter

Computer OS

Java virtual machine

Source code is stored on a diskIn a file with a name ending in

.java

Compiler creates byte codes that are stored on a disk a file with a

name ending in .class

JVM (named java.exe) performssecurity chekcs and translates byte codes to machine language, which executes

Page 11: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

11

Java Program Types

• Applets• Programs embedded in Web page

• Java applications• Called Java stand-alone programs• Console applications

• Support character output

• Windowed applications• Menus• Toolbars• Dialog boxes

Page 12: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

12

First Java Application• Eclipse

• Tutorial

public class First {

public static void main (String [ ] args) { System.out.println (“First Java Application”) }}

Page 13: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

13

Understanding First Class

• Literal string• Will appear in output exactly as entered• Written between double quotation marks

• Arguments• Pieces of information passed to method

• Method • Requires information to perform its task

• Define Java class using any name or identifier • Requirements for identifiers

• Must begin with:• Letter of English alphabet• Or non-English letter (such as α or π)

• Cannot begin with digit

Page 14: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

14

Understanding First Class (continued)

• Requirements for identifiers• Can only contain:

• Letters• Digits• Underscores• Dollar signs

• Cannot be Java reserved keyword (p. 10)• Cannot be true, false, or null

• Access modifier• Defines how class can be accessed

Page 15: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

15

Java Naming Conventions

• Packages• The prefix of a unique package name is always

written in all-lowercase ASCII letters and should be one of the top-level domain names

• Use dots to separate the parts• E.g.: com.sun.eng, com.objectcentral.javatools

• Classes• Class (and interface) names should be nouns

descriptive of the purpose of the class• Names are in mixed case, beginning with a capital

and with the first letter of each internal word capitalized

• Use complete words and avoid abbreviations• E.g.: Point, Shape, MovieEditor, ClientList

Page 16: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

16

Java Naming Conventions (continued)

• Methods• Methods should be verbs descriptive of the purpose of the method• Names are mixed case with the first letter lowercase and the first letter

of each internal word capitalized • There are prefix conventions for general types of methods, such as

using get and set for getters and setters• E.g.: getOrigin, findSmallest, drawGraph, saveMoney

• Variables• Except when used as constants, all variables are named using mixed

case with a lowercase first letter, and with intenral words starting with capital letters

• Use one-letter variable names only for temporary variables• E.g.: myMovie, editedMovie, backgroundColor

• Constants• Names should be all uppercase with words seperated by underscores

(“_”)• e.g.: MAX_SIZE, TERM_LIMIT

Page 17: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

17

main( ) Method

•Static• Reserved keyword • Means method accessible and usable

• Even though no objects of class exist

•void • use in main() method header• Does not indicate main() method empty• Indicates main() method does not return

value when called• Doesn’t mean main() doesn’t produce output

Page 18: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

18

Adding Comments to a Java Class

• Types of Java comments• Line comments

• Start with two forward slashes (//) • Continue to end of current line • Do not require ending symbol

• Block comments • Start with forward slash and asterisk (/*)• End with asterisk and forward slash (*/)

Page 19: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

19

Programming Errors

• Syntax error• Result from errors in cod construction

• E.g.: mistyping, omitting some necessary punctuation, using an opening brace without a corresponding closing brace

• Logical error• Occur when a program does not perform the way it

was intended to

• Run-time error• Cause a program to terminate abnormally

• E.g.• Input error: the user enters an unexpected input value that the program cannot

handle• Division by zero

Page 20: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

20

Using Java Swing Class

• Refers to the new library of GUI• A component set that makes up all the objects

of GUI• Displays output using windows or dialog boxes

• Input Dialog and Output Dialog

• Use packages• Predefined classes grouped into categories of

related classes called packages (sometimes called java class libraries or java applications programming interface (API))

• JOptionPane• Defined in a package called javax.swing

Page 21: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

21

GUI Output

•JOptionPane• Produce dialog boxes

• Dialog box • GUI object resembling window• Messages placed for display

• Package• Group of classes

•import statement• Use to access built-in Java class

Page 22: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

22

Output Dialog

• showMessageDialog ( null, “string”);• A method of class JOptionPane• Two arguments

• Syntax

JOptionPane.showMessageDialog(null, “string”);

Page 23: Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.

23

Common Errors to Avoid

• Mismatched braces, quotation marks, or parentheses

• Misspelling key words• Using capital letters in key words

• Java is a case-sensitive• All key words are written in lower case in Java

• Using a key word as a variable name• Using inconsistent spelling • Forgetting the semicolon at the end of a

statement• Not using the required import statement