ITEC 2610 Chapter Notes - Chapter 8: Object-Oriented Programming, Mutator Method, Instance Variable

73 views11 pages

Document Summary

Problem solving: patterns for object data collecting values. Initialize the instance variable to an empty collection: public question() choices = new arraylist(); Supply a mechanism for adding values: public void add(string option) choices. add(option); The user of a question object can call this method multiple times to add the choices. A property is a value of an object that an object user can set and retrieve. P(cid:396)o(cid:448)ide a(cid:374) i(cid:374)sta(cid:374)(cid:272)e (cid:448)a(cid:396)ia(cid:271)le to sto(cid:396)e the p(cid:396)ope(cid:396)t(cid:455)"s (cid:448)alue a(cid:374)d (cid:373)ethods to get a(cid:374)d set it. public class student private string name; public string getname() { return name; } public void setname(string newname { name = newname; } Problem solving: patterns for object data - managing properties of an object. It is common to add error checking to the setter method: public void setname(string newname) if (newname. length() > 0) { name = newname; } Some objects have behavior that varies depending on what has happened in the past.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents