Skip to main content
main-content
Top

About this book

Making extensive use of examples, this textbook on Java programming teaches the fundamental skills for getting started in a command-line environment. Meant to be used for a one-semester course to build solid foundations in Java, Fundamentals of Java Programming eschews second-semester content to concentrate on over 180 code examples and 250 exercises.

Key object classes (String, Scanner, PrintStream, Arrays, and File) are included to get started in Java programming. The programs are explained with almost line-by-line descriptions, also with chapter-by-chapter coding exercises.

Teaching resources include solutions to the exercises, as well as digital lecture slides.

Table of Contents

Programming Basics

Frontmatter

1. “Hello, World!”

Abstract
Java is one of the most popular programming languages. It is a descendant of the programming language C and is much related to C++. Java, like C++, embodies the concept of object-oriented programming, which allows a programmer to define a type of data with its permissible set of operations.
Mitsunori Ogihara

2. Using Data for Computation

Abstract
The previous chapter presented how to write programs using only System.out.println and System.out.print statements. This chapter introduces how to store, modify, and retrieve information. The medium in which information is stored is called data.
Mitsunori Ogihara

3. Reading Keyboard Input

Abstract
The class Scanner enables reading data from the keyboard, a String object, or a text file using the characters (the white space character ’ ’, the tab-stop ’∖t’, and the newline ’∖n’) as separators (such characters are called delimiters).
Mitsunori Ogihara

4. Decomposing Code into Components

Abstract
In this chapter, we learn how to decompose a source code into multiple methods.
Mitsunori Ogihara

5. Passing Values to and from Methods

Abstract
In this chapter, we will advance the concept of method decomposition and learn how to exchange information with methods.
Mitsunori Ogihara

6. Conditions and Their Use for Controlling the Flow of Programs

Abstract
A condition is a literal, a variable, a formula, or a method call whose value is boolean. Conditional evaluation is the action of obtaining the value of a condition.
Mitsunori Ogihara

Loops

Frontmatter

7. For-Loops

Abstract
A loop is a program structure for executing a block of code repeatedly. The code for a loop usually comes with a specification of when to stop the repetition.
Mitsunori Ogihara

8. Formatted Printing Using printf

Abstract
This chapter discusses the syntax of printf, which we have already seen before.
Mitsunori Ogihara

9. Classes String and StringBuilder

Abstract
Java provides a variety of methods for extracting information from a String data. Since String is an object class, like Scanner, we execute a method on a String data by attaching a period, the name of the method, and the parameters.
Mitsunori Ogihara

10. The Switch Statements

Abstract
The switch statement is a mechanism for controlling the flow of the program based on exact values of one data. Often a switch statement is used in a menu, where the action to be performed is selected based on a value.
Mitsunori Ogihara

11. While-Loops and Do-While Loops

Abstract
The while-loop is a loop that only requires a continuation condition. The structure of a while-loop is simple.
Mitsunori Ogihara

Arrays and Objects

Frontmatter

12. Arrays

Abstract
An array is a finite sequence of elements of some common data type, where an element of an array can be accessed by combing the name given to the array and the position of the element in the sequence, called index. Arrays are reminiscent of number sequence. In mathematics, an element of a number sequence is specified by the name of the sequence and the subscript representing the position of the element in the sequence, e.g., n 0 and n 9. In Java, numbers surrounded by square brackets specify positions and the positions always start from 0.
Mitsunori Ogihara

13. The Class Arrays and Resizing Arrays

Abstract
Arrays is a class that provides static methods for manipulating and examining arrays.
Mitsunori Ogihara

14. Multidimensional Arrays

Abstract
Arrays may have more than one dimension. We call arrays having more than one dimension multi-dimensional arrays. For an integer N ≥ 1, an N-dimensional array as a type is declared with N pairs of brackets []. In the following code, mDouble is declared as a two-dimensional array of double and myFlags is declared as a three-dimensional array of boolean.
Mitsunori Ogihara

15. Class File

Abstract
As mentioned in Chap. 3, data can be read from text files with methods applied to Scanner objects instantiated with objects of type File. File is a class that represents files (including non-text files and file folders).
Mitsunori Ogihara

16. Designing Object Classes

Abstract
In Java, object classes are used to assemble a set of data as a unit.
Mitsunori Ogihara

Advanced Concepts

Frontmatter

17. Interfaces, Inheritance, and Polymorphism

Abstract
An interface is a template for building a Java class. The components of an interface are static methods, constants, and abstract methods. An abstract method is a method header followed by a semicolon and is without a body. All abstract methods have public visibility. An abstract method specifies only its syntax, but not its semantics.
Mitsunori Ogihara

18. Generic Class Parameters and the Java Collection Framework

Abstract
Consider writing an application that interacts with the user to build a list of merchandise items. Each item is represented by its name and price (in dollars). Like the pizza applications from the previous chapter, the application must allow the user to read data from a file, write the data to a file, add an item, remove an item, and view the data. In addition, the program must allow the user to revise the name as well as the price of an item. Furthermore, the program must allow the user to rearrange the items on the list in the increasing order of the names or in the increasing order of the prices.
Mitsunori Ogihara

19. Online and Recursive Algorithms

Abstract
Online algorithms are those that process an indefinite amount of data using a fixed number of variables. Sections 7.​6 and 8.​5 presented programs for computing the Fibonacci sequence {Fi}i ≥ 0 defined by: F0 = 1, F1 = 1, and for all i ≥ 2, Fi = Fi−1 + Fi−2.
Mitsunori Ogihara
Additional information