Thursday, April 9, 2015

Reading Assignment

I read the book How to Program and the “slideshow: Arrays” to further my knowledge of arrays. I have already learned about arrays, so most of what I read was review. However I felt that How to Program went into more depth (array lists) and wrote in a programmer’s point of view. This allowed me to think like a software engineer would and how to implement arrays into my programs more efficiently. Another reason I enjoyed the How to Program book was because the examples and activities gave me a visual and kinesthetic learning approach.

Even though I knew was familiar with the “sideshow: Array” I found it very useful to refresh my knowledge and a great tool for beginners to use and learn from.

More Array Help






Arrays Explained

An array, which stores a fixed-size sequential collection of elements of the same type. Think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

Declaring Array Variables:

dataType[] arrayRefVar;   // preferred way.
 
or
 
dataType arrayRefVar[];  //  works but not preferred way.

Example:

double[] myList;         // preferred way.
 
or
 
double myList[];         //  works but not preferred way.

Creating Arrays:

arrayRefVar = new dataType[arraySize];
·         It creates an array using new dataType[arraySize];
·         It assigns the reference of the newly created array to the variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below:
dataType[] arrayRefVar = new dataType[arraySize];
Alternatively you can create arrays as follows:
dataType[] arrayRefVar = {value0, value1, ..., valuek};

Example:

Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList:
double[] myList = new double[10];
Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9.
Java Array

Processing Arrays:

When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known.

The foreach Loops:

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.

Passing Arrays to Methods:

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:
public static void printArray(int[] array) {
  for (int i = 0; i < array.length; i++) {
    System.out.print(array[i] + " ");
  }
}
You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2:
printArray(new int[]{3, 1, 2, 6, 4, 2});

Returning an Array from a Method:

A method may also return an array. For example, the method shown below returns an array that is the reversal of another array:
public static int[] reverse(int[] list) {
  int[] result = new int[list.length];
 
  for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
    result[j] = list[i];
  }
  return result;
}


Tuesday, March 10, 2015

Warm up 3

Task: Create a piglatin Translator with the following rules
  1. If the English word begins with a vowel ("a", "e", "i", "o", "u"), add "yay"to the end of the word to produce the Pig Latin translation.

  1.  If the English word begins with a consonant (a non-vowel), move all leading consonants to the end of the word and add "ay" to produce the Pig Latin translation. You should treat the letter `y' as a vowel when it occurs after the first character in a word. You should also treat the letter pair "qu" together as a consonant. Here are some examples.
English
Pig Latin
yes
esyay
example
exampleyay
bygone
ygonebay
test
esttay
quiz
izquay
strip
ipstray
trips
ipstray
schnitzel
itzelschnay
rhyme
ymerhay






This assignment was fairly simple however, some parts proved to be a challege. One problem I faced was I could not use one "for loop" when iterrating through the word or phrase. To solve this problem I created a second array called, "constanant" and used of second "for loop" to iterrate though the word or wo for a non-vowel. Another problem was I was not able to print the translated word onto a single pane, instead it has multiple.

Wednesday, March 4, 2015

Warm Up: 1

Task: Enter user full name and split the full name into the different parts (first and last name).

Method 1: Use .indexOf( ) and .subString( )


This method had more steps however very easy to implement and conceptually understand.





Method 2: Use .split( )

I felt this method was faster and easy to implement in my program

Warm up: 2

Task: Build an age checker to see if the user is 18 years old

Method 1: Using if/else statments

In this particular code I use if/else statements which became complex because I nested many if/else statements. Another problem with this method is that I had to input the date therefore, if I wanted to try this code out tomorrow it would not work because I would have to update the current date (Not Practical).        



 Method 2: Using Switch Statement and Date Class               
Image 1



Method 2 had more coding but overall simplified the complexity of the code. As well as reducing the complexity instead, of having to input the current date I used the Date class to automatically get and input the current date.