An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data...

32
An Introduction to Software Development Java Methods Java Methods A & AB A & AB Object-Oriented Programming and Data Structures Maria Litvin Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Chapter 2

Transcript of An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data...

Page 1: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

An Introduction to Software Development

Java MethodsJava MethodsA & ABA & AB

Object-Oriented Programmingand Data Structures

Maria Litvin ● Gary Litvin

Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.

Chapter 2

Page 2: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-2

Objectives:

• Understand the software development process, tools, and priorities

• Understand compilers and interpreters

• Learn about Java Virtual Machine, bytecodes

• Learn to set up and run simple console applications, GUI applications, and applets in Java

• Learn basic facts about OOP

Page 3: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-3

Software Today:

6,460,000,000

Page 4: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-4

Software Applications

• Large business systems

• Databases

• Internet, e-mail, etc.

• Military

• Embedded systems

• Scientific research

• AI

• Word processing and other small business and personal productivity tools

• Graphics / arts / digital photography

• Games

Page 5: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-5

Software Development

• Emphasis on efficiency fast algorithms small program size limited memory use

• Often cryptic code

• Not user-friendly

• Emphasis on programmer’s

productivity team development reusability of code easier maintenance portability

• Better documented

• User-friendly

1950-1960's: Now:

Page 6: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-6

Programming Languages

1940 1950 1960 1970 1980 1990 2000

Machinecode

Assembly languages

FortranBasic

Pascal

Scheme

C C++

JavaLISP

Smalltalk Smalltalk-80

C#

Logo

Python

Page 7: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-7

Software Development Tools

• Editor programmer writes

source code

• Compiler translates the source

into object code (instructions specific to a particular CPU)

• Linker converts one or several

object modules into an executable program

• Debugger steps through the

program “in slow motion” and helps find logical mistakes (“bugs”)

Page 8: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-8

The First “Bug”

“(moth) in relay”

Mark II Aiken Relay Calculator (Harvard University, 1945)

Page 9: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-9

Compiled Languages:Edit-Compile-Link-Run

Editor Sourcecode

Compiler Objectcode

Linker Executableprogram

Editor Sourcecode

Compiler Objectcode

Editor Sourcecode

Compiler Objectcode

Page 10: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-10

Interpreted Languages:Edit-Run

Editor Sourcecode

Interpreter

Page 11: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-11

Compiler vs. Interpreter

• Compiler: checks syntax generates

machine-code instructions

not needed to run the executable program

the executable runs faster

• Interpreter: checks syntax executes appropriate

instructions while interpreting the program statements

must remain installed while the program is interpreted

the interpreted program is slower

Page 12: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-12

Java’s Hybrid Approach:Compiler + Interpreter

• A Java compiler converts Java source code into instructions for the Java Virtual Machine.

• These instructions, called bytecodes, are the same for any computer / operating system.

• A CPU-specific Java interpreter interprets bytecodes on a particular computer.

Page 13: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-13

Java’s Compiler + Interpreter

Editor

Hello.java

Compiler

Hello.class

Interpreter

Hello,World!

Interpreter

Page 14: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-14

Why Bytecodes?

• Platform-independent

• Load from the Internet faster than source code

• Interpreter is faster and smaller than it would be for Java source

• Source code is not revealed to end users

• Interpreter performs additional security checks, screens out malicious code

Page 15: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-15

JDK — Java Development Kit

• javac Java compiler

• java Java interpreter

• appletviewer tests applets without a

browser

• javadoc generates HTML

documentation (“docs”) from source

• jar packs classes into jar

files (packages)

All these are command-line tools, no GUI

Page 16: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-16

JDK (cont’d)

• Available free from Sun Microsystems

• All documentation is online:

• Many additional Java resources on the Internet

http://java.sun.com/javase/index.jsp

Page 17: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-17

Java IDE

• GUI front end for JDK

• Integrates editor, javac, java, appletviewer, debugger, other tools: specialized Java editor with syntax highlighting,

