Showing posts with label Chpt1. Show all posts
Showing posts with label Chpt1. Show all posts

Saturday, February 13, 2016

Exercise 1.3

QUESTION

It is a good idea to commit as many errors as you can think of, so that you see what error messages the compiler produces. Sometimes the compiler tells you exactly what is wrong, and all you have to do is fix it. But sometimes the error messages are misleading. You will develop a sense for when you can trust the compiler and when you have to figure things out yourself.

  1. Remove one of the open squiggly-braces.
  2. Remove one of the close squiggly-braces.
  3. Instead of main, write mian,
  4. Remove the word static.
  5. Remove the word public.
  6. Remove the word System.
  7. Replace println with Println.
  8. Replace println with print. This one is tricky because it is a logic error, not a syntax error. The statement System.out.print is legal, but it may or may not do what you expect.
  9. Delete one of the parentheses. Add an extra one.

SOLUTION

  1. Syntax error
  2. Syntax error
  3. Error: Main method not found in class
  4. Error: Main method is not static in class
  5. Error: Main method not found in class
  6. out cannot be resolved
  7. Method Println(String) is undefined for the type PrintStream
  8. Output of 2 sentences are on the same line
  9. Syntax error, Syntax error

Exercise 1.2

QUESTION


Before you do anything else, find out how to compile and run a Java program in your environment. Some environments provide sample program similar to the example in Section 1.5.
  1. Type in the “Hello, world” program, then compile and run it.
  2. Add a print statement that prints a second message after the “Hello, world!”. Something witty like, “How are you?” Compile and run the program again.
  3.  Add a comment to the program (anywhere), recompile, and run it again. The new comment should not affect the result.
The exercise may seem trivial, but it is the starting place for many of the programs we will work with. To debug with confidence, you have to have confidence in your programming environment. In some environments, it is easy to lose track of which program is executing, and you might find yourself trying to debug one program while you are accidentally running another. Adding (and changing) print statements is a simple way to be sure that the program you are looking at is the program you are running.


SOLUTION


public class ThinkJava1_Ex2 {
 public static void main (String[]args){
System.out.println("Hello, world");
System.out.println("How are you?");
}
}

//This is a comment.

Monday, February 8, 2016

Exercise 1.1

QUESTION

Computer scientists have the annoying habit of using common English words to mean something other than their common English meaning. For example, in English, statements and comments are the same thing, but
in programs they are different.


The glossary at the end of each chapter is intended to highlight words and phrases that have special meanings in computer science. When you see familiar words, don’t assume that you know what they mean!

1. In computer jargon, what’s the difference between a statement and a
comment?
2. What does it mean to say that a program is portable?
3. What is an executable?


SOLUTION

1. 
A statement is a part of a program that specifies a computation.abstract
A comment is a part of a program that contains information about the program, but that has no effect when the program runs

2.
A program is portable when it can be run on more than one kind of computer.

3.
An executable is another name for object code that is ready to run.