Java Reference
In-Depth Information
Objects contain methods that implement their behavior. To
call a method on an object, write its name, followed by a
dot, followed by the method name.
Some programs are interactive and respond to input from
the user. These programs should print a message to the
user, also called a prompt, asking for the input.
A String object holds a sequence of characters. The char-
acters have indexes, starting with 0 for the first character.
Java has a class called Scanner that reads input from the
keyboard. A Scanner can read various pieces of input
(also called tokens) from an input source. It can read
either one token at a time or an entire line at a time.
An exception is an error that occurs when a program has
performed an illegal action and is unable to continue exe-
cuting normally.
Self-Check Problems
Section 3.1: Parameters
1. What output is produced by the following program?
1 public class Odds {
2 public static void main(String[] args) {
3 printOdds(3);
4 printOdds(17 / 2);
5
6 int x = 25;
7 printOdds(37 - x + 1);
8 }
9
10 public static void printOdds( int n) {
11 for ( int i = 1; i <= n; i++) {
12 int odd = 2 * i - 1;
13 System.out.print(odd + " ");
14 }
15 System.out.println();
16 }
17 }
2. What is the output of the following program?
1 public class Weird {
2 public static void main(String[] args) {
3 int number = 8;
4 halfTheFun(11);
5 halfTheFun(2 - 3 + 2 * 8);
6 halfTheFun(number);
 
Search WWH ::




Custom Search