Skip to main content\(
\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 8.5 WrClasses-WE2-P2
Subsection 8.5.1 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)
-
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
Subsection 8.5.2 Classes-WE2-P2
Exercises Exercises
1.
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.
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.
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.
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.
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.
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.