SSD1: Introduction to Information SSD1: Introduction to Information Systems Systems Unit 2: Introduction Unit 2: Introduction to Java and Object- to Java and Object- Oriented Programming Oriented Programming Shen Zhidong [email protected]ISS, Wuhan University
SSD1: Introduction to Information Systems. Unit 2: Introduction to Java and Object-Oriented Programming. Shen Zhidong [email protected] ISS, Wuhan University. 提 纲. 程序设计语言概述 JAVA 简介 Programming with Java. 程序设计语言. 软件的基本组成部分是完成其功能的程序。 程序是指按照时间顺序依次安排的工作步骤。而程序设计则是对这些步骤的编排和优化。 - PowerPoint PPT Presentation
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
SSD1: Introduction to Information SystemsSSD1: Introduction to Information Systems
Unit 2: Introduction to Java and Unit 2: Introduction to Java and Object-Oriented Programming Object-Oriented Programming
James Gosling:James Gosling: Green Team original, FirstPerson employee, original member Java Products Group, lead engineer and keyarchitect of Java technology
Bill Joy:Bill Joy: Cofounder and vice president, Sun Microsystems, Inc., principal designer of the University of California, Berkeley, version of the UNIX operating system
Eric Schmidt:Eric Schmidt: Former Sun Microsystems chief technical officer, Former Novell chairman and CEO, Chairman of the Board and CEO, Google Inc.
JavaJava 可以做什么?可以做什么?1.1. 基本基本 :: Objects, strings, threads, numbers, input and output, Objects, strings, threads, numbers, input and output,
data structures, system properties, date and timedata structures, system properties, date and time 等 。等 。2.2. AppletsApplets 应用应用,嵌入,嵌入 HTMLHTML 文档。文档。3.3. 网络应用网络应用 :: 实现基于实现基于 URL/TCP/UDPURL/TCP/UDP 的网络应用。的网络应用。4.4. 安全应用安全应用 : : 电子签名、公钥和私钥管理、访问控制和用电子签名、公钥和私钥管理、访问控制和用户证书等。户证书等。
4.4. 开发程序更快 开发程序更快 (Develop programs more quickly) (Develop programs more quickly) 5.5. 避免平台依赖性避免平台依赖性 (Avoid platform dependencies with 100% P(Avoid platform dependencies with 100% P
ure Java)ure Java)6.6. 编写一次,随处运行编写一次,随处运行 (Write once, run anywhere) machine-i(Write once, run anywhere) machine-i
ndependent bytecodesndependent bytecodes7.7. 发布软件更容易 发布软件更容易 (Distribute software more easily): (Distribute software more easily): 指对软指对软
public class Program{ public static void main(String[] arg){
System.out.println(“Welcome to Java!”); }}
2.1 Programming with Java2.1 Programming with Java
Programming with ObjectsProgramming with Objects
Java Program DevelopmentJava Program Development
First Look at JavaFirst Look at Java
Element of a Java ServletElement of a Java Servlet
Planning Servlet DevelopmentPlanning Servlet Development
Guidelines for Java DevelopmentGuidelines for Java Development
2.1.1 2.1.1 Programming with ObjectsProgramming with Objects
Programming with JavaProgramming with Java
Fundamentals of Object-Oriented Fundamentals of Object-Oriented ProgrammingProgramming
Fundamentals of JavaFundamentals of Java
参考教材内容参考教材内容
《《 Introduction to Programming UsinIntroduction to Programming Using Java: An Object-Oriented Approach g Java: An Object-Oriented Approach 2nd Edition2nd Edition 》的所需章节:》的所需章节:第一章第一章 , 1.1–1.3, 1.1–1.3 节节 ..
ProgramsProgramsProgramsPrograms are texts that can make a computer do a are texts that can make a computer do a task.task.
Programs are written in a specialized language, Programs are written in a specialized language, called a called a programming languageprogramming language..
The content of a program is called The content of a program is called codecode..
When a computer carries out or runs a program we When a computer carries out or runs a program we say that it say that it executesexecutes the code. the code.
Java is one of the hundreds of programming Java is one of the hundreds of programming languages. It’s a languages. It’s a object-oriented object-oriented language.language.
Programs ExamplePrograms Example
public class Program{public class Program{
public static void main(String[] arg){public static void main(String[] arg){
System.out.println(“Welcome to JSystem.out.println(“Welcome to Java!”);ava!”);
}}
}}
Programs and ModelsPrograms and Models (程序与模型)(程序与模型)
A A modelmodel is a simplified representation. It includes is a simplified representation. It includes features that are considered important to its user features that are considered important to its user while neglecting others.while neglecting others.
Every model shares following Every model shares following characteristicscharacteristics ElementsElements of the model of the model representrepresent other, more complex t other, more complex t
hings.hings. These model elements exhibit consistent These model elements exhibit consistent behaviorbehavior.. The model elements can be grouped into different The model elements can be grouped into different catecate
goriesgories based on their common behaviors. based on their common behaviors. ActionAction external to a model element cause the behavior external to a model element cause the behavior
of the model element.of the model element.
Example 1Example 1
假设有一个软件公司。该公司有一个总假设有一个软件公司。该公司有一个总裁(裁( presidentpresident )和多个副总)和多个副总 (vice presi(vice president)dent) 。每个副总管理一些项目经理。每个副总管理一些项目经理 (pr(project manager)oject manager) 。每个项目经理手下有。每个项目经理手下有很多程序员很多程序员 (Programmer)(Programmer) 。。How to model it ?How to model it ?
行为(行为( BehaviorBehavior )) 多个对象可能具有相同的行为。多个对象可能具有相同的行为。如不同的程序员在不同的项目里面做不同的事情,如不同的程序员在不同的项目里面做不同的事情,但是他们具有一些但是他们具有一些相同的行为(相同的行为( common behaviocommon behaviorr )):编写代码、向合作的同事索要项目需要的数:编写代码、向合作的同事索要项目需要的数据、生成项目报告等。据、生成项目报告等。
面向对象中的基本概念面向对象中的基本概念
消息(消息( MessageMessage )) Each message sent must specify which object is to reEach message sent must specify which object is to re
ceive it, what task that object should perform in resceive it, what task that object should perform in response, and further details that must be supplied to dponse, and further details that must be supplied to describe the task adequately.escribe the task adequately.消息在不同的对象之间建立连接,使得多个对象彼此之间消息在不同的对象之间建立连接,使得多个对象彼此之间可以互动,即对象通过彼此间互通消息来形成互动。可以互动,即对象通过彼此间互通消息来形成互动。
面向对象中的基本概念面向对象中的基本概念JavaJava 程序(程序( Java ProgramJava Program ))
A collection of objects that correspond to the importaA collection of objects that correspond to the important problem elements of the problem being solved or tnt problem elements of the problem being solved or the computation being performed.he computation being performed.
面向对象中的基本概念面向对象中的基本概念类(类( ClassClass )) A category of model elements is called a class in Java.A category of model elements is called a class in Java.
( ( JavaJava 中某一类别模型元素称为中某一类别模型元素称为类类 classclass )) The fundamental job of a Java programmer is proviThe fundamental job of a Java programmer is provi
ding class definitions, or descriptions of how objects ding class definitions, or descriptions of how objects in each class must behavior.in each class must behavior. (( JavaJava 程序员的基本程序员的基本工作就是定义类,或描述每个类的对象的行为)工作就是定义类,或描述每个类的对象的行为)
Object-Oriented ProgramsObject-Oriented ProgramsJava program typically consists of a group Java program typically consists of a group of objects that communicate by sending meof objects that communicate by sending messages. ssages. JavaJava 程序由一组对象组成,对象间通过消程序由一组对象组成,对象间通过消息来通信。 息来通信。
When the program runs on a computer, it cWhen the program runs on a computer, it creates objects from class definitions providreates objects from class definitions provided by the Java programmer. ed by the Java programmer. JavaJava 程序在计算机中运行时,它将根据程程序在计算机中运行时,它将根据程
序员定义的类来创建对象。序员定义的类来创建对象。
Object-Oriented ProgramsObject-Oriented ProgramsA A class definitionclass definition for a class is for a class is a a templatetemplate f for creating objects of that class. or creating objects of that class. 一个类的定义情况将被作为创建该类的对一个类的定义情况将被作为创建该类的对象实例的模板象实例的模板 (template)(template)
It describes how objects of that class must It describes how objects of that class must behavebehave in terms of the types of messages th in terms of the types of messages they can receive and how they respond to thoey can receive and how they respond to those messages. se messages. 类的定义描述了类的对象将会如何对其收类的定义描述了类的对象将会如何对其收
到的消息产生相应的行为和反应。到的消息产生相应的行为和反应。
Example 2Example 2某个公司有一个任务需要完成,该任务某个公司有一个任务需要完成,该任务是要完成一个项目提案是要完成一个项目提案This task involves the This task involves the Vice PresidentVice President of a d of a department and the epartment and the Project ManagerProject Manager of one of one of the many project teams the Vice Presideof the many project teams the Vice President manages. nt manages.
How to model it ?How to model it ?
Example 2Example 2Need two objects to model themNeed two objects to model them an object that models the an object that models the Vice PresidentVice President another that models the another that models the Project Manager.Project Manager.
If the two objects have similar attributes and exhibit the same If the two objects have similar attributes and exhibit the same behavior, the program would need only one class definition tbehavior, the program would need only one class definition to create the two objects. o create the two objects. If the two objects have different attributes and exhibit differeIf the two objects have different attributes and exhibit different behavior, the program would need two class definitions to nt behavior, the program would need two class definitions to create the two objects. create the two objects. 如果两个对象在一个事务中具有相似的属性和相同的行如果两个对象在一个事务中具有相似的属性和相同的行为,那么可以把它们定义为同一个类的对象;否则将分为,那么可以把它们定义为同一个类的对象;否则将分别定义两个类来创建他们。别定义两个类来创建他们。
——the program will need two classes to model them.
Example 2Example 2
The Vice President The Vice President requestsrequests the Project the Project Manager to write Manager to write a proposala proposal for a project. for a project.
In response to this In response to this messagemessage, the Project , the Project Manager starts Manager starts writingwriting the proposal and the proposal and realizes that realizes that financial datafinancial data is needed from is needed from the Vice President. the Vice President.
Example 2Example 2
The Project Manager The Project Manager asksasks the Vice the Vice President for financial data. President for financial data.
In response to this message, the Vice In response to this message, the Vice President President sendssends the the financial datafinancial data to the to the Project Manager. Project Manager.
The Project Manager The Project Manager completescompletes the the proposal with the financial data and other proposal with the financial data and other necessary information. necessary information.
Example 2Example 2AttributesAttributes for the Vice President object: for the Vice President object: Name Name Department Department
AttributesAttributes for the Project Manager object: for the Project Manager object: Name Name Team Team
Example 2Example 2Message(s)Message(s) from the Vice President object that from the Vice President object that the Project Manager object must respond to : the Project Manager object must respond to : Write project proposal Write project proposal
Message(s)Message(s) from the Project Manager object th from the Project Manager object that the Vice President object must respond to: at the Vice President object must respond to: Provide financial data Provide financial data
Example 2Example 2
VicePresident
-name-department
+provideFinacialData()
ProjectManager
-name-team
+writeProjectProposal()
Classes to model Vice President and Project Manager
Fish Simulation:Fish Simulation: An OO Program that An OO Program that Simulates Aquatic LifeSimulates Aquatic Life
Fish Fish Simulation SystemSimulation System models aquatic models aquatic life in a laklife in a lake and to see how the movement of catfish and crocoe and to see how the movement of catfish and crocodiles and the growth of algae are affected by the sizdiles and the growth of algae are affected by the size and density of the populations. e and density of the populations.
For now, the organisms being modeled include For now, the organisms being modeled include catfish, catfish, crocodiles, and algaecrocodiles, and algae. .
Fish Simulation:Fish Simulation: An OO Program that An OO Program that Simulates Aquatic LifeSimulates Aquatic Life
Catfish are capable of eating algae, and crocodile are Catfish are capable of eating algae, and crocodile are capable of eating catfish. capable of eating catfish.
Both catfish and crocodiles can move from cell to cell, Both catfish and crocodiles can move from cell to cell, while algae are stationary. while algae are stationary.
Algae require sunlight to survive. Random amounts of Algae require sunlight to survive. Random amounts of sunlight per cell are provided by the program. sunlight per cell are provided by the program.
Typically, a choice of 20–30 time steps is enough to Typically, a choice of 20–30 time steps is enough to observe the behavior of the organisms and draw some observe the behavior of the organisms and draw some conclusions. conclusions.
从 seqcc.icarnegie.com 网站 2.1.1 节处下载Alife.zip 文件
Fish Simulation:Fish Simulation: An OO Program that An OO Program that Simulates Aquatic LifeSimulates Aquatic Life
建立这样一个描述水中生物群的模型需要什么样的建立这样一个描述水中生物群的模型需要什么样的类类?? What classes would be required to build such a model that simulaWhat classes would be required to build such a model that simula
tes the life of organisms in a lake? tes the life of organisms in a lake? ))
是否这个模拟程序总是需要生成这些类的是否这个模拟程序总是需要生成这些类的对象对象?? (( Does the simulation program always need to create objects of tDoes the simulation program always need to create objects of t
(( How does the number of organisms selected by you in the initiHow does the number of organisms selected by you in the initial configuration of the lake affect the number of objects created bal configuration of the lake affect the number of objects created by the program? y the program? ))
Fish Simulation:Fish Simulation: An OO Program that An OO Program that Simulates Aquatic LifeSimulates Aquatic Life
问题问题 11 中所说的类需要有哪些中所说的类需要有哪些行为行为?? (( What would be the attributes of the classes you thWhat would be the attributes of the classes you th
ought of as answer to #1? ought of as answer to #1? ))
鳄鱼的行为变换基于鲶鱼的位置么?鳄鱼的行为变换基于鲶鱼的位置么? (( Does the crocodile‘s behavior change based on the Does the crocodile‘s behavior change based on the
presence or absence of catfish? presence or absence of catfish? ))
鲶鱼的行为会受水草位置的影响么?鲶鱼的行为会受水草位置的影响么? (( Does the catfish‘s behavior change based on the prDoes the catfish‘s behavior change based on the pr
esence or absence of algae? esence or absence of algae? ))
JavaJava 的关键字的关键字JavaJava 语言中,有一部分标识符是系统定义的,有着专门语言中,有一部分标识符是系统定义的,有着专门的意义和用途,不能用于一般的标识符,这些标识符就的意义和用途,不能用于一般的标识符,这些标识符就叫做叫做保留字保留字或或关键字关键字。。abstract assert boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while
Catfish.javaCatfish.java 给出了对类给出了对类 CatfishCatfish 的定义的定义 The description is provided by the source code encloseThe description is provided by the source code enclose
d within the opening brace and the closing brace.d within the opening brace and the closing brace.
Format, and Comments " Format, and Comments " 第二章的第二章的 "Java Interlude: Variables and Assignment " "Java Interlude: Variables and Assignment " 第二章的第二章的 2.72.7节节第三章的第三章的 "Java Interlude: Packages and the import state"Java Interlude: Packages and the import state
定义响应返回的页面的内容类型(定义响应返回的页面的内容类型( Indicate the content tyIndicate the content type being returned by the responsepe being returned by the response ))获取一个输出流,使用这个输出流将数据传送给客户(获取一个输出流,使用这个输出流将数据传送给客户( RRetrieve an output stream to send data to the clientetrieve an output stream to send data to the client ) ) 获取用户在表单中的输入(获取用户在表单中的输入( Get user input from the form: Get user input from the form: name and destinationname and destination ) ) 建立网页的建立网页的 headerheader (( Start by building the Web page heaStart by building the Web page headerder ))加入图片(加入图片( Add the imageAdd the image ))加入文本信息,并将从请求那里得到的姓名放入到信息中加入文本信息,并将从请求那里得到的姓名放入到信息中(( Add the confirmation message, with the nameAdd the confirmation message, with the name ))结束网页(结束网页( End by building the Web page footerEnd by building the Web page footer ))
/** * Indicate the content type (for example, text/html), * being returned by the response */ response.setContentType("text/html");
/** * Retrieve an output stream to use to send * data to the client */ PrintWriter out = response.getWriter();
TravelRequest.java 文件
/** * Get the user input from the form : * name and destination */ String destination = request.getParameter("Destination"); String name = request.getParameter("Name");
/** * Start by building the web page header */ out.println( "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>"); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>iCarnegie Travel Agency</TITLE>"); out.println("</HEAD>"); out.println("<BODY>");
/** * Add the confirmation message, with the name */ out.println("Hello, " + name + "!"); out.println( "Thanks for your request. You will be contacted shortly.");
/** * End by building the web page footer */ out.println("</BODY>"); out.println("</HTML>");