Overview and Java Review
Computer Science 112 - Introduction to Computer Science II
Why take this class?
This class is designed to introduce you to intermediate-level programming
concepts. You will practice and apply these concepts by developing
intermediate-level programs in Java. Though you may hear lots of people refer
to this class as a Java programming class, the goal of this class is not
simply to teach you how to write Java programs. Rather, you should come away
with a firm understanding of general problem solving concepts that you can
apply broadly. Similarly, this class should provide you with the foundation
to easily learn and utilize any programming language, not just Java.
Discussion questions:
- What do you like about programming?
- What do you find particularly challenging with respect to
programming?
Java Basics
+What is a variable and how do you
declare one?
A variable is a name for a location in memory used to hold a data value.
(L&L pp. 66)
The value stored in a variable may change. A variable is declared as
follows:
+What are valid variable types?
Which types are primitive?
A variable's type may be any of the 8 primitive types. The 8 primitive
types are int, double, float, char, byte, boolean, long, and short. A
variable may also refer to an object. Strings are a common example of
this.
The class used to define an object can be thought of as the type of an
object. (L&L pp. 114).
+What is
initialization?
Initialization is the process of assigning an initial value to a variable.
Following is an example assignment statement that assigns the value 5 to the
variable a and example statement that declares the variable b and assigns it
the value 9.4.
Recall that the single equals sign is often read as gets the value
of (i.e, a gets the value of 5).
+What is an escape
sequence?
An escape sequence is used to represent a special character within a
String. Escape sequences are written as a backslash followed by a second
character. Common examples are \n, \t, \", and \\.
+What is a reference?
A reference is the address or memory location of an object. Recall that
two variables may refer to the same object.
+Explain the difference between ==
and .equals.
== compares the values stored in two variables. When used to compare two
primitive values, it will evaluate to true if the values are the same and
false otherwise. When used to compare two references, it will evaluate to
true if the references refer to the exact same memory location. To evaluate
the actual object contents, .equals is often used.
+What is casting?
Casting is used to convert a variable of one type into a variable of
another type. For example, you might cast a variable of type int to be a
variable of type double in order to ensure that the result of dividing an int
by an int is a floating point number.
int a = 5;
int b = 2;
double c = a/b; //no casting used, c is 2.0
double d = (double)a/b; //casting used, d is 2.5
+Explain DeMorgan's
Theorem.
DeMorgan's Theorem says the following:
!(a || b) = !a && !b
!(a && b) = !a || !b
+Explain the syntax and
functionality of Java's loops? When would you use each type?
Java has three types of loops: for, while, and do-while.
for loops are often used to execute a set of statements a
predetermined number of times. The initialization,
condition, and update are all written in the header of the
loop.
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
while loops execute a set of statements until some
general condition becomes false. The initialization of the control variable
occurs prior to the loop, the condition is checked before the loop body is
ever executed, and the update must appear in the loop body. You can always
rewrite a for loop to be a while loop and vice versa.
int i = 0;
while (i < 10){
System.out.println(i);
i++;
}
do-while loops are different from for and while loops in
that the body of a do-while loop always executes at least once. The control
variable must be initialized outside of the loop, the body of the loop
executes once before the condition is checked, and the update must occur in
the loop body.
int i = 0;
do {
System.out.println(i);
i++;
} while(i < 10);
+What is a class?
A class defines the state or data contained in an object as well as the
behaviors or methods that the object can perform.
+What is an
object?
An object is an instance of a class.
+What is instance
data?
Instance data refers to the variables defined in a class.
+What is a method?
A method is simply a set of statements. The statements are executed when
the method is called.
+What is an accessor method? What
is a mutator method?
Accessor methods provide access to data while mutator methods change data.
What is contained in a method
signature?
A method signature is defined by the method name and the type, number, and
order of its parameters.
+What is contained in a method
header?
A method header contains the method's visibility modifier, the return
type, the method name, and the type, number, and order of its parameters.
+What is a parameter or
argument?
A parameter is a piece of data passed into a method.
+What is a
constructor?
A constructor is a special method that is invoked to create a new instance
of a class. The constructor must have the same name as the class.
+Explain scope.
Scope describes the portion of a program where a particular variable can
be referenced.
+Explain the purpose of the dot
operator.
The dot operator enables access to the members of an object. Most
commonly, the dot operator is used to invoke methods on an object.
String s = "Hello";
int length = s.length();
+What is
overloading?
Overloading refers to having two methods with the same name but different
signatures.
void printGreeting(String firstname) {
System.out.println("Hello, " + firstname);
}
void printGreeting(String firstname, String lastname) {
System.out.println("Hello, " + firstname + " " + lastname);
}
+What is the garbage
collector?
The garbage collector periodically determines which objects are no longer
referred to and releases their resources back to the system.
+Explain the term
import.
The import statement is used to enable a class to use the classes defined
in another package or library.
+Explain the term static.
If a variable is declared static only one instance of the variable is
shared by all objects of the class.
Sami Rollins
Date: 2007-08-10