Java-licious Java Primer: Properties

Posted: February 16th, 2012 | Author: | Filed under: Java, O-O | No Comments »

Hark! It’s the second installment of my “Java-licious Java Primer” series. The series was created for folks that are new to software development (experienced code-heads and vulcans may want to skip this one). In this post we’ll be focusing on “properties”.

Arguments

Methods may require arguments in order to function. Arguments are the values that you pass into a method. Lets compare these two examples…

1
2
3
public static void displayYourName(){
	System.out.println("John Jacob Jingleheimer-Schmidt");
}

Example 1a – a method that does not require any arguments

We can determine that the above method does not require any arguments because the arguments are not defined in the parenthesis “()” on line 1.

1
2
3
public static void displayYourName(String yourName){
	System.out.println(yourName);
}

Example 1b – a method that requires one argument

Check out the code inside of the parenthesis “()” on line 1. “String yourName” means that the method expects you to pass in a string when calling this method. The method will refer to this string as “yourName”.

Remember that methods may (or may not) require arguments.

Returned Values

Additionally methods may return values. Lets compare these two examples…

1
2
3
public static void displayYourName(){
	System.out.println("John Jacob Jingleheimer-Schmidt");
}

Example 2a – a method that does not return a value

1
2
3
public static String getYourName(){
	return "John Jacob Jingleheimer-Schmidt");
}

Example 2b – a method that returns a string

Remember that methods may (or may not) return values.

Methods And Properties

A class is made up of methods and properties. Methods (covered in the previous post) define the behavior of classes. Properties on the other hand define the non-behavioral aspects of a class. For example, if we were to think of a Person as a class, breathing, running and jumping would be considered methods. Name, hair color, height, weight and gender would be some of the properties of the Person class.

Lets create a class called “RegularDrip.java” into a package named “coffee”. Your code should look like the example in Figure 1.

1
2
3
4
5
6
7
8
9
10
package coffee;
 
public class RegularDrip {
 
	private static String whoAreYou = "I am a cup of regular drip coffee";
 
	public static String getWhoAreYou(){
		return whoAreYou;
	}
}

Figure 3a – Class RegularDrip.java

Two items of note here. The first is the code on line 5. This is the classes only property. It’s name is “whoAreYou” and it’s type is a “String”.

5
private static String whoAreYou = "I am a cup of regular drip coffee";

Figure 3b – the whoAreYou property of class RegularDrip.java

Data Types

All properties have a data type. Data types are just that…one of the many different type of data. Our whoAreYou property on line 5 is the “string” datatype. Let’s add another property to our object. Add the code below after line 5.

private static int ouncesRemaining = 8;

Figure 4a – the ouncesRemaining property of class RegularDrip.java

Our RegularDrip class now has two properties! There are a few differences between the two properties that we should look into.

5
6
private static String whoAreYou = "I am a cup of regular drip coffee";
private static int ouncesRemaining = 8;

Figure 4b – the properties of class RegularDrip.java

The second one has a type of “int”. That’s Java-speak for “integer”, which is math speak for “number”. The other difference is on the right side of the equals sign. The whoAreYou property is a string, which means that we have to wrap it’s value in quotes. The ouncesRemaining property however, is an int…which means that we need to declare it’s value without quotes.

Lets Make Coffee

Using MrJava5000.java from the previous lesson lets add a few additional lines of code. After the package declaration add the following code:

import coffee.RegularDrip;

Figure 5a – an import statement

This code is an import statement. It allows us to access the RegularDrip class.

And after the sayHello() method call in the main() method add this code:

RegularDrip myMorningCup = new RegularDrip(); // new
System.out.println(myMorningCup.getWhoAreYou());

Figure 5b – make a new cup of coffee, then tell us about it

This code creates a new instance of RegularDrip named ‘myMorningCup’, then sends the returned value of the RegularDrip instance’s getWhoAreYou() method to the console window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package coffeemakers;
 
import coffee.RegularDrip; // new
 
public class MrJava5000 {
 
	public static void main(String[] args) {
		sayHello();
		RegularDrip myMorningCup = new RegularDrip(); // new
		System.out.println(myMorningCup.getWhoAreYou());
	}
 
