Kiến trúc máy tính và hợp ngữ - Chapter 2: An introduction to software engineering
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( );
System.out.print("Enter your last name: ");
String lastName = kboard.nextLine( );
System.out.println("Hello, " + firstName + " " + lastName);
System.out.println("Welcome to Java!");
}
}
32 trang |
Chia sẻ: huyhoang44 | Lượt xem: 615 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Kiến trúc máy tính và hợp ngữ - Chapter 2: An introduction to software engineering, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
An Introduction to Software EngineeringCopyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.Chapter 2Java MethodsObject-Oriented Programmingand Data StructuresMaria Litvin ● Gary Litvin2nd AP edition with GridWorld1Objectives:Understand the software development process, tools, and prioritiesUnderstand compilers and interpretersLearn about Java Virtual Machine, bytecodeLearn to set up and run simple console applications, GUI applications, and applets in JavaLearn basic facts about OOP2Software Today:1,580,000,000 3Software ApplicationsLarge business systemsDatabasesInternet, e-mail, etc.MilitaryEmbedded systemsScientific researchAIWord processing and other small business and personal productivity toolsGraphics / arts / digital photographyGames4Software DevelopmentEmphasis on efficiencyfast algorithmssmall program sizelimited memory useOften cryptic codeNot user-friendlyEmphasis onprogrammer’s productivityteam developmentreusability of codeeasier maintenanceportabilityBetter documentedUser-friendly1950-1970's:Now: 5Programming Languages1940 1950 1960 1970 1980 1990 2000 2010MachinecodeAssembly languagesFortranBasicPascalSchemeCC++JavaLISPSmalltalk Smalltalk-80C#LogoPythonCobolDFortressGroovyAlgolAda6Software Development ToolsEditorprogrammer writes source codeCompilertranslates the source into object code (instructions specific to a particular CPU)Linkerconverts one or several object modules into an executable programDebuggersteps through the program “in slow motion” and helps find logical mistakes (“bugs”)7The First “Bug”“(moth) in relay”Mark II Aiken Relay Calculator (Harvard University, 1945)8Compiled Languages:Edit-Compile-Link-RunEditorSourcecodeCompilerObjectcodeLinkerExecutableprogramEditorSourcecodeCompilerObjectcodeEditorSourcecodeCompilerObjectcode9Interpreted Languages:Edit-RunEditorSourcecodeInterpreter10Compiler vs. InterpreterCompiler:checks syntaxgenerates machine-code instructions not needed to run the executable programthe executable runs fasterInterpreter:checks syntaxexecutes appropriate instructions while interpreting the program statementsmust remain installed while the program is interpretedthe interpreted program is slower11Java’s Hybrid Approach:Compiler + InterpreterA Java compiler converts Java source code into instructions for the Java Virtual Machine.These instructions, called bytecode, are the same for any computer / operating system.A CPU-specific Java interpreter interprets bytecode on a particular computer.12Java’s Compiler + InterpreterEditor:7KHello.javaCompiler:Hello.classInterpreterHello,World! Interpreter13Why Bytecode?Platform-independentLoads from the Internet faster than source codeInterpreter is faster and smaller than it would be for Java sourceSource code is not revealed to end usersInterpreter performs additional security checks, screens out malicious code14JDK — Java Development KitjavacJava compilerjavaJava interpreterappletviewertests applets without a browserjavadocgenerates HTML documentation (“docs”) from sourcejarpacks classes into jar files (packages)All these are command-line tools, no GUI15JDK (cont’d)Developed by Sun Microsystems (now Oracle); free downloadAll documentation is onlineMany additional Java resources on the Internet IDEGUI front end for JDKIntegrates 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 lineUsually JDK is installed separately and an IDE is installed on top of it.17Types of ProgramsConsole applicationsGUI applicationsApplets18Console ApplicationsC:\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 ... result19Command-Line ArgumentsC:\javamethods\Ch02> javac Greetings.javaC:\javamethods\Ch02> java Greetings Josephine JaworskiHello, Josephine Jaworskipublic 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.20Command-Line Args (cont’d)Can be used in GUI applications, tooIDEs provide ways to set them (or prompt for them)Josephine Jaworski21Greetings2.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!"); }}Prompts22GUI ApplicationsMenusButtonsClickable panelSlider23HelloGui.javaimport java.awt.*;import javax.swing.*;public class HelloGui extends JFrame{ 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 libraries24HelloApplet.javaimport java.awt.*;import javax.swing.*;public class HelloApplet extends JApplet{ public void init( ) { ... } }No main in applets: the init method is called by JDK’s appletviewer or the browser25OOP —Object-Oriented ProgrammingAn 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.26OOP (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.27The 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.28InheritanceA programmer can define hierarchies of classesMore general classes are closer to the topPersonChildAdultBabyToddlerTeen29OOP BenefitsFacilitates team developmentEasier to reuse software components and write reusable softwareEasier GUI (Graphical User Interface) and multimedia programming30Review: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.31Review (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.32
Các file đính kèm theo tài liệu này:
- ch02_2731.ppt