Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 11 August 2013

RegEx - Regular Expresions

I have been playing with RegEx in Java more and more, and it is fun working out how they work and even built some myself.

I needed RegEx to perform checks on strings to validate password and email strings.

Finding RegEx to check if a string has either capitals, lowercase or numbers was quite easy :-

Capital = ".*[A-Z].*";
Lowercase = ".*[a-z].*";
Number = ".*[0-9].*";

Finding Email validation RegEx was a bit harder as there are many different compositions, some being quite loose and others being way to precise.

After a lot of testing I settled on this one :-

Email = "\\b(?!,|/)(\\w+)(?!,|/)((-?\\w+)*)?(?!,|/)(\\.{1})?(\\w*)?@{1}(\\w*)(?!,|/)((-?)(\\w*)?)(?!,|/)(\\.{1}\\w+){1,2}\\b";

But finding a RegEx to find a symbol in a string was a bit harder still.

I read many many forums and tutorials and tried many many different compositions.

I even built an array of every RegEx I had built for each individual symbol using a break before each symbol to ensure no meta characters slipped by :-

@ Symbol = ".*[\\@].*"

Then I looped through he array and tested against my test stings.

Sure it worked, but what a lot of code for something that I though should have been quite straight forward.

Then after far too many hours I looked at this problem from a different angle, if I could not reliably test a string for a symbol why not test a string for a character that was not a capital, lowercase, whitespace, underscore or number.

So I built the following :-

 Regex = ".*[^A-Za-z0-9!@#$%^&*()_+=-{}~`].*";

In RegEx ^ = not, similar to Java ! = not.

This works but once again I am still defining each symbol.

So I kept working and testing until I built :-

BetterRegex = ".*[^A-Za-z0-9\s].*";

It took me far to long but wow I had some fun.

Now it is time to relax ... relax and eat chocolate.


Thursday, 8 August 2013

RegEx - Regular Expresions

RegEx, (Regular Expresions), are truncated code strings that can be used to search other strings for patterns of characters.

RegEx are a lot of fun to play with.


http://en.wikipedia.org/wiki/Regular_expression

A great resource site that can help teach, test and share RegEx strings.

http://gskinner.com/RegExr/

Wednesday, 8 May 2013

Java Color - RGB to Hex


Wow I have had fun today.

I wanted to random generate RGB colors and then convert them to Hex.

I could not use the random generated Red Green Blue colors directly to form a Hex color as the generated numbers could be 1,2,3 characters long. 

It took me a good while with lots of reading an experimentation.

First create int variables that will random each Red Green Blue value.
int red = (int) (( Math.random()*255)+1);
int green = (int) (( Math.random()*255)+1);
int blue = (int) (( Math.random()*255)+1);

Instantiate an instance of Color that will uses the random generated Red Green Blue color ints.
Color RandomC = new Color(red,green,blue);

Create an int that will get the RGB values from the color.
int RandomRGB = (RandomC.getRGB());

Create a String that will convert RandomC RGB values to a HexString.
String RandomRGB2Hex = Integer.toHexString(RandomRGB);

Create an int variable that will convert the HexString to a HashCode.
int EndRGB = RandomRGB2Hex.hashCode();

And now you have a random generated HexCode.

Quite  a few steps, and no doubt within 10 min of posting this I will find a much easier and quicker way.

But for now I am happy that this flow works.

Monday, 15 April 2013

Java JPanel JFrame


I am still trying to get hold of the Aero Glass window borders on to my JPanel and JFrame.

But I have found some help to make JPanel borders translucent.

http://stackoverflow.com/questions/7035583/how-to-add-windows-aero-effects-to-a-jpanel/7036346#7036346

http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html


Original Post Oct 16th 2012


Java Color


Java basic colors are heavy and few.

eg.   setBackground(Color.orange);


But if you want your frames to match your OS colors you can access them;

eg.   setBackground(SystemColor.inactiveCaption);

http://nadeausoftware.com/node/105#UsingJavasSystemColors


Or even better yet use a hex code; 