	public static void sayHello(){
		System.out.println("hello world");
	}	
}

Figure 5c – MrJava5000.java

Run the MrJava5000.java class now and prepare to be amazed as your creation actually produces a (fake) cup of coffee!

Many Ways To Say Hello

Lets take a look at the println() method. We know that

Here are a In all three examples we output the word “hello” to the console window. In each of the three examples we pass a string into the println() method. In the first example we create a string on-the-fly simply by wrapping some text in quotes.

1
2
3
4
public static void main(String[] args) {
	// say hello
	System.out.println("hello...is it me you're looking for");
}

Example 6a – Hello

Check out the println() method call. Note that “hello” is wrapped in quotes because println() expects a string.

1
2
3
4
5
public static void main(String[] args) {
	// say hello again
	String greeting = "hello...is it me you're looking for";
	System.out.println(greeting); 
}

Example 6b – Hello

Again focusing on println(), note that “greeting” is not wrapped in quotes. This is because “greeting” is a variable of type “string”.

1
2
3
4
5
6
7
8
// say hello a third time
public static string getHello(){
	return "hello...is it me you're looking for";
}
 
public static void main(String[] args) {
	System.out.println(getHello());
}

Example 6c – Hello

This time we pass the returned value from the getHello() method into the println() method.

Next Time

Next time we’ll dig deeper into what an “instance” of a class is.


Java-licious Java Primer: Methods

Posted: February 13th, 2012 | Author: | Filed under: Java, O-O | No Comments »

Welcome to the first of the “Java-licious Java Primer” series. The series was created for folks that are new to software development (experienced code-heads and vulcans may want to skip this one). In this post we’ll be covering how to create a class, how to add a method to the class, and how to “call” the method.

Start Here

In order to follow along you will need to download and install the Eclipse IDE. IDE is fancy-talk for “interactive development environment”…which is fancy talk for “the program that we will edit and run java files in”. Download the zip file, and unzip the contents to to c:\Eclipse. To open Eclipse simply browse to c:\Eclipse and double-click eclipse.exe.

Create A New Project

In Eclipse follow these two simple steps to create a new project…

  1. Click on “File” > “New” > “New Java Project”
  2. Name your project “Javalicious” and click “Finish”

Create Your First Class

  1. In the Package Explorer pane, right click on the Javalicious project, select “New” > “Class”
  2. Name your class “MrJava5000″ and give it a package name of “coffeemakers”
  3. Under “Which method stubs would you like to create” make sure that “public static void main(String[] args)” is checked.
  4. Click “Finish”

Your new class is now created and should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
package coffeemakers;
 
public class MrJava5000 {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
	}
 
}

Figure 1 – MrJava5000.java

What Does It All Mean?

Before we proceed with attempting to understand all the strange letters, squiggles and assorted shapes in Figure 1 lets take a step back and talk about code comments. Here is how code comments are written in Java.

// I am a single-line code comment.
/**
* I am a multi-line 
* code comment.
*/
/*
 I am also a multi-line 
 code comment.
*/

Code comments are the ideal place to leave notes for yourself, and to explain what a given section/line of code does to other developers. I’ve modified the code in Figure 1, adding comments and removing a few things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this is the package declaration
package coffeemakers;
 
/* 
this is the class declaration.  it's "public" which means that it can be used by other classes.
note the curly brace '{' after the class name.  it's matching curly brace is on line 14
*/
public class MrJava5000 {
 
	// this is a method named 'main'.  this method begins on line 11 and ends on line 13.
	public static void main(String[] args) {
 
	}
}

Figure 2 – MrJava5000.java with lots of code comments

Package Declaration

So the code on line 2 is the package declaration. It basically says “the MrJava5000 class is found in the folder named ‘coffeemakers’. Think of packages as folders. If we were to change the name to from “coffeemakers” to “coffeemakers.mrjava” Eclipse would simply move the file from [project root]\src\coffeemakers\ to [project root]\src\coffeemakers\mrjava\.

Class Declaration

You may notice several occurrences of the left and right curly braces “{” and “}”. The class declaration on line 8 has a left curly brace. It’s matching right curly brace is found on line 14. Everything between these two symbols is a part of class body.

