Programming Info

Class:

Describes the data and actions that an object has; it is a blue print for creating objects.

Object:

Models of objects from a problem domain and represent individual instances of a class.

Multiple objects can be created from a single class.

Each object has the same fields and methods but the data stored in the object’s fields are unique to that object.

Object State:

State is captured for an object by storing its fields.

Signature:

The header of a method including the parameters.

Methods can have the same name as long as the parameters are different.

When two methods have the same name it is called “method overloading.”

Data Types:

Primitives - Wrappers:

  • byte - Byte
  • short - Short
  • int - Integer
  • long - Long
  • float - Float
  • double - Double
  • char - Character
  • boolean - Boolean

Constructors:

Constructors allow for the instantiation of objects from classes.

By default, all classes have a constructor which takes no parameters.

If you create a constructor and do not explicitly define a constructor with no parameters, it will no longer exist.

It is a good practice to always create the default constructor.

A class that inherits from another class (say Car inherits from vehicle), automatically calls the default constructor (if it exists) for the parent class when it instantiates an object. This is done so that the parent fields can be initialized upon creation.

It is a good practice to always add the code to explicitly call the parent’s constructor.

Default Values:

Java initializes all fields to an initial value regardless of you explicitly stating a default value.

It is still a good idea to explicitly assign a default value.

Comments:

Comments should be included for all methods and fields in order to identify intended purpose and expected parameters.

Scope:

A variable’s scope is the section of code where the variable is visible.

When Java accesses a variable, it finds the closest (in terms of proximity) and references that variable.

As an example:

					
	public Class Sample
	{
		Int x = 0;
		Public Sample(int y)
		{
			Int x;
			X = Y; //this will not set the Class level field because the local variable is closer.
			This.x = y; //This will set the Class level field because we are using the keyword 
					//This. This tells Java that we want to reference This object’s variable //called x
		}
	}
					
					

Accessor and Mutator Methods:

Accessor methods are used to access fields of a class. Fields should almost always be private and accessing the data in a field should be done through an accessor method in order to have more explicit control over a field’s access.

Mutator methods are used to access fields of a class as well. These methods allow us to have more control over the ability to update a field.

Conditional Statements:

During program execution, code is run sequentially unless otherwise interrupted.

Using If, Else If, and Else statements allows us to conditionally execute code.


	Example:
	Int x = 4;
	If( x <= 4)
	{
		//do something
	}
	Else if( x > 4)
	{
		//do something else
	}
	Else 
	{
		//if either of those two statements didn’t run, we have problems.
	}

Logic Operators:

  • && (And)
  • || (Or)
  • ! (Not)

	Examples:
		If( A && B ) //this says that A and B must be true
		If( A || B ) //this says if either A OR B is true
		If( !A ) //this says anything but A

Modulo Operator:

The modulo operator returns the remainder after running a division function on two numbers.


			Int x = 12 % 10; //evaluates to 2
			

Abstraction:

Abstraction refers to the fact that we can ignore how a class is implemented and just use the available methods. In other words, we can think of the object (or sub system) as a black box and just use it without caring about how it does what it does.

Modularization:

This sort of goes along with abstraction in that we can take an object (or sub system) and remove it from the program and replace it without worrying about how it does what it does.

As long as the available methods accept the same parameters and return the same data types, it shouldn’t matter what it does behind the scenes to the calling code.

Debugging:

We will produce code with bugs. It is a fact of life. However, it is important to identify the reason for a bug in order to resolve it.

This is the job of a debugger.

Debuggers should be used often and will help you resolve issues as well as walk through code that you are analysing.

Collections:

Collections allow us to group data and objects and access them for iterating over the collection or by directly accessing the data through its index.

The first item in a collection is stored at index 0.

Flexible Sized Collection:

ArrayList x = new ArrayList();

The above code creates an ArrayList that holds Objects.

When an ArrayList is initialized, it has a size of 0. Once you add to the ArrayList, it has a size of 10 (even if you only put a single item in it).

Once you add an 11th item to the ArrayList, it will grow to 20. Likewise, when you remove the 11th item, it shrinks to 10.

ArrayList is in the java.util package, so the top of your code must import the java.util.ArrayList package.

