Showing posts with label Links. Show all posts
Showing posts with label Links. Show all posts

Tuesday, May 28, 2013

JPanel


"How to Use Panels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)" ( http://bit.ly/ZaWraM )

"JPanel (Java Platform SE 6)" ( http://bit.ly/Rn8WMx )

Wednesday, April 24, 2013

Abstraction. A definition.

From http://programmers.stackexchange.com/questions/16070/what-is-abstraction

"Abstraction is the process of moving from a specific idea to a more general one. For example, take a look at your mouse. Is it wireless? What kind of sensor does it have? How many buttons? Is it ergonomic? How big is it? The answers to all of these questions can precisely describe your mouse, but regardless of what the answers are, it's still a mouse, because it's a pointing device with buttons. That's all it takes to be a mouse. "Silver Logitech MX518" is a concrete, specific item, and "mouse" is an abstraction of that. An important thing to think about is that there's no such concrete object as a "mouse", it's just an idea. The mouse on your desk is always something more specific - it's an Apple Magic Mouse or a Dell Optical Mouse or a Microsoft IntelliMouse - "mouse" is just an abstract concept."



Here's a cat...


Wednesday, April 10, 2013

TreeMap


  • The TreeMap is part of the Java Collections Framework.
  • TreeMap is a concrete class which implements the Map interface, which maps keys to values. 
  • Keys are like the index of an ArrayList except that the index of an ArrayList is an int. A map key can be any object.
  • A map cannot contain duplicate keys - each key maps to one value.
  • A key and it's corresponding value form an entry or entry-set. Let's call them key/value pairs.
  • A TreeMap is sorted by the natural ordering of it's keys.


Instantiation
Map<key,Value> = new TreeMap<Key,Value>();

(As map is an Interface, it cannot be instantiated.)

get(Object key)
Returns the value for the specified key. If a value at that key does not exist, null is returned. While this is most forgiving, always check the return value for null before attempting to use it.

Object o = myMap.get(x);
if (o != null) {...};


containsKey(int key)
Returns True if the specified key is found in the map. (False is not!).

if (myMap.containsKey(x)) {...};


put(Object key, Object value)
Puts the new value at the specified key. If the key already exists, the old value is replaced. So be careful!

myMap.put(x,o);

size()
Returns the number of key/value pairs.

myMap.size();

remove(int key)
Removes the value at the specified key. If no value as associated with the key, the method simply exits.

myMap.remove(x);

putAll(Map)
Copies all of the mappings from the specified map, to this map. Both maps must have the same key and value type. Beware though, if there are values for identical keys, the old value will be replaced with the new value - as per the *no duplicates* rule.
For instance:

Map<Integer, String> mapOne = new TreeMap<Integer,String>();
mapOne.put(10, "ten");
mapOne.put(3, "three");

Map<Integer,String> mapTwo = new TreeMap<Integer,String>();
mapTwo.put(50, "fifty");
mapTwo.put(10, "ten");

mapOne.putAll(mapTwo);

mapOne now contains the following key/value pairs (sorted):

  • 3, "three"
  • 10, "ten"
  • 50, "fifty"


Iterating (looping) using Enhanced For Loop

The following methods are utilised in the enhanced for loop example below:

  • entrySet() returns a view of the key/value pairs in the map.
  • getKey() returns the key in the current iteration.
  • getValue() returns the value of the key in the current iteration

In plain English, we are saying that for each Integer and String, key/value pair in the map.


for (Map.Entry<Integer, String> mapEntries : mapOne.entrySet()) {
      int key = mapEntries.getKey();
      String value = mapEntries.getValue();
      System.out.println("Key is: " + key);
      System.out.println("Value is: " + value);
}

We would expect the output to look like:

3
three
10
ten
50
fifty

Pulling keys or values out of Map and into ArrayList

In this example, a map of Integer,Integer is split into two lists - one for keys and one for values.

List<Integer> listOfMapKeys = new ArrayList<Integer>(mapname.keySet());
List<Integer> listOfMapValues = new ArrayList<Integer>(mapname.values());


But the coolest stuff is yet to come!!! Read on in NavigableMap Interface >>>

(also, will post code download soon too!)

(also, IS ANYONE READING THIS?) :p








Enhanced For Loop

Also called the For-each loop.



For each Line in the array called myLine.

Also see:

"Using Enhanced For-Loops with Your Classes" ( http://bit.ly/15bt7V5 )
"Java: For-each Loop" ( http://bit.ly/HThpAk )


Monday, March 4, 2013

Overriding Methods

Method overriding allows us to provide our own implementation of a method in a child class.

"Overriding and Hiding Methods (The Java™ Tutorials > Interfaces and Inheritance)" ( http://bit.ly/Zn8eop )

"Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/PYQDIV )

Tuesday, February 26, 2013

String.valueOf

DOWNLOAD THE CODE: StringValueOfPkg

Why use this function?
You want to treat a number like a String.

String.valueOf()
The String class has a function call valueOf(). This function will return the String representation of all the numeric data types (int, double, boolean, char, etc).

In the example below, two numbers are stored as int. The first print statement attempts to print each int as if they were strings. In this case, we would be hoping to see 100200 printed to screen. Notice that the concatenation operator is used - which, if you place between 2 numbers, becomes the addition operator.

The second print statement uses the String.valueOf() function. The function, as it is being used here, will return the String representation of each int.

The output for each statement is very different. The first print statement has caused the two int variables to be added (addition) together, whereas the second print statement has caused each int to be printed as a String.



Also see:
"Java - String valueOf() Method" ( http://bit.ly/YSRK2u )

Here's a cat:


Monday, February 25, 2013

Unicode

"Unicode (The Java™ Tutorials > Internationalization > Working with Text)" ( http://bit.ly/13gWvIM )

"Unicode Chart" ( http://bit.ly/13gX0m1 )

Character wrapper class for char

"Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/LUgCyw )

Getting input

DOWNLOAD THE CODE: InputFromScannerPkg

The Scanner class
A class is a template for an object. A class contains methods for performing particular tasks. From one class we can create many object instances.

The Scanner class belongs to the Java library of classes. It contains methods for reading many different types of data, from different types of input.

System.out
You have already seen System.out in action when we print to the console. This is the default stream for output.

This statement causes the words “Print this’ to be printed to the console in Eclipse.

System.out.println(“Print this”);

What you cannot see, however, are the special line ending characters which this method places at the end of the line. When subsequent lines are printed using println(), they each begin on a new line.

Here’s a different way to print to the console:

System.out.print(“Print this”);

Notice that we are now using the print() method. This method does not include special line ending characters. Each subsequent line of characters that is printed using print() will begin immediately where the last line of characters left off.




and the output...


System.in
The following code example uses Scanner’s nextInt() method to read an int input from System.in (default input from console). The input is assigned to the int variable called aNumber.

Scanner myScanner = new Scanner(System.in);
int aNumber = myScanner.nextInt();


Scanner can be told to read different types of data - or tokens. By default, Scanner uses whitespace characters as delimiters of tokens (the characters that separate tokens).

In the case of the nextInt() function, Scanner is using whitespace to delimit tokens, and return the token as an int. (seen in the figure below).

Other Scanner functions of note include:

next() - Return the next token, of any type.
next(String pattern) - the next token is delimited by the pattern supplied as a parameter.
nextLine() - reads the current line and returns it.



and the output...

In the figures above, the user is prompted to enter information. Methods of the scanner class are used to read the input typed into the console, and assign it to variables. The variable values are then printed to the console.

You can read the entire Scanner class specification here:
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Java I/O and Import statements
When we wish to use Scanner:
At the top of the code file we import a reference to the Scanner class by using the line:

import java.util.Scanner;

This line says that we intend to use the class called Scanner from the package called java.util.

In Java, all input and output (I/O) is performed by importing methods from the java.io or java.util packages.

Any source of input or destination of output is called a stream: keyboard, screen, network connection, a file, etc., are all streams.

There are three predefined Streams already open and ready to use in every program:
  •     System.in
  •     System.out
  •     System.err



Object Instantiation
To use the Scanner class, we must first declare an instance of Scanner and give it a name. Similar to declaring a variable in that we declare a type and an identifier. In this case, the type is Scanner.

As well as being declared, an object also needs to be instantiated. The use of the new keyword, instantiates the object called myScanner from the Scanner class.

Scanner myScanner = new Scanner(System.in);

In the next statement, we declare an int variable and assign it the value of the next int that is returned myScanner’s nextInt() function.


int aNumber = myScanner.nextInt();

You will notice that when we instantiated a Scanner object, we have also included System.in, inside of the parenthesis (round brackets). This is an example of passing data to a method. Often, a method needs data passed to it so that it can perform its task.

System.in is the input stream from the keyboard, via the Java console, so, we are telling the Scanner object to use this as its source.

Monday, September 24, 2012

Generating a random number

also see Using the Random class to generate a random int

The Math class contains a static method for random number generation.

To generate a single random int between 0.0 and 1.0 you can use the following:

int number = (int)(Math.random() * 10);


This method returns a value from 0.0 to 0.9. A whole number, from 0 to 9, can be produced by multiplying the result by 10.

Note that casting has been used to convert the return value, which is a double, to int.

And remember, static class methods may be called without creating an instance of the class.

Where many random numbers with a larger range are required, see java.util.Random such as below:

In this example we are going to generate random numbers between 1 and 100:


The statement rand.nextInt(100) + 1 gets the next random number in the range of 1 - 100 inclusive. Need a large number? Adjust to suit.


Thursday, August 9, 2012

Naming a package


"Naming a Package (The Java™ Tutorials > Learning the Java Language > Packages)" ( http://bit.ly/N7Y70U )

Tuesday, August 7, 2012

equals


"Java: ==, .equals(), compareTo(), and compare()" ( http://bit.ly/Nygeae )

"Java Practices -> Implementing equals" ( http://bit.ly/MZN7Tb )

"Object equality - JavaWorld" ( http://bit.ly/MZNpJC )

Tuesday, July 31, 2012

MVC - Model View Controller



"Java SE Application Design With MVC" ( http://bit.ly/NgRC5E )

"Model–view–controller - Wikipedia, the free encyclopedia" ( http://bit.ly/MPnDYu )

Wednesday, July 25, 2012

Tuesday, July 17, 2012

Black Box testing

Testing the functionality provided by the application, rather than the internals of the application.

"Black-box testing - Wikipedia, the free encyclopedia" ( http://bit.ly/NRhj0u )

"Black Box and White Box Testing for Application Blocks" ( http://bit.ly/Ny47wM )