Data Changes & Mutable States - 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 4. Data Changes & Mutable States

In the previous chapters, you’ve defined some facts as data structures and even represented people and cities as data.

However, nothing ever stays the same in data.

Data changes over time - and it’s important to keep track of how data values change and what they currently are.

Modifying your Defined Data Over Time

In reality, modifying the data values you’ve set in place is nearly similar to initializing them in the first place. In most programming languages, the same principles between initializing and updating data apply: identify the data you want to access, use the Equals Operator (=), and set the new data to another value, but usually the SAME data type you’ve originally set. So change data defined as Strings to other Strings, Integers to Integers, and so on.

For example, let’s take a look at Jamie and New York from the past chapter:

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

// 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 Denise”;

Jamie-AGE = 19;

Jamie-LOCATION= NewYork

So let’s say 10 years have passed since we defined Jamie’s data object onto our program. Since then, Jamie got married and changed her last

name. She also moved to Los Angeles. So how would her new Data Object look like?

You’re essentially setting up all your changed data values to their new values. If you wanted to know what these values are, they would give you their current values.

// Jamie Walker is a person

// She has:

// - a name: Jamie Walker (changed from Jamie Denise)

// - an age: 29 (was 19)

// - a City she lives in: LosAngeles (was NewYork)

Jamie-NAME = “Jamie Walker”;

Jamie-AGE = 29;

Jamie-LOCATION= LosAngeles;

and yes, make sure even LosAngeles is defined.

// LosAngeles is a CITY

// LosAngeles has:

// - a name: “Los Angeles”

// - a Latitude and Longitude: 34.0500 and 118.2500

// - a Population count: 3884000

City LosAngeles = new City;

LosAngeles-NAME = “New York”;

LosAngeles-LATITUDE = 34.0500;

LosAngeles-LONGITUDE = 118.2500;

LosAngeles-POPULATION = 3884000;

Keeping Track of your Defined Data

let’s recall the Clock from the previous chapter:

Integer HOUR = 20;

Integer MINUTE = 30;

At the moment, it’s definitely not 8:30 PM anymore; let’s say it’s 10 AM now.

How would the clock change? Easy:

HOUR = 10;

MINUTE = 0;

Then three and a half hours pass. How would the clock change? Again, easy.

HOUR = 13;

MINUTE = 30;

If you wanted the time afterwards, what would it be?

Not 8:30 PM, not 10 AM either. But 1:30 PM.

Here, you’ve been essentially setting up both your integers named HOUR and MINUTE to new values. If you wanted to know what these values are, they would give you their current values.

However, it’s Important that you keep track of your changes. You MUST understand what the changes to your data have been - and you MUST determine whether or not those changes are what you want.

We stress this because one of the many traits a programmer needs to have (and be good at) is managing what happens to your data.

If you don’t believe us, wait until you have your tech interviews for a programming position you’re applying for…

JAVA-03: Data Changes & Mutable State

Changing your Variables’ Data Types in Java

You can’t. Once you’ve set your variable’s data type, DO NOT assign a different data type to it. Otherwise, you’ll get an error when you try to compile or run your code.

On the other hand, there are ways in Java that let you turn one data type into another. But even then, variables will still expect to have the same data types you’ve defined them to have.

Changing Data in Java

Just like our pseudocode, you can set and reset most of your variables using the Equals (=) sign.

Keeping Track of your Data

It’s important. Very important. You’ll see why in this example…

EXAMPLE:

(get your IDE ready…)

Let’s say there’s a square playground in a park within your neighbourhood. The playground is 20 feet long and wide.

You’re babysitting your best friend’s 5-year old son. Let’s call him James. Kids in this playground has X-Y coordinates that tell you how far away they are from the Top-Left Corner of the playground (and there happens to be a bench that you’re sitting in)

// James is a 5-year old kid.

// Kids have:

// - an X (sideways) Coordinate (Integer, between 0 and 20)

