The Creating Coder

Live by the code

Archives May 2020

Lesson 4: Working with Variables

When working with Java or any programming language, we usually have to deal with storing lots of different types of data called variables.  This can be letters, words, integers, irrational numbers, or just about any data type you can imagine.  As we will learn over the next couple of lessons, different situations call for different variable types.

The first variable we will learn is a String.  A String is how we store a word or string of words.  

In order to create our String we have to tell the computer that the variable we are creating is indeed a String.  Look at the code below.

The first word String is where we assign the variable type.  It is important to note that whenever we are making a new variable, the first word must be the type of variable we wish to create.  Another important thing to note, is that the word String must be capitalized!!!

The 2nd word “str” is what the user wants to name/identify the String.  The name of the variable is sometimes referred to as the identifier.  However, there are a couple of rules when making an identifier:

  1. The first symbol of the identifier must be a standard letter, a dollar sign “$”, or the underscore symbol “_”.
  2. After the first character of the identifier is established, the programmer is free to use numbers(0-9.)
  3. We are not permitted to “Reserved Words” as identifiers.  These are words that are already in use/reserved by Java.  Some examples of reserved words would be the word “public” or “class.” 
  4. Make the name short and descriptive of what data you are storing.  For example, if you had a String that had a list of last names it might look like “lastNames” or “last_names.”

Now that we have gone over our the criteria for naming our identifiers, let us return to our String example:

The equal sign  ”=”  is an assignment operator in Java.  In other words, it lets us assign the data type and identifier to the actual data itself.  

Next, we have quotation marks and the String data.  The Quotation marks tells the computer where the String starts and ends.  Therefore, we have one at the start of the sentence and the other one at the end.

Finally, we have a semicolon “;” after our String.  The semicolon tells the computer that we are done with that line of code.  If forgotten, it can cause many compilation errors as the computer will read several separate lines of code as one.  

However, we don’t always use semicolons when we are done with a line of code. Some examples would be when declaring a class or the main method.  One general rule of thumb is that any declarative statements must end with a semicolon – such as declaring our String.

Coding Challenge 1

Now that we have a couple of basic concepts under our belt, I want you to make your own program!  Using our HelloWorld program from lesson 1 as a template, I want you to do the following:

  1. Create a public class with the name “codingRocks”
  2. Implement the main method
  3. Use the system method to print out “Coding Rocks!”

If you get stuck, feel free to refer to Lessons 1-3 for a reminder, or you can look at the product below to see what the final product should look like.  Have fun!

Great job!  You did it! 

In our next section, we will go over variables.

GitHub link.

Lessons 1-3 Review

What does IDE stand for?
Intentional Display Environment
Integrated Development Environment
Good job!
Interesting Developers Edition  
True or False? Capitalization in Java matters.
True
Nice work!
False
Once we are finished with our code, what steps do we take to see our code execute?
First we must compile our code.  Then, once it is done compiling, we run our code.
Awesome!
We run our code, then compile it.
Run the code, compiling it doesn’t matter
Why should our file name match the class name?
In order to easily reference a class when we start to work on programs with multiple classes.
Woohoo!
To help keep our code look neat
What are the four fundamental ideas of Object Oriented Programming?
Conciseness, Encapsulation, Efficiency, and Testing  
Encapsulation, Polymorphism, Abstraction, and Inheritance
Nice!
_____ refers to keeping information in an object and not letting other objects access or change it.
Abstraction
Polymorphism
Encapsulation
Nice!
Inheritance
_____ Broadly speaking, it means the capability of an object to take many forms. 
Polymorphism
Radical!
Abstraction
Encapsulation
Inheritance
_____ refers to presenting only the necessary methods or information in our code.  (This is done to make code less difficult to read.) 
Polymorphism
Encapsulation
Abstraction
You’re a pro!
Inheritance
_____ is inheriting or reusing code from other objects in order to keep our program from having redundant code. 
Polymorphism
Encapsulation
Inheritance
Stupdendous!
Abstraction
True or False? Our objects in OOP are identified by classes.
True
Nice work!
False
True or False? A method is one or more statements that perform some form of operation.
True
Good answer!
False
In OOP, typically classes are  _____, and methods are ______
verbs, nouns
nouns, verbs
Spectacular!
adjectives, verbs
What are the 2 types of methods in Java?
Intuitive, and user-made
Standard library and user-defined
Fantastic!
Good and bad
What is the main method that is used in every Java application?
public static void main(String[] args){  }
Radical!
Public Static void Main(String[]) { }
public main(){ }

Lesson 3: Understanding our HelloWorld program – Methods

After going over classes and Object-Oriented Programming in Lesson 2, we will look at another important aspect of our code – methods.

Nested within the brackets of our class “HelloWorld” (that we created in Lesson 1,) we have our main method:

This is Java’s main method. It is required for every Java application to run.

In order to execute our code, we need this main method.  It is worth memorizing the above code, as we will need it to execute our code for every application in Java.  In a later lesson, we will go over what each word means.  For now, look at the picture below and be aware that:

public static void are all specifying details about the main method (Green.)

main() is our actual main method (Yellow.)  

String[] args is an array; a object that holds data (a feature we will cover in future lessons; Blue)

{ } The curly brackets show where our methods starts and ends (Red.)

What is a method? A method is one or more statements that perform some form of operation.

Methods are associated with taking action and are thought of as verbs, like barking.  Classes are typically nouns, such as a dog. 

Bark()

These methods have parentheses to supply details to the computer (when necessary.)  Similar to classes, to create a method, we must use curly brackets {} to begin and end our method.  Let us look at an example:

