Differentiate class-level (static) vs. instance/object-level variables
Differentiate class-level (static) vs. instance/object behaviors/methods
Define class variables (static) as needed ‘
Name
Data Type
public / private / final
Define instance variables (that you want to be interrelated)
Name
Data Type
private
Create constructor (behavior) that creates initial state of object
public
Same name as class
No return type
Default - no parameters
Logic - initialize all variables
Repeat as needed, adding parameters
Subsection8.4.2Classes-WE2-P1
Consider the SongType class you began in an earlier exercise, as illustrated in the following UML diagram.
Figure8.4.1.
ExercisesExercises
1.
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.
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*/}
}