GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Apr-2008, 19:09
dinyar dinyar is offline
New Member
 
Join Date: Apr 2008
Posts: 1
dinyar is on a distinguished road

I have a program that I wrote in Java and need it converted to C++. It is a Go Back N


JAVA Code:
package simulator;
 
import java.util.Date;
 
 
public class Event {
Date eventTime;
int eventType;
int eventity;
Packet packet; 
public String toString ()
{
     if(packet!=null)
     {
         return "eventTime: "+eventTime+"| eventType: "+    eventType+ "| eventity: "+eventity+"| Packet : "+packet.toString();
     }
     else{
         return "eventTime: "+eventTime+"| eventType: "+ eventType+ "| eventity: "+eventity;
     }
 
 
}
    public int getEventity() {
        return eventity;
    }
    public void setEventity(int eventity) {
        this.eventity = eventity;
    }
    public Date getEventTime() {
        return eventTime;
    }
    public void setEventTime(Date eventTime) {
        this.eventTime = eventTime;
    }
    public int getEventType() {
        return eventType;
    }
    public void setEventType(int eventType) {
        this.eventType = eventType;
    }
    public Packet getPacket() {
        return packet;
    }
    public void setPacket(Packet packet) {
        this.packet = packet;
    }
}
Event.java
/*
* Created on Oct 23, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package simulator;
 
 
public class Message {
public char[] data= new char[20];
public String toString()
{
     return (new String (data));
}
}
Message.java
/*
* Created on Oct 23, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package simulator;
 
import java.io.Serializable;
 
public class Packet implements Cloneable, Serializable {
    private int seqNum;
    private int ackNum;
    private int checkSum;
    public char[] payload = new char[20];
    public int getAckNum() {
        return ackNum;
    }
    public void setAckNum(int ackNum) {
        this.ackNum = ackNum;
    }
    public int getCheckSum() {
        return checkSum;
    }
    public void setCheckSum(int checkSum) {
        this.checkSum = checkSum;
    }
    public int getSeqNum() {
        return seqNum;
    }
    public void setSeqNum(int seqNum) {
        this.seqNum = seqNum;
    }
    public int calcCheckSum()
    {
        return 100;
 
 
    }
    public Object clone() {
 
 
            Cloneable packet = new Packet();
 
        return packet;
    }
    public String toString()
    {
        return " seqNum: "+seqNum+"| ackNum: "+ackNum+"| checkSum: "+checkSum + "| data : " + new String(payload);
    }
 
}
Packet.java
 
/*
* Created on Oct 23, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package simulator;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
 
 
public class Simulator {
    public final static boolean BIDIRECTIONAL = false;
    public final static int TIMER_INTERRUPT = 0;
    public final static int FROM_LAYER5 = 1;
    public final static int FROM_LAYER3 = 2;
    public final static int OFF = 0;
    public final static int ON = 1;
    public final static int A = 0;
    public final static int B = 1;
 
    private ArrayList eventList = null;
    private Event event = null;
 
    private int traceLevel =1;
    private int nSim=0;
    private int nSimMax =0;
    private Date time;
    private double lossProbability;
    private double corruptProbability;
    private double lambda;
    private int nToLayer3;
    private int nLost;
    private int nCorrupt;
 
    public Simulator(){
        eventList = new ArrayList();
    }
 
    public Event getNextEvent(){
        Event tmpEvent=null;
        Iterator iterator = eventList.iterator();
        if (iterator.hasNext()){
            tmpEvent =(Event) iterator.next();
            eventList.remove(tmpEvent);
            return tmpEvent;
        }
        return null;
    }
 
    public void removeEvent(){
 
    }
 
    public void printEvent(Event event){
        System.out.println("The Event is :"+ event.toString());
 
 
    }
 
    public void printMessage(Message msg){
        System.out.println("The message is :"+msg.toString());
 
    }
 
    public void printPacket(Packet pkt){
 
        System.out.println("The packet is :"+pkt.toString());
 
    }
 
    private void aInit(){}
    private void bInit(){}
 
    private void aOutput(Message msg){
        //String msgStr = "Simulator is working";
    //    for(int i=0;i<msgStr.length();i++) msg.data[i]=msgStr.charAt(i);
        Packet packet = new Packet();
        packet.setSeqNum(1);
        packet.setCheckSum(packet.calcCheckSum());
        packet.setAckNum(1);
        packet.payload = msg.data;
        toLayer3(A, packet);
    }
 
    private void bInput(Packet packet){
        System.out.println("Student.bInput() : =>");
        printPacket(packet);
    }
    private void bOutput(Message msg){}
 
    private void aInput(Packet packet){}
 
private void addEvent(Event event){
     if (getTraceLevel() > 2){
          System.out.println("Simulator.addEvent() =>");
          printEvent(event);
     }
        Iterator itr = eventList.iterator();
        Event tmpEvent = null;
        int idx =0;
         while (itr.hasNext()){
             tmpEvent = (Event) itr.next();
             if (event.getEventTime().getTime() < tmpEvent.getEventTime().getTime()){
                 idx=eventList.indexOf(tmpEvent);
                 break;
             }
         }     
          eventList.add(idx,event);
          //printEventList();
    }
    private void aTimerInterrupt(){}
    private void bTimerInterrupt(){}
 
    private void fatalError(){}
 
    private double getJimRand(){    
        Random r = new Random();
         return r.nextDouble();
    }
 
    private void genNextArrival(){
        long x;
        Event Oevent=null;
        if (this.getTraceLevel()>2)
         System.out.println(" GENERATE NEXT ARRIVAL: creating new arrival\n");
        x = (new Double(this.getLambda()*this.getJimRand()*2)).longValue(); 
        Oevent= new Event();
 
        //ISSUE
        Oevent.setEventTime(new Date(this.getTime().getTime()+x));
 
        Oevent.setEventType(this.FROM_LAYER5);
        Oevent.setEventity(this.A);
 
        addEvent(Oevent);
    }
    private void printEventList(){
         System.out.println("Simulator.printEventList() => ");
        Iterator itr = eventList.iterator();
        Event tmpEvent = null;
         while (itr.hasNext()){
             tmpEvent = (Event) itr.next();
             printEvent(tmpEvent);
         }
    }
 
    private void startTimer(int aORb, int increment){
        if (getTraceLevel() >2){
            System.out.println("Simulator.startTimer() => starting timer at " + time.getTime());
        }
        Iterator itr = eventList.iterator();
        Event tmpEvent = null;
         while (itr.hasNext()){
             tmpEvent = (Event) itr.next();
             if (tmpEvent.getEventity() == aORb && tmpEvent.getEventType() == TIMER_INTERRUPT){
                System.out.println("Simulator.startTimer() => WARN: Attempt to start the timer that has already started. ");
                 if (getTraceLevel() >2){
                    printEvent(tmpEvent);
                }
                 return;
             }
         }
        Event event = new Event();
        event.setEventity(aORb);
        event.setEventTime(new Date(time.getTime() + (long) increment));
        event.setEventType(TIMER_INTERRUPT);
        addEvent(event);
    }
    private void stopTimer(int aORb){
        if (getTraceLevel() >2){
            System.out.println("Simulator.stopTimer() => stopping timer at " + time.getTime());
        }
        Iterator itr = eventList.iterator();
        Event tmpEvent = null;
         while (itr.hasNext()){
             tmpEvent = (Event) itr.next();
             if (tmpEvent.getEventity() == aORb && tmpEvent.getEventType() == TIMER_INTERRUPT){
                  eventList.remove(tmpEvent);
                 if (getTraceLevel() >2){
                    System.out.println("Simulator.stopTimer() => removed interrupt event ");
                    printEvent(tmpEvent);
                }
                 return;
             }
         }
         System.out.println("Unable to stop the timer. It was't running!!!");
    }
 
 
    private void toLayer3(int aORb, Packet packet){
 
         if ( getTraceLevel() > 2){
                 System.out.println("Simulator.toLayer3() => Entered :");
                 printPacket(packet);
         }
 
         nToLayer3++;
 
         if (loose()) return;
 
         corrupt(packet);
 
         // Create Event
         Event event = new Event();
         event.setEventity((aORb+1) %2);
         event.eventType = FROM_LAYER3;
         event.setPacket(packet);
         Date lastTime = new Date( getEventLastTime(FROM_LAYER3, event.getEventity()).getTime() + 1 + (long)(9* getJimRand()) );
         event.setEventTime(lastTime);
 
         if ( getTraceLevel() > 2){
                 System.out.println("Simulator.toLayer3() => Exited :");
                 printPacket(packet);
         }
 
         addEvent(event);
        }
 
        private Date getEventLastTime(int layer, int eventity){
          Event tmpEvent = null;
          Date lastTime = time;
            Iterator itr = eventList.iterator();
             while (itr.hasNext()){
                 tmpEvent = (Event) itr.next();
                 if (tmpEvent.getEventType() == FROM_LAYER3 && tmpEvent.getEventity() == event.getEventity())
                     lastTime = tmpEvent.getEventTime();
             }
     return lastTime;
        }
 
    private void toLayer5(int aORb, char[] data){}
 
    private boolean loose(){
         if (getJimRand() < getLossProbability()) {
             nLost++;
             if (getTraceLevel() > 0){
                 System.out.println("Simulator.toLayer3() => packet being lost");
             }
             return true;
         }
         return false;
        }
 
        private void corrupt(Packet packet){
                double jRand = getJimRand();
             if (getJimRand() < getCorruptProbability()) {
                 nCorrupt++;
                 if ( jRand < .75){
                  packet.payload[0]='Z';
                 } else if ( jRand < .875){
                     packet.setSeqNum(9999);
                 } else {
                     packet.setCheckSum(99999);
                 }
                 if (getTraceLevel() >2){
                     System.out.println("Simulator.toLayer3() =>: packet is being corrupted");
                     printPacket(packet);
                 }
             }
        }
 
    private void init(){
        this.setNSimMax(2);
        this.setLossProbability(0.5);
        this.setCorruptProbability(0.0);
        this.setLambda(0.2);
        this.setTraceLevel(3);
 
        nToLayer3 = 0;
        nLost = 0;
        nCorrupt = 0;
        time=new Date();
         // time=0.0; /* initialize time to 0.0 */
        genNextArrival(); 
    }
    public void run(){
        Date time = null;
        int i,j;
        Message msg2give = new Message();
        Packet pkt2give = new Packet();
 
        init();
        aInit();
        bInit();
        while (((event = getNextEvent()) != null) ){        // runs in a infinite loop
     System.out.println("nSim is :"+nSim);
            if (getTraceLevel() > 0) {
             System.out.println("MAINLOOP: ");
         printEvent(event);
     }
 
            time = event.getEventTime();
 
        switch(event.getEventType())    {
         case FROM_LAYER5:
 
                j = nSim % 26;
 
                for (i=0;i<20;i++){
                    msg2give.data[i] = (char) (j+97);
                }
 
         if (getTraceLevel() > 0) {
             System.out.println(" MAINLOOP : data give to student :");
             printMessage(msg2give);
         }
                nSim++;
                if (nSim<nSimMax)
                {
                    genNextArrival();
                } 
 
                if (event.getEventity() == A)
                    aOutput(msg2give);
                else
                    bOutput(msg2give);
 
                break;
         case FROM_LAYER3:
         pkt2give = event.getPacket();
                if (event.getEventity() == A)
                    aInput(pkt2give);
                else
                    bInput(pkt2give);
 
                 break;
 
         case TIMER_INTERRUPT:
             if (event.getEventity() == A)
                aTimerInterrupt();
            else
                bTimerInterrupt();
                 break;
 
         default :
                fatalError();
                 break;
            }
 
        }
 
    terminate:
        System.out.println("Simulator terminated !");
    }
 
    public double getCorruptProbability() {
        return corruptProbability;
    }
    public void setCorruptProbability(double corruptProbability) {
        this.corruptProbability = corruptProbability;
    }
    public double getLambda() {
        return lambda;
    }
    public void setLambda(double lambda) {
        this.lambda = lambda;
    }
    public double getLossProbability() {
        return lossProbability;
    }
    public void setLossProbability(double lossProbability) {
        this.lossProbability = lossProbability;
    }
    public int getNSimMax() {
        return nSimMax;
    }
    public void setNSimMax(int nsimmax) {
        this.nSimMax = nsimmax;
    }
    public Date getTime() {
        return time;
    }
    public void setTime(Date time) {
        this.time = time;
    }
 
    public int getTraceLevel() {
        return traceLevel;
    }
    public void setTraceLevel(int traceLevel) {
        this.traceLevel = traceLevel;
    }
}
Simulator.java
 