eg.   setBackground(Color.decode(“#FFFF00”));

Original Post Oct 15th 2012

Campus Project Team A


UML Model


  • There are many buildings on campus.
  • Each building has a building number, a number of exits, and contains many rooms.
  • Each room has a room number, a room size, and a seating capacity.
  • Some rooms are computer labs and have many computers.
  • Each computer has an asset number and a tag & test date
  • Some rooms have a single overhead data projector.
  • Each overhead data projector has an asset number and a tag & test date.


Additional functionality/notes:

  • Add & remove buildings from campus.
  • Add & remove rooms from building.
  • A building must have a unique building number.
  • A room must have a unique room number.
  • Campus rooms can be searched by room number, to return the matching room.
  • Campus rooms can be searched by seating capacity, to return the matching rooms.
  • Computers & overhead data projectors must have a unique asset number.

Original Post Aug 20th 2012

Group Activity - Inheritance



A Player has the following attributes: 

  • Name
  • Health
  • Strength
  • intelligence
  • Location
    • X coordinate
    • Y coordinate
  • Inventory
    • a collection of InventoryItem's - each InventoryItem has the following attributes
      • ID
      • Price
      • Weight.
A Weapon is a type of InventoryItem. It has a single int attribute: DPS. 
  • An object of Weapon type may be constructed with or without providing a value for this attribute. 
    • A Gun is a type of Weapon. It has a single int attribute: ammoCount. A value for this attribute must be provided in the constructor. 
    • A Stick is a type of Weapon. It has a single int attribute: stickLength. A value for this attribute must be provided in the constructor.


A Tool is a type of InventoryItem. It has a single int attribute: numberOfUses. 

  • An object of Tool type may be constructed with or without providing a value for this attribute. 
    • A Key is a type of Tool. It has a single String attribute: keyName. A value for this attribute must be provided in the constructor. 
    • A Mirror is a type of Tool. It has a single String attribute: mirrorName. A value for this attribute must be provided in the constructor.


Original Post Jul 29th 2012

Inheritance Scenario: XYZ



yClass is a type of xClass. 

A XClass has 3 int states, x,y & z. 

An object of the xClass may be created by providing a value for all 3 of these states and also by providing only x & y. 

The yClass has 2 int states, a & b. 

An object of the yClass may be created by providing a value for a, or values for both a & b. 

The zClass is a type of yClass and has 1 int state called c. 

An object of the zClass may be created with or without supplying a value for c.

Original Post Jul 23rd 2012

Delimiters, Splits, Tokens, and Meta- Characters



So we have this text file that is full of World information on 10 players.

Each Player has the following information;

  • World Name
  • Player Name
  • Player Health
  • Player Strength
  • Player Intelligence
  • Player Location   -   each Player has a starting location
    • each location has 2 coordinates
      • X co-ordinate
      • Y co-ordinate
  • Player Inventory   -   each Player has 10 Items in their Inventory
    • each Item has 5 bits of Information 
      • Item Name
      • Item ID
      • Item Weight
      • Item Value
      • Item Level 

Now this information is all held in this World text file, but how do we read it effectively, it just seems to be a random wall of text.

We use a Delimiter.

In this case the delimiter is set as   ;

What that means is when we scan the text file each character preceded by   ;   will be recorded as a string.

Each of these strings is called a token.

Each token can be broken down further into smaller tokens using the split function.

In this case we have

  First split     :
  Second split     @
  Third split     *
  Fourth split     #
  Fifth split     ?

The issue we come across is that some characters already have assigned functions; these are called Meta- Characters, and using them as delimiters and splits will cause hang and crash issues.

Meta-Characters      .  ^  $  |  *  +  ?  (  )  [  {  \  

If you precede Meta-Characters with two forward slashes \\ it will turn them into a literal character.

eg.    scan.useDelimiter(”\\? “)     or    String[] StringName = Token.split(”\\* “)

Original Post Jun 30th 2012

Blog Task - The Constructor



Wow this turned out to be a big class.

I viewed this Player Account and treated it in a way similar to World of Warcraft.

Each player has a Player Account that must be set up before you can create avatars to play.

Each Player Account has required parameters and also have data types that are themselves class objects.

The DOB class has 3 states, dd, mm, and yyyy.

The Avatar class has 4 states, name, race, sex and class.

Original Post Jun 23rd 2012 

Blog Task - Encapsulation and Visibility


Accessors and Mutators allow exterior objects read or modify access to an objects public states and behaviours.

In UML diagrams each Accessor and Mutator is preceeded by a plus sign, ( + ), which identifies that the object state or behaviour is public.

In UML diagrams a minus sign, ( - ), denotes a state or behaviour is private, and as such is not acceable by on outside object.


What is an Accessor?

An Accessor allows outside objects to read the state or behaviour of an object. 
Accessors are also known as Getters.


What is a Mutator?

A Mutator allows outside objects to modify the state or behaviour of an object.
Accessors are also known as Setters.

Original Post Jun 23rd 2012 

Arrrggg


Java is still really testing me.

Every now and again I feel that I am on the cusp of understanding, but then another thing happens and I revert to confusion.

Maybe I am just to thick to get it.

Original Post Jun 11th 2012

Friday, 12 April 2013

UML - Group Task - Class Diagram - Courtyard



I have scanned the original but it is very faint so I redrew it 

Original Post May 3rd 2012

UML - Data Types In The Wild - Class Diagram - Quiver



As my Avatar in the previous post is an Archer I created a Quiver class

The quiver has a maximum arrow capacity and weight 

It can also only be equipped by an Archer

It increases the Archer accuracy skill

Original Post May 3rd 2012

UML - Data Types In The Wild - Class Diagram - Player



I have a main class called Player Account 

  • The main class has many states
  • The main class can have many objects 


I have an object of the main class called Player Avatar 

  • Each object of the main class has its own states and behaviours


Original Post May 3rd 2012

UML - First Class Diagram - Aeroplane


Original Post May 3rd 2012

Thursday, 11 April 2013

Java Task: URL


Original Post Apr 29th 2012

Java Task: Scores


Original Post Apr 29th 2012

Blog task: Questions


1. What keyword implements inheritance?

extends


2. What is the name of the Component library we have used this week?

javax.swing.   java.awt.


3. Provide the following links:

1.    java specification 7                 
http://docs.oracle.com/javase/7/docs/api/overview-summary.html

2.    java specification 7 - javax.swing                   
http://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html

3.    java specification 7 - JPanel
http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html

4.    java specification 7 – Jframe
http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html

5.    java specification 7 – OptionPane
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html


4. Describe/show a for loop that has been primed to start at 0 and continue 10 times.

int i;  
    for (i = 0; i <=10; i++) { }


5. What is a constructor?

The first method called when instantianting an object/class
A constructor sets variables for a class/object


6. What is the difference between a class and an object?

A class is an overall type of a thing     eg  Dog is a class
An object is an item of the class          eg  Terrier is an object of the Dog class


7. What keyword do we use to instantiate an object from a class?

new


8. Describe the parameters required by the drawOval method.

x and y coordinates  -  for the start position of the draw 
width and height       -  to set the size of the oval/circle


9. What do X & Y screen coordinates mean/refer to?
x is the horizontal axis of the screen 
y is the vertical axis of the screen


10. What is the top left coordinate of the screen?

0,0     or      x=0 , y=0

Original Post Apr 29th 2012

Update Environment Variables 2: Implementation & Blog Task



1.    What is the value of X & Y if

  • a.    R = 400, T = 10           X = 1400         Y = 910
  • b.    R = 4000, T = 20         X = 5000         Y = 920
  • c.    R = 4010, T = 50         X = 0               Y = 0


2.    How many packages may a Java project contain?
There is no concrete limit to how many packages are allowed per project.  


3.    How many classes may a Java package contain?

There is no concrete limit to how many classes are allowed per project.


4.    What are the two types of methods?

Procedure & Function


5.    Explain the differences and similarities between the two types of methods.

Both of the methods execute code, but the Function method will return a value where the Procedure method will not


6.    Of the two types of methods, which one is the Main method?

Procedure


7.    What does the keyword void mean & where is it generally seen?

The keyword Void is contained within the main method Procedure, and it identifies that the procedure does not return a value


8.    What is the special purpose of the Main method?

The main method is where the program execution begins


9.    Explain the purpose of a method parameter?

A parameter is the input value to a method


10.  How is the semi-colon used in the Java syntax?

A semi-colon is used to terminate a statement line


11.  How are the curly braces used in the Java syntax?

Curly brackets, { }, are used to begin and end blocks


12.  What data type stores whole numbers?

Integer      int


13.  What data type stores a collection of keyboard characters?

String       string


14.  What data type stores a single character?

Character     char


15.  What data type stores a True/False value?

Boolean


16.  What is a variable used for?

A variable is used to store a non fixed value in memory 


17.  What is the syntax for a literal string?

string exampleString = “I do love chocolate”;


18.  What is the syntax for a literal character?

char exampleChar =”A”;


19.  What is the syntax for a literal whole number?

int exampleInt = “7”;


20.  What is the increment operator?

+=


21.  What is the decrement operator?

-=


22.  What is the concatenation operator?

+


23.  What keyword is used between each case in a Switch, to stop execution of the Switch?

break


24.  What should always be the last case of a Switch?

else


25.  How is an object defined?

A class is a definition of all state and behaviours belonging to a group or item

Eg     Cake is the Class,    Chocolate, Lemon, Banana, Carrot are all objects of the class

Original Post Apr 25th 2012