Friday, August 27, 2010

A Simple Scientific Calculator in Java

Hi Friends....just had some free time, so thought of creating a "Simple Calculator" in Java...As claims it is "Simple" and don't expect anything Big out of it (Just some basic operations).....I have added the source code for it too....

(You can use it to Create your own Calculator and may be which is more efficient than mine..Just in case u find any flaws in this, pls do mail me abt it..)

Screenshot of "Calculator" that I created in Java




Download the Source Code and the Application


Comments Welcomed...

G.Vivek Venkatesh

Thursday, August 26, 2010

One Bit Sliding Window Protocol

Question - To write a C program to Implement One Bit Sliding Window Protocol.

Screenshot of Output



Program


sender.c (server)

#include "stdio.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "string.h"
#include "sys/socket.h"
#include "stdlib.h"
#include "unistd.h"
main()
{
int sd,i,r,bi,nsd,port,frame,prev_frame=0,count=0;;
char ack[5],buff[30];
struct sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\n Socket problem");
return 0;
}
printf("\n Socket created\n");
bzero((char*)&cli,sizeof(ser));
printf("ENTER PORT NO:\n");
scanf("%d",&port);
printf("\n Port Address is %d\n:",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);

bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser));
if(bi==-1)
{
printf("\n bind error, port busy, plz change port in client and server");
return 0;
}
i=sizeof(cli);
listen(sd,5);
nsd = accept(sd,((struct sockaddr *)&cli),&i);
if(nsd==-1)
{
printf("\ncheck the description parameter\n");
return 0;
}
printf("\nConnection accepted.");
while(count<5)
{

ph:
printf("\nSending FRAME %d to the Receiver...\n",prev_frame);
snprintf(buff,sizeof(buff),"%d",prev_frame);
send(nsd,buff,30,0);

r = recv(nsd,ack,5,0);
if(strcmp(ack,"ack")==0 || strcmp(ack,"ACK")==0)

{ count++;
if(prev_frame==0) prev_frame=1;
else prev_frame = 0;
}
else if(strcmp(ack,"nak")==0 || strcmp(ack,"NAK")==0)
{
printf("\n NAK: So again sending the Previous frame...\n"); goto ph;
}
}
printf("\n Bye..");
send(nsd,"EOF",4,0);
close(sd);
close(nsd);
return 0;
}


receiver.c (client)

#include "stdio.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "string.h"
#include "sys/socket.h"
#include "stdlib.h"
#include "unistd.h"
int main()
{
int sd,con,port,i;
char content[30],ack[3];
struct sockaddr_in cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\n Socket problem");
return 0;
}
bzero((char*)&cli,sizeof(cli));
cli.sin_family = AF_INET;
printf("ENTER PORT NO");
scanf("%d",&port);
cli.sin_port=htons(port);
cli.sin_addr.s_addr=htonl(INADDR_ANY);
con=connect(sd,(struct sockaddr*)&cli,sizeof(cli));
if(con==-1)
{
printf("\n connection error");
return 0;
}
i=recv(sd,content,30,0);
while(strcmp(content,"EOF") != 0)
{
printf("Received from Sender: Frame %s \n",content);
ph:

printf("\n Acknowledgement(ACK/NAK) : ");
scanf("%s",ack);
if(!(strcmp(ack,"ack")==0 || strcmp(ack,"nak")==0 || strcmp(ack,"ACK")==0 || strcmp(ack,"NAK")==0))
{
printf("\n Not a valid Acknowledgement..use ACK or NAK..\n ");
goto ph;

}
send(sd,ack,5,0);
i=recv(sd,content,30,0);
}
printf("\n\n Bye...");
close(sd);
return 0;
}

Download Source Codes

sender.c

receiver.c


Program Written by G.Vivek Venkatesh




Tuesday, August 24, 2010

Object Serialization Program in Java

Question

Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read that file, convert to Rupee if it reads a Dollar, while leave the value as it is if it reads a Rupee.

Program

(credits to Kumresan Sir)


SerializationWrite.java

import java.io.*;
import java.util.*;

