Shared Flashcard Set

Details

Itro Computer Science 2 II
University of San Francisco Sami Rollins
30
Computer Science
Undergraduate 2
10/16/2007

Additional Computer Science Flashcards

 


 

Cards

Term

+What is a variable and how do you declare one? Definition

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:

int a;

double b, c;

Term

+What are valid variable types? Which types are primitive?

Definition

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. 

Term

+What is initialization?


Definition

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.

a = 5;

double b = 9.4

Recall that the single equals sign is often read as gets the value of (i.e, a gets the value of 5).

Term

+What is an escape sequence?

Definition

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 \\.

Term

+What is a reference?

Definition
A reference is the address or memory location of an object. Recall that two variables may refer to the same object.
Term

+Explain the difference between == and .equals. Definition

== 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.

Term

+What is casting?

Definition

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

Term

+Explain DeMorgan's Theorem.

Definition

DeMorgan's Theorem says the following:

!(a || b) = !a && !b

 

!(a && b) = !a || !b

Term

+Explain the syntax and functionality of Java's loops? When would you use each type? Definition

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);

Term

+What is a class?

Definition

A class defines the state or data contained in an object as well as the behaviors or methods that the object can perform.
Term

+What is an object?

Definition
An object is an instance of a class.
Term

+What is instance data?

Definition
Instance data refers to the variables defined in a class.
Term

+What is a method?

Definition

A method is simply a set of statements. The statements are executed when the method is called.

Term

+What is an accessor method? What is a mutator method? Definition

Accessor methods provide access to data while mutator methods change data.

Term

What is contained in a method signature?

Definition

A method signature is defined by the method name and the type, number, and order of its parameters.

Term

+What is contained in a method header?

Definition

A method header contains the method's visibility modifier, the return type, the method name, and the type, number, and order of its parameters.

Term

+What is a parameter or argument?

Definition

A parameter is a piece of data passed into a method.

Term

+What is a constructor?

Definition

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.

Term

+Explain scope.

Definition

Scope describes the portion of a program where a particular variable can be referenced.

Term

+Explain the purpose of the dot operator.

Definition

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();

Term

+What is overloading?

Definition

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);

}

Term

+What is the garbage collector?

Definition

The garbage collector periodically determines which objects are no longer referred to and releases their resources back to the system.

Term

+Explain the term import.

Definition
The import statement is used to enable a class to use the classes defined in another package or library.
Term

+Explain the term static.

Definition

If a variable is declared static only one instance of the variable is shared by all objects of the class.

Term

Identifier Naming

Definition
Method and variable names generally start with a lowercase letter and class names normally start with an uppercase letter. Identifiers are often a multi-word description of what they are identifying, and uppercase letters are used to specify word breaks. For example, a variable representing a first name might be called firstName. A class representing a bank account might be called BankAccount.
Term

Indentation

Definition
Code between an open brace ({) and the corresponding close brace (}) should be indented a consistent number of spaces. This applies to if statements, loops, method definitions, and class definitions. The close brace should always be lined up with the header of the statement. L&L suggest that the open brace be on a new line, however putting the open brace at the end of the header line is also common. Choose your own style, and be consistent!
Term

Design Goals 5

Definition
  1. Reusability - As you develop more programs, you will undoubtedly notice that many of them require similar functionality. If you are careful in designing your programs, you should be able to easily reuse components in future programs. For example, you might notice that lots of your programs require you to maintain a sorted list of items. If you were careful in designing that very first program that required a sorted list, you probably implemented a general purpose class that maintained a sorted list of items and supported methods such as add, remove, and find. You can easily reuse this class in any program that requires a sorted list.
  2. Robustness - It is important to ensure that your programs are robust to failures and errors, and handle all situations gracefully. Among other things, this means that your design should limit the number of errors that might be triggered by misbehaving code. Encapsulation, a fundamental object-oriented principle, refers to limiting how data is modified by making data members private and allowing access to and modification of the data only through limited methods. It is one way to help ensure that your programs are robust.
  3. Extensibility - Anyone with a computer knows that software updates happen continuously. Software is continually being extended to support new functionality. Clearly, in order to avoid having to rewrite a program from scratch each time it is updated, a design should easily support integration of new classes.
  4. Modularity - Designing a program as a set of interoperating yet independent modules makes development and maintenance much simpler. It can be very intimidating to think about writing a program containing hundreds or thousands of lines of code. However, if a programmer takes the time to design the program as several modules that can be implemented and tested independently, the task of implementation becomes much more manageable.
  5. Efficiency - The easy way to do something is not always the most efficient. It is important to understand the resources required to accomplish a task and to ensure that your algorithms make efficient use of those resources. We'll touch on this topic in CS 112, but you will learn more about this in CS 245.
Term
Class Responsibilities Collaborators (CRC) Model
Definition
The CRC model is an approach to design wherein the designers use index cards to represent the classes interacting in a program. For each class, the designers identify the responsibilities of the class and the classes with which it will need to collaborate in order to execute its responsibilities.
Term
Unified Modeling Language (UML)
Definition
UML class diagrams represent each class with a rectangle, inside of which are the data members and methods supported by the class. Dotted arrows are drawn between classes to indicated that one class invokes the methods of another. There are many other types of UML diagrams as well.
Term
Identifying Classes
Definition
The CRC model and UML help you to think through what each class should contain and how to relate one class to another. But, the question of how to identify an initial set of classes still remains. It is certainly worth your time to revisit Chapter 6 (particularly 6.1-6.4) of the L&L text.
Supporting users have an ad free experience!