autoindent, tab setting, etc. clicking on a compiler error message takes you to

the offending source code line

• Usually JDK is installed separately and an IDE is installed on top of it.

Page 18: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-18

Types of Programs

• Console applications • GUI applications

• Applets

Page 19: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-19

Console Applications

C:\javamethods\Ch02> path=%PATH%;C:\Program Files\Java\jdk 1.5.0_07\binC:\javamethods\Ch02> javac Greetings2.javaC:\javamethods\Ch02> java Greetings2Enter your first name: JosephineEnter your last name: JaworskiHello, Josephine JaworskiPress any key to continue...

• Simple text dialog:prompt input, prompt input ... result

Page 20: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-20

Command-Line ArgumentsC:\javamethods\Ch02> javac Greetings.javaC:\javamethods\Ch02> java Greetings Josephine Jaworski

Hello, Josephine Jaworski

public class Greetings{ public static void main(String[ ] args) { String firstName = args[ 0 ]; String lastName = args[ 1 ]; System.out.println("Hello, " + firstName + " " + lastName); }}

Command-line arguments are passed to mainas an array of Strings.

Page 21: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-21

Command-Line Args (cont’d)

• Can be used in GUI applications, too

• IDEs provide ways to set them (or prompt for them)

Josephine Jaworski

Page 22: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-22

Greetings2.javaimport java.util.Scanner;

public class Greetings2{ public static void main(String[ ] args) { Scanner kboard = new Scanner(System.in); System.out.print("Enter your first name: "); String firstName = kboard.nextLine( ); System.out.print("Enter your last name: "); String lastName = kboard.nextLine( ); System.out.println("Hello, " + firstName + " " + lastName); System.out.println("Welcome to Java!"); }}

Prompts

Page 23: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-23

GUI Applications

Menus

Buttons

Clickable panel

Slider

Page 24: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-24

HelloGui.javaimport java.awt.*;import javax.swing.*;

public class HelloGui extends JFrame{ < ... other code >

public static void main(String[ ] args) { HelloGui window = new HelloGui( ); // Set this window's location and size: // upper-left corner at 300, 300; width 200, height 100 window.setBounds(300, 300, 200, 100); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); }}

GUI libraries

Page 25: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-25

HelloApplet.java

import java.awt.*;import javax.swing.*;

public class HelloApplet extends JApplet{ public void init( ) { ... } < ... other code >}

No main in applets: the init method is called by JDK’s appletviewer or the browser

Page 26: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-26

OOP —Object-Oriented Programming

• An OOP program models a world of active objects.

• An object may have its own “memory,” which may contain other objects.

• An object has a set of methods that can process messages of certain types.

Page 27: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-27

OOP (cont’d)

• A method can change the object’s state, send messages to other objects, and create new objects.

• An object belongs to a particular class, and the functionality of each object is determined by its class.

• A programmer creates an OOP application by defining classes.

Page 28: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-28

The Main OOP Concepts:

• Inheritance: a subclass extends a superclass; the objects of a subclass inherit features of the superclass and can redefine them or add new features.

• Event-driven programs: the program simulates asynchronous handling of events; methods are called automatically in response to events.

Page 29: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-29

Inheritance

• A programmer can define hierarchies of classes

• More general classes are closer to the top

Person

Child Adult

Baby Toddler Teen

Page 30: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-30

OOP Benefits

• Facilitates team development

• Easier to reuse software components and write reusable software

• Easier GUI (Graphical User Interface) and multimedia programming

Page 31: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-31

Review:

• What are some of the current software development concerns?

• What are editor, compiler, debugger used for?

• How is a compiler different from an interpreter?

• Name some of the benefits of Java’s compiler+interpreter approach.

• Define IDE.

Page 32: An Introduction to Software Development Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.

2-32

Review (cont’d):

• What is a console application?

• What are command-line arguments?

• What is a GUI application?

• What is the difference between a GUI application and an applet?

• What is OOP?

• Define inheritance.