// - a Y (up-down) Coordinate (Integer, between 0 and 20)

class Kid{

int x;

int y;

}

James is running around the playground. Since you’re babysitting, James is NOT allowed to set foot outside the playground.

You can’t see James (because you’re probably reading this instead) but you know what his X-Y coordinates are. Therefore, either X or Y coordinate cannot go below 0 or past 20.

First, let’s initialize James as a Kid, then give him a location. He’s at (12,3)

// James is a 5-year old kid with coordinates (12,3)

Kid James = new Kid();

James.x = 12;

James.y = 3;

And clearly, James is going to move around the playground randomly. In the past few seconds, James moved around like so:

-5 feet to left

-10 feet to right

-2 feet to up

-3 feet to right

-4 feet down

-12 feet left

-5 feet up

-2 feet down

-5 feet right

-3 feet up

-5 feet right

-2 feet down

-4 feet right

Question: Did James leave the playground? (did either X or Y go below 0 or above 20?)

Let’s put James’ movements on Java code. Before that, recall James’ initial X-Y coordinates. We’ll also comment James’ current coordinates after his every move. Remember: moving left & up goes CLOSER to 0.

// James is a 5-year old kid with coordinates (12,3)

Kid James = new Kid();

James.x = 12;

James.y = 3;

// 5 feet to left

James.x -= 5; // James is at (7,3)

// 10 feet to right

James.x += 10; // James is at (17,3)

// 2 feet to up

James.y -= 2; // James is at (17,1)

// 3 feet to right

James.x += 3; // James is at (20,1)

// 4 feet down

James.y += 4; // James is at (20,5)

// 12 feet left

James.x -= 12; // James is at (8,1)

// 5 feet up

James.y -= 5; // James is at (8,-4): James is outside the Playground!!!

// 2 feet down

James.y += 2; // James is at (8,-2): James is still outside the Playground!!!

// 5 feet right

James.x += 5; // James is at (13,-2): James is still outside the Playground!!!

// 3 feet up

James.y -= 3; // James is at (13,-5): James is still outside the Playground!!!

// 5 feet right

James.x += 5; // James is at (18,-5): James is still outside the Playground!!!

// 2 feet down

James.y += 2; // James is at (18,-3): James is still outside the Playground!!!

// 4 feet right

James.x += 4; // James is at (22,-3): James is still outside the Playground!!!

So James has been outside the playground for quite a long time. He just ran past the closest edge and past your bench. If you were babysitting James, you’d be in big trouble…

In cases where you set clear boundaries for your data, yet you have some code that sets your data to go past those boundaries (and nothing is done about it), it might create problems for your code later on.

Now you understand how crucial it is to keep track of your data. There are many aspects of coding that depend on it, such as Debugging and making sure your code works the way you intend it to.

On another example, if you designed a video game and your characters go out of bounds, your video game wouldn’t be as good, won’t it?

JAVA Workshop #2

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

}

}

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

World Cup of Football (or Soccer)

Let’s say there’s a Football (Soccer) game between All-Star National Teams of Two Countries.

Add this import on the top of your Java file:

import java.util.Random;

Then, insert this class into your Java Code:

// a Team has:

// - a Score (Integer, minimum 0)

class Team{

int score;

}

Afterwards, look in your code for a line named ‘public static void main’ (there should only be one line in your code with this)

Within the curly brackets of that ‘public static void main’ line, insert this following code:

// —————

// The game Begins:

Team Country1 = new Team();

Country1.score = 0;

Team Country2 = new Team();

Country2.score = 0;

// It is now halftime,

// Each Team scores a random amount of points between 2 and 8

// FIRST HALF:

Country1.score += 2 + (int)(Math.random()*8);

Country2.score += 2 + (int)(Math.random()*8);

// —————

Which team is winning? How would you know? And how would you show it?

How would you edit the above code to PRINT the scores for each team?

Also, how would you edit the code to add the SECOND half of the game?

Test and run your code in your IDE of choice.