Temperature Conversion Program in Java

Here's a simple program to convert temperatures between degrees Celcius, Fahrenheit and Kelvin written in Java. There's no GUI used here - all output is done via the command line.
The forumulae for the conversions were obtained from Temperature conversion forumlas on Wikipedia

File: Converter.java

/**
* Conversion between temperatures in Celcius, Fahrenheit and Kelvin
* Uses conversion forumulas from the wikipedia:
* http://en.wikipedia.org/wiki/Temperature_conversion_formulas
* User: Steven
* Date: 27-Dec-2005
*/
public class Converter {
public Converter()
{

}

// Method to convert from degrees Celcius to degrees Fahrenheit
public static float celciusToFahrenheit(float degCelcius)
{
float degFahrenheit;
degFahrenheit = degCelcius * 9/5 + 32;
return degFahrenheit;
}

// Method to convert from degrees Fahrenheit to degrees Celcius
public static float fahrenheitToCelcius(float degFahrenheit)
{
float degCelcius;
degCelcius =  (degFahrenheit - 32) * 5/9;
return degCelcius;
}

// Method to convert from degrees Celcius to degrees Kelvin
public static float celciusToKelvin(float degCelcius)
{
float degKelvin;
degKelvin = degCelcius + 273.15f;
return degKelvin;
}

// Method to convert from degrees Kelvin to degrees Celcius
public static float kelvinToCelcius(float degKelvin)
{
float degCelcius;
degCelcius = degKelvin - 273.15f;
return degCelcius;
}

// Main method demonstrating usage of above methods
public static void main(String[] args)
{
System.out.print("100 degrees Celcius in Fahrenheit is: ");
System.out.println(celciusToFahrenheit(100));

System.out.print("-40 degrees Fahrenheit in Celcius is: ");
System.out.println(fahrenheitToCelcius(-40));

System.out.print("0 degrees Celcius in Kelvin is: ");
System.out.println(celciusToKelvin(0));

// to convert from Kelvin to Fahrenheit, combine two methods:
System.out.print("373.15 degrees Kelvin in Fahrenheit is: ");
System.out.println(celciusToFahrenheit(kelvinToCelcius(373.15f)));
}
}

Leave a Comment

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

How To Add A New Line in a C# or Visual Basic TextBox

If you're building a Windows Form Application, for example with C# or Visual Basic, then you may need to add a new line character, for example in a multiline TextBox. You do this using Enviroment.NewLine to insert the line break as follows:

textBox1.Text="First Line" + Environment.NewLine + "Second Line";

My preferred use is with the += operator in C#:

textBox1.Text = "Line One";
textBox1.Text += Environment.NewLine;
textBox1.Text += "Line Two";

This way, you can add extra lines of output to the TextBox as you require it.

References: Environment.NewLine on MSDN

Comments (10)

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Generating Random Numbers in PHP

