an unusual java class

May 13, 2011 Leave a comment

I was reading a forum thread in www.daniweb.com where someone was asking if there is any way to pick what method to run on run time rather than specifying what method to run during development. I have thought of the same problem before, and I decided to see if this is possible by trying to actually build a class that does this. Here is the code:


import javax.swing.*;

public class MixTapes{

public static void main(String[] args){
int s = 0;
while(s != -1){
s = new Integer(JOptionPane.showInputDialog("pick method:\n1 fo method1\n2 for method2\n25 for method25\n-1 for quiting")).intValue();
pickWhichMethodToRun(s);
}

}

public static void method1(){
System.out.println("First method has been chosen");
}

public static void method2(){
System.out.println("Second method has been chosen");
}

public static void method25(){
System.out.println("Twenty-fifth method has been chosen");
}
public static void pickWhichMethodToRun(int s){

switch(s){
case 1:
method1();
break;

case 2:
method2();
break;

case 25:
method25();
break;

default:
System.out.println("haven't picked an existing method");
}

}

}

Categories: Java

Anther php script

May 12, 2011 Leave a comment

I have a site at www.gagi.zxq.net where I post my Java applets. Zymic.com offers free hosting, and so I set up a small site there for showing the applets I build. Since there is no database support for free hosting, I have not built any script for running my site, but I only transfer files via ftp.

However, since the number of my applets will inevitably increase, I figured I need to either acquire database support, or find a way around. I decided that I need to find a way around to build a mechanism using PHP, without any database.

My simple plan is as follows:

I need a directory that will hold the html files that reference java classes.

I need another directory that holds my java classes.

With a PHP script, I access the folder where my html files reside, and for each file in the folder, I generate a link on my index page to the html file.

However, a problem appeared along the way; the script would generate two links too many. The first link leading to the folder where the PHP file resides, and the second link leading to the folder that contains the folder where the PHP file resides. For the script and the modification, read the code in a jpeg file because wordpress won’t let me post php code:
null

Categories: PHP

proving that square root of 2 is irrational

March 26, 2011 Leave a comment

Alright, I think I have a good way to prove that a natural number’s square root is either natural or irrational. Take for example 1, 4, or 9. Each of these number’s square root is a natural number (namely, 1, 2, 3). However, the square root of numbers such as 3, 5, 6, 7 and so on is not natural. We know that the square roots of those numbers are irrational, but just in case someone hopes that there is a natural number whose square root is a rational number, we will crush their hope proving that there is no such natural number.

As we often do with proofs, we assume the contrary to what we want to prove. We want to prove that there is no such natural number whose square root is rational. But let us assume that there is and let us name it M. So what then?! 😛

Not much, just that we assumed that there is a non-natural, rational number whose square equals M. Let us name this number Q.

Since Q is a rational number, we know that the number of digits after the decimal point is finite and that its last digit is not zero (any trailing zero after the last non zero digit can be ignored). As a result, we can safely assume that there is a multi digit natural number Nk and another natural number Na such that:

Q = Nk / (10^Na).

Also, we have assumed that Q^2 = M, which yields that Q^2 = Nk^2 / ((10^Na)^2) = M. This leads to:

Nk^2 = 10^(2Na)*M.

Since Nk’s last digit is non zero, Nk^2 ends with a non zero digit, and therefore is not divisible by 10. However, 10^(2Na)*M is divisible by 10 and equals Nk^2 (non divisible by 10), which is a contradiction. Therefore, we know that Q is neither natural nor rational, which means it must be irrational. 🙂

Since 2 is a natural number, and its square root obviously is a non natural number, then it is irrational based on the above.

Hope anyone finds this of any help.

P.S. I had this in drafts since I was a student, before I started working, and I do not know why I did not publish it. I am publishing it now.

Categories: Math

od command in Linux and a class in Java

February 18, 2011 Leave a comment

Because of my strong desire to earn LPI certification (Linux Professional Institute Certification), in between my studies, I also read the LPIC-1 Linux Guide. Today I was working on the od command in Linux. It is not hard to type od filename and see what it returns, but I was having a hard time to understand what exactly was going on so I started to convert numbers from base eight to ten and then to base two (I know… I could just convert them to base two directly :P) to see what was going on, but I was making calculation mistakes, and since I also like coding, I thought I’d build a Java class that does conversions for me and make my studying od easier. Read on for what od actually does, and for the code in Java.

