WEEK 2 - OBJECT ORIENTED JAVA PROGRAMS AND ARRAYS REVIEW Flashcards
What are programs made of?
Objects
What defines objects?
Classes
What is a class?
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
When are objects created and what defines them?
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
What is in a class?
Information or data (what the objects will be made of) and functionality (what the object will do)
- The elements of a class that contain information (data) are called variables. The variables defined for the whole class are called fields.
- The elements of a class that execute commands to implement functionality are called methods.
What is the syntax for a field variable?
<visibility> <type> <name>;
* Visibility modifier is optional
</name></type></visibility>
How to declare a field variable and initialize it in a combined statement
<visibility> <type> <name> = <value>;
</value></name></type></visibility>
What is the automatically initialized value given to field variables?
Integer, floating point and char = 0 or 0.0.
Boolean = false.
String and any class = null.
What are user-defined types?
User-defined types can be predefined by the class libraries that are provided by the Java runtime. Classes are user-defined types.
What is the . (dot) operator?
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.
What are attributes?
Information in field variables.
Example.
public class Circle {
private double radius = 10.0;
}
An object is made of ________?
Fields.
What are fields?
Variables declared directly in the class definition block (not in a method).
Create an example of a method
public void methodName() {
}
Create an example of a method with one parameter
public void methodName(dataType paraName) {
}
Create an example of a method with two parameters
public void methodName(dataType paraName, dataType paraName) {
}
What is a method call statement?
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
What are the two “styles” of method call?
- Without an object variable (calling another method of the same class)
- With an object variable (calling a method of that object)
How does a method return a value it calculated to the caller?
Using the return statement.
What is the syntax of a return statement?
<visibility> <dataType> <methodName> (dataType param?, ...) {
return methodExpression;
}
Ex.
public int addNumbers(int num1, int num2)
{
return num1 + num2;
}
</methodName></dataType></visibility>
What are accessor (getter) methods?
Accessor methods return the value of a private variable, allowing other classes access to that stored variable.
What is the syntax for an accessor (getter) method?
<visibility> <dataType> <getMethodName> () {
return variableName;
}
Example.
public String getFirstName() {
return _firstName;
}
public double getRadius() {
return _radius;
}
</getMethodName></dataType></visibility>
What are mutator (setter) methods?
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.
What is the syntax for a mutator method?
<visibility> <dataType> <setMethodName> (dataType paramName) {
variable = newVariable;
}
public void setFirstName(String newName) {
_firstName = newName;
}
public void setRadius(double radius) {
_radius = radius;
}
</setMethodName></dataType></visibility>