Chapter 2 Exercises


Site hosted by Angelfire.com: Build your free website today!
Ex2.1
1. What is a protable program? Can be run on different types of computers without change.
2. Describe two features of Java that make it a better language than C++. Java is easier to use than C++, and less error prone.

Ex2.2
1. What does JVM stand for? Java virtual machine.
2. What is byte code? Pseudomachine language. Describe how the JVM uses byte code. It uses it as machine language for an imaginary Java computer.
3. What is an applet? Small Java programs already translated into byte code. Describe how applets are used. They run in a JVM that is incorportated into a web browser.

Ex2.4
1. Give a short definition of "program". 2. What is the effect of the message println? This indicates that the System.out object should advance to the beginning of the next line after printing a string.
3. Describe how to use the System.out object. This is used to display strings in the following format: System.out.println("text");
4. Write a sequence of statements to display your name, address, and phone number in the terminal window.
- System.out.println("Kevin");
- System.out.println("424 S Little Tor Rd");
- System.out.println("634-5623");

Ex2.5
1. Name the three steps in writing and running a program. Edit, compile, execute.
2. What are compile-time errors? Also known as syntax errors they are mistakes detected by the compiler.
3. Find the compile time errors in the following statements:
- a. System.out.println("Here is an error); Missing closing quotation mark
- b. System.out.println("Here is another error"; Missing closing Parenthesis
4. Why is readability a desirable characteristic of a program? This alows for work to be done more efficiently.

Ex2.6
1. What is a variable in a program and how is it used? This names a location in memory where a value can be stored; this value can be changed and used repeatedly.
2. Describe the role of the assignment (=) operator in a program. This assigns a value to a variable.
3. What is a KeyboardReader object? This reads inputs entered at the keyboard.
4. Explain the difference between a variable of type double and a variable of type KeyboardReader. Type double variables contain only floating-point numbers, where as type KeyboardReader contain characters.
5. Describe the difference between print and println and give an appropriate example of the use of each. Print leaves the cursor where it is after the last character, while println causes the cursor to go to the next line.
- System.out.print("HI"); System.out.print("THERE");
- output: HI THERE
- System.out.println("HI"); System.out.println("THERE");
- output: HI
- THERE

Ex2.7
1. What figues are drawn by each of the following snippets of code and where will they be located in the graphics window?
a.
pen.home();
pen.down();
pen.move(60);
pen.turn(180);
pen.up();
pen.move(30);
pen.turn(90);
pen.move(30);
- line up from center then halfway down there's a smaller line towards the left.

b.
pen.home();
pen.down();
int i;
for(i=1;i<=3;i++){
pen.move(30);
pen.turn(60);
}
- half a hexagon and in upper left.

2. Write a for loop that prints your name a dozen times.
- int i;
- for (i=1; i<=12;i++){
- System.out.println("Kevin");
- }

Review Questions
1. List three reasons why Java is an important programming language.
It is compiled into byte code which makes it highly portable, it can be integrated directly into web pages, and it is very secure.

2. What is byte code?
It is the code generated by the java compiler and is interpreted by the Java virtual machine.

3. What is the JVM?
The JVM converts the byte code into the computer's machine language.

4. List 2 objects that are used for terminal input and output in Java programs.
KeyboardReader and System.out

5. Give examples of two compile time errors.
system.out.println("test");
System.out.println("test)";

6. What steps must be followed to run a Java program?
Compile to byte code, execute byte code(converted into machine code by JVM).

7. State the purpose of a program comment.
The purpose is to clarify what that section of the program is doing. This makes it easier to understand and fix.

8. What is the purpose of an import statement in a Java program?
This incorporates more classes into your program and gives you acces to other objects and methods so you don't have to write them yourself.

Fill In The Blank
1. Two user interface styles are Terminal I/O and the graphical user interaface.
2. The message println is used to output data to the terminal window.
3. The message readInt() is used to input an integer from the keyboard.
4. A variable names a place where data can be stored in a java program.
5. An assignment operator stores the value of the expression in the variable.
6. Programs manipulate objects by sending them messages.

Home