od stands for octal dump. If one just types in a terminal od filename, the output is a nine column retsult of numbers in base eight. The first column indicates the bytes represented in that row. for example, column one in row 1 will output 0000000 and row two, column one will output 0000020. To understand what this means, lets first convert 0000020 to decimal, getting 16. Now, the 0000000 and 0000020 indicate that columns two to nine in row one contain bytes one through 16. Now one might ask: if there are 16 bytes represented, why only eight columns (excluding the first)? Well, every two bytes are combined to give one octal number. Say for example you have bytes AB, and you pass this content to od command. the output will be :
0000000 041101 000012
0000003
If one converts 041101 into binary, the result is:
0 100 001 001 000 001, which equals the following two bytes:
01000010 01000001, which then equal the ACII, binary, values of characters A and B.
The 000012, will be output even if od is passed no text at all. I looked up the ASCII table, and found out that octal 000012 (decimal ten) is the value of new line character. So, seems like od assumes that every file, even an empty one ends with a new line character, so it outputs either 000012 or what it gets after combining this byte with another (in case the number of bytes in a file is odd, else, 000012 is not combined with any other byte and is output as is).

UPDATE: The 0000012 ASCII values is actually the form feed character. More on that in the following Wikipedia link.

The number 0000003 in column one, row two, in this case, indicates that there are three bytes shown in the previous row. Note that each row has at most nine columns. If there are no bytes to fill all nine columns of a row, the row will have less columns.

Now, the od command may be passed parameters to output decimal ASCII values of each byte separately (od -t dC filename), or hexadecimal values obtained by combining two consecutive bytes (od -x filename). There are other options that may be passed to od command, but the logic behind it is explained here, I believe.

As far as the code in Java goes, I will only copy paste the code. The comments in it are sufficiently explanatory:


import java.util.*;
import javax.swing.*;

public class ConvertNumber{

public static void main(String[] args){
//this line of code prompts a user to specify the method they want to use
String met = JOptionPane.showInputDialog("which method do you need:\ndecToAny(type d), or the method\nto convert a number with a lower base to decimal(type td)").toUpperCase();

//this block of code is executed if the user wants to run the method that generates a decmial numbers representation
//in a base lower than ten
if(met.equals("D")){
String s = JOptionPane.showInputDialog("type in the decimal number and the base you want it converted to with a comma in between (type dec-num, new-base (new base should be lower than ten greater than 1))");
StringTokenizer st = new StringTokenizer(s, ",");
int x = new Integer(st.nextToken().trim()).intValue();
int y = new Integer(st.nextToken().trim()).intValue();

System.out.println(decToAny(x, y));
}

//this block of code is executed if the user wants a number converted to base ten
if(met.equals("TD")){
String s = JOptionPane.showInputDialog("give the number and the base it is in with a comma in between (num,base)");
StringTokenizer str = new StringTokenizer(s, ",");
int num = new Integer(str.nextToken().trim()).intValue();
int b = new Integer(str.nextToken().trim()).intValue();
System.out.println(toDec(num, b));
}
}

public static String decToAny(int x, int baza){
String rez = "";
//this method converts a number that's in base ten to a base lower than ten and greater than two
//the number to convert is x and the base to which to convert is baza
//the result is a string. all one needs to do to convert it to int is the "new Integer(rez).intValue()" line
while(x > 0){
rez = x%baza + rez;
x = x/baza;
}

return rez;
}

//this method converts a number x in base baza into a decimal number
public static int toDec(int x, int baza){
int rez = 0;
int lim = ("" + x).length();
for(int i = 0; i < lim; i++){
//this line converts number x to string. it then iterates through that string from character
//at position 0 to the last and converts the character back to int and multiplies it with
//base raised to the power indicated by lim - i - 1 (lim is the number of digits in the number)
//since character at position zero is raised to a power that is less than the number od digits by one
//you get that from the following lim - i - 1.
rez = new Integer("" + ("" + x).charAt(i)).intValue() * (int)Math.pow(baza, lim - i - 1) + rez;
}
return rez;

}

}

If there is anything you would want me to add or clarify further as far as anything related here goes, please comment with the question you would want me to tackle. I will study it, and will make a post for it.

Categories: Java, Linux

easyLife makes life easier

January 14, 2011 Leave a comment

Hey ther. This time I am going to share with you a nice tool I found that helps transition to Linux’s distro Fedora 14 easier.

