8.2.2. WrClasses-WE2-P2¶
Subgoals for Writing a Class 2/4
Name it
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
Classes-WE2-P2
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; } }
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; } }
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; } }
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; } }
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; } }
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; } }
You have attempted of activities on this page