Wednesday, February 23, 2011

Solution of Question 3 - REVISED

class CalendarDemo {
public static void main(String args[]) throws IOException
{
String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String wday[] = {" ", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

String date;
Calendar calendar = Calendar.getInstance();
int d,m,y;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Date in the format dd/mm/yy ");
date=ob.readLine();
StringTokenizer b=new StringTokenizer(date,"/");
d=Integer.parseInt(b.nextToken());
m=Integer.parseInt(b.nextToken());
y=Integer.parseInt(b.nextToken());

calendar.clear();
calendar.set(Calendar.YEAR, y);
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DATE, d);

System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(wday[calendar.get(Calendar.DAY_OF_WEEK)]);

}
}

Solution 4 - Words in reverse in a sentence

import java.io.*;
class vaibhav
{
public static void main(String args[])throws IOException
{
int i,j,c,x=0,u=0,k=0;String t;
BufferedReader o=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter a sentence==");
String s=o.readLine();
s=s+" ";
int l, l1;
l=s.length();
for(i=0;i {
char ch=s.charAt(i);
if(ch==' ')
{
t="";
t=s.substring(x,i);
l1=t.length();
char d=' ';
if(t.charAt(l1-1)=='.' || t.charAt(l1-1)=='?' || t.charAt(l1-1)=='!')
{
d=t.charAt(l1-1);
t=t.substring(0,l1-1);

}
StringBuffer f=new StringBuffer(t);
f.reverse();
System.out.print(f);
System.out.print(d+" ");
x=i+1;
}
}
}
}

Solution 3 - Print day of the Week for the Date

import java.io.*;
import java.util.Calendar;
class DayofDate {
public static void main(String args[]) throws IOException
{
String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String wday[] = {" ", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Calendar calendar = Calendar.getInstance();
int d,m,y;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Date");
System.out.println("Enter Day");
d=Integer.parseInt(ob.readLine());
System.out.println("Enter Month");
m=Integer.parseInt(ob.readLine());
System.out.println("Enter Date");
y=Integer.parseInt(ob.readLine());

calendar.clear();
calendar.set(Calendar.YEAR, y);
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DATE, d);

System.out.print("Date: ");
System.out.print(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(wday[calendar.get(Calendar.DAY_OF_WEEK)]);

}
}

Tuesday, February 22, 2011

SOLUTION FOR QUESTION 2 - TOWER OF HANOI

import java.io.*;
public class hanoi
{
static int moves=0; //number of moves so far
static int getInt()
{
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
line = in.readLine();
int i = Integer.valueOf(line).intValue();
return i;
}

catch (Exception e)
{
System.err.println("***Error: Input is not a number.\n" +
"Input assumed to be the number 1");
return 1;
}
}

static void hanoi(int height, char fromPole, char toPole, char withPole)
{
if (height >= 1)
{
hanoi(height-1, fromPole, withPole, toPole);
moveDisk(fromPole, toPole);
hanoi(height-1, withPole, toPole, fromPole);
}
}

static void moveDisk(char fromPole, char toPole)
{
moves++;
System.out.print(fromPole);
System.out.print(toPole);
System.out.print(((moves % 20)==0) ? '\n' : ' ');
}

public static void main(String[] args)

{
long time1, time2; //for benchmarking
int TowerHeight;
char FromPole='A', ToPole='B', WithPole='C';
System.out.println("Enter Tower height...");
System.out.print("?");
TowerHeight = getInt();
time1 = System.currentTimeMillis();
hanoi(TowerHeight, FromPole, ToPole, WithPole);
time2 = System.currentTimeMillis();
System.out.println();
System.out.print(time2-time1); //print execution time in msec
System.out.println(" msec execution time");
}
}

Monday, February 21, 2011

SOLUTION OF QUESTION 1

import java.io.*;

class numcombi

{

String str="";

int a=0;

int b=0;

public void combi() throws IOException

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a number : ");

int num = Integer.parseInt(br.readLine());

System.out.println("Combinations :");

for(int i=1;i<=num/2;i++)

{

str="";

extract(num,i);

}

}

public void extract(int n, int m)

{

a=m;

str+=" "+a;

b=n-a;

System.out.println(str+" "+b);

if(b<=m)

{

return;

}

extract(b,m);

}

public static void main(String arg[]) throws IOException

{

numcombi obj= new numcombi();

obj.combi();

}

}

Saturday, February 19, 2011

Changes are good

Now access to the web page https://sites.google.com/site/javaprogramsisc/ is purely on invitation. So use your gmail id to send request and you will receive link for the web page

Sunday, February 13, 2011

Sunday, January 30, 2011


I am back.
If any questions ask me.