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
Subsection10.5.1
ExercisesExercises
1.
Q13: Click on all the attributes of the class (4 of them).
public class Book {private String title;private String author;private String publisher;private int copyrightDate;public Book() {title = "";author = "";publisher = "";copyrightDate = -1;
}
public Book(String t, String a, String p, int c) {title = t;author = a;publisher = p;copyrightDate = c;
}
}
2.
Q14: Click on the header of the default constructor (1).
public class Book {private String title;private String author;private String publisher;private int copyrightDate;public Book() {title = "";author = "";publisher = "";copyrightDate = -1;
}
public Book(String t, String a, String p, int c) {title = t;author = a;publisher = p;copyrightDate = c;
}
}
3.
Q15: Click on the header of the overloaded constructor (1).
public class Book {private String title;private String author;private String publisher;private int copyrightDate;public Book() {title = "";author = "";publisher = "";copyrightDate = -1;
}
public Book(String t, String a, String p, int c) {title = t;author = a;publisher = p;copyrightDate = c;
}
}
4.
Q16: Click on the overloaded constructor parameters (4).
public class Book {private String title;private String author;private String publisher;private int copyrightDate;public Book() {title = "";author = "";publisher = "";copyrightDate = -1;
}
public Book:(String t, String a, String p, int c) {
title = t;author = a;publisher = p;copyrightDate = c;
}
}
5.
Q17: Click on the line where the copyrightDate attribute is initialized in the default constructor (1).
public class Book {private String title;private String author;private String publisher;private int copyrightDate;public Book() {title = "";author = "";publisher = "";copyrightDate = -1;
}
public Book(String t, String a, String p, int c) {title = t;author = a;publisher = p;copyrightDate = c;
}
}
6.
Q18: Click on the line where the author attribute is initialized in the overloaded constructor (1).
public class Book {private String title;private String author;private String publisher;private int copyrightDate;public Book() {title = "";author = "";publisher = "";copyrightDate = -1;
}
public Book(String t, String a, String p, int c) {title = t;author = a;publisher = p;copyrightDate = c;
}
}