The “Main” Method

Lines 11 through 13 contain the a method. This is a special method named ‘main’. It is special in that it will run whenever you run the class.

11
12
13
public static void main(String[] args) {
 
}

Figure 3 – The “main” Method

Lets Add Another Method

Below the closing curly brace for the main method(and before the closing curly brace for the class) add the following code.

public static void sayHello(){
  System.out.println("Hello world");
}

Figure 3a – The “sayHello” Method

This is a new method named “sayHello”. It calls a method named “println”. This method displays whatever text is contained within the parenthesis and quotes.

Your code should look like this…

1
2
3
4
5
6
7
8
9
10
11
12
package coffeemakers;
 
public class MrJava5000 {
 
	public static void main(String[] args) {
 
	}
 
	public static void sayHello(){
		System.out.println("hello world");
	}	
}

Figure 3b – MrJava5000.java, including our new “sayHello” method

Now let’s run this Java class and see what happens. Highlight MrJava5000.java in the Package Explorer window and press the green play button. Observe the results. Actually that’s a joke, as there are no results to observe. The reason why we didn’t see the “hello world” text that we wanted to is due to the fact that we aren’t “calling” the helloWorld() method from our main() method. Let’s do that now by adding the following code to the main method.

sayHello();

Figure 4a – calling the “sayHello” method

OK, now we have added code inside of the main() method that calls the helloWorld() method. Your code should now look like this…

1
2
3
4
5
6
7
8
9
10
11
12
package coffeemakers;
 
public class MrJava5000 {
 
	public static void main(String[] args) {
		sayHello();
	}
 
	public static void sayHello(){
		System.out.println("hello world");
	}	
}

Figure 4b – MrJava5000.java, ready to say hello

Run the code again and you should see the following text in the Console panel.

hello world

You’ve just created your first Java class. When run, this classes main method runs. The main method calls the sayHello method, which in turn outputs the text “hello world” to the console panel. This of course means that you are one step closer to becoming a Java legend!


New To Java? Read This Book

Posted: February 2nd, 2012 | Author: | Filed under: Java, O-O | 2 Comments »

If you are new to the strange and wonderful world of Java development, Head First Java is the essential primer.

Do you have a favorite “Intro to Java” or “Intro to Object Oriented Development” resource? Please share in the comments :)


Selenium RC, Now With More Big Daddy Kane

Posted: January 10th, 2012 | Author: | Filed under: O-O, Selenium | No Comments »

This post is a part of my Learn Selenium series.

Inheritance

In object-oriented programming, a class can inherit the methods and properties from a parent class. This means that we can add code to a parent class that can be used in one or more child objects. The advantage is that one would only have to write code one time for it to be available in many places.

Lets take a look at the example in Figure 1a:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package awesomeTests;
 
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
 
@SuppressWarnings("deprecation")
 
public class AnAwesomeTest extends SeleneseTestCase {
	public void setUp() throws Exception {
		setUp("http://code.christophervigliotti.com/", "*chrome");
	}
	public void testAwesome() throws Exception {
		selenium.open("/");
		selenium.click("link=Excel Entity Service");
		selenium.waitForPageToLoad("30000");
		selenium.type("id=comment", "Test comment.");
		selenium.click("id=submit");
		selenium.waitForPageToLoad("30000");
	}
}

Figure 1a. AnAwesomeTest.java

This is a basic Selenium RC test case. Lets take a closer look at line 8.

