WEEK 2 - OBJECT ORIENTED JAVA PROGRAMS AND ARRAYS REVIEW Flashcards

1
Q

What are programs made of?

A

Objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What defines objects?

A

Classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a class?

A

A class defines what objects look like and are things implemented during the design time.

They are defined through:
- Using the class keyword
- All Java code consists of classes
- All Java code is “part of” some class
- Big programs can have many classes
- Think of a class as a recipe to create objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When are objects created and what defines them?

A

Objects are created from classes at run time.

What defines an object?
- Each object’s properties are defined by the class it was created from
- Many objects can be made from one class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is in a class?

A

Information or data (what the objects will be made of) and functionality (what the object will do)

  1. The elements of a class that contain information (data) are called variables. The variables defined for the whole class are called fields.
  2. The elements of a class that execute commands to implement functionality are called methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax for a field variable?

A

<visibility> <type> <name>;

* Visibility modifier is optional
</name></type></visibility>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to declare a field variable and initialize it in a combined statement

A

<visibility> <type> <name> = <value>;
</value></name></type></visibility>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the automatically initialized value given to field variables?

A

Integer, floating point and char = 0 or 0.0.

Boolean = false.

String and any class = null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are user-defined types?

A

User-defined types can be predefined by the class libraries that are provided by the Java runtime. Classes are user-defined types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the . (dot) operator?

A

An accessibility operator used to access an element that is defined inside an object you must use the object variable followed by the . (dot) operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are attributes?

A

Information in field variables.

Example.
public class Circle {
private double radius = 10.0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

An object is made of ________?

A

Fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are fields?

A

Variables declared directly in the class definition block (not in a method).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create an example of a method

A

public void methodName() {
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create an example of a method with one parameter

A

public void methodName(dataType paraName) {
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Create an example of a method with two parameters

A

public void methodName(dataType paraName, dataType paraName) {
}

17
Q

What is a method call statement?

A

A method is called using a method call statement which gives
1. The name of the method to be called
2. The list of values to be passed as parameters (without data types!)
3. Each value could be a calculation or expression

18
Q

What are the two “styles” of method call?

A
  1. Without an object variable (calling another method of the same class)
  2. With an object variable (calling a method of that object)
19
Q

How does a method return a value it calculated to the caller?

A

Using the return statement.

20
Q

What is the syntax of a return statement?

A

<visibility> <dataType> <methodName> (dataType param?, ...) {

return methodExpression;

}

Ex.
public int addNumbers(int num1, int num2)
{
return num1 + num2;
}
</methodName></dataType></visibility>

21
Q

What are accessor (getter) methods?

A

Accessor methods return the value of a private variable, allowing other classes access to that stored variable.

22
Q

What is the syntax for an accessor (getter) method?

A

<visibility> <dataType> <getMethodName> () {
return variableName;
}

Example.
public String getFirstName() {
return _firstName;
}
public double getRadius() {
return _radius;
}
</getMethodName></dataType></visibility>

23
Q

What are mutator (setter) methods?

A

Mutator methods reset the value of a private variable. This gives other classes the ability to modify the value stored in that variable without having direct access to the variable itself.

24
Q

What is the syntax for a mutator method?

A

<visibility> <dataType> <setMethodName> (dataType paramName) {
variable = newVariable;
}

public void setFirstName(String newName) {
_firstName = newName;
}
public void setRadius(double radius) {
_radius = radius;
}
</setMethodName></dataType></visibility>

25
Q

What is a constructor?

A

A constructor is a special method whose role is to initialize the object when an object is created.

The constructor is called ONLY when an object is created (‘new’).

Name of the constructor is ALWAYS the same as the name of the class.

A constructor is normally used to initialize field variables defined by the class.

26
Q

What are the four rules of constructors?

A
  1. Constructors must have the same name as the class itself.
  2. Constructors do not have a return type – not even void.
  3. Constructors are invoked using the new operator when an object is created.
  4. Constructors play the role of initializing objects.
27
Q

What is a no-arg constructor?

A

A constructor with no parameters.

28
Q

What is a default constructor?

A

A constructor that has no parameters.

A default constructor is automatically generated if no other constructors are defined for a given class/

29
Q

What is method overloading?

A

Method overloading means more than one method with the same name.

Any method can be overloaded.

The list of parameters must be different in each method.

30
Q

What is a static method?

A

Methods that are shared by all objects of the same class, and are independent from any particular object (instance).

31
Q

What are static fields?

A

Static field variables are shared by all objects of the same class,
and are independent from any particular object (instance).
There is only one copy of a static field variable.