Differentiate class-level static vs. instance/object-level variables
Differentiate class-level static vs. instance/object behaviors/methods
Define instance variables (that you want to be interrelated)
Name
Data Type
private
Define class variables static as needed
Name
Data Type
public / private / final
Create constructor (behavior) that creates initial state of object
Overloaded constructor (with as many parameters)
public
Same name as class
No return type
Default - no parameters
Logic - initialize all variables
Repeat as needed, adding parameters
Create 1 accessor and 1 mutator behaviors per attribute
Accessors
Name is get_<attr_name>
Public
Return type same data type as attribute
No parameters
Logic - return value
Mutators
Name is set_<attr_name>
Public
Return type is void
Parameter is same data type as attribute
Logic validates input parameter and sets attribute value
Write toString method
public
Returns String
No parameters
Logic - convert needed attributes to a format that can be printed
Write equals method
public
Returns boolean
Parameter - instance of the class
Logic - compare attributes for equity
Create additional methods as needed
ExercisesExercises
1.
Q1: Considering the following incomplete class definition, which of the following constructors can be added to the SomeClass class without causing a compiler error?
public class SomeClass {
public SomeClass(int first ) {
/* implementation not shown */ }
public SomeClass(int first, int second) {
/* implementation not shown */ }
public someClass(int first, String second) {
/* implementation not shown */ }
} // end SomeClass
public SomeClass( ) …
public SomeClass(String first, int second) …
public SomeClass(String first) …
I only
I and II only
I and III only
II and III only
I, II and III
2.
Q2: Considering the following class declaration:
public class SomeClass {
private int num;
SomeClass (int n) {
num = n;
}
public void increment (int more) {
num = num + more;
}
public int getNum() {
return num;
}
}
The following code segment appears in another class. What is the resulting output?
SomeClass one = new SomeClass(100);
SomeClass two = new SomeClass(100);
SomeClass three = one;
one.increment(200);
System.out.println("d d d", one.getNum(), two.getNum(), three.getNum());
100 100 100
300 100 100
300 100 300
300 300 100
300 300 300
3.
Q3: Consider a class with the following instance variables and incomplete method updateAge. Method updateAge should update the instance attributes based on the parameter extraMonths, which represents the number of months to be added to the age. Which of the following code segments could be used to replace /* body of updateAge */ so that the method will work as intended?
private int years; // age of item in years
private int months; // age of item in months, 0 <= months <= 11
// example, item can be 1 year 3 months old
public void updateAge (int extraMonths) { //extraMonths >= 0
/* body of updateAge */
}
I only
II only
III only
II and III only
I, II and III
4.
Q4: Consider a class with the following instance attributes and partial method declaration. The class should represent student information.
private int assignmentCompleted;
private double testAverage;
public boolean isPassing( ) {
/* body of isPassing */
}
A student can pass a programming course if at least one of the following conditions is met:
The student has a test average that is greater than or equal to 90.
The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments.
Which of the following code segments could be used to replace /* body of isPassing */ so that the method will work as intended?