Sunday, March 29, 2009

Puzzle 11 – Alphabet Soup.

Language – Java | Type – Concept | Last date 01-Apr-2009 9:00 p.m. IST | Points 2

I always knew that characters could be represented as numbers. So when my little brother wanted to learn his alphabets I thought of writing up a program just to get all of them neatly printed. I started with this … but something went wrong. Can you help me out?

package com.twister;
public class TestOperator{
public static void main(String args[]) {
char x='a';
x
=x+1;
System.out.println(
"Value of x is " + x);
}
}

Got an answer? Why don’t you leave it here.


Puzzle 10 – Solution

The key here is that ‘division is the reverse of multiplication.’

It is pretty easy to multiple two integers using repeated addition. However it’s a bit difficult to extend the same logic for floating point numbers. The easiest way to perform multiplication without using the ‘*’ sign is to do a division.

package com.twister;
public class Multiply{
public static void main(String args[]){
float num1 = 20.5f;
float num2 = 2.0f;
float product = num1/(1.0f/num2);
System.out.println(
"The product of the Numbers is : "+product);
}
}

Sorry no correct answers this week!

Wednesday, March 25, 2009

Puzzle 10 – Go forth and multiply.

Language – Java | Type – Problem (Code) | Last date 29-Mar-2009 | 12:00 p.m. IST | Points 5

Here I was quietly doing my work (Ok ok I was playing mind sweeper) when my manager comes to me and ask me to write a code that could multiple two floating point numbers for him. Why he does not use a calculator, I cannot imagine, but I started to write the program anyway.
As you know my key board gives me a lot of trouble and the ‘*’ sign does not work. Help me out folks!


Write a program to multiple two floating point numbers.
Twist: Do not use the “*” (multiplication sign) in the entire program.

Got an answer? Why don’t you leave it here.

Puzzle 9 - Solution

The key to solving this puzzle is understanding 'hardware limitation'. Floating point numbers are represented in IEEE floating point format - however floating point operation - addition, subtraction, multiplication - don't give an accurate result but always have a small error associated with them due to hardware limitation.

The for loop for(double d = 0.0; d != 1.0; d = d + 0.1) does floating point addition. However as floating point addition is not 100% accurate adding 0.1 10 times gives us a number slightly more than 1.0 (say 1.00000001). The equality condition (1.0 == 1.00000001) fails and the loop continues forever.

Key Learning

With floating point numbers it is better to use the greater-or-equal or less-or-equal operator instead of equality operator i.e. use '<=' or '=>' instead of '=='.

Sorry no correct answers this week!

Sunday, March 22, 2009

Puzzle 9 – Double Fun

Language – Java | Type – Concept | Last date 25-Mar-2009 9:00 p.m. IST | Points 2

The other day I started writing code for a mission critical application – and I needed to loop over. Bored with the standard integer way of looping I decided to be adventurous and used doubles instead. Can you tell me if I am doing something wrong out here?
1 package com.twisters;
2 public class MyDouble {
3 public static void main(String[] args) {
4 for(double d = 0.0; d != 1.0; d = d + 0.1){
5 System.out.println("Doing a strategic thing here…");
6 }
7 }
8 }

Got an answer? Why don’t you leave it here.

Puzzle 8 – Solution

The key here is the local variable ‘i’ is overwritten every time the inner for loop runs.

The Loop iterate in the manner depicted below,

Outer Loop : i = 0
Inner Loop : i = 0 to 5
Outer Loop : i = 6
Inner Loop : i = 0 to 5
Outer Loop : i = 6
and this pattern keeps repeating.

Congrats to
1. Joe Smith
2. Tanzy
for getting the correct answer.

Joe & Tanzy - you can leave a link to your profile/blog or site if you want it to show up in the Top Score Section.

Wednesday, March 18, 2009

Puzzle 8 – Misleading Count.

Language – Java | Type – Concept | Last date 22-Mar-2009 12:00 p.m. IST | Points 2

This is why ‘i’ am so important …

package com.twisters;
public class MyLoop {
public static void main(String[] args) {
for(int i=0;i<10;i++){
for(i=0;i<5;i++){
System.out.println("Testing");
}
}
}
}
Got an answer? Why don’t you leave it here.

Puzzle 7 – Solution

The trick here is to generate a '-1' and then multiple the number to be subtracted with it to get a negative of the subtractant.

package com.twisters;
import java.util.Scanner;
public class SubtractFloat{

public static void main(String args[]){

float a,b,result;
System.out.print("Enter the first num : ");
a = new Scanner(System.in).nextFloat();
System.out.print("Enter the second num : ");
b = new Scanner(System.in).nextFloat();
result = a + (~0)*b; //alternately we can use cos(PIE) instead of ~0
to generate -1
System.out.println("Result of subtracting is : "+result);
}
}

Sorry no correct answers this week!

Sunday, March 15, 2009

Puzzle 7 – Add and Subtract

Language – Java | Type – Problem (Code) | Last date 18-Mar-2009 9:00 p.m. IST | Points 5

It’s easy to add up. Subtracting however is another ball game …

Write a program to subtract two floating point numbers.
Twist: Do not use the “-” (minus sign) in the entire program.