* Created on Oct 23, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;
 
import simulator.Simulator;
 
 
public class TestSimulator {
 
    public static void main(String[] args) {
        Simulator sim= new Simulator();
        sim.run();
    }
}
Attached Files
File Type: doc Programming Assignment.doc (51.0 KB, 27 views)
Last edited by admin II : 05-Apr-2008 at 04:17. Reason: Please surround your Java code with [java] your code [/java]
  #2  
Old 05-Apr-2008, 09:19
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 331
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: I have a program that I wrote in Java and need it converted to C++. It is a Go Back N


That's nice. Read the guidelines.
__________________
www.blake-foster.com
  #3  
Old 05-Apr-2008, 09:44
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: I have a program that I wrote in Java and need it converted to C++. It is a Go Ba


CPP / C++ / C Code:
#include <cstdlib>
int main () {
   char * compile_cmd = "javac *.java"; // <-- command to compile
   char * run_cmd = "run the thing"; // <-- command to run the thing
   system (compile_cmd);
   system (run_cmd);
   return 0;
}
Obviously I'm being sarcastic. However, have you even started to do this? Or are you looking for someone to spend several hours of their time (without pay) to do this for you?
Realize that we will help as much as is necessary - but this is not a free code resource. Return is directly proportional to effort put forth from your end.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:37.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.