class Currency implements Serializable
{
protected String currency;
protected int amount;

public Currency(String cur, int amt)
{
this.currency = cur;
this.amount = amt;
}
public String toString()
{
return currency + amount;
}
public String dollarToRupee(int amt)
{
return "Rs" + amt * 45;
}
}
class Rupee extends Currency
{
public Rupee(int amt)
{
super("Rs",amt);
}
}
class Dollar extends Currency
{
public Dollar(int amt)
{
super("$",amt);
}
}
public class SerializationWrite
{
public static void main(String args[])
{
Random r = new Random();
try
{
Currency currency;
FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing to file using Object Serialization:");
for(int i=1;i<=25;i++)
{
Object[] obj = { new Rupee(r.nextInt(5000)),new Dollar(r.nextInt(5000)) };
currency = (Currency)obj[r.nextInt(2)]; // RANDOM Objects for Rs and $
System.out.println(currency);
oos.writeObject(currency);
oos.flush();
}
oos.close();
}
catch(Exception e)
{
System.out.println("Exception during Serialization: " + e);
}
}
}

SerializationRead.java

import java.io.*;
import java.util.*;

public class SerializationRead
{
public static void main(String args[])
{
try
{
Currency obj;
FileInputStream fis = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Reading from file using Object Serialization:");
for(int i=1;i<=25;i++)
{
obj = (Currency)ois.readObject();
if((obj.currency).equals("$"))
System.out.println(obj + " = " + obj.dollarToRupee(obj.amount));
else
System.out.println(obj + " = " + obj);

}
ois.close();
}
catch(Exception e)
{
System.out.println("Exception during deserialization." + e);
}
}
}


Output

vivek@ubuntu:~/Desktop$ javac SerializationWrite.java
vivek@ubuntu:~/Desktop$ java SerializationWrite

Writing to file using Object Serialization:
$4645
Rs105
$2497
$892
Rs1053
Rs1270
$1991
Rs4923
Rs4443
Rs3537
Rs2914
$53
$561
$4692
Rs860
$2764
Rs752
$1629
$2880
Rs2358
Rs3561
$3796
Rs341
Rs2010
Rs427

vivek@ubuntu:~/Desktop$ javac SerializationRead.java
vivek@ubuntu:~/Desktop$ java SerializationRead

Reading from file using Object Serialization:
$4645 = Rs209025
Rs105 = Rs105
$2497 = Rs112365
$892 = Rs40140
Rs1053 = Rs1053
Rs1270 = Rs1270
$1991 = Rs89595
Rs4923 = Rs4923
Rs4443 = Rs4443
Rs3537 = Rs3537
Rs2914 = Rs2914
$53 = Rs2385
$561 = Rs25245
$4692 = Rs211140
Rs860 = Rs860
$2764 = Rs124380
Rs752 = Rs752
$1629 = Rs73305
$2880 = Rs129600
Rs2358 = Rs2358
Rs3561 = Rs3561
$3796 = Rs170820
Rs341 = Rs341
Rs2010 = Rs2010
Rs427 = Rs427


Download Source Codes

SerializationWrite.java

SerializationRead.java

Thursday, August 19, 2010

DNS Server Client Program


(Screenshot of output)


dnsserver.c


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "errno.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#define MAXBUFFLEN 500
int main(int argc,char *argv[])
{
int sockfd,port,bi;
char buff[MAXBUFFLEN-1];
struct sockaddr_in servaddr,cliaddr;
int addr_len,numbytes;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
perror("Socket()");
return 0;
}
printf("\n Enter the port no:");
scanf("%d",&port);
printf("\n The Port no is:%d",port);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = INADDR_ANY;
memset(servaddr.sin_zero,'\0',8);
bi=bind(sockfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr));
if(bi==-1)
{
perror("bind");
return 0;
}
addr_len = sizeof(struct sockaddr*);
numbytes = recvfrom(sockfd,buff,MAXBUFFLEN-1,0,(struct sockaddr*)&cliaddr,&addr_len);
printf("\n Server Got-Packet from %s",inet_ntoa(cliaddr.sin_addr));
printf("\n Server packet is %d bytes long.",numbytes);
buff[numbytes]='\0';
printf("\n The Server packet contains %s",buff);
close(sockfd);
return 0;
}

