From where I come from everything is driven by statistic. Its no wonder that I am so much attached to number & tracking. I track the number of visitors on the blog - Google Analytics, Feed Subscribers, Twitter followers even my donate widget is one which shows how much progress I have made - at 0 right now :(!!
I sure would love any help to improve on any of these numbers ;)
The number that's pretty interesting though is the question rating. Its a pointer to the type of questions that you folks find interesting. I try to align questions to what you folks are looking for - in terms of type and difficulty though not always immediately.
Breaking out from my rant and getting to the puzzle at hand -
Have you ever had to code to silly standards. Well here is a typical construct!
package com.twisters;
public class ExtraLoad{
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
Get this code to compile – don't comment or delete any code.
Standard says -
You may not have more than one constructor in the code - Silly isn't it!
Sure you could rename the method to Extraload1(String), ExtraLoad2(String,String) but that would not be an interesting solution, would it? So here another clause - No changes to any part of main() method.
As usual looking forward to your solutions!!
By Putting a Var-Arg Constructor of type String or Object, we can make this program get compiled! As it satisfies the rules of having only one constructor! It can be the solution for this puzzle.
ReplyDeletepackage com.twisters;
public class ExtraLoad
{
ExtraLoad(String... x) // Just Introduce a Var-Args Constructor
{
}
public static void main(String[] args)
{
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
@ the Moderator
ReplyDeletehi! I did send my solution by name of avinash for the puzzle 43. But i dont know why it was not considered. what makes my solution wrong from others?? It satisfies all the rules which u have given in the puzzle.
ExtraLoad(String...args)
ReplyDelete{
for(int i=0;i<args.length;i++)
System.out.print(args[i]+" ");
System.out.println();
}
insert this code in class ExtraLoad
nsureshreddy@live.com
ExtraLoad(String...args)
ReplyDelete{
for(String a:args)
System.out.print(a+" ");
System.out.println();
}
//nsureshreddy@live.com
public class ExtraLoad {
ReplyDeletepublic ExtraLoad(String... string) {
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
Use varargs as constructor paratemer.
ReplyDeleteBoring puzzle. Its not even a puzzle, just some kind of test if person ever read any java book.
Common, add some tricky puzzle!
public ExtraLoad(String... string){}
ReplyDeletepublic ExtraLoad(String... s){};
ReplyDeletepublic class ExtraLoad{
ReplyDeletepublic static void main(String[] args){
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
public ExtraLoad(String... var){
}
}
hi
ReplyDeleteWe can write constructor having different parameters
Also if u want we can initialize them using this keyword
public class ExtraLoad{
ExtraLoad()
{
}
ExtraLoad(String str)
{
}
ExtraLoad(String str ,String str1)
{
}
ExtraLoad(String str ,String str1,String str2)
{
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
Add a constructor as:
ReplyDeletepublic ExtraLoad(String... args) {
}
ExtraLoad(String...args)
ReplyDelete{
for(String a:args)
System.out.print(a+" ");
System.out.println();
}
// insert this constructor in class ExtraLoad
public ExtraLoad(String... args) {}
ReplyDeleteNow that one was too easy... using varargs solves this "issue"...
ReplyDeletepublic ExtraLoad(String... args){}
package com.twisters;
ReplyDeletepublic class ExtraLoad{
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
public ExtraLoad(String... s){
}
}
Just add a constructor with a var-arg parameter :)
ReplyDeleteExtraLoad(String... args) {}
Is this what u really expected ?
ReplyDeletepublic class ExtraLoad{
ExtraLoad(String... s)
{
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
Thanks Java 5 for your cool varargs construct.
ReplyDeletepackage com.twisters;
public class ExtraLoad{
public ExtraLoad(String...strings) {
//do whatever here
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
Add the following constructor:
ReplyDeletepublic ExtraLoad(String... args) {}
public ExtraLoad(String... var){
ReplyDelete}
or
public ExtraLoad(String[] var){
}
public ExtraLoad(String... string) {
ReplyDelete}
package com.twisters;
ReplyDeletepublic class ExtraLoad {
public ExtraLoad(String... args) {
// ...
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
Java 1.5 - Variable length arguments !
ReplyDeletepackage com.twisters;
public class ExtraLoad {
public ExtraLoad(String... args) {
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}
we can solve this puzzle by using the concept of varargs (variable arguments).
ReplyDeletepackage com.twisters;
public class ExtraLoad{
ExtraLoad(String... a){
System.out.println("Hello this is the solution");
}
public static void main(String[] args) {
ExtraLoad ex = new ExtraLoad();
ExtraLoad ex1 = new ExtraLoad("Hello");
ExtraLoad ex2 = new ExtraLoad("Hello", "World");
ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
}
}