Java How to Program (early objects), 9E BY Paul Deitel Harvey Deitel
Email: Richard@qwconsultancy.com
1
Introduction to Computers and Java: Solutions
Man is still the most extraordinary computer of all. —John F. Kennedy
Good design is good business. —Thomas J. Watson, Founder of IBM
How wonderful it is that nobody need wait a single moment before starting to improve the world. —Anne Frank
Objectives In this chapter you’ll learn: Exciting recent developments in the computer field. ■ Computer hardware, software and networking basics. ■ The data hierarchy. ■ The different types of programming languages. ■ Basic object-technology concepts. ■ The importance of the Internet and the web. ■ A typical Java programdevelopment environment. ■ To test-drive a Java application. ■ Some key recent software technologies. ■ How computers can help you make a difference. ■
..
2
Chapter 1
Introduction to Computers and Java: Solutions
Self-Review Exercises 1.1
Fill in the blanks in each of the following statements: . a) The company that popularized personal computing was ANS: Apple. b) The computer that made personal computing legitimate in business and industry was . the ANS: IBM Personal Computer. . c) Computers process data under the control of sets of instructions called ANS: programs. , , , , d) The key logical units of the computer are the and . ANS: input unit, output unit, memory unit, central processing unit, arithmetic and logic unit, secondary storage unit. , and e) The three types of languages discussed in the chapter are . ANS: machine languages, assembly languages, high-level languages. f) The programs that translate high-level language programs into machine language are . called ANS: compilers. is a smartphone operating system based on the Linux kernel and Java. g) ANS: Android. software is generally feature complete and (supposedly) bug free, and ready h) for use by the community. ANS: Release candidate. which allows the dei) The Wii Remote, as well as many smartphones, use a(n) vice to respond to motion. ANS: accelerometer.
1.2
Fill in the blanks in each of the following sentences about the Java environment: a) The command from the JDK executes a Java application. ANS: java. command from the JDK compiles a Java program. b) The ANS: javac. file extension. c) A Java program file must end with the ANS: .java. d) When a Java program is compiled, the file produced by the compiler ends with the file extension. ANS: .class. that are executed by the Java e) The file produced by the Java compiler contains Virtual Machine. ANS: bytecodes.
1.3
Fill in the blanks in each of the following statements (based on Section 1.6): a) Objects have the property of —although objects may know how to communicate with one another across well-defined interfaces, they normally are not allowed to know how other objects are implemented. ANS: information hiding. , which contain fields and the set of b) Java programmers concentrate on creating methods that manipulate those fields and provide services to clients. ANS: classes. ..
Exercises
3
c) The process of analyzing and designing a system from an object-oriented point of view . is called ANS: object-oriented analysis and design (OOAD). , new classes of objects are derived by absorbing characteristics of existing d) With classes, then adding unique characteristics of their own. ANS: inheritance. is a graphical language that allows people who design software systems to use e) an industry-standard notation to represent them. ANS: The Unified Modeling Language (UML). of the object’s f) The size, shape, color and weight of an object are considered class. ANS: attributes.
Exercises 1.4
Fill in the blanks in each of the following statements: a) The logical unit of the computer that receives information from outside the computer . for use by the computer is the ANS: input unit. . b) The process of instructing the computer to solve a problem is called ANS: computer programming. is a type of computer language that uses Englishlike abbreviations for mac) chine-language instructions. ANS: Assembly language. is a logical unit of the computer that sends information which has already d) been processed by the computer to various devices so that it may be used outside the computer. ANS: The output unit. and are logical units of the computer that retain information. e) ANS: The memory unit, the secondary storage unit. is a logical unit of the computer that performs calculations. f) ANS: The arithmetic and logic unit (ALU). is a logical unit of the computer that makes logical decisions. g) ANS: The arithmetic and logic unit (ALU). languages are most convenient to the programmer for writing programs h) quickly and easily. ANS: High-level. . i) The only language a computer can directly understand is that computer’s ANS: machine language. is a logical unit of the computer that coordinates the activities of all the other j) logical units. ANS: The central processing unit (CPU).
1.5
Fill in the blanks in each of the following statements: is now used to develop large-scale enterprise applications, to enhance the a) functionality of web servers, to provide applications for consumer devices and for many other purposes. ANS: Java. initially became widely known as the development language of the UNIX opb) erating system. ANS: C. ..
4
Chapter 1
Introduction to Computers and Java: Solutions
c) The Web 2.0 company
is the fastest growing company ever.
ANS: Foursquare.
programming language was developed by Bjarne Stroustrup in the early d) The 1980s at Bell Laboratories. ANS: C++. 1.6
Fill in the blanks in each of the following statements: , , , a) Java programs normally go through five phases— and . ANS: edit, compile, load, verify, execute. provides many tools that support the software development process, b) A(n) such as editors for writing and editing programs, debuggers for locating logic errors in programs, and many other features. ANS: integrated development environment (IDE). , which executes Java programs. c) The command java invokes the ANS: Java Virtual Machine (JVM). is a software application that simulates a computer, but hides the underd) A(n) lying operating system and hardware from the programs that interact with it. ANS: virtual machine (VM). takes the .class files containing the program’s bytecodes and transfers e) The them to primary memory. ANS: class loader. examines bytecodes to ensure that they are valid. f) The ANS: bytecode verifier.
1.7
Explain the two compilation phases of Java programs. ANS: The two compilation phases that Java programs typically go through include one in which source code is translated into bytecodes which are portable across JVMs and a second in which the bytecodes are translated into machine language for the actual computer on which the program executes. In early Java versions, the JVM was simply an interpreter for Java bytecodes. This caused most Java programs to execute slowly because the JVM would interpret and execute one bytecode at a time. Today’s JVMs typically execute bytecodes using a combination of interpretation and so-called justin-time (JIT) compilation. In this process, The JVM analyzes the bytecodes as they are interpreted, searching for hot spots—parts of the bytecodes that execute frequently. For these parts, a just-in-time (JIT) compiler—known as the Java HotSpot compiler—translates the bytecodes into the underlying computer’s machine language. When the JVM encounters these compiled parts again, the faster machine-language code executes.
1.8 You are probably wearing on your wrist one of the world’s most common types of objects— a watch. Discuss how each of the following terms and concepts applies to the notion of a watch: object, attributes, behaviors, class, inheritance (consider, for example, an alarm clock), abstraction, modeling, messages, encapsulation, interface and information hiding. ANS: The entire watch is an object that is composed of many other objects (such as the moving parts, the band, the face, etc.) Watch attributes are time, color, band, style (digital or analog), etc. The behaviors of the watch include setting the time and getting the time. A watch can be considered a specific type of clock (as can an alarm clock). With that in mind, it is possible that a class called Clock could exist from which other classes such as watch and alarm clock could inherit the basic features in the clock. The watch is an abstraction of the mechanics needed to keep track of the time. The user of the watch does not need to know the mechanics of the watch in © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.