Test 1 Review Sheet
This is a checklist of topics that have been covered (or reviewed, in the
case of basics that are common to C++ and Java) in the course so far.
Building Basic Java Programs
-
A Java source file must end with the extension
.java
and should have the exact same name as the public class name.
-
To compile a class in a Java source code file (say
Yadda.java
)
type:
javac Yadda.java
-
Each class (there may be more than one) in a source code file, is
compiled into a
.class
file.
-
To execute a java application (say named
Yadda.class
) type
java Yadda
-
The
main
method has the following signature
public static void main(String[] args)
-
Single line comments in Java start with
//
Block-style comments in Java start with /*
and end
with */
Basic Elements
-
Identifiers are the user-defined names in programming.
(ex: variables, constants, methods, classes, labels, packages, etc.)
-
Made up of characters letters, underscores (_), dollar
signs ($), or digits.
-
Must NOT start with a digit as the first character
-
Must not be a reserved word
-
Case Sensitive
- Built-in (primitive) types (boolean, char, byte, short, int,
long, float, double)
- Arithmetic operators
- Assignment operator =
-
Shortcut Operators (
+= -= /= *= %=
++ --
)
-
Know which type conversions are automatically allowed, and how to do
casting for others.
-
Comparison operators
-
Boolean operators
The short-circuit operators (conditional operators) are (&& and
||), know how these differ from the unconditional operators (& and
|)
Control Structures (Chapters 4-5)
-
Know the
if
statement
-
Know the
switch
statement
remember that a break
statement is need at the end of
each case
statement, otherwise execution falls
through to the next case
statement. The
default
case is executed when no other case matches.
-
Know the ternary operator or Conditional expressions
booleanExpression ? expression1 : expression2
If booleanExpression
is true
then the
result is expression1
else the result is
expression2
-
Know
for, while
and do
loops
Given one of them be able to create identical behaving loops using
the other two.
Be able to write loop-based algorithms.
- break and continue statements -- know what effect
each one has
Methods (Chapter 6)
-
Know how to create and code a method
-
Know how to call a method, given its definition (or more specifically,
its prototype)
-
Know the difference between passing a primitive type to (or
returning from) a method vs. passing an array or an object
to (or returning from) a method
-
Know how to call the
Math
methods we've used so far (in
examples or homeworks), like sqrt
and random
Console I/O
- System.out -- standard output (object of type
PrintStream
- Understand how to do console output with System.out,
including the methods:
- print()
- println()
- printf()
- System.in -- standard input
- Be able to do basic console input with the Scanner class
Arrays (Chapter 7)
-
Understand arrays, this includes multi-dimensional arrays
- Know how to declare an array variable, and then create the array
with the new operator
int[] array1 = new int[size];
-
Understand how array indexing is used, this includes
multi-dimensional arrays
Remember array indexing starts at zero and NOT
one.
given
array =
{
{1, 2, 3, 4},
{5, 6, 7},
{8}
}
Then array[1][2]
is equal to 7.
-
Know that the length of an array (say named
array
) can be
found by using array.length
-
Know that the reference (name) of an array can point to any array
of that type
(two array references can point to the
same array)
-
Understand all the ways to declare an array variable. All the
following are the same and are legal
(the first is the preferred way):
int[][][] thingy;
int[][] thingy[];
int[] thingy[][];
int thingy[][][];
-
Understand how to declare, create, and initialize an array all at
once
int[] bubba = {2, 5, -56};
- Know how to pass arrays in and out of methods
Some practice array algorithms to try (coding practice)
(You should be able to do these and other similar array algorithms)
- Compute and print the sum (product, average) of the elements of a
numerical array.
- A method that returns the maximum element of an array
- A method that prints all array elements that are between two given
values (parameters)
- A method that counts up all the even numbers in an array
- A method that returns an array that contains all odd elements from
an original array (incoming parameter)
Newer Java 1.5.0 features discussed
- Enhanced for-loops (Deitel 7.6)
- Variable-length parameter lists (Deitel 7.11)
- class java.util.Scanner, for easy stream input --
know how to create a Scanner object and use it to get data from
standard input
Classes and Objects (chapter 8)
- Objects and classes and the relationship between them
- Constructors: what they are and how to build them
- The modifiers public and private
- The static modifier, and the difference between
static variables and methods vs. instance variables and methods, as
well as which variables can be accessed from which methods
- Understand how to declare an object reference variable and
then create the object with
new
, along with how
to invoke the chosen constructor
- Understand how objects are passed in and out of methods
- Class scope vs. local scope (like in a method)
- The keyword this
- Arrays of objects (array of reference variables, each can attach to
an object)
Tools
- Understand packages, how to use the import statement to
bring a class or package into a program, and that java.lang
is automatically part of every program
- Understand how to use the package statement to place a
class into a programmer-defined package. This includes understanding
some javac and java command line options:
- -d : for specifying where the result of a javac
command will be placed. Also helps build a package hierarchy
- -classpath : for specifying the locations of all classes or
packages needed (for both javac and java)
- Know how to use javac and java commands to
compile and run java applications, including how to run from a
runnable jar file
- Know the basic usage of the -d option on
javac
- Know the usage of the -classpath (or -cp) flag on
both javac and java
- Know how to create a
jar
file (including a "runnable"
jar
file).
- javadoc -- understand the basics of javadoc
documentation
Strings
-
Know how to construct a String object
-
Know the common String methods: length(), concat(), equals(),
compareTo(), charAt(), substring(), trim(), toLowerCase(),
toUpperCase(), replace()
-
Know the common StringBuilder (also StringBuffer
methods: construction from a String, append(), insert(),
delete(), length(), charAt(), substring()
-
Understand that a String is immutable. A StringBuilder is mutable.
(Know what this means!)
-
Understand how to process command line arguments
Inheritance and Polymorphism (chapters 9 and 10)
- Superclasses and subclasses, and deriving a class
with the keyword extends
- The use of super to invoke parent constructors
and methods
-
Understand overriding methods in derived classes
- Every class is derived in some way from class Object.
Understand these 3 special methods inherited from Object by
every class:
- equals()
- toString()
- clone()
- Understand the modifiers protected and final, and
what they do when applied to class data and methods
-
Abstract classes and their relationship with abstract methods
- Understand polymorphism and dynamic binding
- How base class variables can be attached to derived class objects
- How base class methods can be overridden in derived classes
- How dynamic binding allows method calls (through base class
variables) to still invoke child versions of overridden
methods
- When casting is necessary between parent and child, and when it is
not
-
Understand interfaces, how to declare one and how to extend one
-
Know how a class declares that it will implement an interface
-
Understand relationship of data and methods with interfaces
- Understand the basic interfaces discussed in examples
(Comparable and Clonable)
Exception Handling (chapter 11)
-
Understand Exceptions, understand differences between
checked exceptions, unchecked exceptions, and errors
-
Understand try, catch, finally blocks and relationship
between them
-
Understand the throws and throw keywords and when to
use both
-
Custom exception types can be built, derived from Exception or one of
its derived classes
Nested Classes and Inner Classes
- Understand the concept of a nested class, along with their
relationship with enclosing class.
- Can be static or non-static
- An inner class is a nested class that is non-static
- Know what the '.class' file of a nested class looks like
- Know what an anonymous inner class is (and how it differs from
a regular inner class)
Sorting Arrays
- Know how to use the features of class Arrays for basic
sorting
- Understand what is meant by the "natural ordering" of a type
- Understand how to create another sorting rule using the
Comparator interface
- Understand which sort() method to use (from class
Arrays) and how to use, for either situation
Files and Streams
- Understand what streams are
- input streams vs. output streams
- byte streams vs. character streams
- formatted text files vs. binary files
- sequential files vs. random-access files
- InputStream, OutputStream, Reader, Writer classes (know which are
byte streams and which are character streams)
- understand the use of class File
- File stream classes (byte and char)
- Buffered streams
- Know how to open an input file and read data from it
(read() method)
- Know how to open an output file and write data to it
(write() method)
- Understand the uses of special classes like Formatter and
Scanner, especially in the reading and writing of
files
- Know the predefined stream objects System.out,
System.in, and System.err
- Understand what object serialization is (for writing and reading
objects to and from files). This includes understanding:
- interface Serializable
- classes ObjectOutputStream and
ObjectInputStream
Programming hints
Here is a sample programming problem similar to how I might ask a
question on the test
Sample Exercise
Write a method that will return the number of elements in an array
that are greater than a given number.
Given this sample main
method:
public static void main(String[] args)
{
int[] array = {14, 6, 3, 2, 8, 5};
System.out.println("Given the following: array = ");
printArray(array);
System.out.println();
System.out.println("The number of elements greater than 5 is " +
greater(array, 5));
}
The output is:
Given the following: array = {14, 6, 3, 2, 8, 5}
The number of elements greater than 5 is 3
Write the method called greater