Matching Data with Functions - 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 5b. Matching Data with Functions

Here’s an inevitable truth when it comes to functions: they will almost always involve data in any way.

Later on, you’ll find that all sorts of different data structures will have at least one key function associated with it.

Also, some data structures, by default, will have associated go-to templates to use in programming. Remember this well; if you’re given a certain data structure to work with, you should already have the function structure you’ll need in mind.

Functions using Atomic Data

There’s usually no structure or template involved when dealing with Atomic Data.

Functions may have atomic data as inputs or outputs when necessary.

Here are some pseudocode to demonstrate:

// INPUT: - a Name (String)

// OUTPUT: - an ID (Number)

// EFFECT: ?????

number procedureA(String name) {… return ID}

// INPUT: - none

// OUTPUT: - none

// EFFECT: ?????

void procedureB() {…}

Also, Functions can even modify existing variables.

// Player1 Score, as an Integer

Integer Score = 0

// INPUT: - none

// OUTPUT: - none

// EFFECT: increments the score by one

void score1() { Score += 1}

Functions using Composite Data

The key thing to remember here is that, for every component a composite data structure has, its associated function will have a template that accesses and deals with each component (regardless of what data type each component is). Also, each component will be treated as whatever data type it is; a String treated as a String, composite data as composite data, and so on.

We demonstrate this in pseudocode:

// Structure of a Book:

CompositeStructure Book {

String AUTHOR

String TITLE

Integer PAGECOUNT

}

// INPUT: - a Book

// OUTPUT: - none

// EFFECT: ?????

void bookTemplateFunc(Book b) {

b.AUTHOR //do something

b.TITLE // do something

b.PAGECOUNT // do something

}

Here’s an example of a printing function, based on the above template:

// INPUT: - a Book

// OUTPUT: - none

// EFFECT: prints book details

void printDetails(Book b) {

printString(b.AUTHOR)

printString(b.TITLE)

printInteger(b.PAGECOUNT )

}

Methods: Functions for Object-Oriented Programming

In Object-oriented programming, data and procedures are bundled in data structures called classes.

Functions are called Methods and class variables are called Fields.

You can think of Methods within a class as ‘behaviours’ - or what actions an instance of that class can do.

To describe Class Methods in comments, simply list the behaviours it can do:

// A Space Invaders Tank Class can:

// - move left

// - move right

// - shoot a missile

// A Dog Class can:

// - walk

// - bark

// - sit

// - eat

Functions for Sequences

(You’ll cover this in later chapters…)

Basically, data elements of the same type can be grouped together into sequenced collections. Examples can be lists and strings.

For these, functions process each element one by one until all data elements are covered.

Functions for more Sophisticated Data Structures

(You’ll cover this in later chapters…)

JAVA-04: Function Structure

Functions in JAVA

The syntax structure in Java looks very similar to our pseudo code.

Java function structure looks like so:

<output’s data type> functionName( <input data type> input1Name) {

// any code here,

return <data or variable with output’s data type>;

}

Here are a few key notes in Java functions:

-Since Java is purely Object-Oriented Programming, Functions in Java are mainly called Methods. They belong within classes and they describe behaviors that the class does.

-You place your function code within the curly brackets of your function.

-If your Java function returns data, you put the word ‘return’, then a variable name or some data value you want to return. Once the function reaches the ‘return’ line, it will output whatever data you’ve set it to, then the function will finish running.

-If your Java function returns nothing (the output value is called “void”) you don’t have to put a return line.

Function Example

Here’s what the createID() function looks like in Java. We’ll also consider numbers as data type Double:

// INPUT: - a name (String)

// OUTPUT: - an ID (Number)

// EFFECT: generates a random number for a given name

double createID(String input) {

return rand();

}

JAVA Workshop #3

Designing & Calling Functions

First, go to an IDE of your choice. Online IDE’s include Rextester (rextester.com), CodeChef (www.codechef.com/ide), and Ideone (https://ideone.com/).

On your Main Function, replace ALL code within it, then copy-paste all the code within the dotted lines below. Make sure it’s between the curly brackets of the main() function.

public static void main(String args[]) {

// - - - - - - - - - - - - - - - - - - - - - - - - -

class calculator{

// INPUT: - two Integers

// OUTPUT: - a result(Integer)

// EFFECT: add two integers together & give result

__ add(__ a, __ b){

return a + b;

}

// INPUT: - two Integers

// OUTPUT: - a result(Integer)

// EFFECT: subtract 1st integer from 2nd & give result

__ subtract(__ a, __ b){

return ____;

}

};

// Your Bank Account is an Integer

int BANKACCOUNT;

// Create a Calculator Object named ‘c’

calculator c = new calculator();

// Income & Expenses, as Integers

int PAYCHEQUE = 6000;

int LIVINGEXP = 3000;

int FUNSTUFF = 1000;

int TRAVEL = 3000;

// How much would fun stuff and travel be together?

int FUNTRAVEL = c.add(___, ___);

System.out.println("Fun Stuff & Travel Together: " + FUNTRAVEL);

// Your Paycheque, after paying your living expenses?

// (HINT: call the calculator object c, then access one of its methods…)

BANKACCOUNT = c.________(______, _____);

System.out.println("Bank Account Balance, normal:" + BANKACCOUNT);

// Your Paycheque, after paying your living expenses AND fun stuff?

// (HINT: call the calculator object c, then access one of its methods…)

BANKACCOUNT = ________

BANKACCOUNT = ________

System.out.println("Bank Account Balance, w/ Fun Stuff: " + BANKACCOUNT);

// - - - - - - - - - - - - - - - - - - - - - - - - -

}

Before you begin, your code’s main function should look exactly to the code above.

Now, fill in the blanks. If you filled in the blanks with proper code, you should have the printed lines below:

Fun Stuff & Travel Together: 4000

Bank Account Balance, normal: 3000

Bank Account Balance, w/ Fun Stuff: 2000

Good luck!