import.java.util.ArrayList

//Alternatively you can use (import.java.util.*) to import everything in the java.util //package rather than just the ArrayList code.

The in the above code is a Generic class. It identifies what object type the ArrayList will hold.

ArrayLists allow you to add and remove items through methods by passing an object into the particular method needed.

  • x.add(new Object());
  • x.remove(index);
  • x.remove(object);
  • ArrayLists also have other great methods such as:
  • x.contains(Obejct);
  • x.isEmpty();
  • x.indexOf(Object);
  • x.lastIndexOf(Object);
  • x.get(index);

Fixed Size Collections:

Int[] x = new int[25];

The above code creates an Array of integers with 25 indexes.

Regardless of the number of items in the array, it will always be a length of 25.

Just as ArrayLists can be accessed through indexes, so too can arrays.

Int y = X[3] //this gets the integer in index 3 and stores it in y

X[3] = 2 // this replaces whatever is in index 3 with the number 2

Iterating over a collection:

There are numerous ways to “loop” through a collection.


	For Loops are great if you know how many times you need to loop
		For(int j = 0; j < 100; j++)
		{
			//do something
		}
	

	While Loops are great if you can dynamically identify when to stop.
		boolean x = true;
		While (x == true)
		{
			//Do something
			//Make sure x is set to false at same point inside this loop.
		}		
	

	Foreach Loops are great for collections
		For(Object objectVariable : Obejects)
		{
			//Do Something to objectVariable
		}
	

	Iterators:
		Iterators are able to be used from all Java collections and gives a way to process only a portion of the collection.
		The Iterator requires an import from the java.util.Iterator package.
		It has two useful methods:
			hasNext() //This returns true if the collection has an object in the next index
			next() //this returns the object in the next index.
	Example:
		private Iterator itemsIterator = items.iterator();
		public void whileIteration()
		{
			while(itemsIterator.hasNext()){
				System.out.println(itemsIterator.next());
			}
		}

		public void forIterator()
		{
			for(Iterator it = items.iterator(); itemsIterator.hasNext();){
				System.out.println(it.next());
			}
		}
			

Anonymous Objects:

In one of the above examples, you may have noticed a piece of code:

x.add(new Object());

In this add method, we are stating that we want to create a new Objects to add to the collection, but we are not defining the variable name. Since we are just creating an object in order to pass it, there is no need to actually define the object’s variable since it will never be used.

This is known as an anonymous object.

Static:

The Static keyword allows us to identify a variable or method that can be accessed without actually instantiating an object for the class.

This can be considered a kind of global variable for the class type.


	As an example:
		Say we want to create a class that represents a procedure in a medical office.
		Now, let’s say we have a service fee that applies to every procedure regardless of what the procedure is.
		We could implement a Static variable that stores a value for this service fee.
		This value would able to be accessed through the class itself rather than an instance of the class.
	
	The following code:
		Procedure p = new Procedure();
		int value = p.serviceFee;
	Becomes:
		int value = Procedure.serviceFee;
	
	As you can see we are accessing the service fee variable using the Class Name itself rather than an instance of the procedure class.
			

Static can also be applied to methods as well as entire classes.

Constants:

A constant is a variable that is not allowed to change. It is forever defined.

One use for such a variable can be seen in many video games for gravity.

Once the developer defines the gravitational constant, it should never need to be modified.

In order to declare something as a constant, you use the keyword “final.”

private final int gravity = 17;

Inheritance:

One of the powerful features of Object Orient Programing (OOP) is called inheritance.

Inheritance allows us to reduce code duplication and have a hierarchy of classes and subclasses.

A Child Class (or sub class) will extend a Parent class and inherit fields and methods.

This makes it possible to have a single class with specific fields and methods and other classes simply have access to those field and methods as if they were a part of it.


	As an example:
		Say you have a class called Person.
		Person has a field called, name.
		Then we extend Person in two new classes called Man and Woman.
		We don’t have to implement a field in each of these classes, we simply have access to the name field in the Person class.

		Public Class Person
		{
			String name;
			public void setName(String pName)
			{
				name = pName;
			}
		}

		Public Class Man extends Person
		{
			public String getName()
			{
				return this.name;
			}
		}