Chapter 3 Assignment

?Modularization is the process of dividing large things (problems) into smaller parts, while abstraction is the ability to ignore details to focus on the bigger picture. Modularization and abstraction thus complement each other. It is like divide the car into independent modules (wheel, engine, gear box, seat, steering wheel, etc.) and get separate people to work on separate modules independently. They view that module as a single component that is used to build more complex components.

屏幕快照 2017-01-06 下午9.36.10.png屏幕快照 2017-01-06 下午9.36.21.png

Classes define types: A class name can be used as the type for a variable. e.g NumberDisplay is a classes define type since it is a class but is also used as variables(fields) in ClockDisplay class.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%887-40-38

The word “private” in the field declaration is followed by a type and a field’s name. Here we use the class NumberDisplay as the type for the fields named hours and minutes which also tell us that class names can be used as types.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%887-40-31

Objects diagram

There are three objects here. It shows us a dynamics view which means it shows the situation at runtime (when the application is running).

屏幕快照 2016-12-31 下午7.45.46.png

Objects is not stored in a variable directly, but rather an object reference is stored in the variable. In the diagram, the variable is shown as a white box, and the object reference is shown as an arrow.

Class Diagram

There are two classes. We say that ClockDisplay depends on(makes use of) NumberDisplay. It shows us a static view which means it depicts what we have at the time of writing the program.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%887-45-50

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%888-12-27

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%888-12-31

Primitive types are the non-object types. (int, boolean, double) Primitive values are stored directly in a variable.

Object types are those defined by classes. (e.g String, NumberDisplay) Objects are not stored directly in the variable, but instead a reference to the object is stored.

3.7 time”00″ will not be displayed

3.9 ! false;  (34!=33) && ! false

3.10 if(a==true && b==false || a==false && b==false){System.out.println(“true”);}

3.11 if(a==true || b==true){System.out.print(“true”);} if(a==false || b==false){System.out.println(“false”);}

3.12 if (a=b)

String concatenation

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%8810-51-58

The plus operator (+) represents,

  • Addition (42+5 → 47)
  • String concatenation (“Mr.”+”Pete”→ “Mr.Pete”)

3.14 Yes. The first one will return a int type or may regard it as syntax error, the second one returns a String type.

Modulo Operator (%)

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2016-12-31-%e4%b8%8b%e5%8d%8810-51-53

If I set limit=12. Then when the original value is 0-10, the output value will be 1-11. When original value=11, the output value will be 0 since value = (11+1)%12=0. Then the output value continually be 1-11, 0, 1-11, 0, 1-11, 0…

3.16 2

3.17 屏幕快照 2016-12-31 下午10.54.51.png

3.18 0,1,2,3,4

3.19 0,1,2,3,…,m-1

3.20 see above

3.21

屏幕快照 2016-12-31 下午11.21.50.png

ClockNumber

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%887-14-18

3.23

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%887-31-02

3.25

屏幕快照 2017-01-02 下午7.39.31.png

3.27

Overloading

A class may contain more than one con- structor, or more than one method of the same name, as long as each has a distinctive set of parameter types.

屏幕快照 2017-01-02 下午8.18.16.png

Internal Method Call

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%888-46-37

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%888-46-29

both from ClockDisplay class.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%888-31-19

External Call Method

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%888-40-55

from ClockDisplay class.

屏幕快照 2017-01-02 下午8.41.01.png

from NumberDisplay class.

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%888-40-40

3.30

print(String filename, boolean doubleSided)
printer.print(String filename, boolean doubleSided)
getStatus(int delay)
printer.getStatus(int delay)

3.31 & 3.22 !!! Review them!!!

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%889-07-46

3.21

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%889-07-30%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%889-07-20

3.22

屏幕快照 2017-01-02 下午9.29.25.png%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-02-%e4%b8%8b%e5%8d%889-29-35

this. Statement

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-05-%e4%b8%8b%e5%8d%889-18-15%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-05-%e4%b8%8b%e5%8d%889-18-20%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-05-%e4%b8%8b%e5%8d%889-18-29

this. statement is used when there is name overloading occurs. In the above case, both fields and parameters called from, to, and message. By using this., we can achieve the following effect:

%e5%b1%8f%e5%b9%95%e5%bf%ab%e7%85%a7-2017-01-05-%e4%b8%8b%e5%8d%889-18-32

3.43

Question: Why the process return back to the upper part???

3.46???

 

Work Cited

Barnes, David J. and Kölling, Michael. Objects First with Java. 2012, New Jersey: Pearson Education, Inc. Print.

留下评论