Tagged with

# Java

## Problem Solving with code – Speeding Fine

We are going to start a series of posts on code problem solving. This will really help you guys that are self taught software developers. No matter how simple or complex we get with these problems it’s always good to take 10 to 20 mins and practice. All of these post will be using Java but feel free to post your solutions using whatever language you like. I would encourage you to take the extra time to write some unit tests so that you know if your code works or not.

Solve the problem on paper or a whiteboard using pseudocode first and post a picture of your solution in the comments below! Here is an example of some really rough code on paper that I was writing to solve another problem.

The first problem we are going to talk about is speeding fine.

Below is the solution to the speeding fine problem. Post a link to an image of how you solved it or create your own gist for everyone to see.

## How to create a bubble sort with Java and Haxe

There are tones of posts online that cover bubble sort. Lately I’ve been brushing up on my Java and also I like Haxe so here are two videos covering bubble sort in each of the two languages.

Java Bubble Sort Algorithm

```import java.util.Arrays;

/**
* Created by matthew.wallace on 2/10/17.
*/
public class Main {

public static void main(String[] args) {

int[] list = {4,6,2,90,245,3,21,356,42};

System.out.println("before sort : " + Arrays.toString(list));
int i,j,temp;

for (i = 0; i < list.length - 1 ; i++) {
for (j = 0; j < list.length - 1 - i ; j++) {

if( list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}

System.out.println("after sort : " + Arrays.toString(list));
}
}
```

Haxe Bubble Sort Algorithm

```package ;
class Main {

public function new() {

var list:Array<Int> = [7,5,1,2,45,37,42,183,3];

var temp:Int;

trace("unsorted list : "+ list);

for(i in 0...list.length - 1) {
for(j in 0...list.length - 1 - i) {

if ( list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}

trace("sorted list : "+ list);

}

public static function main() {
new Main();
}
}```

## How do you create a queue from stack algorithm

This will be the first in a series of posts on some common algorithms you can use to solve various problem. I find it super helpful to do one or two of these per day just to keep things fresh in your mind. These are especially helpful to know during coding interviews.

Queue from Stack Java Example.

Queue from Stack Haxe Example.

So what is a “Stack”. Well, it’s a container of objects to put it simply. These objects are put in using the last-in first-out out method. To explain this in layman terms lets imagine you have a box and you put in a nickel, a dime, a penny and a quarter in that order. You come back to the box a few seconds later and reach in one at a time to pull out the money that you put in. The order that you can expect the money would be in reverse because when removing from a stack you must remove from the top down. So the first item to leave the box would be the quarter, then the penny, then the dime, then the nickel.

Here is an example of creating and adding items to a Stack in Java.

```import java.util.Stack;

public class Queue {
private Stack<Integer> newStack;

public Queue() {
oldStack = new Stack<Integer>();
newStack = new Stack<Integer>();
}

public boolean enqueue(int element) {
boolean rvalue = true;

try {
newStack.push(new Integer(element));
} catch (Exception error) {
rvalue  = false;
System.out.println("There was an error adding the item to the Queue : " + error.getMessage());;
}

return rvalue;
}

}
```

Here is an example in Haxe of a Stack using a typed Array.

```package ;
class Queue {

var newStack:Array<Int> = [];

public function new() {
}

public function enqueue(element:Int):Bool {

var rvalue:Bool = true;

try {
newStack.push(element);
} catch (error:Error) {
rvalue = false;
trace("Something went wrong with added an element to the Queue: " + error);
}

return rvalue;
}

}```

Next we talk about the concept of a “Queue”. The Queue is a container that uses the first-in first-out principle. In our example above when explaining what a stack does, in order to implement the same example regarding a queue you would have a box and you would place a nickel, a dime, a penny and a quarter into the box in that order and then when removing them you would expect the money to come out of the box in the order in which you placed it, so you would see a nickel come out of the box first followed by the dime, then the penny, then lastly the quarter.

Here is an example of using some Queue logic with a Java Stack.

```import java.util.Stack;

public class Queue {

private Stack<Integer> oldStack;
private Stack<Integer> newStack;
private int topElement = -1;
public Queue() {
oldStack = new Stack<Integer>();
newStack = new Stack<Integer>();
}

public Integer dequeue() {
topElement = -1;
if(oldStack.empty()) {
while(!newStack.empty()) {
topElement = newStack.peek();
oldStack.push(topElement);
newStack.pop();
}
}

if(!oldStack.empty()) {
Integer i = oldStack.peek();
topElement = i.intValue();
oldStack.pop();
}

}
}```

Here is an example of creating Queue logic with a Haxe typed Array.

```package ;
import haxe.io.Error;
class Queue {

var oldStack:Array<Int> = [];
var newStack:Array<Int> = [];
var topElement:Int = -1;

public function new() {
}

public function dequeue():Int {

topElement = -1;

if(oldStack.length <= 0) {
while(newStack.length > 0) {
topElement = newStack.pop();
oldStack.push(topElement);
}
}

if(oldStack.length > 0) {
topElement = oldStack.pop();
}

}
}```

## Red5 and Flex Basics Part 1 – Keep Track Of Users

In this video I am going to show you the basics of starting your first Red5 java application and we will use Flex to talk to the server and the server will keep track of a list of users that are logged in to the application. As soon as a user closes there browser or leaves the applications page the user will be removed from the applications list of users. This is really helpful for building applications where users are interacting with other users. This could be a Chat or Video application or even an online game where users can play against each other.

Before you can start you will need a couple of things installed. Eclipse with Flex Builder plugin, The Java EE development environment, and finally the Red5 Server Plugin for Eclipse. In the video I will explain a little bit about why you need this setup and where to go to get it but I will not cover installation at all.

I hope you guys enjoy and let me know if you have any questions.

Also included is the source files. Red5Tut.zip Red5TutClient.zip

If you are interested in taking this corse then submit your email. I'll make sure you get a discount for signing up early.