Showing posts with label cat. Show all posts
Showing posts with label cat. Show all posts

Friday, March 7, 2014

Using the Random class to generate a random int

Code sample at pastebin


The Random class

java.util.Random
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Importing java.util.Random

You will need the following imports statement at the top of your code file:
import java.util.Random;

Generate any random number

Random r = new Random();
r.nextInt();
generates a random number in the int range (-2,147,483,648 to 2,147,484,647).

Generate a random within a maximum range

Random r = new Random();
r.nextInt(max);

generates a random number in the positive int range, inclusive of 0 but exclusive of max.
eg.
r.nextInt(500);
generates a number from 0 to 499. If you want to include 500 in the range, add + 1.

r.nextInt(500 + 1);

Generate a random within a minimum and maximum range

Random r = new Random();
r.nextInt(max - min) + min;

generates a random number in the positive int range, inclusive of min but exclusive of (max - min) and then adds the minimum to correct the range.

eg.
r.nextInt(500 - 300) + 300;

Generates a number from 0 to 299. You can then add your minimum so now the number has the range of 299 to 599. Remember, the actual number will be exclusive of the max so if you want to include the max add + 1.

r.nextInt((500 - 300) + 1) + 300;



Here's a cat.

Wednesday, October 9, 2013

Using GoogleCode repository with Eclipse

There are free code repositories such as GoogleCode for storing a project’s codebase. Github also has a free usage tier. The catch is that all hosted code must be open source. As such anything you store in the repository is public, and can be downloaded. You are, however, in control of who can check code in to the project.


In this tute, we are going to use GoogleCode with the Subclipse Subversion plugin for Eclipse.


Setting up a project at Google code


  • Go to Google Code https://code.google.com/
  • Click Create a new project
  • Enter all fields as required -
  • for version control system, enter Subversion
  • for source code licence - enter Apache Licence 2.0
  • Click Create Project
  • You will then be presented with your project’s home page.
  • Click on the source tab
  • There are two svn checkout commands:
    • The first one is the one you will use to check your projects in and out with - note that it includes a URI, and your username at the end of the command.
    • The second one may be used by anyone to check out your project
  • Bookmark this page.



Install the Subversion plugin into Eclipse


Next, install the Subclipse Subversion Eclipse plugin:
  • From the main menu > Help > Install new software > Add new
  • enter the following:
  • Click OK
  • Allow the list of available software to populate
  • Select/Accept/Download/Install the subclipse plugin
  • You will be asked to restart. Do so.


Connect existing Eclipse projects to your Google code repo


  • Create a new project or select an existing project (any type of project)
  • Right click the project > Team > Share





  • Select the Create a new repository location option > Next
  • Enter the URL component of the SVN check in command (from your Google code repo)





  • Click Next
  • Leave the next lot of options as default > Next





