Compound/Composite Data - 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 2. Compound/Composite Data

From the last chapter’s example, you can start to wonder that there just has to be a way to group all that data: your name, age, location, whether or not you have pets, etc.

Also, notice that a person can neither be a String, Number, Integer, nor Boolean. A person just holds too much data to be defined as either one of the above.

So what do we do?

What a Composite Data Structure is

From the previous example, you can think of all the data you’ve defined as small parts of a whole. But what is this “whole”?

Enter Composite Data.

A Composite Data structure includes many parts of data within it.

Those parts of the Composite could be whatever you wish to declare. Strings, Integers, Booleans, Lists, and even Other Composite Data.

Identify & Defining a Composite Data Structure

When you were asked earlier to define what type of data are you dealing with, what if you designed & defined data for an object that you couldn’t identify as atomic data? What if it had plenty of Characteristics? What if there was more depth in that object?

The key thing to remember in identifying composite data is depth. There are more parts to that ‘thing’ you were trying to define as data. If there’s more to anything than just a name, number, or true/false switch, then it’s probably going to be a composite data structure.

Representing information as Composite Data

Let’s take YOU as an example. You are a Person. As a person, you’re not JUST a name or number; you are comprised of a lot of data. An endless amount of data, rather.

The Elements that Comprise your Data

As an example of Composite Data, let’s define you.

For now, let’s start with the basics.

Remember: you are a Person. Using simple pseudocode, let’s define that:

// I am a PERSON

CompositeStructure PERSON;

An Explanation for your Composite Data’s Parts

Similar to what you did earlier for defining data, it’s also best that you identify what your composite data structure is comprised of.

For practice, use comments to describe what your data structure has.

Following the example above, You have a Name and an Age as well. Let’s include that:

// I am a PERSON

// A person has:

// - a name (string)

// - an age (number)

CompositeStructure Person {

String NAME;

Number AGE;

{

You live in a City. Oh but wait, a City isn’t just a name is it? It’s comprised of plenty of data 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;

{

Let’s not forget about YOU now. You live in a City, remember?

// I am 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;

}

Notice what happened here. A compound data structure within another compound data structure!

JAVA-02: Compound/Composite Data

In designing Java classes, the syntax is again very similar to the pseudocode we’ve used earlier. However, we define our composite data structure as a Data Class.

Think of classes as “blueprints” for representing & creating your objects as data. If you were to create a data object, you would simply define that data object by stating that it’s using the same “blueprints” you’ve defined earlier - your Data Class

Java: an Object-Oriented Programming Language Only

As you can see, data structures classes form the foundation of Java code.

A java class file will always have its EXACT case-sensitive filename as one of its classes. At the very minimum, a java class file will have at least one class. For example, a java file named ClassOne.java will have a class named ClassOne.

ClassOne.java:

class ClassOne {

// code here

}

Here’s how a data structure with two attributes would look like, including the Explanation Comments:

// Our class has:

// - an attribute variable (String)

// - another attribute variable (Integer)

class className {

String attribute1;

int attribute2;

}

Initializing a Java Object

To create an Object based on your Java class, it would be just like setting a Java variable’s initial data value. However, you’ll be setting that variable as a new Object. This object will have the same data structure as the class you set it as.

For Java classes, there are a few special lines of code you’ll need to create called a Constructor.

A defined constructor within a class will have the following structure:

public ExactClassName(datatype input) {

// insert any code here

}

Often times, the class constructor will be right below all of the attributes of that class.

Overall, initializing a Java object would look like the following notation:

ClassName OBJECTNAME = new ClassName();

The word ‘new’, along with the constructor for that class, initializes your object.

You’ve already defined that your object will have the data structure of whatever class you’ve assigned it to.

Accessing Class Attributes

This follows a similar structure to our PseudoCode.

To access a Class Attribute, you first have a variable object that has been assigned that Class structure. Then follow it up with a period (.), then the class attribute you want to access.

Let’s say we have a class with two attributes in it:

class Class {

String attribute1;

int attribute2;

}

Next, let’s recall setting object1 into a class:

Class object1 = new Class();

Then accessing object1’s attributes would look like the following notation:

object1.attribute1

object1.attribute2

Can you guess what data type you end up with when you access these two class attributes? If you look at your data definition for your Class, you’ve defined the attributes for that class with specific data types. Therefore:

object1.attribute1 (this returns a String)

object1.attribute2 (this returns an Integer)

EXAMPLE:

Think about a book. A physical book that probably sits near your shelf. Let’s start defining the Composite Data Structure for it.

First, let’s use comments to describe what our our data will look like.

// A Book Has:

Now think of the little attributes a book has. Is the cover a Hardcover, Paperback, or something else? How many pages does it have? Is it a Fiction book or not? What’s the title? Who’s the Author? How much did that Book cost?

// A Book Has:

// - a Cover Type (String)

// - a Page Count (Integer)

// - a Title (String)

// - an Author Name (String)

// - a price (Number)

In Java, numbers with decimals can have either data type float or double. For now, let’s go with double.

Now, let’s design the Book Class in Java.

class Book {

String coverType;

int pageCount;

String title;

String authorName;

double price;

}

For fun, let’s also create a Harry Potter book object.

Book harryPotter = new Book();

JAVA Workshop #1

Go to an IDE of your choice. You may also use online IDE’s such as rextester.com, ideone.com, or www.codechef.com/ide. (if you do, make sure to set your Programming Language to Java).

If you use one of the online IDE’s, you should see something similar to this:

// —————————————

import java.util.*;

import java.lang.*;

import java.io.*;

// INSERT MORE IMPORTS HERE

// INSERT MORE CLASSES HERE

class (whatever class name)

{

public static void main(String args[])

{

// INSERT CODE HERE

}

}

// —————————————

Classy Kids

Design a data class definition for a car. Think carefully. What attributes would that car have?

First, use comments to describe what attributes you want the car to have. Then figure out what Data Types those attributes are. This is totally up to you. Make it up as you go along!

Then, code your data class to include those attributes.

Then, create a data object, which can be a car you really like (or hate).
Afterwards, test and run your code in an IDE of your choice.

You can also use a free IDE such as http://rextester.com/runcode