Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

1.16. Exercises

Exercise 1-1. Write a program that counts from 1 to 15, printing out each number, and then counts backward by twos back to 1, again printing out each number.

Exercise 1-2. Each term of the Fibonacci series is formed by adding the previous two terms. What sort of series do you get if you add the previous three terms? Write a program to print the first 20 terms of this series.

Exercise 1-3. Write a program that takes two numbers and a string as command-line arguments and prints out the substring of the string specified by the two numbers. For example:

% java Substring hello 1 3

should print out:


Handle all possible exceptions that might arise because of bad input.

Exercise 1-4. Write a program that interactively reads lines of input from the user and prints them back out, reversed. The program should exit if the user types "tiuq".

Exercise 1-5. The SortNumbers class shows how you can sort an array of doubles. Write a program that uses this class to sort an array of 100 floating-point numbers. Then, interactively prompt the user for numeric input, and display the next larger and next smaller number from the array. You should use an efficient binary search algorithm to find the desired position in the sorted array.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint