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: Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s2 contains the substring "art". Otherwise, the method should return false.
Which of the following method calls demonstrates that the method does NOT work as intended?
containsArt("rattrap", "similar", "today")
containsArt("start", "article", "Bart")
containsArt("harm", "chortle", "crowbar")
containsArt("matriculate", "carat", "arbitrary")
containsArt("darkroom", "cartoon", "articulate")
2.
Q2: Considering the following class, which of the following methods can be added to the SomeMethods class without causing a compiler error?
public class SomeMethods {
public void one(int first) {
/* implementation not shown */ }
public void one(int first, int second) {
/* implementation not shown */ }
public void one(int first, String second) {
/* implementation not shown */ }
} // end SomeMethods
public void one(int value) …
public void one(String first, int second) …
public void one(int first, int second, int third) …
I only
I and II only
I and III only
II and III only
I, II and III
3.
Q3: 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.printf("d d d", one.getNum(), two.getNum(), three.getNum());
100 100 100
300 100 100
300 100 300
300 300 100
300 300 300
4.
Q4: Considering the following instance variables and incomplete method that are part of a class that represents an item. Method updateAge is used to update the variables based on the parameter extraMonths that represents the number of months to be added to the age.
Figure9.12.1.
I only
II only
III only
II and III only
I, II and III
5.
Q5: Considering the following instance variables and method that appear in a class representing 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?