To create random numbers in PHP, you can use the rand() function call, which takes either zero or, optionally, two parameters determining the minimum and maximum random numbers (inclusive) that you'd like. If you don't specify a maximum then it will default to a value, RAND_MAX, set in the PHP configuration. The function returns an integer, generated "randomly." (Computers can't generate real random numbers, but this tries and is suitable for most purposes.)

You can get the value of RAND_MAX with the function getrandmax(). Then if you desire a random number larger than that, you have to specify the min and max bounds as parameters, eg rand(0, 100000) will return a random number between 0 and 100000 inclusive - so both 0 and 100000 can be returned.

For example:

<!--?php // random number between 0 and RAND_MAX:
echo rand(); // output: 28688 (for example)
// random number between 37 and 50:
echo rand(37, 50); // output: 44
// find out the value of RAND_MAX:
echo getrandmax(); // output: 32767
// get a larger random number by specifying min and max:
echo rand(0, 1000000); // output: 351990
?>

The output shown in the comments above is as an example. Since it is generating random numbers, the numbers generated are very likely to be be different every time.

Comments (1)

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Search Google for Synonyms

It's possible to search Google for synonyms of a word, ie other words that Google thinks has the same meaning. To do this, simply precede the term by a tilde: ~, for example: ~blog will return results for terms such as weblog and blogger as well as those for blog. If you're only interested in the synonyms then you can exclude the original word in the usual way (ie by prefixing a minus sign: - to the term.

So ~blog -blog will return results for weblog, blogger and log but exclude those with the term blog.

This could be useful from a SEO perspective: if you are writing articles which target certain keywords, then it may be useful to target other words which Google associates with those keywords, too.

Comments (1)

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Hide a hard drive from My Computer

Here's a little privacy hack for users running Windows XP. If you have a hard drive with some kind of mildly sensitive information on it, then you can stop it appearing in My Computer but you'll still be able to access it by installing a utility that Microsoft created, Tweak UI. It allows you to tweak many smaller settings which don't feature in any of the standard Windows preference menus. One such option is to control which drives appear when you open up My Computer and which don't.

TweakUI

Once you've installed Tweak UI then you have to find and expand the My Computer node in the tree menu on the left, then select Drives. Once you've done that, then you'll be shown drives A to Z (even if they're not actually drives you have - if you later install a hard drive W: then the setting here will determine if it is shown then), each with a check box next to it. Uncheck the drives that you don't want to be shown. To access the drive once you've done that then you'll need to enter the drive letter, eg C: in the address bar of Windows Explorer.
I should point out that this isn't a secure solution - it doesn't do anyone to actually stop anyone accessing information on the drive, however it can be useful to prevent friends and coworkers stumbling upon information you'd rather they don't see.

Although I've mentioned that this is a privacy tip, that's not what I use it for. I have a dual boot set up with another version of Windows, though XP is my main one, and the drive from that other install shows up in My Computer. I use Tweak UI to get rid of the drive letter there, purely for tidiness. I know I could just remove the drive from that version of Windows through the Administrative Tools in the Control Panel, but it is sometimes convenient to have access to it so I use this method. Tweak UI is good at resolving small annoyances such as that.

Comments (1)

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Web Hosting Terminology

This article will define what various terms used by web hosting companies mean. It's not possible (and sometimes insulting to the reader) to cover everything so this will just cover the most commonly used and important terms.

Bandwidth (or Transfer or Data Transfer)
Bandwidth is the amount of traffic that is sent (and recieved) by your website. It is usually measured in gigabytes (GB), where 1 GB = 1024 megabytes (MB), and it is usually allocated on a monthly basis, for example you could have 5GB/month of bandwidth, which means that there is a limit of 5GB of files that can be downloaded from your site within a month. Bandwidth is only a concern if you host a lot of files, for example images or movies, as webpages are usually small file sizes.

Colocation Server (or Colocation Hosting)
This is similar to Dedicated Hosting (see below), except the server is owned by a customer and they pay the colocation hosting company to host the server for them - that involves paying a charge for bandwitch which the hosting company provide, the physical space which the server takes up and the power that the server uses. With this set up the customer is responsible for their hardware.

Control Panel
This is a web page with a usable interface which the customer can connect to when they want to tweak their hosting settings, view the transfer statistics, or utilise other features provided with their hosting.

Dedicated Server (or Dedicated Hosting)
This is a server which is owned by the hosting company which they will rent out to you for a monthly fee along with an allotment of bandwidth which you can use with it. This means that you get a whole server to yourself to use for what you desire, this is needed for scripts which use a lot of server resources (for example large message boards, for one of which I pay for a dedicated server). As a customer, you would have full control over the server and could change the configuration as required, a facility not available when you have to share a server with others. With a dedicated server, the web hosting company is responsible for the hardware as they own it, not the customer.

Disk Space
This is the amount of space on the server that you have to upload your files on, it's usually measured in MB or GB.

MySQL
This is a type of database (as is PostgreSQL) which allows the customer to store and retrieve information on the server. A lot of scripts require that a database is present to function.

Reseller Hosting
This is similar to Virtual Hosting (see below), but the customer is given the ability to create small hosting accounts of their own, which they can then give or sell to people if they desire. It usually allows the customer more features, bandwidth and diskspace than with normal virtual hosting.

Scripts
(eg PHP, Perl, ASP)
These are server side scripting languages which allow the customer to use already written scripts, or write their own, which provide additional features for the website and increased interactivity (for example message boards and content management systems).

Virtual Hosting (or Shared Hosting)
This is where a webhosting company puts many customers' webpages on the same server - so the server resources are shared amongst all the customers using it. This means that applications which cause a high server load are not suitable, and if one customer uses one then other customers will suffer.

Leave a Comment

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Dedicated Servers: A Summary

What is dedicated hosting?
This is a server which is owned by the hosting company which they will rent out to you for a monthly fee along with an allotment of bandwidth which you can use with it. This means that you get a whole server to yourself to use for what you desire, this is needed for scripts which use a lot of server resources (for example large message boards, for one of which I pay for a dedicated server). As a customer, you would have full control over the server and could change the configuration as required, a facility not available when you have to share a server with others. With a dedicated server, the web hosting company is responsible for the hardware as they own it, not the customer.

What are the advantages of it?
You can do anything you like on it that you like (although most hosts have acceptable use policies which state that illegal content is not allowed, and in some cases they prohibit connecting to IRC). This means that you can login to the server, set up programs as you desire, then run them without having to worry about the effects on server resources for other people. You can run games servers (for games such as Half-Life, etc), IRC servers or bots, or just run a web server which uses a lot of resources (and with a special setup which isn't found in shared hosting).

What are the disadvantages?
The main disadvantage is the cost, which will put many people off getting one. The cheapest seem to come at about $50/month, and the most expensive cost thousands of dollars each month. The price can be reflective of the quality (such as the cost of running - or your host renting a space in - the datacentre, the hardware in the server, the technical support provided by your host and so on), alternatively it can just be an over inflated price for what is a poor quality product. It's important to compare the prices and packages offered from several companies before coming to a decision, also you should ask your friends if they have any opinions on the matter, and research information about the hosting companies and datacentres - are the constantly offline due to DDOS attacks? do they have a poor record of customer service?
Another disadvantage is the added responsibility involved in keeping the software on your server secure, for example you may have to update builds of apache, control panel software, etc as security holes are found.

Is it for me?
That depends, if you want to a game server then you'll need one - but it may be easier for you to buy one from a dedicated games server comapny, they know their stuff and would probably be able to install and configure the servers for you. However they would place restrictions on what you could do with the server and maybe the setup.
If you want to run a small IRC server, bot or bouncer then you would probably be easiest paying a smaller fee to hire one of those from an IRC or shell provider company, also you don't have to worry about restrictive datacentre AUPs which prohibit IRC use.
If you are starting a small web forum or site then you probably won't need one initially, wait until it grows and you can determine if you need the server to ensure further growth is ok.
Otherwise you could possibly look into getting one, you could even email hosting providers asking what package they think your site would require (although take their replies with a pinch of salt, they could try to sell you stuff you don't actually need).

Leave a Comment

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Excel Tip: Evaluate part of a formula directly in the formula bar

Quite an unassuming tip today, but potentially useful (and not immediately obvious).

In Excel, if you have a complex formula within a cell, you can immediately evaulate part of it by selecting the part (dragging across it with the mouse or by using arrow keys), then press F9.
For example, if you have:
=cos(3+2/5)
then you select the 3+2/5 part:
=cos(3+2/5)
and press F9:
=cos(3.4)
This is useful for simplifying formulae, and also diagnosis when something isn't working as expected.  Note that if you press enter (or click on another cell), it will save the formula with the evaluated number in the cell, rather than the original one.  To avoid saving it, press Escape when you're done editing.
So for the above example, enter saves the formula =cos(3.4) in the cell, while pressing escape returns it to =cos(3+2/5).

Leave a Comment

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

How To: Read a file's contents with PHP

PHP provides three built-in functions which allow you to easily read the contents of a file on your webserver. This is useful when, for example, another program may write information to the file and you could access that information through your script.

In this example, we're going to use the following example 5 line file, saved as file.txt:

line 1
line 2
line 3
line 4
line 5

In the same directory as file.txt, we're going to work with the PHP file test.php. We'll outline the file reading functions below.

readfile()

readfile() is the least useful of the file functions. As a parameter, it takes a string which specifies the location of the file to read - this can be relative or absolute; since our file is the same directory as the script, we just need to pass the string 'file.txt' as this paramter. We call the function using the code readfile('file.txt') and it prints out the contents of the file straight to the browser:

// readfile() writes the contents of the file straight to the browser
echo 'Contents of file.txt using readfile():<br>';
readfile('file.txt');

The disadvantage of readfile() is that it doesn't allow you to manipulate the file contents before displaying it - the next two functions we're going to look at will allow you to do that.

file()

The code file('file.txt') will read the contents of file.txt into an array, which you can then manipulate in your scripts and display yourself. Each line of the file is stored in a seperate element of the array, this means that we can access and manipulate each line of the file seperately using normal array notation.

// file() loads the contents of file.txt into an array, $lines
// each line in the file becomes a seperate element of the array.
$lines = file('file.txt');

// now loop through the array to print the contents of the file
echo 'Contents of file.txt using file():<br>';
foreach ($lines as $line)
{
echo htmlspecialchars($line) . '<br>';
}

// we can also access each line of the file seperately
echo '3rd line of the file: "' . htmlspecialchars($lines[2]) . '"<br>';

In this example, we've loaded the contents of file.txt into an array called $lines then used foreach to loop through the array and display each line on the user's browser. We then use $lines[2] to access the third line of the file (the element at array index 2, since line 1 is $lines[0]).

You may notice that we've used a function called htmlspecialchars() when displaying the file's contents - this enables that special characters used in HTML, such as > and " are displayed correctly. This illustrates the power of file() over readfile() since readfile() was unable to perform this kind of processing.

get_file_contents()

The last method we will look at is get_file_contents('file.txt') which is similar to file() however rather than returning an array, it returns a string with all the lines of the file. We can manipulate and display this in a similar way as with file():

// file_get_contents() reads the file and places the contents in a string
$fileString = file_get_contents('file.txt');
echo 'Contents of file.txt using file_get_contents():<br>';
echo nl2br( htmlspecialchars($fileString) );

Since we have no way of seperating the lines with this method, we've used the nl2br() function which converts line breaks in the string (represented by the special character n) into the HTML line break <br /> so that the file will display correctly on the visitor's browser.

The full contents of test.php is below:

<!--?php<br /--> // file() loads the contents of file.txt into an array, $lines
// each line in the file becomes a seperate element of the array.
$lines = file('file.txt');

// now loop through the array to print the contents of the file
echo 'Contents of file.txt using file():
';
foreach ($lines as $line)
{
echo htmlspecialchars($line) . '&lt;br&gt;';
}

// we can also access each line of the file seperately
echo '3rd line of the file: "' . htmlspecialchars($lines[2]) . '"&lt;br&gt;';
echo '&lt;br&gt;';

// file_get_contents() reads the file and places the contents in a string
$fileString = file_get_contents('file.txt');
echo 'Contents of file.txt using file_get_contents():
';
echo nl2br( htmlspecialchars($fileString) );
echo '&lt;br&gt;';

// readfile() writes the contents of the file straight to the browser
echo 'Contents of file.txt using readfile():&lt;br&gt;';
readfile('file.txt');
?&gt;

The output from test.php is:

Contents of file.txt using file():
line 1
line 2
line 3
line 4
line 5
3rd line of the file: "line 3 "

Contents of file.txt using file_get_contents():
line 1
line 2
line 3
line 4
line 5

Contents of file.txt using readfile():
line 1 line 2 line 3 line 4 line 5

Leave a Comment

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
Loading ... Loading ...

How To: Find Items in PHP Arrays

The problem: we have an array of items in PHP and we want to find out if a specific item is in the array. In code we can define the array as:

IE we have an array called $fruitBasket which contains 5 strings, each of which is the name of a fruit. We want to find out if there is an Apple in the $fruitBasket - is there an element "Apple" in the array?

We do this with the following code:

<?
// create an array of strings called $fruitBasket:
$fruitBasket = array( "Apple", "Orange", "Mango", "Lemon", "Pear" );
// use the in_array() function to check if "Apple" is in the array:
if( in_array("Apple", $fruitBasket) )
{
  echo "Apple is in the array";
}
else
{
  echo "Apple is not in the array";
}
?>

This code uses the in_array() method to check if the element "Apple" exists in the array $fruitBasket:
in_array("Apple", $fruitBasket)

in_array() takes two parameters here: firstly the object we're looking for, in this case "Apple", and secondly the array which we're looking in, $fruitBasket. It then returns a boolean value: true if "Apple" is in the array, or false if it isn't.

References: php.net manual: in_array() function

Leave a Comment

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...