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.4.1
Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
ExercisesExercises
1.
Q9: Put the code in the right order to complete the default constructor.
public class SongType{
---
//attributes declared here
//...
//default constructor
public SongType(){
---
title = "";
artist = "";
length = 0;
---
}
}
2.
Q10: Put the code in the right order to complete the specific overloaded constructor.
public class SongType{
---
//attributes declared here
//...
//overloaded constructor
public SongType(String n, String a, double ln){
---
title = "";
artist = "";
length = 0;
---
}
}
3.
Q11: Which of the following is NOT true about constructors?
Constructors must be named the same name as the class
Default constructors have no parameters
Classes can only have a single constructor
Constructors must be public
Constructors have no return type, not even void
4.
Q12: Two constructors are shown for the Point class below. Is this code valid?
public class Point {
private int x;
private int y;
public Point (int one, int two) {/*LOGIC*/}
public Point (int a, int b) {/*LOGIC*/}
}