Click Finish






  • Enter your Google Code username and password:
    • username: your email addresspassword: your Google code generated password (return to your Google code project source page and click on the googlecode.com password link to generate a password.

  • Click OK





  • Click OK on the next screen.


Notice that your project now has an *asterisk on it’s name. This is to indicate that the current version of the project has not yet been checked in to the repo. There is also repo location information next to the project name.





Your project and Eclipse is now ready to use the repo (it is not checked in yet).


Commit the project to the repo

  • Right click the project >Team > Commit





  • Click Select All
  • You can enter a comment with the commit if you like.
  • Click OK
  • The local copy of the project is now committed to the repo.
  • Your project icon now looks like this:




  • Return to Google code and click Source > Browse
In the svn tree under directories, expand the trunk node.






You can click and view the java files in the src directory too!


Connect Eclipse to existing projects in the Google code repo

  • Find the project you wish to check out on Google Code and select browse > source (you can use my code repo to test on)
  • Copy the check out URL
  • In Eclipse, from the main menu select Import > Other > SVN > Checkout projects from SVN > Next
  • Select Create a new repository location > next
  • Copy in/enter the project’s check out url for example
  • https://my-diploma-workspace.googlecode.com/svn
  • > Next
  • The list should populate with all of the project folders available in the repository. Select the one/s you require and select Next > Next > Finish.



Here's a cat.




Wednesday, September 18, 2013

Overriding Methods

A child class can override concrete methods from the parent. This is done by precisely duplicating the parent's method signature, in the child.

Let's override the toString() method which our class inherits from the Object class:




Here's the call:




Here's the output:




When overriding, ensure the method signature is reproduced perfectly.

Here's a cat.



Friday, August 30, 2013

HashMap


A HashMap is just like TreeMap except that a HashMap is not ordered by its keys.

See long post on TreeMap for methods and iteration technique. Go!


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...


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

What is a variable?


Variables are used to store values in a program. A value might be a name, a number, an address, and so on. These values can then be displayed or processed to create new information.

Think about a calculator. Each time you press a button, the calculator remembers what you have pressed. When you press the equals button, the calculator forms a mathematical equation using your series of key presses.

There are many types of values. Some values are made up of letters and numbers (such as an address) and some values are simply numbers. Each type of data can be stored in a program using a variable.

For instance, in a Java program, we could do the following:

String userName = “C2012000”;
String password = “monkey5000”;
String address = “30 Happy Place, Uptown”;
int numberOfLogins = 12;


The words String and int are types of Java variables - or data types.

Now we have 4 stored values that can be used again later in the program. Perhaps we need to print these values to the screen or perhaps we need to know where a letter must be mailed to. Perhaps we need to take an average of how many times a user has logged in over a particular time frame.

Alphanumeric & Numeric data.
Alphanumeric data is any collection of keyboard characters. It may contain letters, numbers & other special characters. Values such as these are stored in an variable that can hold alphanumeric values. This type of variable is called String.

String whatTheDogSaid = “Right on partner number 2!”;

A String may contain numbers, such as a house number in an address. But if we need to store a number so that we can do some kind of maths on that number, we need to use a variable type that can store numbers as numbers.

Numeric data are numbers. Generally speaking, data is considered numeric where it is being used for maths purposes - such as counting, adding, subtracting, and so on. Some numbers are whole numbers, such as the number of students on a bus. Some numbers contain a floating point, such as currency values, or body weight. For this reason, there is more than 1 type of variable that can hold numeric data. For instance, an int variable can store a whole number and a Double variable can store a floating point number.

int numOfStudentsOnBus = 22; 
double dogWeight = 12.2;

The number of students on a bus is going to be stored as a whole number, so we store it in a variable that can store whole numbers, such as int.

A number representing weight contains a floating point so we store this number in a variable type that can store floating point numbers such as double.

There are many data types in the Java language!

Here's a cat:


Sunday, June 3, 2012

if statements


Is used for implementing selection algorithms. Executes a certain code section only if a condition is true.

Format:

if (expression) {
   'Statements.
}

The expression to be evaluated is placed within parenthesis. Curly braces are used to open a block, immediately after the expression. If the result of evaluating the expression is true, all statements within the curly braces will execute. If the expression returns false, the statements within the curly braces will be skipped.

An expression is a combination of literal values or variables, and operators.

In the example below, the user is asked to enter a number. The if statement determines if the number is less than (less than operator < ) 100 and prints a message to screen, if it is. It then goes on to ask the user to enter their name. The if statement determines if the name has a length (String length) of more than 5 characters and prints a message to screen if it does.


Note that the if statement opens a code block with curly braces.

To recap, if the user enters a number that is less than 100, they will get the message "The number you entered was less than 100" and if the user enters a name that has a length greater than 5, they will get a message saying "The length of your name is longer than 5 characters long".

Here is the output:




Evaluating multiple expressions in an if statement

We can use the logical AND operator && to evaluate multiple expressions.


In this example, if the user enters a number that is greater than 50 AND less than 100, they will get the message "The number you entered was greater than 50 AND less than 100" and if the user enters a name that has a length greater than 5 AND less than 20, they will get a message saying "The length of your name is longer than 5 characters long AND shorter than 20".





also see:
"The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/zflbUx )




Here's a cat...







Saturday, June 2, 2012

Switch

CODE DOWNLOAD: UsingSwitch.zip


A Switch can be used to evaluate a number of numeric types such as byte, short, char & int.

Let's say you want to check the value of a single variable and based on this value, one of many things may happen.

In the example below, the user enters a number. This number is then checked by the switch. The first case to match, executes a statement - in this case, printing a message to let the user know what the entered value was.


If the user entered a value not listed in the cases, the default section executes. It is good practice to always include a default case in your switch.

Notice that the final statement in each case is break. This causes the switch to stop checking down the list.

We can also execute a common statement for multiple cases, as in the example below:


Of course, we could use a number of if then else statements to achieve the same result, however, as we are only checking the value of a single variable, the switch is a more elegant and readable solution (subjective).

Here's a cat:




"The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/y2TVYE )