一、习题介绍
第九章
Check Point:P329 9.1,9.5,9.6,9.9,9.19,9.21,9.29,9.32
Programming Exercise:9.6,9.11
二、习题及答案
Check Point:
9.1
Describe the relationship between an object and its defining class.
9.1 描述一个对象与其定义类之间的关系。
答:一个对象是其定义类的实例。类是对象的蓝图或模板,定义了对象的属性(数据字段)和行为(方法)。当创建一个类的新实例时,我们说这个对象是类的实例化。
9.5
What are the differences between constructors and methods?
9.5 构造函数与方法有什么区别?
答:构造函数是一种特殊的方法,用于在创建对象时初始化对象的状态。它在对象实例化时自动调用,并且没有返回类型,甚至连 void 也不是。而普通方法是类的一部分,可以在对象的生命周期中任何时候调用,并且可以有返回值。
9.6
When will a class have a default constructor?
9.6 何时一个类会有默认构造函数?
答:如果一个类没有定义任何构造函数,编译器会自动提供一个无参的默认构造函数。这个默认构造函数将对类的所有成员变量进行默认初始化。
9.9
What is NullPointerException?
9.9 什么是NullPointerException?
答:NullPointerException是一个运行时异常,当应用程序试图使用一个为null的对象引用进行操作时抛出。
9.19
Can you invoke an instance method or reference an instance variable from a static
method? Can you invoke a static method or reference a static variable from an
instance method? What is wrong in the following code?
9.19 从静态方法中可以调用实例方法或引用实例变量吗?
答:不可以。静态方法属于类本身,而不是类的任何特定实例,因此它不能访问非静态的实例方法或变量。
9.19 从实例方法中可以调用静态方法或引用静态变量吗?
答:可以。实例方法可以调用静态方法和引用静态变量,因为它们属于类,而不是类的特定实例。
9.21
What are the benefits of data field encapsulation?
9.21 数据字段封装的好处是什么?
答:封装是面向对象编程的一个核心概念,它隐藏了对象的内部状态和实现细节,只暴露出一个可以被外界访问和操作的接口。这提高了代码的安全性和可维护性,同时也使得对象的状态更容易控制和修改。
9.29
If all the data fields in a class are private and of primitive types, and the class doesn’t
contain any setter methods, is the class immutable?
9.29 如果一个类的所有数据字段都是私有的,并且都是基本数据类型,而且该类没有设置方法,那么这个类是否是可变的?
答:不是。如果一个类是不可变的,那么他的所有数据域必须是私有的,而且没有对任何一个数据域提供公共的set方法。
9.32
Describe the role of the this keyword.
9.32 描述this关键字的作用。
答:this 关键字引用了当前对象的引用。它可以用来访问当前对象的成员变量和方法,特别是在方法内部需要区分成员变量和参数时。
Programming Exercise:
*9.6
(Stopwatch) Design a class named StopWatch. The class contains:
■ Private data fields startTime and endTime with getter methods.
■ A no-arg constructor that initializes startTime with the current time.
■ A method named start() that resets the startTime to the current time.
■ A method named stop() that sets the endTime to the current time.
■ A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds.
Draw the UML diagram for the class and then implement the class.
Write a test program that measures the execution time of sorting 100,000 numbers using
selection sort.
* 9.6
设计一个名为Stopwatch的类。这个类包含:
私有数据字段startTime和endTime带有getter方法。
一个无参数构造函数,用当前时间初始化startTime。
一个名为start()的方法将startTime重置为当前时间。
一个名为stop()的方法,将endTime设置为当前时间。
一个名为getElapsedTime()的方法,用于返回事件的经过时间
秒表以毫秒计
为类绘制UML图,然后实现类。
编写测试测量100,000个数字排序执行时间的程序选择排序。
// UML类图
/*
class StopWatch {
- startTime: long
- endTime: long
+ getStartTime(): long
+ getEndTime(): long
- StopWatch()
+ start(): void
+ stop(): void
+ getElapsedTime(): long
}
*/
public class StopWatch {
private long startTime;
private long endTime;
public StopWatch() {
this.startTime = System.currentTimeMillis();
this.endTime = 0;
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime;
}
public void start() {
startTime = System.currentTimeMillis();
}
public void stop() {
endTime = System.currentTimeMillis();
}
public long getElapsedTime() {
if (endTime == 0) {
return System.currentTimeMillis() - startTime;
}
return endTime - startTime;
}
}
*9.11
(Algebra: 2 * 2 linear equations) Design a class named LinearEquation for a
2 * 2 system of linear equations:
The class contains:
■ Private data fields a, b, c, d, e, and f.
■ A constructor with the arguments for a, b, c, d, e, and f.
■ Six getter methods for a, b, c, d, e, and f.
■ A method named isSolvable() that returns true if ad - bc is not 0.
■ Methods getX() and getY() that return the solution for the equation.
Draw the UML diagram for the class and then implement the class. Write a test
program that prompts the user to enter a, b, c, d, e, and f and displays the result.
If ad - bc is 0, report that “The equation has no solution.” See Programming
Exercise 3.3 for sample runs.
* 9.11
(代数:2 * 2线性方程)为a设计一个类LinearEquation
2 * 2线性方程组:
这个类包含:私有数据字段a、b、c、d、e和f。
带有参数A、b、c、d、e和f的构造函数。
■a、b、c、d、e和f的6个getter方法。
一个名为isSolvable()的方法,当ad - bc不为0时返回true。
getX()和getY()方法返回方程的解。
为类绘制UML图,然后实现类。
编写测试:
提示用户输入a、b、c、d、e和f并显示结果的程序。
如果ad - bc为0,则报告“该方程无解”。
看到编程示例运行的练习3.3。
// UML类图
/*
class LinearEquation {
- a: double
- b: double
- c: double
- d: double
- e: double
- f: double
+ LinearEquation(a: double, b: double, c: double, d: double, e: double, f: double): void
+ getA(): double
+ getB(): double
+ getC(): double
+ getD(): double
+ getE(): double
+ getF(): double
+ isSolvable(): boolean
+ getX(): double
+ getY(): double
}
*/
public class LinearEquation {
private double a, b, c, d, e, f;
public LinearEquation(double a, double b, double c, double d, double e, double f) {
this.a = a; this.b = b; this.c = c;
this.d = d; this.e = e; this.f = f;
}
public double getA() { return a; }
public double getB() { return b; }
public double getC() { return c; }
public double getD() { return d; }
public double getE() { return e; }
public double getF() { return f; }
public boolean isSolvable() {
return (a * d) != (b * c);
}
public double getX() {
if (!isSolvable()) throw new IllegalStateException("方程无解");
return (d * e - b * f) / (a * d - b * c);
}
public double getY() {
if (!isSolvable()) throw new IllegalStateException("方程无解");
return (a * f - c * e) / (a * d - b * c);
}
}
结语
人不一定要生得漂亮
但却一定要活得漂亮
!!!