Documentos de Académico
Documentos de Profesional
Documentos de Cultura
A & AB
Object-Oriented Programming and Data Structures
Maria Litvin Gary Litvin
Chapter 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
Software Today:
6,460,000,000
2-3
Software Applications
Large business
systems
Word processing
and other small business and personal productivity tools
Graphics / arts /
digital photography
Games
2-4
Software Development
1950-1960's: Now:
Emphasis on
efficiency
Emphasis on
Programming Languages
C
Assembly languages 1940 1950 Scheme LISP Logo 1960 Fortran Machine code Basic Smalltalk Smalltalk-80 1970 1980
C++
C# Java
1990
2000
Pascal
Python
2-6
Linker
Compiler
translates the source into object code (instructions specific to a particular CPU)
Debugger
steps through the program in slow motion and helps find logical mistakes (bugs)
2-7
(moth) in relay
Mark II Aiken Relay Calculator (Harvard University, 1945)
2-8
Editor
Compiler
2-10
Interpreter:
checks syntax generates machine-code instructions not needed to run the executable program the executable runs faster
checks syntax executes appropriate instructions while interpreting the program statements must remain installed while the program is interpreted the interpreted program is slower
2-11
Hello.java
Hello.class
Interpreter
Hello, World!
Interpreter
2-13
Why Bytecodes?
Platform-independent Load from the Internet faster than source
code
Source code is not revealed to end users Interpreter performs additional security
checks, screens out malicious code
2-14
javadoc
Java compiler
java
Java interpreter
appletviewer
jar
2-15
JDK (contd)
Available free from Sun Microsystems All documentation is online:
http://java.sun.com/javase/index.jsp
2-16
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
2-17
Types of Programs
Console
applications
GUI applications
Applets
2-18
Console Applications
Simple text dialog:
prompt p input, prompt p input ... p result C:\javamethods\Ch02> path=%PATH%;C:\Program Files\Java\jdk 1.5.0_07\bin C:\javamethods\Ch02> javac Greetings2.java C:\javamethods\Ch02> java Greetings2 Enter your first name: Josephine Enter your last name: Jaworski Hello, Josephine Jaworski Press any key to continue...
2-19
Command-Line Arguments
C:\javamethods\Ch02> javac Greetings.java C:\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 + " } } Command-line arguments are passed to main as an array of Strings. " + lastName);
2-20
Josephine Jaworski
2-21
Greetings2.java
import 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( ); Prompts System.out.print("Enter your last name: "); String lastName = kboard.nextLine( ); System.out.println("Hello, " + firstName + " " + lastName); System.out.println("Welcome to Java!"); } }
2-22
GUI Applications
Menus Clickable panel
Buttons
Slider
2-23
HelloGui.java
import java.awt.*; import javax.swing.*;
GUI libraries
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); } }
2-24
HelloApplet.java
import java.awt.*; import javax.swing.*; public class HelloApplet extends JApplet { public void init( ) No main in applets: the init { method is called by JDKs ... appletviewer or the browser } < ... other code > }
2-25
2-26
OOP (contd)
A method can change the objects state, send
messages to other objects, and create new objects.
2-27
2-28
Inheritance
A programmer can define hierarchies of
classes
2-29
OOP Benefits
Facilitates team development Easier to reuse software components and
write reusable software
2-30
Review:
What are some of the current software
development concerns?
Define IDE.
2-31
Review (contd):
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?
2-32