51
IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : [email protected]

IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : [email protected]@kaplan.edu

Embed Size (px)

Citation preview

Page 1: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

IT258 Foundation of Programming Using Java

Unit 2 Seminar :(Chapter 1 )

Instructor : Vladimir Gubanov, PhDEmail : [email protected]

Page 2: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

A reminder:

1. Our Seminars :

they will be each Thursday , from 9 PM to 10 PM EST

2. My Office Hours :

Mondays, 7PM to 8PM

Saturdays, 9AM to 10 AM

3. My email :

[email protected]

Page 3: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

Chapter One:Creating Your First Java Classes

Page 4: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Objectives

• Object-oriented programming concepts

• Learn about Java• Analyze a Java application that uses

console output• Save, compile, run, and modify a

Java application• Create a Java application using GUI

outputJava Programming, Fifth Edition

4

Page 5: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

5

Object-Oriented Programming Concepts

• Procedural programming – Sets of operations executed in sequence– Variables hold values– Procedures group operations in logical units

• Object-oriented programs – Create classes first– Create objects from classes when a

functionality of the class needs to be used– Object-oriented programming includes:

– Encapsulation– Inheritance– Polymorphism

Page 6: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

6

Understanding Objects and Classes• Objects

– Made up of attributes, methods, events

• Attributes– Characteristics that define object– Value of attributes is object’s state

• Class – Describes objects with common properties– Definition – template for objects– Instance of a class – an object to use

Page 7: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

OBJECT

• Represents something you would find in the “real-world”

• Objects can be described by their attributes(properties) and behaviors(methods)

• In Java, an object is represented by a software module, which contains a collection of related data and procedures

Object: Car

Page 8: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

PROPERTY (or field)

• Attribute which describes the object:

• In Java, a property is represented by a variable associated with the object

Property: Color Make Year VIN

Page 9: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Method: Start Accelerate Turn Left Stop

METHOD

• Behavior of the object or what the object can do

• In Java, a method is represented by a public subroutine or function related to the object

Page 10: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

State

Object: CarProperty: GasolineLevel OilLevel Speed

STATE

• The collection of an object’s property values determine its state

• The object’s state can affect its behavior

Page 11: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

VIN: 323422344

VIN: 323422345

VIN: 323422346

Class: Car

Objects:

CLASS

• A category of objects – “abstraction” • Template for the object, also called “factory”• Allows multiple objects to have common

methods and properties • An object is an “instance” of a class

Page 12: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Object: CarProperties: Color GasolineLevel

Make OilLevel Model Speed

YearMethods: Stop Turn Left Start Turn Right Accelerate

Interface

INTERFACE• The set of public methods and properties

which are available to access or manipulate the object

• An object may have many interfaces

Page 13: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

window door

roomA blueprint is the specification of aHouse. This blueprint specifies a house with room, door, and window.

CLASS : elaborate

Page 14: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

class

object

A class is the specification from which a developer generates objects

A blueprint is the specification from which a builder generates houses

Page 15: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

ENCAPSULATION

• Hides the internal workings of an object from the rest of the application.

• You may change Implementation while interface remains constant

• In Java properties and methods are used to get information and perform actions in an application that are hidden within classes:

you do not know how they are implemented , but can use then as “black box”

Page 16: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

INHERITANCE: Very Important

• Ability to reuse functionality in a class.• A base class defines the common

functionality: Subclasses inherit that functionality

and may add new functionality– i.e.: A general class of Sport Car is used to

create sub-classes of Camaro and FireBird. They have many similar (re-used) features but are different cars, because they have other properties (methods) different.

Page 17: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

17

Learning About Java

• Java– Developed by Sun Microsystems – Object-oriented language – Advantages :

• Simple, Distributed , Interpreted, Secure• Architecture neutral, Portable, Multithreaded

– Can be run on wide variety of computers – Runs on hypothetical computer known as

Java virtual machine (JVM)

Page 18: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

18

Learning About Java (continued)

• Java (continued) – Can be run on wide variety of computers – Does not execute instructions on

computer directly– Runs on hypothetical computer known

as Java virtual machine (JVM)

• Source code – Programming statements written in high-

level programming language

Page 19: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

The Java Virtual Machine – special “basic computer”

Java is platform independent. What this means is that a program written inJava should be executable in any computer with any chipset.

This is done by means of the Java Virtual Machine (JVM). The JVM interacts witha computer through bytecode.

Computer JVM Program

Page 20: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

20

Java Program Types

• Applets– Programs embedded in Web page

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

• Support character output

– GUI based applications• Menus

• Toolbars

• Dialog boxes

Page 21: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming Process:

• Write a specification for the program (what the program is supposed to do)

• Design the program • Choose algorithms and decide how data will be

stored , develop pseudocode and/or flowchart• Write the program (Java language, indeed!)• Compile the program (to lower-level language)• Execute the program• Debug the program• Maintenan the program in a workable condition

Page 22: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

22

Analyzing a Java Application That Uses Console Output

• Even simplest Java application – Involves fair amount of confusing syntax

• Goal of the first application :– Print “First Java application” on screen

Page 23: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

23

Analyzing a Java Application That Uses Console Output :

Page 24: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

24

Understanding the First Class

• Everything used within Java program must be part of a class

• 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 25: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

25

Understanding the First Class

• Requirements for identifiers– Can only contain:

• Letters

• Digits

• Underscores

• Dollar signs

– Cannot be Java reserved keyword– Cannot be true, false, or null

