Skip to main content
Contents
Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 10.5 WrClasses-WE2-P2
Subgoals for Writing a Class.
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)
Define class variables
static
as needed
Create constructor (behavior) that creates initial state of object
Overloaded constructor (with as many parameters)
Logic - initialize all variables
Repeat as needed, adding parameters
Create 1 accessor and 1 mutator behaviors per attribute
Return type same data type as attribute
Parameter is same data type as attribute
Logic validates input parameter and sets attribute value
Logic - convert needed attributes to a format that can be printed
Parameter - instance of the class
Logic - compare attributes for equity
Create additional methods as needed
Subsection 10.5.1
Exercises Exercises
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;
}
}
You have attempted
of
activities on this page.