Just recently, I somehow damaged my Fedora files on my dual boot system (the other OS is Windows XP). Then, I somehow damaged my internet connection in xp, and so I thought I boot my Fedora 14. I put in the cd, reinstalled Fedora and wanted to eat something. I also wanted to listen to some music, but it would take a little bit of searching around for me, so I gave up. After I ate, I started searching, and what I found is a pretty cool app called easyLife.

easyLife is a simple, and light on memory, app that gives you a list of applications to chose from, and can install them for you on your system. For someone that just wanted to start using Linux and worries whether that is too much of a challenge, then easyLife makes life easier. It is not like life without easyLife is much harder for newbies in Linux world, but easyLife makes things quicker.

Amongst the applications it offers are Adobe Flash and many media players, which are the first things a regular Desktop user wants on their system. In fact, there are other appas like Utils, which include PDF Virtual Printer, 7zip, unrar, isomaster and other tools, that easyLife can install for you. easyLife can also install Java for you 🙂 If you are curious and up for something new go check it. It’s easy to use.

Once you donwload it from this link, you go to Places, then Downloads on your system. After that, you find the file easylife-2.3-3.noarch.rpm. You double click on this file. You are promted to install or cancel, and you of course chose install. After easyLife is installed, you will have an icon on your desktop that corresponds to easyLife. Double click on that, and you will be shown after a few seconds the list of apps you can install through easyLife. Chose anything you’d like. Other than codecs and flash player, I also chose themes 🙂

Enjoy!

Categories: Linux

Ray Kurzweil’s life must be painful

January 11, 2011 Leave a comment

A couple of days ago a facebook friend posted something about Ray Kurzweil. Out of curiosity, I googled this guy’s name to see who he was. As you will too, I clicked on wikipedia’s page on Ray and read about his life until now. He seems to be known as anything a regular individual is not.

What?

Yes. This guy seems to have made impossible come to life. Based on wikipedia, this guy made a computer program that composed musical pieces based on the pieces of human composers. He even built a program that helps a machine read and ‘understand’ what it reads. No, he obviously does not believe in impossible. To prove this, he went as far as to predict that technology will develop so much by 2050, that microscopic machines will be able to enter and travel the human body and repair damages, thus making human lifespan longer. Add to that the fact that technology develops at an exponential rate, and that just means that, if he is correct, and if we live long enough to see that technology come to life, we will live around… well, ask Ray about that 😛

Anyways, I told another friend about this guy and asked him to read about his predictions. Friend’s response was: I doubt that last one (referring to the microscopic machines traveling human bodies).

After I went to sleep, I grew conscious of some discomfort inside me that persisted since I read Ray’s bio in wikipedia. After some thinking, I realized that my discomfort came from my belief that Ray might be right, and then I realized how lucky my friend that did not believe Ray’s prediction really was. I mean, I want to live long enough to see technology develop so much that it can make my life even longer 🙂 And then after then I thought: well, Ray must believe his idea strongly. And since the discomfort I felt is tied to how much you believe the possibility of this prediction, I decided Ray must be on a rush… a painful one. He most likely loves life and would like to see such technology come to life. And since he is amongst those that are closest to being able to contribute to the creation of such technology, the pressure is higher on him.

However, I am sure his philosophical take on life and death is an intelligent one, making that pressure easier on him, if he actually feels it. We all know an end is inevitable, no matter how long our lifespan is, and that just makes it easier for people to come to terms with it. Plus, although we are conditioned to view death as the worst that can happen, we do not have any evidence to believe that. Death definitely is a change, but not necessarily an end to existence. I think the idea of nonexistence is what scares people… but even if we cease existing after death, what’s there to be feared? Pain? There is no pain in nonexistence 🙂 If we fear lack of joy (since there is no joy in nonexistence either), then let us enjoy life, and hope we see the technology come.

Categories: Uncategorized

elementary transformations on a matrix

December 17, 2010 Leave a comment

The following is a complete class with a method for performing elementary transformations on any matrix, regardless of the number of rows and columns. It is written in Java. All explanations are given preceding each method.

I copied and tried the code from my blog, and realized that:

WordPress will change quotes into some other character, so if you copy the code, you will have to replace what you get for quotes with actual quotes.

Same for minus (-) sign. Just replace what you get with the actual – character.

