Test 1 Review Sheet
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
-
Know how to create a
jar
file.
-
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
-
Understand (and be able to code) recursive methods
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
-
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. 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)
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
Basic GUI
- Understand the basic use of class
JOptionPane
- Know that this is part of the
javax.swing
package
- Understand the basic types of JOptionPanes (message dialog, input
dialog, confirm dialog (yes/no/cancel)).
Programming hints
Here is a sample programming problem similar to how the test will
ask a question.
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
The method signature is:
public static int greater(int[] array, int number)