dnsclient.c

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "errno.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#include "netdb.h"
int main(int argc,char *argv[])
{
int sockfd,port;
struct sockaddr_in servaddr;
struct hostent *he;
int addr_len,numbytes;
if(argc!=3)
{
fprintf(stderr,"Client Usage : %s \n",argv[0]);
exit(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
perror("gethostbyname error");
exit(1);
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
{
perror("Socket.");
return 0;
}
printf("\n Enter the port no:");
scanf("%d",&port);
printf("\n The port no is : %d",port);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr = *((struct in_addr*)he->h_addr);
memset(&(servaddr.sin_zero),'\0',8);
numbytes = sendto(sockfd,argv[2],sizeof(argv[2]),0,(struct sockaddr*)&servaddr,sizeof(struct sockaddr));
printf("\n Sent %d bytes to %s",numbytes,inet_ntoa(servaddr.sin_addr));
close(sockfd);
return 0;
}

Download Source Code

dnsserver.c

dnsclient.c


UDP Time Server Program

tserver.c

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "time.h>
int main()
{
int sfd,r,bi,port;
char buff[1024];
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("Socket");
return 0;
}
memset(servaddr.sin_zero,'\0',sizeof(servaddr.sin_zero));
printf("\n Enter the port no:");
scanf("%d",&port);
printf("\n The port no is:%d",port);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = INADDR_ANY;
bi=bind(sfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(bi==-1)
{
perror("Bind()");
return 0;
}
clilen = sizeof(cliaddr);
r=recvfrom(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&clilen);
buff[r]=0;
time_t ticks;
ticks = time(NULL);
snprintf(buff,sizeof(buff),"%24s\r\n",ctime(&ticks));
sendto(sfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr));
exit(0);
return 0;
}

tclient.c

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "netinet/in.h"
int main()
{
int listenfd,port,r;
char buff[1024];
struct sockaddr_in servaddr,cliaddr;
socklen_t servlen;
listenfd = socket(AF_INET,SOCK_DGRAM,0);
if(listenfd==-1)
{
perror("Socket");
return 0;
}
memset(servaddr.sin_zero,'\0',sizeof(servaddr.sin_zero));
printf("\n Enter the port no:");
scanf("%d",&port);
printf("\n The port no is:%d",port);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = INADDR_ANY;
sendto(listenfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,sizeof(servaddr));
r=recvfrom(listenfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,&servlen);
buff[r]=0;
printf("\n The time received from the server:%s",buff);
exit(0);
return 0;
}

Download Source Code


tserver.c

tclient.c

Saturday, August 14, 2010

Blog Reaches 100th Post!!!!





Hi guys, with this , the blog has successfully reached its 100th post, after it was started on August 4, 2009. :) :)

I hereby thank u all for ur Support and interest shown towards this blog....

Hope this Blog is useful for you....

Comments and Suggestions to improve this blog are Welcomed....!!!

With Regards,

G.Vivek Venkatesh

Wish u all a Happy Independence Day....Jai hind....

LISP-Like List in Java (Simplified Version)

I have simplified the previous code that I had written for LISP-like list in Java in this post,

Program

import java.util.*;


// To implement LISP-like List in Java to perform functions like car, cdr, cons.