• Access modifier– Defines how class can be accessed

Page 26: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

26

Understanding the First Class

Page 27: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Understanding the StatementThat Prints the Output

Java Programming, Fifth Edition

27

Page 28: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Some Illigal in Java Class names

Java Programming, Fifth Edition

28

Page 29: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Understanding the First Class (continued)

Java Programming, Fifth Edition

29

Page 30: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

30

Understanding the main() Method• static

– Means method accessible and usable • Even though no objects of class exist

• void – Use in main() method header– Indicates main() method does not return

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

output

Page 31: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Another Java Program: Example.java/*/* This is a simple Java program.This is a simple Java program.

Call this file Example.javaCall this file Example.java*/*/class Example class Example {{ //A Java program begins with a call to main().//A Java program begins with a call to main().

public static void main(String[] args) public static void main(String[] args) {{System.out.println("Java drives the Web."); System.out.println("Java drives the Web."); // System.out : console output// System.out : console output} // println() displays the string} // println() displays the string

} // passed to it } // passed to it

Here: Java is case sensitive language!!

• a source file : the same name as class with .java extension

• comments(ignored by the compiler): single line and multiline

• a subroutine is called a method; main() method is the entry point

• public: access specifier ; static – can be called before an object is

created from a class ;

void - does not return a value

Change .println to Println and compile

Page 32: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Shell Code – create and use as a template for any class

Java Programming, Fifth Edition

32

Do not forget to same in the file AnyClassName.java

Page 33: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

33

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 34: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

34

Saving, Compiling, and Runningand Modifying a Java Application• Saving a Java class

– Save class in file with exactly same name and.java extension

• For public classes• Class name and filename must match exactly

• Compiling a Java class– Compile source code into bytecode– Translate bytecode into executable statements

• Using Java interpreter– Type javac First.java

Page 35: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

35

Running a Java Application

• Run application from command line– Type java First

• Shows application’s output in command window

• Class stored in folder named Java on C drive

Page 36: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

36

Running a Java Application

In order javac and java programs be able to find First.java and First.class files - set up Environmental Path variable

Page 37: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Modifying a Java Class

• Modify text file that contains existing class

• Save file with changes – Using same filename

• Compile class with javac command• Interpret class bytecode and execute

class using java command

Java Programming, Fifth Edition

37

Page 38: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

38

Creating a Java ApplicationUsing 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 39: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

39

Creating a Java ApplicationUsing GUI Output

Page 40: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

40

Correcting Errors

• First line of error message displays:– Name of file where error found– Line number– Nature of error

• Next lines identify:– Symbol – Location

• Compile-time error– Compiler detects violation of language rules – Refuses to translate class to machine code

Do not expect miracles : an error message will only approximately indicate where an error is and what it could be – no guarantee that these are 100% correct

Page 41: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

41

Correcting Errors andFinding Help (continued)• Parsing

– Process compiler uses to divide source code into meaningful portions

• Logic error– Syntax correct but produces incorrect

results when executed– Usually more difficult to find and resolve

• Java API – Also called the Java class library– Prewritten Java classes

Page 42: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

42

You Do It

• Your first application• Adding comments to a class• Modifying a class• Creating a dialog box

Page 43: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Don’t Do It

• File’s name must match name of class• Don’t confuse names parentheses, braces,

brackets, curly braces, square brackets, and angle brackets

• Don’t forget to end a block comment• Don’t forget that Java is case sensitive• End every statement with semicolon

– Do not end class or method headers with semicolon

• Recompile when making changes

Java Programming, Fifth Edition

43

Page 44: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

44

Summary

• Computer program – Set of instructions that tells a computer what to

do• Object-oriented programs

– Classes– Objects

• Java virtual machine (JVM)– Standardized hypothetical computer

• Everything in a Java program must be part of a class

Page 45: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

45

Summary (continued)

• Access modifier– Word that defines circumstances under which

class can be accessed• All Java applications must have method named main()

• Program comments – Nonexecuting statements – Add to file for documentation

• javac– Compile command

Page 46: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Java Programming, Fifth Edition

46

Summary (continued)

• java– Execute command

• JOptionPane– GUI– Provides methods for creating dialogs

Page 47: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Quiz• True or False: Object-oriented programming is a style of programming

in which sets of operations are executed one after another in sequence.

• Answer: False

• Writing ____ programs involves creating classes, creating objects from those classes, and creating applications.

• Answer: object-oriented Answer: False

• A(n) ____ of a class is an existing object of a class.

• Answer: instance

• Programming statements written in a high-level programming language are called ____.

• Answer: source code47

Page 48: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Quiz (cont.)

• Stand-alone programs are called Java ____.

• Answer: applications

• Answer: True

• True or False: Not all classes have a main() method.

• Answer: True

• Line comments start with ____.

• Answer: two forward slashes (//)

48

Page 49: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Quiz (cont.)

• True or False: In Java, if a class is public (that is, if you use the public access modifier before the class name), you must save the class in a file with exactly the same name and a .class extension.

• Answer: False and *=

• To compile a file named First.java, you type ____ First.java and then press Enter.

• Answer: javac

• To run the First application from the command line, you type ____ First.

• Answer: java49

Page 50: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

Quiz (cont.)• A(n) ____ is a GUI object resembling a window in

which you can place messages you want to display.

• Answer: dialog box

50

Page 51: IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD Email : vgubanov@kaplan.eduvgubanov@kaplan.edu

This is the end of our Unit 2 Seminar

•Any questions ?

51