Somewhere in the line 93 or so, you should have k–, which WordPress turns into k-. Change that too, and this class should work fine.

I also left a main method inside. I used that to test my code while I wrote it. Hope this is of any help to you.

CODE STARTS WITH THE NEXT LINE

import javax.swing.*;

public class ElementaryTransformations{

public static void main(String[] args){
int x = new Integer(JOptionPane.showInputDialog(“Rendi i matrices?”)).intValue();
int[][] y = GetMatrix(x, x + 1);
y = SwitchRows(y, x, 2);
ShowMatrix(y);
System.out.println();
ShowMatrix(Transform(y));
}
//this method gets the matrix from a user. the matrix will have x rows, and y cols
public static int[][] GetMatrix(int x, int y){
int[][] rez = new int[x][y];
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
rez[i][j] = new Integer(JOptionPane.showInputDialog(“Ama vleren e elementit ” + i + “,” + j)).intValue();
}
}
return rez;
}
//this method makes a matrix appear in a console
public static void ShowMatrix(int[][] x){
for(int i = 0; i < x.length; i++){
for(int j = 0; j < x[0].length; j++){
System.out.print(x[i][j] + ” “);
}
System.out.println();
}
}

//this method switches positions of two rows. it puts a row ‘a’ in position ‘b’
//and row ‘b’ in position ‘a’
//when using this method, the first row is row 1, not 0, and the last row is
//x.length instead of x.length – 1.
public static int[][] SwitchRows(int[][] x, int a, int b){
int[][] rez = x;
//this if statment orders variables a and b by magnitude
//because of the way the method is written, if a is greater than b
//the method won’t work.
if(a > b){
int temp = a;
a = b;
b = temp;
}
int[][] temp = new int[x.length + 1][x[0].length];
//these two for statments build a temporary double array, out of the
//array given to the method. the row a is copied in position a and a+1
//thus, temp array has one row more than the original array (matrix)
for(int i = 0; i < x.length + 1; i++){
for(int j = 0; j < x[0].length; j++){
if(i < a){
temp[i][j] = rez[i][j];
}
if(i >= a){
temp[i][j] = rez[i – 1][j];
}
}
}
//these two for loops copy the temp matrix into rez matrix, putting row ‘b’
//in position ‘a’ and putting row ‘a’ in position ‘b’
for(int i = 0; i < x.length; i++){
for(int j = 0; j < x[0].length; j++){
if(i == a – 1){
rez[i][j] = temp[b][j];
}
if(i == b – 1){
rez[i][j] = temp[a][j];
}
}
}
return rez;
}
//this is the method that performs elementary transformations on the given matrix x
//elementary transformations continue until all matrix elements below the main diagonal
//equal zero
//(main diagonal is the one from upper left to lower right corner)
public static int[][] Transform(int[][] x){
int[][] rez = x;
for(int i = 0; i < rez.length; i++){
if(rez[i][i] == 0){
if(NonZeroRowthisCol(rez, i, i) != -1){
SwitchRows(rez, i, NonZeroRowthisCol(rez, i, i));
}
}
if(rez[i][i] != 0){
for(int j = i + 1; j < rez.length; j++){
//this part is tricky; if we start with k = i, and proceed all the way
//up to when k = rez[0].length… rez[j][k] will first turn into zero
//(that is our purpose) and as a result, all elements in row j, except the first, will be
//multiplied with rez[i][i] and reduced by zero
//(rez[j][i] = 0 => rez[j][i]*rez[i][k] = 0 => rez[j][k]*rez[i][i] – rez[j][i]*rez[i][k] = rez[j][k]*rez[i][i] – 0)
//yielding an inacurate result. so, what we need to do is to turn rez[j][i] into zero, after we
//update elements in other columns along j row
for(int k = rez[0].length – 1; k >= i; k–){
rez[j][k] = rez[j][k]*rez[i][i] – rez[j][i]*rez[i][k];
}
}
}
}
return rez;
}//this method finds a row ‘i’ that is lower than row ‘m’ and has a nonzero
//element in column ‘n’
//as a result, this method returns ‘i’, which reprezents the position of such a row
public static int NonZeroRowthisCol(int[][] x, int m, int n){
int rez = -1;
for(int i = m + 1; i < x.length; i++){
if(x[i][n] != 0){
rez = i;
}
}
return rez;
}

CODE ENDS WITH THE LAST CURLY BRACKET

Categories: Java