Monday, June 21, 2010

Solution to the tricky program 2

 Solution to the program http://javacoderblog.blogspot.com/2010/05/tricky-program-1-to-java-beginners.html

import java.io.*;
public class NMM
{
static
{

final PrintStream out = System.out;

PrintStream newOut = new PrintStream(out)
{

public void println(String str1)
{
print("Great ");
print(str1);
super.println("Programmer");
}
};

System.setOut(newOut);
}


public static void main(String [ ]argums)
{
System.out.println("Java ");
}
}

Tuesday, May 18, 2010

A tricky program 2 to Java Beginners,

Question:- You should bring the output by not modifying the main method,The output is "Great Java Programmer".

class NMM //No main method
{
public static void main(String []args)
{
System.out.println(“Java”);
}
}Seems to be tricky for you right?.Let us see what is answer in the next post..If you know the answer just post as comment or mail to me.

UPDATE: Check out the solution here http://javacoderblog.blogspot.com/2010/06/solution-to-tricky-program-2.html

Java program that runs without Main method

Most of you heard about this popular question.this post is for those who did not know about this.
While Learning Java in Computer Centre,my Mam asked me this question. I replied, immediately “Mam, Applets run without main method,is this is answer?” ;) . It is also a right answer I think..But it is not the answer what she expected. She asked me to write a program again, without main method,, after some time I gave her the answer,[ I read related article already ]. I wrote the following program.
class NMM //No main method
{
static
{
System.out.println(“This is the Java Program That Runs Without Main Method”);
System.exit(0);
}
}
Reason is, before the main block getting executed, the static initializations gets executed. So You might ask , why this System.exit(0) line. This is to terminate the program, when it throws exception because of the class not having main method.

My Solution to Google Code Jam Problem Theme Park


I have successfully Solved the Theme Park Problem.The following is the problem and its solution...

Problem

Roller coasters are so much fun! It seems like everybody who visits the theme park wants to ride the roller coaster. Some people go alone; other people go in groups, and don't want to board the roller coaster unless they can all go together. And everyone who rides the roller coaster wants to ride again. A ride costs 1 Euro per person; your job is to figure out how much money the roller coaster will make today.
The roller coaster can hold k people at once. People queue for it in groups. Groups board the roller coaster, one at a time, until there are no more groups left or there is no room for the next group; then the roller coaster goes, whether it's full or not. Once the ride is over, all of its passengers re-queue in the same order. The roller coaster will run R times in a day.
For example, suppose R=4, k=6, and there are four groups of people with sizes: 1, 4, 2, 1. The first time the roller coaster goes, the first two groups [1, 4] will ride, leaving an empty seat (the group of 2 won't fit, and the group of 1 can't go ahead of them). Then they'll go to the back of the queue, which now looks like 2, 1, 1, 4. The second time, the coaster will hold 4 people: [2, 1, 1]. Now the queue looks like 4, 2, 1, 1. The third time, it will hold 6 people: [4, 2]. Now the queue looks like [1, 1, 4, 2]. Finally, it will hold 6 people: [1, 1, 4]. The roller coaster has made a total of 21 Euros!

Input

The first line of the input gives the number of test cases, TT test cases follow, with each test case consisting of two lines. The first line contains three space-separated integers:Rk and N. The second line contains N space-separated integers gi, each of which is the size of a group that wants to ride. g0 is the size of the first group, g1 is the size of the second group, etc.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of Euros made by the roller coaster.

Limits

1 ≤ T ≤ 50.
gi ≤ k.

Small dataset

1 ≤ R ≤ 1000.
1 ≤ k ≤ 100.
1 ≤ N ≤ 10.
1 ≤ gi ≤ 10.

Large dataset

1 ≤ R ≤ 108.
1 ≤ k ≤ 109.
1 ≤ N ≤ 1000.
1 ≤ gi ≤ 107.

Sample:-

My Solution To The Problem In Java
import java.io.*;
import java.util.regex.Pattern;
public class thirdpart1
{
static int groups[];
static int N;
static int testcases=0;
public static void main(String args[])throws Exception
{
File file = new File("C-small.in");
    FileInputStream fis =new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
testcases=Integer.parseInt( dis.readLine() );
int mno=0;
while( mno
{
 int euros=0;
mno++;
            String ab=dis.readLine();
String REGEX = " ";
String INPUT = ab;
Pattern p = Pattern.compile(REGEX);
String[] items = p.split(INPUT);
int R=Integer.parseInt(items[0]);
int K=Integer.parseInt(items[1]);
N=Integer.parseInt(items[2]);
groups=new int[N+555000];
String ab2=dis.readLine();
REGEX = " ";
INPUT = ab2;
p = Pattern.compile(REGEX);
String[] items1 = p.split(INPUT);
int k5=0;
for (int i3 = 0; i3 < items1.length; i3++)
{k5++;
groups[k5]=Integer.parseInt(items1[i3]);
}
int coastriders[]=new int[551000];

for(int mn=1;mn<=R;mn++)
{
int z=0,totalcost=0,j=1;
for(j=1;j<=N;j++)
{
if(   ( (totalcost+groups[j])<=K )    )
{
z++;
totalcost=totalcost+groups[z];
coastriders[z]=groups[z];
}
else
break;
}
for(int nm=1;nm<=z;nm++)
euros=euros+coastriders[nm];
shifter(z,coastriders);
}
System.out.println("Case #"+mno+": "+euros);
}
}
static void shifter(int z,int coastriders[])
{
int s=1,i=1;
int temp[]=new int[z+551000];
i=z+1;
for(s=0;i<=N;i++)
{
s++;
temp[s]=groups[i];
}
int h=N-z;
for(i=1;i<=h;i++)
groups[i]=temp[i];
for(s=1;i<=N;i++)
{
groups[i]=coastriders[s];
s++;
}
}
}

Google Developers

http://www.youtube.com/GoogleDevelopers
This channel is the home for videos of interest to developers. It will contain interviews, screencasts, and anything that the Google Code folk think is entertaining.

Hope You love it..!

JAVAC "welcomeprogrammers.java"

LOCATION:- start.jar->welcomeprogrammers.java 
public class welcomeprogrammersSmilies
{
public static void main( String []args )
         {
          System.out.println("Hello ! Programmers");
          System.out.println("I Welcome all Programmers to my Blog ! Let Java Rocks");
         }
}