Defining & Designing your 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 1. Defining & Designing your Data

Anything in this universe can be represented by data. Your name, age, gender, what car you drive, what city you live in, country, planet, galaxy, and so on.

To design great apps, games, and any other digital tool you can think of, you’ll have to identify what type of data you’re dealing with.

Identify your Data Type

There are two questions to ask yourself when you’re designing data to represent something.

First, identify whether or not you can define that ‘thing’ as an Integer, Number, Boolean (yes/no type things), or String. For example, your name is a String (a sequence of letters), your Age is a Number, and you live in a City or Town. Your city of town has a name - another String.

Second, identify whether or not that thing is a part of a whole; included in a larger thing. For example, you may have a friend named Jamie. She’s included in a list of your friends.

Representing your information as a Data Type

In most programming languages, you can define your data in a line of code. Here we define your data as a Global Variable - meaning this editable line of data is available throughout your program.

In most languages, you’ll very likely declare the Type of data you represent, as well as the Name of your data.

Let’s start with your name, using pseudocode for now:

String NAME;

In the above line of code, you defined your data as a String of characters, labelled as a NAME.

An Explanation for your Data

A good practice in defining your data is to make comments above the line of code that explains why you have your data the way it is. One of the lines can be in the format of “____ is a (Data Type)”.

In most programming languages, comments usually start with two slashes (//). For the pseudocode we use here, we’ll do the same

For the above line, here’s the example:

// My Name is a String

String NAME;

Here are some more examples:

// My Age is a Number

Number AGE;

// I live in a City or Town, with a Name

// My city’s name is a String

String CITY;

// I am either hungry or not

// My hunger status is a Boolean

Boolean HUNGRY?;

// I either have a pet or I don’t

// Whether or not I have a pet is a Boolean

Boolean HASPET?;

JAVA-01: Defining & Designing your Data

JAVA Comments

In Java, comments are just the same as other common languages like C, C++, PHP, and even our own pseudocode.

Single-line comments start with two slashes (//) and end when the line breaks to the next line of code.

Multiple comment lines start with a slash and a star (/*) and end with the reverse: a star and a slash (*/). Often, multiple comment lines also start with stars (*) on each line.

Here’s an example:

/* This is a

*multi-line

*comment */

Essential Java Syntax

Out of most programming languages out there, Java has one of the more strict code syntax.

Similar to other code such as C, C++, and PHP, lines of code in Java end with a semicolon (;). This is very important; Java programmers commonly have errors in their code just because their lines don’t end with a semicolon.

Atomic Data in Java

Atomic data in Java is defined VERY strictly.

Booleans in Java are strictly lower case. If your booleans are all upper-case (TRUE), or first-letter uppercase (True), most Java compilers will report these as errors. So for booleans, stick with all lower-case letters. So either (true) or (false).

Strings in Java can only start and end with double parentheses (“). Do not use single parentheses (‘) to start and end strings. For example, “String” is a string, but ’String’ is not.

Instead, single characters in Java start and end with single parentheses (‘). For example, ‘a' is a single character.

Floats in Java always end with the letter f. For example, some Floating Point Integers in Java can be 1.2f and 0.75f.

On the other hand, Java uses the data type Double to also represent decimal numbers. For example, 1.2 and 0.75 can be Doubles.

Java Data Definitions

Let’s define some data in Java.

Luckily, the pseudocode we’ve used earlier follows a very similar syntax to what we use here with Java. You will have to define your data type once you code the data you want to store. However, Java uses distinct, case-sensitive words to define your data variables.

For Strings, define with this data type: String (note the capital S!)

For Integers, define with this data type: int

For Booleans, define with this data type: boolean

For Floats (decimal numbers), define with this data type: float

For Doubles (decimal numbers), define with this data type: double

Overall, your data definitions in Java will look like our pseudocode:

(data type) (variable name);

So, let’s practice. Let’s start with defining the data for your friend’s name. Let’s call her Haley. First, let’s add a comment line to note what data type we want to deal with:

// Haley’s name is a String

Now we define the data for her name. Remember Java’s the end-of-line semicolon:

// Haley’s name is a String

String NAME;

The Java compiler knows that NAME is going to be a String, since you’ve defined that variable as that data type.

PRACTICE:

Let’s start by declaring things that follow into each of: Strings, Integers, and Booleans.

First, 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)

You should see something similar to this:

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

import java.util.*;

import java.lang.*;

import java.io.*;

class (whatever class name)

{

public static void main(String args[])

{

// INSERT CODE HERE

}

}

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

Now, copy-paste the below code to where you would INSERT your code above. Afterwards, fill in the blanks.

// ######

// Katie’s name is what Java data type?

____ NAME = "Katie";

// Katie’s age is what Java data type?

___ AGE = 21;

// Katie is EITHER married OR not.

// Katie’s marriage status is what Java data type?

___ ISMARRIED = true;

System.out.println(NAME);

System.out.println(AGE);

System.out.println("Is " + NAME + " married? " + ISMARRIED);

// ######

If you try to run your code - and it runs correctly - you should see the following:

Katie

21

Is Katie Married? true