0% found this document useful (0 votes)
12 views8 pages

Learning Outcome F.1. Creating User-Defined Classes in Programs To Promote Software Reuse. - Data Members

Uploaded by

reubenklatzer01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Learning Outcome F.1. Creating User-Defined Classes in Programs To Promote Software Reuse. - Data Members

Uploaded by

reubenklatzer01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Principles of Programming B

(PPB115D/PPG115D/PPBFD/PPGF15D)

Compiled by
Miss V. Booi
All rights reserved. Apart from any fair dealing for the purpose of research
criticism or review as permitted under Copyright Act, no part of this
document may be reproduced or transmitted in any other form or by any
means, electronic or mechanical, including photocopy and recording, without
permission in writing from the publisher.

1
Learning Outcome F.1. Creating user-defined classes in programs to promote
software reuse.

1. Data Members

In the previous lesson we learned that the state of an object is what is describing the object.
We call the state of an object its data members. Data members is nothing else than global
variables of a class. You will only declare data members, not define them. You learned that
data members of a class are encapsulated and that you need to use the methods to access
the data members. That is why we don’t initialize them. Later in the lesson you will learn
about constant data members that must be initialize and declared as final and also about
class variables that belong to a class and must be declared as static.

2
1.1 Syntax of declaring data members:

<access modifier> <data type> <data member name >;


The first (left-most) modifier used lets you control what other classes have access to a
member field. For the moment, consider only public and private.

• public modifier—the field is accessible from all classes.

• private modifier—the field is accessible only within its own class.

In the spirit of encapsulation, it is common to make fields private. This means that they can
only be directly accessed from the class. We still need access to these values, however. In
the next lesson you will learn about methods, and then with public methods you can still
access these private members from other classes.

All variables must have a type. You can use primitive types such as int, float, boolean,
etc. Or you can use reference types, such as strings, arrays, or objects. Using your
primitive types is straight forward, but a little later we will look at having a data member
of type object.

The floating point data types consist of two types:

1. Float:

a. When indentifying this type you will use the float Java keyword.

b. It has a precision of 6 or 7, which means it can store up to 6 to 7

decimals c. Example:

public float price;

2. Double:

a. When identifying this type you will use the double Java keyword.

b. It has a precision of 15, which means it can store up to 15

decimals c. Example:

public double number;

The Boolean type data members will take on one of two values, true or false. You will use
this data type when something is either yes or no. Below is an example of declaring a
boolean type data member:
3
public boolean single;

The integral data types consist of five types:

1. Character:

a. The character type data member is identified with the char Java keyword.

b. Every single character, not just alphabetical characters can be stored in this

type. c. Example:

public char gender;

2. Integer:

a. The integer type data member is identified with the int Java

keyword.

b. Integer types are numbers that does not have a decimal.

c. It can store a value between -2147483648 and

2147483647. d. Example:

public int age;

3. Short:

a. A short data type is identified with the short Java keyword.

b. It is just another type of integral that stores a shorter integral value between -
32768 and 32767, which means is uses less memory space to store a value
than integer types.

c. Example:

public short age;

4. Long:

a. A long type data member is identified with the long Java keyword.

b. It is also just another type of integral data type that stores bigger values
than integer types between -9223372036845477558808 and
9223372036845477558807. This means that it will use up more memory
4
space when storing a value this big.

c. Example:

public long seconds;

5. Byte:

a. Stores byte values and uses the byte Java

keyword. b. We are not using it in this course.

All variables, whether they are fields, local variables, or parameters, follow the same
naming rules and conventions that were covered previously. The rules and conventions for
naming your variables can be summarized as follows:

• Variable names are case-sensitive. A variable's name can be any legal identifier — an
unlimited-length sequence of Unicode letters and digits, beginning with a letter, the
dollar sign "$", or the underscore character "_". The convention, however, is to
always
begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign
character, by convention, is never used at all. You may find some situations where
auto- generated names will contain the dollar sign, but your variable names should
always avoid using it. A similar convention exists for the underscore character; while
it's technically legal to begin your variable's name with "_", this practice is
discouraged. White space is not permitted.

• Subsequent characters may be letters, digits, dollar signs, or underscore


characters.
Conventions (and common sense) apply to this rule as well. When choosing a name
for your variables, use full words instead of cryptic abbreviations. Doing so will
make your code easier to read and understand. In many cases it will also make your
code self- documenting; fields named cadence, speed, and gear, for example, are
much more intuitive than abbreviated versions, such as s, c, and g. Also keep in
mind that the name you choose must not be a keyword or reserved word.

• If the name you choose consists of only one word, spell that word in all lowercase
letters. If it consists of more than one word, capitalize the first letter of each
subsequent word. The names gearRatio and currentGear are prime examples of this
convention. If your variable stores a constant value, such as static final int
NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and
separating subsequent words with the underscore character. By convention, the
underscore character is never used elsewhere.
5
1.2 UML class diagram:

When a class is represented in a UML diagram the syntax for representing the data
members will take the following form:

ClassName

<access modifier> <data member name>: <data type>

The access modifier will be a (+) for public and a (-) for private.

The data member name is the name of the variable and must stick to the coding

conventions. The data type of the data member is indicated after the colon. Again this

could be a primitive
type or a reference type.

For example:

+ color: String
- numberOfGears: int
+ ownerName: String

1.3 Constant data member:

A constant data member is a variable that’s value does not change. It must be declared with
the final keyword and must also be initialized. The naming convention changes for constant
values:

• Must be all capital letters

• Words must be separated by underscores.

The syntax for declaring a constant data member will look like this:

<access modifier> <final keyword> <data type> <DATA_MEMBER_NAME>=<value>;

So when you see this type of data member in the UML class diagram you will know that
it is constant. An example of a constant data member:

public final double INTEREST_RATE = 0.15;

6
1.4 Class variables or class data:

A class variable is any field declared with the static modifier; this tells the compiler that
there is exactly one copy of this variable in existence; regardless of how many times the
class has been instantiated. A field defining the number of gears for a particular kind of
bicycle could be marked as static since conceptually the same number of gears will apply to
all instances. The code static int numGears = 6; would create such a static field.
Additionally, the keyword final could be added to indicate that the number of gears will
never change.

Class variables are referenced by the class name itself, as in

Bicycle.numGears;

This makes it clear that they are class variables. When we work with methods you will also
learn about static methods and their purpose.

To summarize:
• Signature of declaring
• Understand the difference between public and private
• Looked at the primitive data types
• Understand the constant (final) data members
• Understand class variables that are identified with the static Java keyword.
• How to represent data members in a UML class diagram.

7
Activities

Create the following classes with their data members:

Person
- name: String

- surname: String
- id: String
- age: short
- height: double
- shoeSize: int
- gender: char

Book
+ author: String
- isbn: String
- title: String

- numberOfPages: int

Pizza
- size: char
- type: String

- numberOfTopings: int
- base: char

IceCream
- name: String
- flavor: String

- price: double
+ VAT: double
+ liters: double
8

You might also like