The name of the class is Dog and the method bark() is inside of the dog class.  Also note, that in order to execute our method, we have to use the main method.  *Once again ignore the static, void, and public words.  We will discuss them later.

     

It is important to know that there are 2 types of methods we will use in Java:

  1. Standard Library methods
  2. User-defined methods

Standard Library methods are methods that are provided by Java.  Some examples would be our main method or the System.out.println() method.

Both of these methods are built into Java and are ready to use without the programmer having to define what action it needs to take. 

In our HelloWorld example, we see that the System.out.println() is a prebuilt method designed to print whatever is in its parenthesis.

User-defined methods are methods that the user must make/define.  For example, there is no pre-built method for bark().  We would have to create a barking method (as seen above) in order for the method to work.

Key points from this lesson:

A Method is one or more statements that perform some form of operation.

In Object Oriented Programming, classes are typically nouns, and methods are verbs.

The main method that is used in every java application and is derived from the Standard Library is:

There are 2 types of methods: Standard Library and User-defined.

System.out.println() is a method derived from the Standard Library and allows the programmer to print whatever is put inside the parenthesis.

The semicolon ” ; ” is used at the end of our print line statement to tell the computer that we have nothing further to add. Think of it as a period to a sentence, but instead of telling a reader that the sentence is over, we are telling the computer that our line of code is over.

We will go over semicolons more in future lessons as well as methods.

GitHub link.

Lesson 2: Understanding our HelloWorld program – Classes and Object-Oriented Programming

After writing our first Java program in Lesson 1, it is important to go over our code and make sure that we are familiar with what we have written. If you don’t understand everything by the end of this lesson, that is alright. Some of what we go over will make more sense with time.

First lets talk about classes. In this case, our class name was “HelloWorld.” When creating classes in Java, it is necessary to enclose everything within curly brackets { }.

In order to understand the concept of classes, its important to take a step back and look at the bigger picture in Java.

Java is an Object-Oriented Programming language. Object-Oriented Programming is used to organize software by creating objects and assigning methods and variables to them.

One good example would be having a fish as a class.  Then, under our fish class, we can have different types of fish with different characteristics/traits.  Because we already have the fish class, we can use that as a base for our specific fish, instead of starting from scratch for every fish.

By having a fish class, we can have all three unique fish inherit traits from the broad fish class.

There are four important ideas that make up the fundamentals of OOP. 

Encapsulation: This refers to keeping information in an object and not letting other objects access or change it.

Polymorphism: Broadly speaking, it means the capability of an object to take many forms.  (The best way to remember this is to look at the Greek roots of the word.  Poly means “many” and morphism means “forms.”

Abstraction:  Only presenting the necessary methods or information in our code.  This is mainly done to make our code less difficult to read.

Inheritance:  Inheriting/reusing code from other objects in order to keep our program from having redundant code.

Now that we have gone over the fundamentals of OOP, what should we takeaway from this?

Our objects (in OOP) are identified by classes.  Once again, do not be worried if you don’t understand everything I just went over.  Just keep in mind that Java is an OOP and be familiar with the 4 principle definitions of an OOP listed above.

Now that we have a basic idea of classes and OOP, we will go over methods in Lesson 3. 

GitHub link.

Lesson 1: Lets make our first Java program!

It is customary for all programmers to create a Hello World program as their first program.  We shall honor this tradition and make our very own!

Step 1:  Open up your Integrated Development Environment (I will be using JGrasp.)

Step 2:  Lets create a class to hold our program!  To do this, lets write the following:


Step 3:  We can now save our program.  It is important to have our file name the same as our class.  In this case, since our class is “HelloWorld”, we will also save our file name as “HelloWorld.”

*Note: it is not mandatory, but it is a good practice for future programing projects.

Step 4:  Now that we have established our class, we are going to add our main method into the class:

(Don’t worry, we will explain exactly what that means in later lessons.)

Make sure to add the Main method and brackets inside of your class.

Step 5: Now between the brackets of our main method we are going to add our printing method and what we want printed inside of it:

Step 6:  Compile and run your code and you should see “Hello World!” in the Run I/O console at the bottom of the screen.

If you are using JGrasp, our compiler button is the green plus sign and our run button is the red figure running.

Congrats!!!! You have made your first Java program!!! 

Notice how when you compiled the program, the indentation changed?  This is an important feature and helps you understand the structure of a program more easily.

Now, lets go over some key points:

What is an IDE?

IDE stands for Integrated Development Environment. It is used for creating, compiling, and finally running your code.

Why should our file name match the class name?

Although it isn’t technically necessary now, in the future we will be creating programs with multiple classes. In order to reference these classes, we will want to have our names matching.

Does capitalization matter?

Yes.

I have no idea what I just did, should I be worried?

Don’t worry. In the next lesson we will go over what we just wrote.

GitHub link.

Before we get started

Hey there future programmer!

Congratulations on taking your first step into learning Java!  I really hope you enjoy this course.  Before we get started will need two things:

  1. The latest version of Java JDK (Java Development Kit)
  2. An Integrated Development Environment (IDE)

To download the latest version of Java you can visit the oracle website at https://www.java.com/en/download/ . Otherwise you can google Java JDK.  

Next, we will need an IDE.  This will provide us a workspace for our coding.  I choose to use JGrasp for simplicity.  However, if you have a preference for another IDE, it should not affect the course in any way.

If you want to use JGrasp you can find it under “download” at: https://www.jgrasp.org/javafx.html

If you have any issues getting set up, leave a comment.  Otherwise, the JGrasp and Oracle website provide instructions on how to properly install their software.  

Also, if you ever have the need to download any of the examples provided throughout this course, feel free to visit the GitHub repository.