19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

28 Chapter 1 <strong>Introduction</strong> <strong>to</strong> Computers, Programs, and <strong>Java</strong><br />

You will get Fahrenheit 67 degrees, which is wrong. It should be 95.0. In <strong>Java</strong>, the division<br />

for integers is an integer—the fractional part is truncated—so in <strong>Java</strong> 9 / 5 is 1. To get<br />

the correct result, you need <strong>to</strong> use 9.0 / 5, which results in 1.8.<br />

In general, syntax errors are easy <strong>to</strong> find and easy <strong>to</strong> correct, because the <strong>com</strong>piler gives indications<br />

as <strong>to</strong> where the errors came from and why they are wrong. Runtime errors are not difficult<br />

<strong>to</strong> find, either, since the reasons and locations for the errors are displayed on the console<br />

when the program aborts. Finding logic errors, on the other hand, can be very challenging. In the<br />

up<strong>com</strong>ing chapters, you will learn the techniques of tracing programs and finding logic errors.<br />

✓Point✓ Check<br />

1.45 What are syntax errors (<strong>com</strong>pile errors), runtime errors, and logic errors?<br />

1.46 Give examples of syntax errors, runtime errors, and logic errors.<br />

1.47 If you forget <strong>to</strong> put a closing quotation mark on a string, what kind error will be raised?<br />

1.48 If your program needs <strong>to</strong> read integers, but the user entered strings, an error would<br />

occur when running this program. What kind of error is this?<br />

1.49 Suppose you write a program for <strong>com</strong>puting the perimeter of a rectangle and you<br />

mistakenly write your program so that it <strong>com</strong>putes the area of a rectangle. What kind<br />

of error is this?<br />

1.50 Identify and fix the errors in the following code:<br />

1 public class Wel<strong>com</strong>e {<br />

2 public void Main(String[] args) {<br />

3 System.out.println('Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>!);<br />

4 }<br />

5 }<br />

1.51 The following program is wrong. Reorder the lines so that the program displays<br />

morning followed by afternoon.<br />

1 public static void main(String[] args) {<br />

2 }<br />

3 public class Wel<strong>com</strong>e {<br />

4 System.out.println("afternoon");<br />

5 System.out.println("morning");<br />

6 }<br />

KEY TERMS<br />

Application Program Interface (API) 16<br />

assembler 10<br />

assembly language 10<br />

bit 4<br />

block 17<br />

block <strong>com</strong>ment 17<br />

bus 2<br />

byte 4<br />

bytecode 21<br />

bytecode verifier 22<br />

cable modem 8<br />

central processing unit (CPU) 3<br />

class loader 22<br />

<strong>com</strong>ment 17<br />

<strong>com</strong>piler 10<br />

console 16<br />

dot pitch 8<br />

DSL (digital subscriber line) 8<br />

encoding scheme 4<br />

hardware 2<br />

high-level language 10<br />

integrated development environment<br />

(IDE) 16<br />

interpreter 10<br />

java <strong>com</strong>mand 21<br />

<strong>Java</strong> Development Toolkit (JDK) 16<br />

<strong>Java</strong> language specification 16<br />

<strong>Java</strong> Virtual Machine (JVM) 21<br />

javac <strong>com</strong>mand 21<br />

keyword (or reserved word) 17<br />

library 16<br />

line <strong>com</strong>ment 17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!