/oop-with-java

Case studies of object oriented programming with java programming language

Primary LanguageJava

Object-Oriented Programming Concepts

  1. Real-world objects contain state and behavior.
  2. A software object's state is stored in fields.
  3. A software object's behavior is exposed through methods.
  4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.
  5. A blueprint for a software object is called a class.
  6. Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.
  7. A collection of methods with no implementation is called an interface.
  8. A namespace that organizes classes and interfaces by functionality is called a package.
  9. The term API stands for Application Programming Interface.

Language Basics

Variables

  1. The term "instance variable" is another name for non-static field.
  2. The term "class variable" is another name for static field.
  3. A local variable stores temporary state; it is declared inside a method.
  4. A variable declared within the opening and closing parenthesis of a method is called a parameter.
  5. What are the eight primitive data types supported by the Java programming language? byte, short, int, long, float, double, boolean, char
  6. Character strings are represented by the class java.lang.String.
  7. An array is a container object that holds a fixed number of values of a single type.

Operations

Answers to Questions

  1. Consider the following code snippet: arrayOfInts[j] > arrayOfInts[j+1]
    Question: What operators does the code contain?
    Answer: >, +
  2. Consider the following code snippet:
    int i = 10;
    int n = i++%5;
    a. Question: What are the values of i and n after the code is executed?
    Answer: i is 11, and n is 0.

    b. Question: What are the final values of i and n if instead of using the postfix increment operator (i++), you use the prefix version (++i))?
    Answer: i is 11, and n is 1.

  3. Question: To invert the value of a boolean, which operator would you use?
    Answer: The logical complement operator "!".

  4. Question: Which operator is used to compare two values, = or == ?
    Answer: The == operator is used for comparison, and = is used for assignment.

  5. Question: Explain the following code sample: result = someCondition ? value1 : value2;
    Answer: This code should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

Expressions, Statements, and Blocks

  1. Operators may be used in building expressions, which compute values.
  2. Expressions are the core components of statements.
  3. Statements may be grouped into blocks.
  4. The following code snippet is an example of a compound expression. 1 * 2 * 3
  5. Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a semicolon
  6. A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

Control Flow Statements

  1. The most basic control flow statement supported by the Java programming language is the if-then statement.
  2. The switch statement allows for any number of possible execution paths.
  3. The do-while statement is similar to the while statement, but evaluates its expression at the bottom of the loop.
  4. Question: How do you write an infinite loop using the for statement? Answer:
for ( ; ; ) {

}
  1. Question: How do you write an infinite loop using the while statement? Answer:
while (true) {

}

Classes and Objects

Classes

Answers to Questions

  1. Consider the following class:
public class IdentifyMyParts {
    public static int x = 7;
    public int y = 3;
} 

a. Question: What are the class variables?

Answer: x

b. Question: What are the instance variables?

Answer: y

c. Question: What is the output from the following code:

IdentifyMyParts a = new IdentifyMyParts(); 
IdentifyMyParts b = new IdentifyMyParts(); 
a.y = 5; 
b.y = 6; 
a.x = 1; 
b.x = 2; 
System.out.println("a.y = " + a.y); 
System.out.println("b.y = " + b.y); 
System.out.println("a.x = " + a.x); 
System.out.println("b.x = " + b.x); 
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

Answer: Here is the output:
a.y = 5
b.y = 6
a.x = 2
b.x = 2
IdentifyMyParts.x = 2

Because x is defined as a public static int in the class IdentifyMyParts, every reference to x will have the value that was last assigned because x is a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only one x: when the value of x changes in any instance it affects the value of x for all instances of IdentifyMyParts.