Unit 5: Writing Classes Flashcards

1
Q

What are the “big three”?

A
  1. The instance variables
  2. Accessor and Mutator methods
  3. A toString method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Instance Variables

A

Private variables that are used

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

Accessor Methods

A

An instance method that gets the value of a property of an object

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

Mutator Methods

A

An instance method that sets the value of a property of an object

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

What differentiates private and public variables?

A

A private variable can only be directly accessed from within the class. A public variable can be accessed from outside the class.

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

Static Variables

A
private static type name;
or,
private static type name = value;
– Example:
private static int theAnswer = 42;
static variable: Stored in the class instead of each object.
– A "shared" global field that all objects can access and modify.
– Like a class constant, except that its value can be changed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Final Static fields

A
public static final type name;
or,
public static final type name = value;
– Example:
public static final int NUMOFMONTHS = 12;
Final static variable:
– A class constant whose value cannot be changed. Usually public.
– ALL CAPS by convention.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Instance Variables

A

private type name;
or,
private type name = value;
– Example:
private int id = 243342;
instance variable: Stored in an object instead of the class.
– each object has its own copy of the instance variable.

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

Static vs Instance Call

A
A static method is called through the name of the class.
An instance method is called through the name of an object. 
public class Main {
public static void main(String[] args) {
BankAccount a = new BankAccount("Jim Smith");
//getID is instance
// uses object name + dot notation to call
System.out.println(a.getID());
//getNumAccounts is static
// uses class name + dot notation to call
System.out.println(BankAccount.getNumAccounts());
       }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between an interface and an abstract class?

(A) There is no difference.

(B) Abstract classes can have methods with bodies (code in them), but interfaces can not.

(C) Abstract classes can be instantiated, while interfaces can not.

(D) Abstract classes can be extended, but interfaces can not.

(E) Abstract classes can declare abstract methods, but interfaces can not.

A

(B) Abstract classes can have methods with bodies (code in them), but interfaces can not.

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

Scope

A
• scope: The part of a program where a variable exists.
– From its declaration to the end of the { } braces
• A variable declared in a for loop exists only in that loop.
• A variable declared in a method exists only in that method.
public static void example() {
int x = 3;
for (int i = 1; i <= 10; i++) {
System.out.println(x);
}
// i no longer exists here
} // x ceases to exist here
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The “this.” keyword

A

“this.” : Within a non-static method or a constructor, the keyword
this is a reference to the current object—the object whose
method or constructor is being called.
– Refer to a field: this.field
– Call a method: this.method(parameters);
– One constructor this(parameters);
can call another:

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

Variable shadowing

A
• shadowing: 2 variables with same name in same scope.
– Normally illegal, except when one variable is a field.
public class Point {
private int x;
private int y;
...
public Point(int x, int y) {
}
– In most of the class, x and y refer to the instance variables.
– In the constructor, x and y refer to the method's parameters.
The instance 
variables x and y 
are global 
variables.

The parameter
variables x and y
are local variables

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

Fixing shadowing

A
"this" can be omitted if it is clear which variable is being referenced. The keyword "this" is helpful to fix the shadowing 
problem. 
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Inside the constructor(or any method with shadowing):
– To refer to the data field x, say this.x
– To refer to the parameter x, say x
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Constructors

A
One constructor can call another using the this keyword. This 
helps us reuse another constructor's code.
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(){
this(0, 0);
     }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Point Revisited

A
public class Point {
private int x;
private int y;
public Point(){
this(0, 0);
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public double distanceToPoint(Point other){
int dx = this.x – other.x;
int dy = this.y – other.y;
return Math.sqrt(dx * dx + dy * dy);
}
public double distanceToOrigin(){
Point origin = new Point();
return this.distanceToPoint(origin);
} }
}
17
Q

A “swap” method?

A
• Does the following swap method work? Why or why not?
public static void main(String[] args) {
int a = 7;
int b = 35;
// swap a with b?
swap(a, b);
System.out.println(a + " " + b);
// 7 35 (unchanged)
}
public static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
18
Q
  1. Which statement about parameters is false?
    (A) The scope of parameters is the method in which they are defined.
    (B) Static methods have no implicit parameter this.
    (C) Two overloaded methods in the same class must have parameters with different names.
    (D) All parameters in Java are passed by value.
    (E) Two different constructors in a given class can have the same number of
    parameters.
A

(C) Two overloaded methods in the same class must have parameters with different names.

19
Q
2. Which of the following will cause an error message?
I Date d1 = new Date(8, 2, 1947);
Date d2 = d1;
II Date d1 = null;
Date d2 = d1;
III Date d = null;
int x = d.year();
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
A

(C) III only

20
Q
3. A client program creates a Date object as follows:
Date d = new Date(1, 13, 2002);
Which of the following subsequent code segments will cause an error?
(A) String s = d.toString();
(B) int x = d.day();
(C) Date e = d;
(D) Date e = new Date(1, 13, 2002);
(E) int y = d.year;
A

(E) int y = d.year;

21
Q
  1. Which of the following correctly constructs a Date object in a client class?
    (A) Date d = new (2, 13, 1947);

(B) Date d = new Date(2, 13, 1947);

(C) Date d;
d = new (2, 13, 1947);

(D) Date d;
d = Date(2, 13, 1947);

(E) Date d = Date(2, 13, 1947);

A

(B) Date d = new Date(2, 13, 1947);