Got an answer? Why don’t you leave it here.

Puzzle 6 - Solution

The key to the puzzle is ‘Equals Method has been overridden not overloaded’.

The equals method in the Object class and hence the Base class has the signature public boolean equals(Object o).

When we call child.equals(base) then the equals method in the Child class gets executed since it has a overloaded equals method with signature public boolean equals(Base b).

However when we call base.equals(child), even though base points to a Child Object compile time bindings ensure that the Base version of the equals method namely equals(Object o) gets called.
The output that is printed by this program is Child Equals Base

Sorry no correct answers this week!

Wednesday, March 11, 2009

Puzzle 6 – Overloaded

Language – Java | Type – Concept | Last date 15-Mar-2009 12:00 p.m. IST | Points 2

Have you ever felt over loaded with work? Here is something to cheer you up …
package com.twisters;
class Base{}
public class Child extends Base{
public boolean equals(Base b){
return false;
}
public static void main(String[] args) {
Base base;
Child child;
child = new Child();
base = child;

if(child.equals(base))
System.out.println("Child Equals Base");

if(base.equals(child))
System.out.println("Base Equals Child");
}
}
Got an answer? Why don’t you leave it here.

Puzzle 5 - Solution

The key to solving this puzzle is 'Operator Precedence'.
The post decrement operator '--' has precedence over the subtraction operator '-'.

The evaluation steps are as follows,

Step 1 : result = (a--)-b;
Step 2 : result = (5--)-1;
Step 3 : result = 4

The post decrement happens for 'a' so its value is decremented by 1.

The result is,

Value of a is : 4
Value of b is : 1
Value of result is : 4

Sorry no correct answers this week!

Sunday, March 8, 2009

Puzzle 5 – The Sign

Language – Java | Type – Concept | Last date 11-Mar-2009 9:00 p.m. IST | Points 2

Too many signs spoil the code …
package com.twisters;
public class Signs{
public static void main(String[] args) {
int result;
int a = 5, b =1;
result = a---b;
System.out.println("Value of a is : " + a);
System.out.println("Value of b is : " + b);
System.out.println("Value of result is : " + result);
}
}

Got an answer? Why don’t you leave it here.

Puzzle 4 - Solution

The solution to the last puzzle is,

import java.util.Scanner;
public class Swap{

static int temp;
public static void swap(int num1,int num2){
System.out.println("Original nos are-Num1 : "
+ num1 + " Num2 : " + num2);
if(num1 > num2){
while(num1 > num2){
num1--;temp++;
}
while(temp >0){
temp--;num2++;
}
}
else{
while(num1 < num2){
num1++;temp--;
}
while(temp <0){
temp++;num2--;
}
}
System.out.println("Swaped nos are-Num1 : "
+ num1 + " Num2 : " + num2);
}

public static void main(String args[])
{
int num1,num2;
swap(new Scanner(System.in).nextInt(),
new Scanner(System.in).nextInt());
}
}

Sorry no correct answers this week!

Wednesday, March 4, 2009

Puzzle 4 – The Swap

Language – Java/C++ | Type – Problem (Code) | Last date 8-Mar-2009 12:00 p.m. IST | Points 5

Swapping can be easy, but this is the hardest swap ever!

Write a program that swaps two numbers.
Twist: Do not use “=” (equal sign) in entire program.

(Hard isn't it? )

Got an answer? Why don’t you leave it here.

Puzzle 3 - Solution

The key to solving this puzzle is ' Look at the Object and not the Reference'.

When we create the object of Child any function that we call is first searched in the child class and only then in the base class.

Hence when we execute baseRef.baseCall() the execution happens as follows,

a. First look for the baseCall() in the Child class. Since it is not found look for it in the Base class.
b. Call the function baseCall() in the Base class. This function in turn calls test().
c. Search for test() in the Child class. Since it is found in the Child class, call the function test() in child class

Output of the program is Function Test in Child Executed.

Sorry no correct answers this week!

Sunday, March 1, 2009

Puzzle 3 – Inheritance

Language – Java | Type – Concept | Last date 4-Mar-2009 09:00 p.m. IST | Points 2

Inheritance can be fun, especially if you get loads of money. But then again Inheritance can be a big headache. Especially in Java, if you inherit more that you can chew…
package com.twisters;
class Base{
public void baseCall(){
test();
}
public void test(){
System.out.println("Function Test in Base Executed");
}
}

public class Child extends Base {
public static void main(String[] args) {
Base baseRef = new Child ();
baseRef.baseCall();
}
public void test(){
System.out.println("Function Test in Child Executed");
}
}
Got an answer? Why don’t you leave it here.

Puzzle 2 - Solution

The key to solving this problem are 'Integer arithmetic always gives an Integer' and 'Floating arithmetic takes precedence over Integer arithmetic'.
The steps in which the above operation will perform are,

Step 1 – Multiple 2.0 with 2 = 4.0
Step 2 – Divide 4.0 by 4 = 1.0
Step 3 – Divide 2 by 4 = 0
Step 4 – Add 1.0 and 0 = 1.0

The answer to Puzzle 2 is 1.0

Sorry no correct answers this week!