class Lisp
{


public Vector car(Vector v)
{
Vector t = new Vector();
t.addElement(v.elementAt(0));
return t;
}

public Vector cdr(Vector v)
{
Vector t = new Vector();
for(int i=1;i t.addElement(v.elementAt(i));
return t;
}

public Vector cons(String x, Vector v)
{
v.insertElementAt(x,0);
return v;
}

public Vector cons(int x, Vector v)
{
v.insertElementAt(x,0);
return v;
}

}

class Lisp_List2
{
public static void main(String[] args)
{
Lisp a = new Lisp();
Vector v = new Vector(4,1);
Vector v1 = new Vector();

System.out.println("\n Creating a List of Strings....\n");

v.addElement("S.R.Tendulkar");
v.addElement("M.S.Dhoni");
v.addElement("V.Sehwag");
v.addElement("S.Raina");

System.out.println("\n The Contents of the List are " + v);
System.out.print("\n The CAR of this List....");
System.out.println(a.car(v));
System.out.print("\n The CDR of this List....");
v1 = a.cdr(v);

System.out.println(v1);
System.out.println("\n The Contents of this list after CONS..");
v1 = a.cons("Gambhir",v);

System.out.print(" " + v1);

v.removeAllElements();
v1.removeAllElements();

System.out.println("\n\n Creating a List of Integers....\n");

v.addElement(3);
v.addElement(0);
v.addElement(2);
v.addElement(5);

System.out.println("\n The Contents of the List are " + v);
System.out.print("\n The CAR of this List....");
System.out.println(a.car(v));
System.out.print("\n The CDR of this List....");
v1 = a.cdr(v);

System.out.println(v1);
System.out.println("\n The Contents of this list after CONS..");
v1 = a.cons(9,v);

System.out.print(" " + v1);
}
}

Output

Creating a List of Strings....


The Contents of the List are [S.R.Tendulkar, M.S.Dhoni, V.Sehwag, S.Raina]

The CAR of this List....[S.R.Tendulkar]

The CDR of this List....[M.S.Dhoni, V.Sehwag, S.Raina]

The Contents of this list after CONS..
[Gambhir, S.R.Tendulkar, M.S.Dhoni, V.Sehwag, S.Raina]

Creating a List of Integers....


The Contents of the List are [3, 0, 2, 5]

The CAR of this List....[3]

The CDR of this List....[0, 2, 5]

The Contents of this list after CONS..
[9, 3, 0, 2, 5]

Download Source Code

Friday, August 13, 2010

Lisp Like List in Java

Question

Implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons'. If L is a list [3, 0, 2, 5], L.car() returns 3, while L.cdr() returns [0,2,5].


Lisp Operations

Car


The car of a list is, quite simply, the first item in the list. For Ex: car of L.car() of a list [3 0 2 5] returns 3.

Cdr


The cdr of a list is the rest of the list, that is, the cdr function returns the part of the list that follows the first item. For ex: cdr of a list [3 0 2 5] returns [0 2 5]

Cons


The cons function constructs lists. It is useful to add element to the front of the list; it is the inverse of car and cdr. For example, cons can be used to make a five element list from the four element list.

(source – Wikipedia)

Length


Returns the length of the list.


Program


import java.util.*;

import java.io.*;

// To implement LISP-like List in Java to perform functions like car, cdr, cons.

class Lisp

{

Vector v = new Vector(4,1);

int i;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader inData = new BufferedReader(isr);

String str;

//Create a LISP-Like List using VECTOR

public void create()

{

int n;

System.out.print("\n Enter the Size of the list:");

//Get Input from the user...Use I/p stream reader for it...

try

{

str = inData.readLine();

n = Integer.parseInt(str);

for(i=0;i

{

int temp;

System.out.print("\n Enter the element " + (i+1) + " of the list:");

str = inData.readLine();

temp=Integer.parseInt(str);

v.addElement(new Integer(temp));

}

}

catch (IOException e)

{

System.out.println("Error!");

System.exit(1);

}

}

//Display the content of LISP-List

public void display()

{

int n=v.size();

System.out.print("[ ");

for(i=0;i

{

System.out.print(v.elementAt(i) + " ");

}

System.out.println("]");

}

/*

The car of a list is, quite simply, the first item in the list.

For Ex: car of L.car() of a list [3 0 2 5] returns 3.

Gives the CAR of LISP-List

*/

public void car()

{

System.out.println("\n CAR of the List: " + v.elementAt(0));

}

/*

The cdr of a list is the rest of the list, that is, the cdr function returns the part of the list that follows the first item.

For ex: cdr of a list [3 0 2 5] returns [0 2 5]

*/

public void cdr()

{

int n=v.size();

System.out.print("\n The CDR of the List is:");

System.out.print("[ ");

for(i=1;i

{

System.out.print(v.elementAt(i) + " ");

}

System.out.println("]");

}

/*

The cons function constructs lists. It is useful to add element to the front of the list; it is the inverse of car and cdr. For example, cons can be used to make a five element list from the four element list.

For ex: adding 7 to [3 0 2 5], cons(7) gives [7 3 0 2 5]

*/

public void cons(int x)

{

v.insertElementAt(x,0);

System.out.print("\n The List after using CONS and adding an element:");

display();

}

// Returns the length of the LISP-List

public int length()

{

return v.size();

}

}

class Lisp_List

{

public static void main(String[] args)

{

Lisp a = new Lisp();

a.create();

System.out.print("\n The contents of the List are:");

a.display();

a.car();

a.cdr();

a.cons(5);

System.out.println("\n\n The length of the list is " + a.length());

}

}


Output


Enter the Size of the list:4

Enter the element 1 of the list:3

Enter the element 2 of the list:0

Enter the element 3 of the list:2

Enter the element 4 of the list:5

The contents of the List are:[ 3 0 2 5 ]

CAR of the List: 3

The CDR of the List is:[ 0 2 5 ]

The List after using CONS and adding an element:[ 5 3 0 2 5 ]

The length of the list is 5


Download the Source Code

Thursday, August 12, 2010

Vehicle Class Hierarchy in Java

Question

Design a Vehicle class hierarchy in Java. Write a test program to demonstrate
polymorphism.

(Program written by Santhosh Kumar S)


Program

import java.util.*;
abstract class Vehicle
{
boolean engstatus=false;
abstract void start();
abstract void moveForward();
abstract void moveBackward();
abstract void applyBrake();
abstract void turnLeft();
abstract void turnRight();
abstract void stop();
}
class Porsche extends Vehicle
{
void truckManufacture()
{
System.out.println("Porsche World Wide Car manufactures ");
}

void gearSystem()
{
System.out.println("\n automatic gearing system.. So don't worry !!");
}
void start()
{
engstatus=true;
System.out.println("\n engine is ON ");
}
void moveForward()
{

if(engstatus==true)
System.out.println("\nCar is moving in forward direction .. Slowly gathering speed ");
else
System.out.println("\n Car is off...Start engine to move");
}
void moveBackward()
{ if(engstatus=true)
System.out.println("\n Car is moving in reverse direction ...");
else
System.out.println("\n Car is off...Start engine to move");
}
void applyBrake()
{
System.out.println("\n Speed is decreasing gradually");
}
void turnLeft()
{
System.out.println("\n Taking a left turn.. Look in the mirror and turn ");
}
void turnRight()
{
System.out.println("\n Taking a right turn.. Look in the mirror and turn ");
}
void stop()
{
engstatus=false;
System.out.println("\n Car is off ");
}
}
class Volvo extends Vehicle
{
void truckManufacture()
{
System.out.println("Volvo Truck Manufactures");
}
void gearSystem()
{
System.out.println("\nManual Gear system...ooops....take care while driving ") ;
}
void start()
{
engstatus=true;
System.out.println("\n engine is ON ");
}
void moveForward()
{

if(engstatus==true)
System.out.println("\n truck is moving in forward direction .. Slowly gathering speed ");
else
System.out.println("\n truck is off...Start engine to move");
}
void moveBackward()
{ if(engstatus=true)
System.out.println("\n truck is moving in reverse direction ...");
else
System.out.println("\n truck is off...Start engine to move");
}
void applyBrake()
{
System.out.println("\n Speed is decreasing gradually");
}
void turnLeft()
{
System.out.println("\n Taking a left turn.. Look in the mirror and turn ");
}
void turnRight()
{
System.out.println("\n Taking a right turn.. Look in the mirror and turn ");
}
void stop()
{
engstatus=false;
System.out.println("\n Truck is off ");
}
}

class Vehicle1
{
public static void main(String[] args)
{
Porsche v1=new Porsche();
v1.truckManufacture();
v1.start();
v1.gearSystem();
v1.moveForward();
v1.applyBrake();
v1.turnLeft();
v1.moveForward();
v1.applyBrake();
v1.turnRight();
v1.moveBackward();
v1.stop();
v1.moveForward();

Volvo v2= new Volvo();
v2.truckManufacture();
v2.start();
v2.gearSystem();
v2.moveForward();
v2.applyBrake();
v2.turnLeft();
v2.moveForward();
v2.applyBrake();
v2.turnRight();
v2.moveBackward();
v2.stop();
v2.moveForward();

}
}

Download the Source Code

Tuesday, August 10, 2010

Single Pass Assembler Program

Question - Implement a single pass assembler.

Program

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int count[20];
void main()
{
FILE *f1,*f2,*f3,*f4;
int linenumber,locctr,f;
char lbl[10],mne[10],opd[10],ch,mneu1[10],sval[10];
char sadr[10],slbl[10],op1[10],label[10];
void wordcount();
printf("Word count for Input Program:");
wordcount();
printf("\n Output \n");
printf("\n Source Code \t Object code \n\n");
f1=fopen("INPUT.C","r");
f2=fopen("SYMTAB.C","w+");
f4=fopen("INTER.C","w");
fscanf(f1,"%s %s %x\n",lbl,mne,&locctr);
linenumber=2;
while(!feof(f1))
{
if(count[linenumber]==1)
{
fscanf(f1,"%s\n",mne);
fprintf(f4,"%x\t%s",locctr,mne);
}
if(count[linenumber]==2)
{
fscanf(f1,"%s%s\n",mne,opd);
fscanf(f4,"%X \t %s \t %s \n",locctr,mne,opd);
printf("%s\t%s\t",mne,opd);
f3=fopen("OPCODE.C","r");
while(!feof(f3))
{
fscanf(f3,"%s %s \n",mneu1,op1);
if(strcmp(mne,mneu1)==0)
printf("%s\t",op1);
}
fclose(f3);
f=0;
rewind(f2);
while(!feof(f2))
{
fscanf(f2,"%s %s %s \n",sadr,slbl,sval);
if(strcmp(opd,slbl)==0)
{
printf("%s\n\n",sadr);
f=1;
}
}
if(f==0)
printf("0000\n");
}
if(count[linenumber]==3)
{
fscanf(f1,"%s %s %s \n",lbl,mne,opd);
fprintf(f4,"%X \t %s \t %s \t %s \n",locctr,lbl,mne,opd);
fprintf(f2,"%X \t %s \t %s \n", locctr, lbl,opd);
if((strcmp(mne,"RESW")==0) || (strcmp(mne,"RESB")==0))
printf("%s \t %s \t 00 \t 000 %s \n\n",lbl,mne,opd);
else
printf("%s \t %s \t 00 \t 000 %s \n\n",lbl,mne,opd);
}
linenumber+=1;
if(strcmp(mne,"WORD")==0)
locctr+=3;
else if(strcmp(mne,"BYTE")==0)
locctr+=strlen(opd);
else if(strcmp(mne,"RESW)==0)
locctr+=3 * (atoi(opd));
else if(strcmp(mne,"RESB")==0)
locctr+=atoi(opd);
else
locctr+=3;
}
fclose(f1);
fclose(f2);
fclose(f4);
getch();
}

void wordcount()
{
FILE *f3;
char c;
int word=0,i=1;
printf("\n Word Count");
f3=fopen("INPUT.C","r");
c=fgetc(f3);
while(c!=EOF)
{
if(c==' ')
word+=1;
if(c=='\n')
{
word+=1;
count[i]=word;
printf("\n No.of words in %d:%d",r,word);
i+=1;
word=0;
}
c=fgetc(f3);
}
fclose(f3);
}

INPUT.c

WC START 1000
FIRST WORD 5
SECOND WORD 6
THIRD RESW 1
LDA FIRST
ADD SECOND
STA THIRD
END

OPCODE.c

LDA 00
ADD 18
SUB 1C
STA 0C

SYMTAB.c

FIRST 1009
SECOND 100C
THIRD 100F

INTER.c

1000 LDA FIRST
1003 ADD SECOND
1006 STA THIRD
1009 FIRST WORD
100C SECOND WORD
100F THIRD RESW
1012 END
1015 ENDL



Download Source Code


Note - Source code for two other files namely "SYMTAB.c" and "INTER.c" will be uploaded soon...

Friday, August 6, 2010

Calendar Class in Java

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Used to provide an example that exercises most of the functionality of the
* java.util.Calendar Class
* @author Vivek Venkatesh G
* @version
*/
public class CalendarExample {

/**
* Calendar's getTime() method returns a Date Object
* This can then be passed to println() to print today's date (and time) in the traditional format
* @param No parameter for this function
* @return No return value for this function
*/
public static void doCalendarTimeExample()
{
System.out.println("CURRENT DATE/TIME\n\n");
Date now = Calendar.getInstance().getTime();
System.out.println("Calendar.getInstance().getTime():"+ now);
System.out.println();
}

/**
* Simple Date Format from java.text.package
* @param for this no parameter required
*/

public static void doSimpleDateFormat()
{
System.out.println("Simple Date Format \n\n");
// Get Today's Date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(" It is now :"+ formatter.format(now.getTime()));
System.out.println();
}

/**
* Simple Date difference from java.text.Package
*/
public static void doDataDifference()
{
System.out.println("DIFFERENCE BETWEEN TWO DATES\n\n");
Date startDate1 = new GregorianCalendar(1994,02,14,14,00).getTime();
Date endDate1 = new Date();
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println("Difference between " + endDate1);
System.out.println(" and "+ startDate1 + "is" + (diff/(1000L*60L*60L*24L)) + "days");
System.out.println();
}

public static void doGetMethods()
{
System.out.println("CALENDAR GET METHODS");
Calendar c=Calendar.getInstance();

System.out.println("YEAR : " + c.get(Calendar.YEAR));
System.out.println("MONTH : " + c.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
System.out.println("DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("HOUR : " + c.get(Calendar.HOUR));
System.out.println("AM_PM : " + c.get(Calendar.AM_PM));
System.out.println("HOUR OF DAY(24-hour) : " + c.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE : " + c.get(Calendar.MINUTE));
System.out.println("SECOND : " + c.get(Calendar.SECOND));
System.out.println();

}

public static void main(String[] args)
{
System.out.println();
doCalendarTimeExample();
doSimpleDateFormat();
doDataDifference();
doGetMethods();
}
}


Download the Source Code