Tuesday, August 14, 2007

Can I Count The Ways? Let me...

I lost my lunch writing this, so in recompense, I decided to play some games online. There were so many options. “Like the Mahjong game?” Smiled. “I speak of it all the time, right?” “Okay which now?” “Won't tell. Since you can be Harry Porter, how soon will Oz be at your doorstep?” “What's the game, Cyp?”
But before you play, the code is long, your fingers have need some getting used to the typing.Nice day.



/*
*I JUST TRIED DOING A LITTLE BINARY OPERATORS
*HERE AFTER HAVING A LOOK AT SOME OLD CALC
*MACHINE. HOPY YOU LIKE IT.
*/

package overall;

import java.io.*;
import java.util.*;
import java.lang.*;

public class Postfix {
static List stackList = new ArrayList();
static List operatorList = new ArrayList(); //ignore generics.
//the index counts to tail lof list
static int listCounter = -1;

protected static void checkInput(String keyedIn){
//check for exit character once and for all
if (keyedIn.equals("@")){
System.exit(0);
}
//not ready to exit?
//make sure first number is a number
if (listCounter == -1){ //nothing in list at all
try{
//see if we can parse the keyed in figure to int
int received = Integer.parseInt(keyedIn);
//insert first number into the list
stackList.add(received);
//increase the counter
++listCounter;
System.out.println("Key in an operator, please.");
}
catch(NumberFormatException e){
System.err.println("First number must be a number. Thanks.");
}
}
//where the keyedin number is not the first number i.e something in stack
//we expect an operator for next number
//check if an operator is already waiting for an operand
int opCheck = operatorList.size();
if (opCheck == 0) {
//no operator waiting! if operand replace it with previous.
try{
int checking = Integer.parseInt(keyedIn);
//replace first number and count is still 0
stackList.remove(0);
stackList.add(checking);
}
//if operator, what was expected
catch(NumberFormatException e){
//send to operator then
doOperation(keyedIn);
}
}
//operator waiting, waiting for a number.
//number expected
if(opCheck > 0){
try{
int checked = Integer.parseInt(keyedIn);
//if number add to list and increase count
stackList.add(checked);
++listCounter;
compResult();
}
//if not number ask for number
catch(NumberFormatException e){
System.out.println("Input a valid number please.");
}
}
}

protected static void doOperation(String ourOperator){
//whenever any character is assumed operator, check for
//validity of operator
if (ourOperator.equals("/") || ourOperator.equals("*") || ourOperator.equals("%")
|| ourOperator.equals("-") || ourOperator.equals("+")){
//perform required operation. operator valid
char calc = ourOperator.charAt(0);
//do calculation now
doCalculate(calc);
}
else {
//ask for valid operator
System.out.println("Key in a valid operator please.");
}
}

protected static void doCalculate(final char theOperator){
//store in operator list
operatorList.add(theOperator);
//if only one operand in list, wait for next input
//lock on stack size
int contains = stackList.size();
if (contains == 1){
//lock on the size
System.out.println("Insert next number for operation.");
}
else if(contains == 2){
compResult();
}
}

public static void compResult(){
//get the last two elements in the stack list
//get the head of the operator list
//do the binary operation
//output the result
//get stack list tail
int stackTail = stackList.remove(listCounter);
--listCounter;
int tailAfterLast = stackList.remove(listCounter);
--listCounter;
//get operator list head
Object opHead = operatorList.remove(0);
System.out.print(tailAfterLast + " " + opHead + " "+stackTail + " is = ");
//do the binary operation
//check what operator we're using first though
if (opHead.equals('+')){
tailAfterLast += stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('-')){
tailAfterLast -= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('%')){
tailAfterLast %= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('*')){
tailAfterLast *= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else if (opHead.equals('/')){
tailAfterLast /= stackTail;
System.out.println(tailAfterLast);
//keep the result in head of operand list
stackList.add(tailAfterLast);
++listCounter;
}
else {
System.out.println("We're going nowhere.");
}
}

public static void main(String[] args) throws IOException{
BufferedReader bufR = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any character. First character must be a number");
System.out.println("Enter @ to end the program.");
boolean keepOn = true;
//keep getting input until single @ entered or end of operation
while (keepOn){
//get the input from keyboard
String dInserted = bufR.readLine();
//check whether it is an operator, operand or @ sign
checkInput(dInserted);
}
}
}

What Angle Does Three Make?

Am sure you guessed the answer, a triangle. Take three edges and join them by their edges, what you get is a triangle. Measure the angle if you can. We're following up on our staircase faces to build a triangle. You just have to think in alternation. How? Twist the stairs 180 degrees to the left, leave the first one remaining and voila!, you have a triangle. That's what three makes. Now four? A Mahjong game; read my lips, there is even a Mahjong messenger online. Officially. But if you go for poker...



/*
*TRY DOING A DIAMOND AFTER THIS CODE
* :-)
*/
package overall;

public class AtTriangle {

public static void main(String[] args){
//@ character unicode representation as char
char c = '\u0040';
//declaring our array
char[][] atTriangleArray;
//we now allocate only the rows
atTriangleArray = new char[6][];
//next the columns are allocated
for (int i=0; i<atTriangleArray.length; i++){
//for the six rows there are eleven columns
atTriangleArray[i] = new char[11];
//if this is the first column assign at to the
//6th column
switch(i){
case 0: atTriangleArray[i][5] =c; break;
case 1: atTriangleArray[i][4] =c;
atTriangleArray[i][5] =c;
atTriangleArray[i][6] =c;
break;
case 2: atTriangleArray[i][3] =c;
atTriangleArray[i][4] =c;
atTriangleArray[i][5] =c;
atTriangleArray[i][6] =c;
atTriangleArray[i][7] =c;
break;
case 3: atTriangleArray[i][2] =c;
atTriangleArray[i][3] =c;
atTriangleArray[i][4] =c;
atTriangleArray[i][5] =c;
atTriangleArray[i][6] =c;
atTriangleArray[i][7] =c;
atTriangleArray[i][8] =c;
break;
case 4: atTriangleArray[i][1] =c;
atTriangleArray[i][2] =c;
atTriangleArray[i][3] =c;
atTriangleArray[i][4] =c;
atTriangleArray[i][5] =c;
atTriangleArray[i][6] =c;
atTriangleArray[i][7] =c;
atTriangleArray[i][8] =c;
atTriangleArray[i][9] =c;
break;
case 5: atTriangleArray[i][0] =c;
atTriangleArray[i][1] =c;
atTriangleArray[i][2] =c;
atTriangleArray[i][3] =c;
atTriangleArray[i][4] =c;
atTriangleArray[i][5] =c;
atTriangleArray[i][6] =c;
atTriangleArray[i][7] =c;
atTriangleArray[i][8] =c;
atTriangleArray[i][9] =c;
atTriangleArray[i][10] = c;
break;
default : break;
}
}
for(int i=0; i<atTriangleArray.length;i++){
for(int j=0; j<atTriangleArray[i].length; j++){
System.out.print(atTriangleArray[i][j]);
}
System.out.println();
}
}
}

Life is easy when you're on rails, especially when you are three: you, your girl beside you and your dog. I'd take a vacation to anywhere i'd call home. Now if you mind my talking about a home, I've been watching the prices of some real estate here. I can tell you without blinking that they are SPECIAL OFFERS and for you.

AM FEELILNG LIKE WOW!

Just finished watching the TV and a commercial was ringing in my head: Move On Up! I really didn't understand if they were to move me up or if my savings account was to move them up, so I wrote a code for steps and ladders which if you take the initiative, you're surely going on up.



/*
*we're building a staircase based on java's native handling
*of multidimensional arrays, as arrays of arrays
*
*/
package overall;

public class TFaces {

public static void main(String[] args){
char c = '\u0040';
char[][] atArray;
//declare the 2-dim array first with only the rows
//columns for each row are unallocated
atArray = new char[5][];

//now allocate columns for rows
for(int i=0; i<atArray.length; i++){
//each row has one step greater than
//its preceding one
atArray[i]= new char[i+2];
}
//initialise and print out the array
for(int i=0; i<atArray.length; i++){
for(int j=0; j<atArray[i].length; j++){
atArray[i][j] = c;
System.out.print(atArray[i][j]);
}
System.out.println();
}
}
}

If you can, pick as many images as you can find and put them faces on the staircases. You'll love the wow! effect. Picasa just popped out on my desktop. With picasa, you can make your computer images all searchable. Searchable. You don't need to think: where was that picture of I and Micheal taken in 1996 at Mimmy's matriculation? Nope. Picasa has it; straight in ya face!

Never Multi-thread Your System Clock 4

MAYBE THE FINAL SOLUTION

Threads. Threads. Threads. Do you need a real nice and cool loan for a house from your best bank? Have to check your credit score first, right? Well, for this you just have to do no checking. A trip to Hawaii or to New Zealand or to Malaysia or anywhere in the world. Anywhere in the world. Is it for free? Yes, but for your time. How? Right from the labs of google comes, Google Maps. It's just absolutely wonderful. I fell in love with google maps from the word go. Go anywhere. Let's see. Why not try the city of London?
Yeah, the final solution. Back from that travel, you can use just one thread and get as much time in any time zone that you so desire. The code below diverts off the piggy-backing threads class on these aspects: I removed the constructors for time zone threads and used a single thread in the run method. Then formatted the time instance in a single method, zoned(). Therefore, within this method, I can and you can create as many time zone instances as you like.



package overall;

import java.text.DateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;

public class SingleThread implements Runnable {

public void run() {
//the limit on time
final long LIMIT = 1000*60*5;
//guard on time spent doing this shit
long timeSpent = 0;
//present time guard
Date dater = new Date();
//we'll catch an exception on this computation
try{
while(timeSpent < LIMIT){
//call the time formatter
zoned();
//sleep three second
Thread.sleep(1000*3);
//update the system time
Date now = new Date();
//compute time difference between start and now
timeSpent = now.getTime() - dater.getTime();
}
}
//catch any exception please
catch(Exception e){
e.printStackTrace();
}
}

protected void zoned(){
//an hour of time
long oneHour = 1000*60*60;
//get the date formatter
DateFormat formatter = DateFormat.getTimeInstance();
//declare the time zone. set initially to utc
SimpleTimeZone simp = new SimpleTimeZone((int)oneHour*0, "UTC");
//set the timezone of our dates to utc
formatter.setTimeZone(simp);
//print out the utc time at this moment in time
System.out.println("Time in UTC (London) is: "+ formatter.format(new Date()));
//set the time zone to hawaii time
simp.setID("HST");
simp.setRawOffset((int)oneHour*10);
formatter.setTimeZone(simp);
System.out.println("Time in HST (Hawaii) is: "+formatter.format(new Date()));
//set time zone new zealand time
simp.setID("NST"); //new zealand standard time
simp.setRawOffset(-(int)oneHour*12);
formatter.setTimeZone(simp);
System.out.println("Time in NST (Fiji) is: "+formatter.format(new Date()));
}

public static void main(String[] args){
//declare an instance of our time
SingleThread instar = new SingleThread();
//call its thread
Thread t = new Thread(instar);
//start the thread
t.start();
}
}

When I was young, we piggy-backed to get into the movie theater. This is how: A buys the ticket for the film, enters theater, the innocent one, wraps ticket on his pen, runs to the first floor of the building and throws it to B. Yeah, the ticket's already marked but the film house has a toilet that is at the outside. If B has a good tongue and can give and take shit, B can enter and so can C, and so can D, and so... The timing just has to be right. Piggy-backing and fond memories...!