Topics Discussed: Arrays, subscripts, iterating through arrays

Source Code Below


Topics discussed: Same as above

Source Code Available Here
Homework is here

Arrays are the most basic multi-variable container made available in java. The syntax is pretty simple, but the implementation can be confusing if you don’t have a programming background. The basic specifications of an array is that it’s a basic, 0-indexed, template’d, data structure that can be created with either explicit values or in an empty state.

int anArray[] = new int[7]; // Creates an array that can hold 7 ints.
for(int i=0; i< anArray.length(); i++){
    anArray[i] = i; // sets the array equal to it's index based off the iteration in a loop.

Arrays can be iterated through easily with loops, especially by for loops.

The Subscript
The subscript is the way that we access a certain part of an array. anArray[0] would refer to the ‘first’ (remember, 0-indexed) location in the array. Therefore the subscript in this case would be 0. Changing the subscript from within a loop is fairly common practice in Object oriented programming.

Topics Discussed: Methods, passing by value, method arguments, return types, void

Source Code Available Here

Methods are the primary way of breaking a program into individual component processes and is the key behind a concept known as Modularization. The general idea behind modular programming is breaking a large program down into smaller, more manageable parts; as this makes programming quicker, and debugging far more direct. On the other hand, excessive modularization can make for excessively verbose source code and difficult breaks in logic.

Using Methods
The syntax for using methods is pretty straightforward and will be described in depth below:

(access modifier)(static / non)(return type)(name)(argument list){
     //payload of method

Now let’s break the above example into smaller, more manageable parts:

  • Access modifier: The access modifier tells the compiler what can / can’t access the method, we’ll get more into this soon
  • Static / Nonstatic: This is fairly trivial, but it’s worth mentioning that you can’t directly call a non-static method from a static interface (calling a non-static from main for example).
  • Return Type: The return type is the type of value the compiler can expect to send back to the interface that you “called” the method from
  • Name: The name of the method can have the same name as any variable (doesn’t violate codespace, etc.)
  • Argument list: When the method is called, it can be passed variables(we’ll get more into this later) that can then be used locally in the method. The argument list tells the compiler what types of variables to expect and what to name them internally in the method

A few examples of methods and their respective calls would be as follows

//Example 1:

//Placed outside of main
public static void displayItems(){
     System.out.println("Items go here");

//placed inside main

//Example 2:

//Placed outside of main
public static void multiplyNumbers(int numberOne, int numberTwo){
     System.out.prtinln("The product of the two numbers is: " + (numberOne*numberTwo));

//Placed inside of main:
int a = input.nextInt();
int b = input.nextInt();


//Example 3 -- with return type

//Placed outside of main
public static int multiplyNumbers(int numberOne, int numberTwo){
     return (numberOne*numberTwo);

//Placed inside of main:
int a = input.nextInt();
int b = input.nextInt();

int product = multiplyNumbers(a,b);
     System.out.prtinln("The product of the two numbers is: " + product);

Return types
Return types are used to set a value of to be used in some process inside of the interface you called it from (for now that will most likely be main). This isn’t just limited to int, this can be any of the normal types that we’ve discussed so far, and even objects when we get to that point later on. So this could be boolean, string, a 1/2 dimensional array, etc. On the other hand Void means that absolutely nothing will be returned to the interface in which the method was called. This is useful when the program doesn’t need the values anymore, as they are destroyed when the method ends.

Passing variables, and the argument list
The argument list defines what we can / cannot pass to our method. There will be times when this is great for debugging, since you might have a variable that is waiting for 5 items, but only gets 4, the compiler will tell you that it’s awaiting the 5th. Also, very important to note is that up to this point we have been passing variables into our method by value, that means that the contents of our variable are being sent over, not the variables themselves. Therefore, any changes to our variables that might take place in the method only “count” in that method, here’s an example:

     public static void prodSum(int x, int y){
        //x = j,  k = y
        System.out.println("The product is: " +(x*y)); // produdct of our numbers
        System.out.println("The sum is: " +(x+y)); // produdct of our numbers
        x +=25;

           System.out.println("Enter number 1: ");
           j= input.nextInt(); // input 10
           System.out.println("Enter number 2: ");
           k=input.nextInt(); // input 20
           sum = prodSum(j,k);
           System.out.println("The sum is: "+ sum);

           System.out.println("j: " + j); // will still equal 10
           System.out.println("K: " + k); // will still equal 20

In the coming lessons we’ll be introducing how to pass by reference, which is where we will be passing the variable itself (sort of) to the function for extra processing.

Topics discussed: Encapsulation by separating methods into new files.
Source Code Available Here

Program encapsulation
The concept of program encapsulation is simple: break things down into their component parts so when something breaks, it’s easy to track, and easier to fix. The more ‘parts’ you break your program into (via methods) the easier this becomes. But as you increase the number of methods, the length of a single file becomes unmanageable and huge. Somewhere around 100-400 lines, a file becomes “hard” to read, so keeping it below that level is optimal (in my opinion) if at all possible.

Adding in additional files
In netbeans we will select a new file, and then simply add a new java class file to an existing project. The only thing that really changes in terms of code is that when we call a method from another file, we need to include the class name in the call to that method. Something like this:

//Normal header here

int x=0;
x= HandleX.addfive(x);


public class{

public static int addfive(int x){

return (x+5);

}//End method

}// End class

In the example above you’ll notice that the method is called by it’s filename first when we are in another file. This happens because we need to tell the compiler where to find the method that we’re trying to use.

Topics discussed: Access modifiers, public, private, static
Source Code Available Here

Access modifiers
Access modifiers are our way of sectioning off our program and making it harder to accidentally use / misuse methods not contained within the calling class. There are 4 access modifiers and they basically work like this:

  1. Public: Public as an access modifier means that the ANYTHING can call on your class / function. This means that anyone / any function can call it for any reason, we’ll see why that’s a problem later on.
  2. Protected: Protected is the exact same as public except that it cannot be called by anything, only things that exist in the same package / class / subclass as where the call originated from.
  3. not specified: Only accepts calls from the class / package and no subclasses.
  4. Private: Only accepts calls from within the class.

  5. Using public as an access modifier
    When we use public as an access modifier, the entire program (or any other program) can call upon our methods / objects if they are familiar with our interface. We might get into external calls at a much later lesson but for now let’s look at internal same-project calls:

    ///// /////
    int x= 1, y=7;
    //the syntax to call a public method in another file is methodFile.methodName
    sum = methodFile.sum(x,y)
    public static int sum(int x, int y){
    return (x+y);//Will return the sum of x+y to

    Using private as a modifier
    If you’re in doubt about whether or not you should be using private as a modifier, you should be using private as a modifier. When we get into larger programs in the future with 4 or 5 different files, data will be moving all over the place, and controlling the flow of that data is extremely important. The only way to ensure that nothing gets passed incorrectly is to safeguard methods / classes that we want to keep safe.

Topics Discussed: Class Wrappers and changing types using toString / parseint / parseDouble

Source code

Class wrappers:

Class wrappers exist to help us manipulate primitives of similar type. Some code that you’ll want to be aware of is:

Double d = null; // Class wrappers can accept null values
Double.parseInt(d); //Class wrappers can be used to change the variable type as long as the data is the same
**NOTE** When changing variable type, there can be a loss of precision.   Example
double d= 4.779;
int i = Double.parseInt(d); // I would now be equal to 4.   Everything after the decimal would be dropped.