Data Initialization - Your Code Structure and Foundations - Java Programming Box Set: Programming, Master's Handbook & Artificial Intelligence Made Easy; Code, Data Science, Automation, problem solving, Data Structures & Algorithms, 1st Edition (2015)

Java Programming Box Set: Programming, Master's Handbook & Artificial Intelligence Made Easy; Code, Data Science, Automation, problem solving, Data Structures & Algorithms, 1st Edition (2015)

PART I: Your Code Structure and Foundations

Chapter 3. Data Initialization

Let’s recall the Composite Data Structure of a Person:

// A person has:

// - a name (string)

// - an age (number)

// - a City they live in (Composite data City)

CompositeStructure PERSON {

String NAME;

Number AGE;

City LOCATION;

}

We’ll also create two Atomic Data pieces as the Time: two integers.

Integer HOUR;

Integer MINUTE;

You have have noticed one thing: You’ve defined what types of data you’re dealing with - but we don’t know any people yet. Nor do we know what time it is!

Now we INITIALIZE our data. Now that we’ve defined what types of data we have, we then set our data for the first time.

Initializing Atomic Data

First, we’ll set the time.

Let’s say it’s 8:30 PM. We’ll set up our time as is.

To set data, most programming languages use the Equals (=) operator. Here, we’ll do the same.

HOUR = 20;

MINUTE = 30;

DON’T make this mistake…

But what if we tried to set HOUR and MINUTE to another data type?

HOUR = aaaa;

MINUTE = Composite{Integer; String; Boolean} ;

Notice earlier that you’ve set both HOUR and MINUTE as Integers. Here, we’re trying to set up those data as different data types. In some programming languages, it’s not going to work. And in most cases, your code might not work because of this.

Here, you’ve set up your data as Integers - therefore, you need to initialize & change them as Integers.

Lesson learned: if you set up your data as a certain data type, unless you really know what you’re doing, DO NOT try to set up that data as another data type!

Initializing Composite Data Structures

Now, let’s define an actual Person using our data structure.

There are FOUR KEY steps to do this:

STEP ONE:

Identify & describe a data object you’re trying to create.

For practice, use comments to describe what that object is & what it’s like.

We’ll use a friend of yours called Jamie, for example. We use comments to describe her:

// Jamie Denise is a person

// She has:

// - a name: Jamie Denise

// - an age: 19

// - a City she lives in: New York

STEP TWO:

Declare what type of composite data your object is.

In this example, we declare Jamie as a person:

// Jamie Denise is a person

Person Jamie;

STEP THREE:

You INITIALIZE your object’s data structure, so that your object actually IS represented by the Composite Data in your program. In most languages, you declare that you have a new ‘case’ or instance of this object. Think of this step as “registering” your new object into your data program.

In this example, we INITIALIZE Jamie as a data object that HAS the Person Composite Data Structure

// Jamie Denise is a person

Person Jamie = new Person;

Again, just like Atomic Data, we use the Equals (=) operator to set data.

STEP FOUR:

Identify your object. Then, for each data part that your Composite Data is made of, set those initial values.

Remember Jamie’s Attributes?

// Jamie Denise is a person

// She has:

// - a name: Jamie Denise

// - an age: 19

// - a City she lives in: New York

Now let’s initialize each attribute onto our Data Object Jamie. You first need to identify the data object you’re trying to reach. In this case, it’s Jamie.

Next (and this is important!), identify which attribute you’re planning to reach. Here’s it’s best to reference the Data Structure you’ve defined earlier:

CompositeStructure PERSON {

String NAME;

Number AGE;

City LOCATION;

}

Let’s set all three of Jamie’s Attributes:

// Jamie Denise is a person

// She has:

// - a name: Jamie Denise

// - an age: 19

// - a City she lives in: New York

Person Jamie = new Person;

Jamie-NAME = “Jamie”; // <— a “String”: Remember?

Jamie-AGE = Nineteen;

CITY = NewYork;

Okay, we’re done.
Hold on. This code is wrong. Why?

DON’T make these mistakes…

This line: Jamie-AGE = Nineteen; won’t work. Why?

Just a friendly reminder. Make sure the data type you’re trying to set MATCHES the data type you’ve defined. In most programming languages, this is one of the most common mistakes programmers make. Nineteen is definitely not a Number data type, nor is it a String (where’s the “Quotation marks?”). However, 19 works.

Jamie-AGE = 19;

Also, This line: CITY = NewYork; won’t work. Why?

What’s CITY? Did we mean Jamie’s current CITY?

Remember to first identify the DATA OBJECT you’re accessing. AND THEN that object’s attributes.

Well, let’s try that.

Jamie-CITY = NewYork;

This line: Jamie-CITY = NewYork; won’t work either. Why?

Because Jamie is a data object that follows the Person Composite Data Structure you’ve defined. And note how that Structure does NOT have any attributes named CITY in it.

Again, Remember to first identify the DATA OBJECT you’re accessing. AND THEN access that object’s correct attributes.

The Person Structure includes a separate City data structure, but it certainly isn’t called CITY.

CompositeStructure PERSON {

String NAME;

Number AGE;

City LOCATION;

}

Oh, so it should be Jamie-LOCATION= NewYork;

But you’re missing one more thing. Where in your program is NewYork defined?

Well, that can be arranged. Let’s recall the City data Structure and define the NewYork data object as well:

// This is a CITY

// A City has:

// - a name (string)

// - a Latitude and Longitude (2 numbers)

// - a Population count (an integer, above 0)

CompositeStructure City {

String NAME;

Number LATITUDE;

Number LONGITUDE;

Integer POPULATION;

{

// NewYork is a CITY

// NewYork has:

// - a name: “New York

// - a Latitude and Longitude: 40.7127 and 74.0059

// - a Population count: 8406000

City NewYork = new City;

NewYork-NAME = “New York”;

NewYork-LATITUDE = 40.7127;

NewYork-LONGITUDE = 74.0059;

NewYork-POPULATION = 8406000;

and now, we fully complete Jamie’s data entry:

// Jamie Denise is a person

// She has:

// - a name: Jamie Denise

// - an age: 19

// - a City she lives in: New York

Person Jamie = new Person;

Jamie-NAME = “Jamie”;

Jamie-AGE = 19;

Jamie-LOCATION= NewYork