Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Saturday, September 3, 2011

calculator using applet

//Calculator Applet

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
<applet code=calc width=400 height=350>
</applet>
*/
public class calc extends Applet implements ActionListener // step1
{
double currentValue = 0;// currentValue stores the number currently being displayed
double previousValue = 0;// previousValue stores the number that was displayed before
boolean waitingForDigit = true;
char lastOperation = '=';
boolean computationError = false;

JTextField MainDisplay;
Font f1 = new Font("LCD", 1, 21);
Font f2 = new Font("DigifaceWide", 1, 30);
LayoutManager Layout;
JPanel pNum,pMast,pSym;
JButton b[];//button dec
public void init()
{
pNum=new JPanel();
pSym=new JPanel();
pNum.setBackground(Color.BLUE);
pSym.setBackground(Color.BLACK);
pMast=new JPanel();
pMast.setBackground(Color. BLACK);
MainDisplay=new JTextField();
MainDisplay.setFont(f2);
b=new JButton[23];
for(int i=0;i<=9;i++)
b[i]=new JButton(String.valueOf(i));
b[10] = new JButton("+/-");
b[11] = new JButton(".");
b[12] = new JButton("=");
b[13] = new JButton("/");
b[14] = new JButton("*");
b[15] = new JButton("-");
b[16] = new JButton("+");
b[17] = new JButton("sqrt");
b[18] = new JButton(" 1/x");
b[19] = new JButton("Sin");
b[20] = new JButton("cos");
b[21] = new JButton("tan");
b[22] = new JButton("C");
//seting color for buttons
for (int i = 0; i < b.length; i++)
{
b[i].setFont(f1);
if (i < 10)
b[i].setForeground(Color.blue);
else
b[i].setForeground(Color.red);
}
pNum.setLayout (new GridLayout(4, 3, 2, 2));
pSym.setLayout (new GridLayout(4, 3, 2, 2));
//Set the NUmbers
for (int i = 7; i <= 9; i++)
pNum.add(b[i]);
for (int i = 4; i <= 6; i++)
pNum.add(b[i]);
for (int i = 1; i <= 3; i++)
pNum.add(b[i]);
pNum.add(b[0]);//0
pSym.add(b[19]);//SIN
pSym.add(b[20]);//COS
pSym.add(b[21]);//TAN
pSym.add(b[17]);//sqrt
pSym.add(b[10]);//+-
pSym.add(b[18]);//1/x
pSym.add(b[16]);//+
pSym.add(b[15]);//sub
pSym.add(b[14]);// mul
pSym.add(b[13]);//division
pSym.add(b[22]);//C
pSym.add(b[12]);//=
for (int i = 0; i<=22; i++)
b[i].addActionListener(this);
pMast.setLayout(new BorderLayout(10,10));
pMast.add(pNum, BorderLayout.WEST);
pMast.add(pSym, BorderLayout.EAST);
pMast.add(MainDisplay,BorderLayout.NORTH);
this.setLayout(new BorderLayout(10,10));
add(pMast, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)//ae contains source event
{
String label = ae.getActionCommand(); // The button label
char first = label.charAt(0); // the first char of the label
String x;
if (first == 'C')
{
currentValue = 0;
waitingForDigit = true;
computationError = false;
} else if (label.equals("+/-"))
{
currentValue = -currentValue;
waitingForDigit = true;
} else if ('0'<=first && first <='9')
{
//int digit = first - '0'; // Convert to an int
x=String.valueOf(first);
int digit=Integer.parseInt(x);
if (waitingForDigit)
{
currentValue = digit;
waitingForDigit = false;
} else
currentValue=(currentValue*10)+digit;
} else if(first==' '||first=='S'||first=='s'||first=='c'||first=='t')
{
lastOperation = first;
computeLastOperation();
lastOperation = '=';
waitingForDigit = false;
} else
{ // +,-,*,/, or =
computeLastOperation();
lastOperation = first;
previousValue = currentValue;
waitingForDigit = true;
}
displayCurrentValue();
}

private void displayCurrentValue()
{
if (computationError)
MainDisplay.setText(" Error ");
else
MainDisplay.setText(Double.toString(currentValue));
}

private void computeLastOperation()
{
switch (lastOperation)
{
case '=':
break;
case 's':currentValue=Math.sqrt(currentValue);
break;
case '+':currentValue=previousValue + currentValue;
break;
case '-':currentValue=previousValue - currentValue;
break;
case '*':currentValue=previousValue * currentValue;
break;
case ' ':
if (currentValue==0)
computationError = true;
else
currentValue = 1 / currentValue;
displayCurrentValue() ;
break;
case 'S':currentValue=Math.sin(currentValue);
break;
case 'c':currentValue=Math.cos(currentValue);
break;
case 't':currentValue=Math.tan(currentValue);
break;
case '/':
if (currentValue==0)
computationError = true;
else
currentValue = previousValue / currentValue;
break;
}
}
}

Tower of Hanoi

/*
Tower of Hanoi
*/
import java.util.*;

class gameexp extends Exception
{
private int exec;
gameexp (int a)
{
exec=a;
}
public String toString()
{
if(exec==1)
return"Disk Heavier";
else if(exec==2)
return"No Element Available";
else if(exec==3)
return "Invalid Disk";
else if(exec==4)
return "Congrats!! you have successfully completed the game";
else if(exec==5)
return "Sorrry Try Again !!!!Game Over!! !";

return " ";
}

}
class toh
{
Scanner s=new Scanner(System.in);
int first[]=new int[10];
int second[]=new int[10];
int third[]=new int[10];
String f1[]=new String[10];String f2[]=new String[10];String f3[]=new String[10];
int top1,top2,top3;
int count,n1,n2,n3;
//count=(int)(Math.pow(2,num)-1);
String disc(int n,int num)
{
String dis="";
for(int i=1;i<(n*2);i++)
{
dis="";
for(int k=n;k<num;k++)
dis+=" ";
for(int j=1;j<=i;j++)
dis+="=";
i++;
}
return dis;
}
/*
=
===
=====
*/

void intial(int num)
{
int n;
for(int i=0;i<num;i++)
{
first[i]=i+1;
second[i]=0;
third[i]=0;
}
top1=0;
top2=num;
top3=num;
n1=num;
n2=0;
n3=0;
count=(int)(Math.pow(2,num)-1);
}
void display(int num)
{
//System.out.print("\n top1 ="+top1+" \t top2 ="+top2+"\ttop3="+top3);
System.out.print("\n MOVES LEFT :"+count);
System.out.print("\n ------------------------------------------------------------------------------");
System.out.print("\n\n\tTower 1\t\tTower 2\t\tTower 3");
for(int i=0;i<num;i++)
{
if(first[i]==0)
f1[i]=" ";
else f1[i]=disc(first[i],num);//Integer.toString(first[i]);
if(second[i]==0)
f2[i]=" ";
else f2[i]=disc(second[i],num);//Integer.toString(second[i]);
if(third[i]==0)
f3[i]=" ";
else f3[i]=disc(third[i],num);//Integer.toString(third[i]);
}
for(int i=0;i<num;i++)
{
System.out.print("\n\t"+f1[i]+"\t\t"+f2[i]+"\t\t"+f3[i]);
}
System.out.print("\n ------------------------------------------------------------------------------");
}
void transfer(int num) throws gameexp
{
System.out.print("\n SET YOUR MOVES:\n" );
int ch,ch1,ch2,f=1;
int check=0;
while(count!=0)
{
try{
System.out.print("\n ---> Move From(which tower) :");
ch1=s.nextInt();
System.out.print("\n ---> Move To (which tower) :");
ch2=s.nextInt();
if(ch1==1&&ch2==2)
ch=1;
else if(ch1==1&&ch2==3)
ch=2;
else if(ch1==2&&ch2==1)
ch=3;
else if(ch1==2&&ch2==3)
ch=4;
else if(ch1==3&&ch2==1)
ch=5;
else if(ch1==3&&ch2==2)
ch=6;
else throw new gameexp(3);
switch(ch)
{
case 1:
//
// System.out.print("\n top1 ="+top1+" \t top2 ="+top2+"\tt1="+first[top1]+"\tt2="+second[top2]);
if(n1!=0){
if(first[top1]<second[top2]||second[top2]==0)
{
top2--;
second[top2]=first[top1];
first[top1]=0;
top1++;n1--;n2++;count--;
display(num);
}
else{
throw new gameexp(1);
}}
else
throw new gameexp(2);
break;
case 2:
if(n1!=0){
if(first[top1]<third[top3]||third[top3]==0)
{
top3--;
third[top3]=first[top1];
first[top1]=0;
top1++;n1--;n3++;
count--;
display(num);
}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 3:
if(n2!=0){
if(second[top2]<first[top1]||first[top1]==0)
{
top1--;
first[top1]=second[top2];
second[top2]=0;
top2++;n2--;n1++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 4:
if(n2!=0){
if(second[top2]<third[top3]||third[top3]==0)
{top3--;
third[top3]=second[top2];
second[top2]=0;
top2++;n2--;n3++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 5:
if(n3!=0){
if(third[top3]<first[top1]||first[top1]==0)
{top1--;
first[top1]=third[top3];
third[top3]=0;
top3++;n3--;n1++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 6:
if(n3!=0){
if(third[top3]<second[top2]||second[top2]==0)
{top2--;
second[top2]=third[top3];
third[top3]=0;
top3++;n3--;n2++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
}
}
catch(gameexp e)
{
System.out.println("\nERROR :"+e);
}
}
if(count==0)
for(int i=0;i<num;i++)
if(third[i]==i+1)
check++;
if(count==0&&check==num)
throw new gameexp(4);//won
else
throw new gameexp(5);//fail
}
void rules()
{
System.out.print("\n RULES \n1.You cannot place a larger disk onto a smaller disk \n2.The final completed tower should be on tower 3 \n3.you only get (2^n)-1 moves to finish the game successfully");
}
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
toh t=new toh();
int n,f=1,k;
System.out.print("\n Do you wish to see the rules? \n1.yes \n2.no \nEnter your choice: ");
k=s.nextInt();
if(k==1)
t.rules();
while(f==1)
{
System.out.print("\n\n");
System.out.print("\n Welcome to the Game of tower of Hanoi");
System.out.print("\n Num of Disc =");
n=s.nextInt();
t.intial(n);
t.display(n);
try{
t.transfer(n);
}catch(gameexp e)
{
System.out.print("\n MOVES OVER :"+e);
}
System.out.print("\n Press 1 to continue playing or any other key to exit from the game: ");
f=s.nextInt();
}
}
}




C/C++ Programs for Starters: car applet in java

C/C++ Programs for Starters: car applet in java: import java.util.*; import java.applet.*; import java.awt.*; /* */ public class ...

car applet in java

import java.util.*;
import java.applet.*;
import java.awt.*;
/*
<applet code=carapp width=1000 height=450>
</applet>
*/
public class carapp extends Applet
{
Thread t;
int X[]={314,314,360,360,477,477,530,564};
int Y[]={174,115,115,92,92,114,114,175};
boolean red=false;
int slep;
boolean green=true;
boolean yellow=false;

class car implements Runnable
{
car()
{
for(int x=0;x<8;x++)
{ Y[x]+=100;}

t=new Thread(this,"car");t.start();
}

public void run()
{

for(;;)
{

if(red&&X[7]<600&&X[0]>300)
{
//STOP THE CAR
}
else
for(int x=0;x<8;x++)
{ X[x]+=10;}
repaint();
if(X[0]>1000)
{
for(int x=0;x<8;x++)
{ X[x]-=1200;}

}
try
{
t.sleep(slep);
}catch(Exception e)
{}
}
}
}

class signal implements Runnable
{
signal()
{
t=new Thread(this,"signal");t.start();
}
public void run()
{

while(true)
{
Random r=new Random();
slep=r.nextInt(100);
repaint();
try{
if(green)
{
green=false;
yellow=true;
}else if(yellow)
{
yellow=false;
red=true;
}else if(red)
{
red=false;
green=true;
}

t.sleep(3000);
}catch(Exception e){}



}



}
}

public void init()
{
new car();
new signal();
}
public void paint(Graphics g)
{
//g.drawLine(-1000,200,1000,250);
g.setColor(Color.gray);
g.fillRect(0,170, 1000,150);
g.setColor(Color.red);
g.fillPolygon(X,Y,8);
g.setColor(Color.black);
g.fillOval(20+X[0],255,80,80);
g.setColor(Color.black);
g.fillOval(170+X[0],255,80,80);


g.drawRect(580,90, 50,100);
g.drawOval(600,100,20,20);
g.drawOval(600,130,20,20);
g.drawOval(600,160,20,20);

if(red){
g.setColor(Color.red);
g.fillOval(600,100,20,20);
}
if(yellow){
g.setColor(Color.yellow);
g.fillOval(600,130,20,20);

}
if(green){
g.setColor(Color.green);
g.fillOval(600,160,20,20);

}


}



}
We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs