How to look at code and understand what you're seeing.
A variable – Something which holds a value, a sort of invisible box.
Data types – Just as the name describes, different types of data / information, for example my name is of data type String, “Bryan Irwin”.
Methods – A simple list of instructions which the computer carries out. Example, a recipe for baking cookies is a sort of method.
Function – Just like a method, except with the addition of expecting to give the user back some value, (depending upon the data type of that function/method) in java they call both functions and methods, methods.
Loops – This is a sort of repeatable process, which enables you to have the program do something over and over again, depending upon what you need.
Data types – Just as the name describes, different types of data / information, for example my name is of data type String, “Bryan Irwin”.
Methods – A simple list of instructions which the computer carries out. Example, a recipe for baking cookies is a sort of method.
Function – Just like a method, except with the addition of expecting to give the user back some value, (depending upon the data type of that function/method) in java they call both functions and methods, methods.
Loops – This is a sort of repeatable process, which enables you to have the program do something over and over again, depending upon what you need.
Conditional logic – This would include if, if/else, and switch statements. Example:
If (someConditionIsTrue)
{
//Do
something
}
Input / Output, (I.O for short) - This would be any
sort of input or output you want to give or get from the user.
·
Example 1: if you wanted to print
something to the console window, (this being output) you could use a
System.out.println(“Print this text”); this would give you, (the programmer)
feedback on various bugs, errors, odd outcomes from a program.
·
Example2: You want to get input from
the user, (something the user does) you could use a Scanner scan = new Scanner
(System.in); object. Where when
connected to some variable, (string, int, etc.) the user would give some sort of
value to the scanner object.
Arrays – An array is a sort of data structure, and is pretty much a way to hold
various amounts of data. I picture this to be a sort of big cardboard box,
where inside that box is a bunch of smaller boxes. You can access each of the
smaller box locations through their corresponding indexes, (usually numbered 0,
1, 2, 3 etc.)
A Class file – This is a simple way to describe everything you want other people to know
about a given Object. The class file describes what the object does, if it has
a name, what it might look like to the user, and how we can interact with it.
Example2: Take
the dime in the above picture, in a class file named “Dime.java” we would have
a few instance variables, some which hold the name, weight, type of metal,
currency value, etc. We might write public methods, which enable us to combine
in stacks of various currency levels, (e.g. $1, $5) and that method would
calculate how many dimes would be needed to build that stack. This is a simple
example, and not meant to be over thought.
Constructors –
The constructor sets an object up in a usable state, and what this means in
short, is if you were to have a party at your house. You would have to setup
tables, buy cups, food, drinks, and then invite guests. The constructor does
this for your Class file. It sets it up so you can use the various public
methods.
Now that we've gone over some basic concepts in
programming, let’s look at how to spot each one inside simple and complex code.
The Variable.
For the purpose of this section, I’ll keep all variable of data type as
int.
We define a variable in this fashion, int myAge = 33;
Notice int is the data type.
“myAge” is the name we give the variable.
33 is the value we assign to the variable, rather we copy it inside the
variable.
So how do we spot a variable in mixtures of code? I start by looking for
the = sign, and then look at the value on the right side of it. Generally if
it’s a number it is a variable on the left side of the = sign.
Another
way I look for a variable, is if I see the data type defined in front of it.
Notice, in the last picture, where it reads “int total = 0;” and “double mean
=”
These are both variables, (the datatype is different), but they’re still variables.
These are both variables, (the datatype is different), but they’re still variables.
Also
within that same picture, inside the for-loop, “int i = 0;” is used as the
counter variable of that for loop.