java语言程序设计-基础篇--第八版--英文课件(第14章)
合集下载
java语言程序设计-基础篇-课件(第1章)英文
Memory
CPU
Communication Devices e.g., Modem, and NIC
Input Devices e.g., Keyboard, Mouse
Output Devices e.g., Monitor, Printer
CPU
4
The central processing unit (CPU) is the brain of a computer. It retrieves instructions from memory and executes them. The CPU speed is measured in megahertz (MHz), with 1 megahertz equaling 1 million pulses per second. The speed of the CPU has been improved continuously. If you buy a PC now, you can get an Intel Pentium 4 Processor at 3 gigahertz (1 gigahertz is 1000 megahertz).
Memory
CPU
Communication Devices e.g., Modem, and NIC
Input Devices e.g., Keyboard, Mouse
Output Devices e.g., Monitor, Printer
Monitor Resolution and Dot Pitch
How Data is Stored?
Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies. For example, character „J‟ is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the minimum storage unit.
JAVA基础8.14
类的定义 [修饰符] class 类名{ } 属性的定义 [修饰符] 数据类型 属性名称; 方法的定义 [修饰符] 返回值类型 方法名称([参数]){ }
15
Java 基础
邮箱 1666976530@
对象的创建和使用
1、构造器(构造方法) 通过构造器用于构造该类的对象。Java 通过 new 关键字来调用 构造器,从而返回该类的对象。是一种特殊的方法: [修饰符] 类名 (参数列表){ //n 条语句 } * 1、构造器没有返回值类型 * 2、方法名称要和类名保持一致 * 3、一个类中可以有多个构造器,但是参数列表不同:参数的顺序不同、参数的类型 不同、参数的个数不同 * 4、构建类的对象 通过 new 关键字 例:Mobile stu=new Mobile(); * 5、对象方法和对象属性访问 通过“.”操作符 <object>.<member> * 6、系统会自动添加一个无参的构造器 * 7、当含有有参的构造器时 会把默认的无参的构造器干掉 *
java 版 一般建议使用 javase 版本
b、解压缩,即可运行解压文件中的 eclipse.exe 来打开 Eclipse 开发工具
13
Java 基础
邮箱 1666976530@
编写HelloJava
/* * 1、public 四个权限修饰符之一 * 2、class 类 * 3、HelloJava 类名 * * java文件构成 * * } * * main方法 * 1、固定写法 * 2、java中的主方法 也是java程序的入口方法 * * 注意 * 1、如果有public修饰符话 java文件名要和public修饰的类名保持一致 * 2、一个java文件之多有一个public修饰的类 */ [修饰符] class 类名{
java 第八版 中文版课件 第十四章
• 事件模型三要素的设计规范 • 事件源: EventSource类 • 事件对象:EventObject类 • 事件监听: EventListener接口 • 事件模型三要素的特性 – java.util.EventObject是所有事件对象的根 – java.util.EventListener是所有事件监听的根,它是一个标 记接口,在该接口中无方法 – 在java.util中,EventObject事件对象类是所有事件对象的 父类,事件对象要继承它;一个事件对象中会封装事件源 对象。如EventObject(source)事件对象的构造中有source 事件源;EventObject还有一个getSource()方法获得事件 源
AWT事件模型的概念和特点
– AWT事件模型 • awt事件模型:sun公司设计定义的java事件模型的 应用。同样包含三要素:事件源,事件对象,事件 监听 • awt事件模型是java的GUI组件的事件模型。 • awt设计了很多组件,不同组件会有不同的事件发生, 设计了不同的事件,不同的事件也就有不同的监听 器来处理
单选/复选/下拉选框的选项变化发生该事件 文本框/文本域/其他文字组件的文字变化发生该事件 容器事件(往容器中加/删组件时发生该事件) 光标事件(获得/失去光标焦点时发生该事件) 输入事件,分为键盘输入/鼠标输入
ComponentEvent
FocusEvent InputEvent WindowEvent
– 事件源负责事件的生成,事件对象可以注册监听 – 一个监听者是监听某一个事件发生,然后处理事件 – 监听者通过事件来联系事件源 – 一个监听者可以监听多个事件源 – 一个事件源的事件也可由多个监听者来监听 – 事件源发生事件给监听处理 • 实现java事件模型的步骤 – 写一个类继承EventObject,作为事件 – 写一个类实现EventListener接口,作为监听器并监听。 – 在其他类/监听类中发起事件,然后交给监听去处理 – 写一个类继承EventSource,作为事件源类,并向其注册 监听器。
java语言程序设计-基础篇--课件(第8章)英文
Example: Defining Classes and Creating Objects
10
Objective: Demonstrate creating objects, accessing data, and using methods.
TV
TestTV Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
1
Chapter 8 Objects and Classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Motivations
2
After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these Java features are not sufficient for developing graphical user interfaces and large scale software systems. Suppose you want to develop a graphical user interface as shown below. How do you program it?
java语言程序设计-基础篇-课件(第13章)英文
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Exception Advantages
5
QuotientWithMethod
Exception Tyble
Exception Error
ClassNotFoundException IOException RuntimeException Many more classes
LinkageError VirtualMachineError
Many more classes
1
Chapter 13 Exception Handling
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
2
Motivations
When a program runs into a runtime error, the program terminates abnormally. How can you handle the runtime error so that the program can continue to run or terminate gracefully? This is the subject we will introduce in this chapter.
▪ To explore the advantages of using exception handling (§13.3).
java语言程序设计-基础篇--第八版--英文课件(第10章)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Reference the Hidden Data Fields
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
What Class is Immutable?
6
For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object.
public class Foo { private int i = 5; private static double k = 0; void setI(int i) { this.i = i; } static void setK(double k) { Foo.k = k; } } Invoking f1.setI(10) is to execute this.i = 10, where this refers f1 Invoking f2.setI(45) is to execute this.i = 45, where this refers f2
java语言程序设计-基础篇-课件(第3章)英文
4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 Set3
8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 Set4
16 20 24 28
17 21 25 29
18 22 26 30
19 23 27 31
Set2
Set5
7
if (boolean-expression) { statement(s); }
if (radius >= 0) { area = radius * radius * PI; System.out.println("The area" + " for the circle of radius " + radius + " is " + area); }
2 10 18 26
3 11 15 23 31
4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 Set3
2
If you assigned a negative value for radius in Listing 2.1, ComputeArea.java, the program would print an invalid result. If the radius is negative, you don't want the program to compute the area. How can you deal with this situation?
(b)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
java语言程序设计-基础篇--第八版--英文课件(第17章)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
3
-iconTextGap: int
-selected(): booleanLiang, Introduction to Java The state of the button. True if the check box or Inc. radio Programming, Eighth Edition, (c) 2011 Pearson Education, All button is selected, false if it's not. 0132130807 rights reserved.
JCheckBox JRadioButton
JPasswordField
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Default Icons, Pressed Icon, and Rollover Icon
A regular button has a default icon, pressed icon, and rollover icon. Normally, you use the default icon. All other icons are for special effects. A pressed icon is displayed when a button is pressed and a rollover icon is displayed when the mouse is over the button but not pressed.
Java课件Chap14
• Important classes for text file input (from the file): – BufferedReader – FileReader
• Note that FileOutputStream and FileReader are used only for their(PrintWriter and BufferedReader ) constructors, which can take file names as arguments. – PrintWriter and BufferedReader cannot take file names as arguments for their constructors.
Java Text Files Source files Occasionally input files Occasionally output files
Java Binary Files Executable files (created by
compiling source files) Usually input files Usually output files
• To use these classes your program needs a line like the following: import java.io.*;
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
} outputStream.close();
Closing the file
System.out.println("... written to out.txt.");
• Note that FileOutputStream and FileReader are used only for their(PrintWriter and BufferedReader ) constructors, which can take file names as arguments. – PrintWriter and BufferedReader cannot take file names as arguments for their constructors.
Java Text Files Source files Occasionally input files Occasionally output files
Java Binary Files Executable files (created by
compiling source files) Usually input files Usually output files
• To use these classes your program needs a line like the following: import java.io.*;
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
} outputStream.close();
Closing the file
System.out.println("... written to out.txt.");
《Java程序设计》电子课件
this.copper=c; // 给类成员变量z赋值
}
2024/10/20
宋波,李晋,李妙妍,张悦
String getModel( ) { return “金牌 = ”+gold+“ 银牌
=”+silver+“ 铜牌="+copper; }
2024/10/20
宋波,李晋,李妙妍,张悦
public static void main( String args[ ]) {
5. return color;
6. }
7. public float count(){ 8. int num; 9. if(num<0)
• 错误语句,因为局 部变量num还没有
10. return 0;
被赋值就使用
11. else
12. return price*num;
13. } 2024/10/20
2024/10/20
宋波,李晋,李妙妍,张悦
2. 类体
① 构造和初始化新对象的构造方法; ② 表示类及其对象状态的变量; ③ 实现类及其对象的方法; ④ 进行对象清除的finalize()方法。
2024/10/20
宋波,李晋,李妙妍,张悦
3.1.2 成员变量与局部变量
• 成员变量(类):
➢ 在类体中声明的变量,且不属于任何 一个方法时。
Olympics2 o2=new Olympics2( );
System.out.println("Before changModel:"+o2.getModel());
o2.changeModel(100,100,100);
System.out.println("After changeModel:"+o2.getModel());
java语言程序设计-基础篇--第八版--英文课件(第9章)
ቤተ መጻሕፍቲ ባይዱ
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
animation
11
String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java";
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
3
To use the String class to process fixed strings (§9.2). To use the Character class to process a single character (§9.3). To use the StringBuilder/StringBuffer class to process flexible strings (§9.4). To distinguish among the String, StringBuilder, and StringBuffer classes (§9.2-9.4). To learn how to pass arguments to the main method from the command line (§9.5). To discover file properties and to delete and rename files using the File class (§9.6). To write data to a file using the PrintWriter class (§9.7.1). To read data from a file using the Scanner class (§9.7.2). (GUI) To open files using a dialog box (§9.8).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
animation
11
String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java";
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
3
To use the String class to process fixed strings (§9.2). To use the Character class to process a single character (§9.3). To use the StringBuilder/StringBuffer class to process flexible strings (§9.4). To distinguish among the String, StringBuilder, and StringBuffer classes (§9.2-9.4). To learn how to pass arguments to the main method from the command line (§9.5). To discover file properties and to delete and rename files using the File class (§9.6). To write data to a file using the PrintWriter class (§9.7.1). To read data from a file using the Scanner class (§9.7.2). (GUI) To open files using a dialog box (§9.8).
java语言程序设计-基础篇--第八版--英文课件(第14章)
5
object cannot be created from abstract class
6
An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.
TestGometricObject
Run
abstract method in abstract class
An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.
8
concrete method overridden to be abstract A subclass can override a method from its superclass to define it abstract. This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined abstract.
java语言程序设计-基础篇-课件(第5章)英文
i is now 5
14
public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k);
public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; } return result;
}
animation
Trace Method Invocation
Defining Methods
A method is a collection of statements that are grouped together to perform an operation.
Define a method return value type method name formal parameters
parameter list actual parameters (arguments)
method signature return value
Calling Methods
Testing the max method
12
This program demonstrates calling a method max to return the largest of the int values
14
public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k);
public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; } return result;
}
animation
Trace Method Invocation
Defining Methods
A method is a collection of statements that are grouped together to perform an operation.
Define a method return value type method name formal parameters
parameter list actual parameters (arguments)
method signature return value
Calling Methods
Testing the max method
12
This program demonstrates calling a method max to return the largest of the int values
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
GeometricObject[] geo = new GeometricObject[10];
10
The Abstract Calendar Class and Its GregorianCalendar subclass
java.util.Calendar #Calendar() +get(field: int): int +set(field: int, value: int): void +set(year: int, month: int, dayOfMonth: int): void +getActualMaximum(field: int): int +add(field: int, amount: int): void +getTime(): java.util.Date Constructs a default calendar. Returns the value of the given calendar field. Sets the given calendar to the specified value. Sets the calendar with the specified year, month, and date. The month parameter is 0-based, that is, 0 is for January. Returns the maximum value that the specified calendar field could have. Adds or subtracts the specified amount of time to the given calendar field. Returns a Date object representing this calendar’s time value (million second offset from the Unix epoch).
TestGometricObject
Run
abstract method in abstract class
An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.
5
object cannot be created from abstract class
6
An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.
9
abstract class as type
You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct.
Abstract Classes and Abstract Methods
4
GeometricObject
-color: String -filled: boolean The # sign indicates protected modifie r -dateCreated: java.util.Date #Geo metric Object() #Geo metric Object(color: string, filled: boolean) +getColor(): St ring +setColor(colo r: String): void +isFilled(): boolean +setFilled(filled : boolean): void +getDateCreated(): java.util.Date +toString(): String +getArea(): double Abstract methods are ita lic ized +getPerimeter(): double Methods getArea and getPerimeter a re overridden in Circ le and Rectangle. Superclass methods are generally omitted in the UM L d iagra m for subclasses. Abstract class
GeometricObject Circle Rectangle
Circle
-radius: double +Circle() +Circle (radius: double) +Circle (radius: double, color: string, filled: boolean) +getRadius(): double +setRadius(radius: double): void +getDia meter(): double -width: double
Rectangle
-height: double +Rectangle() +Rectangle(width: double, height: double) +Rectangle(width: double, height: double, color: string, filled: boolean) +getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(height: double): void
8
concrete method overridden to be abstract A subclass can override a method from its superclass to define it abstract. This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined abstract.
1
Chapter 14 Abstract Classes and Interfaces
Motivations
You learned how to write simple programs to display GUI components. Can you write the code to respond to useபைடு நூலகம் actions such as clicking a button?
abstract class without abstract method A class that contains abstract methods must be abstract. However, it is possible to define an abstract class that contains no abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass.
7
superclass of abstract class may be concrete A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract.
2
HandleEvent
Run
Objectives
3
To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar classes (§14.3). To specify common behavior for objects using interfaces (§14.4). To define interfaces and define classes that implement interfaces (§14.4). To define a natural order using the Comparable interface (§14.5). To enable objects to listen for action events using the ActionListener interface (§14.6). To make objects cloneable using the Cloneable interface (§14.7). To explore the similarities and differences between an abstract class and an interface (§14.8). To create objects for primitive values using the wrapper classes (Byte, Short, Integer, Long, Float, Double, Character, and Boolean) (§14.9). To create a generic sort method (§14.10). To simplify programming using automatic conversion between primitive types and wrapper class types (§14.11). To use the BigInteger and BigDecimal classes for computing very large numbers with arbitrary precisions (§14.12). To design the Rational class for defining the Rational type (§14.13).
10
The Abstract Calendar Class and Its GregorianCalendar subclass
java.util.Calendar #Calendar() +get(field: int): int +set(field: int, value: int): void +set(year: int, month: int, dayOfMonth: int): void +getActualMaximum(field: int): int +add(field: int, amount: int): void +getTime(): java.util.Date Constructs a default calendar. Returns the value of the given calendar field. Sets the given calendar to the specified value. Sets the calendar with the specified year, month, and date. The month parameter is 0-based, that is, 0 is for January. Returns the maximum value that the specified calendar field could have. Adds or subtracts the specified amount of time to the given calendar field. Returns a Date object representing this calendar’s time value (million second offset from the Unix epoch).
TestGometricObject
Run
abstract method in abstract class
An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.
5
object cannot be created from abstract class
6
An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.
9
abstract class as type
You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct.
Abstract Classes and Abstract Methods
4
GeometricObject
-color: String -filled: boolean The # sign indicates protected modifie r -dateCreated: java.util.Date #Geo metric Object() #Geo metric Object(color: string, filled: boolean) +getColor(): St ring +setColor(colo r: String): void +isFilled(): boolean +setFilled(filled : boolean): void +getDateCreated(): java.util.Date +toString(): String +getArea(): double Abstract methods are ita lic ized +getPerimeter(): double Methods getArea and getPerimeter a re overridden in Circ le and Rectangle. Superclass methods are generally omitted in the UM L d iagra m for subclasses. Abstract class
GeometricObject Circle Rectangle
Circle
-radius: double +Circle() +Circle (radius: double) +Circle (radius: double, color: string, filled: boolean) +getRadius(): double +setRadius(radius: double): void +getDia meter(): double -width: double
Rectangle
-height: double +Rectangle() +Rectangle(width: double, height: double) +Rectangle(width: double, height: double, color: string, filled: boolean) +getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(height: double): void
8
concrete method overridden to be abstract A subclass can override a method from its superclass to define it abstract. This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined abstract.
1
Chapter 14 Abstract Classes and Interfaces
Motivations
You learned how to write simple programs to display GUI components. Can you write the code to respond to useபைடு நூலகம் actions such as clicking a button?
abstract class without abstract method A class that contains abstract methods must be abstract. However, it is possible to define an abstract class that contains no abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass.
7
superclass of abstract class may be concrete A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract.
2
HandleEvent
Run
Objectives
3
To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar classes (§14.3). To specify common behavior for objects using interfaces (§14.4). To define interfaces and define classes that implement interfaces (§14.4). To define a natural order using the Comparable interface (§14.5). To enable objects to listen for action events using the ActionListener interface (§14.6). To make objects cloneable using the Cloneable interface (§14.7). To explore the similarities and differences between an abstract class and an interface (§14.8). To create objects for primitive values using the wrapper classes (Byte, Short, Integer, Long, Float, Double, Character, and Boolean) (§14.9). To create a generic sort method (§14.10). To simplify programming using automatic conversion between primitive types and wrapper class types (§14.11). To use the BigInteger and BigDecimal classes for computing very large numbers with arbitrary precisions (§14.12). To design the Rational class for defining the Rational type (§14.13).