Section 11.1. Identifying the Objects and Fields


[Page 344 (continued)]

11.1. Identifying the Objects and Fields

Say that we want to write a program to track the grades of students. One way to start identifying the classes that we will need is to underline the nouns in the problem description. If we do this we get grade and student. Which of these should be a class, and which should be a field?

To decide whether a noun needs to be a class or a field, determine whether it has more than one piece of data associated with it. A student should have a name and some grades. A student has more than one piece of data associated with it. So student should be a class. Nouns that have other nouns associated with them are classes.

What about name? Well, a name can be represented by a string and it belongs to a student, so it will be a field of a Student class. What about grade? Well, a grade can be represented by a number and it also belongs to a student, so it too will be a field of a Student class. Simple data that can be represented by numbers or strings are usually fields. To determine which class the field belongs to, ask who owns the data, or who should know the data. A student should know her name and her grades.

Making it Work Tip: Class Names should be Singular

Class names should be singular, not plural. We called this the Student class, not the Students class. We can create many Student objects from this Student class.




Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net