8
public class AnAwesomeTest extends SeleneseTestCase {

Figure 1b. Line 8 of AnAwesomeTest.java

Translated from Java-speak into English this line reads as follows. “This public class is named ‘AnAwesomeTest’ and is a child of ‘SelesenseTestCase’. AnAwesomeTest.java is a child of parent class SelesenseTestCase.java. This means that the logic found SelesenseTestCase.java and it’s ancestors is available for us to call upon. If you look at the setUp() and testAwesome() methods we are calling methods from the parent class at line 10, as well as on lines 13-18.

Lets take a look at a second Selenium test class…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package awesomeTests;
 
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
 
@SuppressWarnings("deprecation")
 
public class AnotherTest extends SeleneseTestCase {
	public void setUp() throws Exception {
		setUp("http://code.christophervigliotti.com/", "*chrome");
	}
	public void testSomething() throws Exception {
		// your test code goes here
	}
}

Figure 2.  AnotherTestCase.java

Like AwesomeTestCase, AnotherTestCase is a parent of SeleneseTestCase. 

Leveraging A New Parent

Now if we were to create a new parent class for these two classes we would be able to combine some of the redundant logic into one place.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package awesomeTests;
 
import com.thoughtworks.selenium.*;
// not being used! import java.util.regex.Pattern;
 
@SuppressWarnings("deprecation")
public class BigDaddyKane extends SeleneseTestCase {
 
	// properties
	public int pageReloadTime = 3000;
	public String url = "http://code.christophervigliotti.com/";
	public String browser = "*chrome";
 
	// methods
	public void setUp() throws Exception {
		// was setUp("http://code.christophervigliotti.com/", "*chrome");
		setUp(url, browser);
	}
}

Figure 3a. The awesomely named BigDaddyKane.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package awesomeTests;
 
@SuppressWarnings("deprecation")
public class AnAwesomeTest extends BigDaddyKane {
	public void testAwesome() throws Exception {
		selenium.open("/");
		selenium.click("link=Excel Entity Service");
		// was selenium.waitForPageToLoad("3000");
		selenium.waitForPageToLoad(pageReloadTime);
		selenium.type("id=comment", "Test comment.");
		selenium.click("id=submit");
		// was selenium.waitForPageToLoad("3000");
		selenium.waitForPageToLoad(pageReloadTime);
	}
}

Figure 3b. Modified AnAwesomeTestCase.java

1
2
3
4
5
6
7
8
package awesomeTests;
 
@SuppressWarnings("deprecation")
public class AnotherTest extends BigDaddyKane {
	public void testAwesome() throws Exception {
		// your test code goes here
	}
}

Figure 3c. Modified AnotherTestCase.java

Since BigDaddyKane contains the same import statements and setUp() method we are able to remove them from AnAwesomeTestCase and AnotherTestCase. Code re-use FTW!

Leveraging Properties

If you take another look at BigDaddyKane.java you’ll notice some other new code.

9
10
11
12
// properties
public int pageReloadTime = 3000;
public String url = "http://code.christophervigliotti.com/";
public String browser = "*chrome";

Figure 4, properties of BigDaddyKane

These are a few properties that I added to the class. The url and string properties are used in line 17 of BigDaddyKane.java. The pageReloadTime property is used on lines 9 and 13 of AnAwesomeTest.java. This modification allows for further code re-use, and specifies a single place where the page reload time, url and browser are defined.

Summary

Parent classes are a great way to help you both pay tribute to golden age rapper Big Daddy Kane and maximize code re-use in your Selenium RC tests.

Ready to dive deeper into the world of Java and Selenium RC? Check out my post on how your code can access test data via an Excel file.


Diving Head First Into Head First Design Patterns

Posted: July 6th, 2010 | Author: | Filed under: Design Patterns, O-O | 1 Comment »

Over the last few days I’ve been sinking my teeth into Head First Design Patterns. I’m really enjoying both learning about Design Patterns as well as slinging some Java code.


Building, Extending and Calling ColdFusion Components

Posted: May 12th, 2009 | Author: | Filed under: ColdFusion, O-O | 5 Comments »

If you don’t know what a ColdFusion component is you may want to review this before continuing.

Every system that I’ve developed over the last few years has included a Utility component. This component is where I store helper functions. The example below contains a single function that takes one argument, dumps it out on the screen then calls CFABORT.

Read the rest of this entry »


Shell

Posted: May 8th, 2009 | Author: | Filed under: ColdFusion, MVC, O-O | No Comments »

My first pet project is “Shell”, a simple O-O ColdFusion Sample Application. I’ve spent about 90 minutes on it at this point (translation: it’s not done), so consider it more of a “sketch” at this point.

Shell – Version 0.0.0.1b (pardon the hokey over-use of the shell metaphor in the code).