Announcement Announcement Module
Collapse
No announcement yet.
Implementing Aspects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Implementing Aspects

    Hello,

    Im new to aspect programming and i have created a program that i would like to add some aspects to. What im looking for is some guidence on where i might be able to add some interesting or useful aspects into my code. I've been looking at some of the basic stuff like tracing and advice so im thinking some of this and maybe a few more complex implementations if at all possible. But im having trouble seeing where i could add them to my code.

    My program is a sim of a 100 metre sprint from the first round to the last with heats added to each round.

    Any advice would be mightly helpful

    Code:
    public class RaceSim {
    
    	public static void main(String[] args) {
    		
    		
    
            //Creating 8 heats
    		Heat heat1Round1 = new Heat1();
    		Heat heat2Round1 = new Heat2();
    		Heat heat3Round1 = new Heat3();
    		Heat heat4Round1 = new Heat4();
    		Heat heat5Round1 = new Heat5();
    		Heat heat6Round1 = new Heat6();
    		Heat heat7Round1 = new Heat7();
    		Heat heat8Round1 = new Heat8();
    		
    		
    		  Runner[] r = new Runner[65];  
    	     
    		  
            for(int i = 1; i <= 64; i++) {  
            	
                   r[i] = new Runner("firstname "+i, "lastname "+i, 0.00); 
              
    		 }  
    
    		
            int count=1;
            
            for(int i=1; i<=64;i++){
            	
            	if(count<=8){
            		heat1Round1.addRunner(r[i]);
            	}
            	else if(count<=16){
            		
            		
            		heat2Round1.addRunner(r[i]);
            		
            	}else if(count<=24){
            		
            		heat3Round1.addRunner(r[i]);
            		
            	}else if(count<=32){
            		
            	
            		heat4Round1.addRunner(r[i]);
            		
            	}else if(count<=40){
            		
            		heat5Round1.addRunner(r[i]);
            		
            	}else if(count<=48){
            		
            		heat6Round1.addRunner(r[i]);
            		
            	}else if(count<=56){
            		
            		heat7Round1.addRunner(r[i]);
            		
            	}else {
            		
            		heat8Round1.addRunner(r[i]);
            		
            	}
            	
            	
            	
            		count+=1;
             }
        	Round round1 = new Round1();
        	
        		
        	round1.addHeat(heat1Round1);
    		round1.addHeat(heat2Round1);
    		round1.addHeat(heat3Round1);
    		round1.addHeat(heat4Round1);
    		round1.addHeat(heat5Round1);
    		round1.addHeat(heat6Round1);
    		round1.addHeat(heat7Round1);
    		round1.addHeat(heat8Round1);
        	round1.displayRunners();   
    	
    		heat1Round1.runRace();
    		heat2Round1.runRace();
    		heat3Round1.runRace();
    		heat4Round1.runRace();
    		heat5Round1.runRace();
    		heat6Round1.runRace();
    		heat7Round1.runRace();
    		heat8Round1.runRace();
    				
    		//Finding winners for heats
    		heat1Round1.getWinnersFromHeat();
    		heat2Round1.getWinnersFromHeat();
    		heat3Round1.getWinnersFromHeat();
    		heat4Round1.getWinnersFromHeat();
    		heat5Round1.getWinnersFromHeat();
    		heat6Round1.getWinnersFromHeat();
    		heat7Round1.getWinnersFromHeat();
    		heat8Round1.getWinnersFromHeat();
    		
    	    round1.displayHeats();
    		
    
    		
    		//creating heats for round2
    		Heat heat1Round2 =new Heat1();
    		Heat heat2Round2 =new Heat2();
    		Heat heat3Round2 =new Heat3();
    		Heat heat4Round2 =new Heat4();
    		//getting previous winners form heats from round 1	
    		heat1Round2.getWinnersFromPreviousRound(heat1Round1);
    		heat1Round2.getWinnersFromPreviousRound(heat2Round1);
    		heat2Round2.getWinnersFromPreviousRound(heat3Round1);
    		heat2Round2.getWinnersFromPreviousRound(heat4Round1);
    		heat3Round2.getWinnersFromPreviousRound(heat5Round1);
    		heat3Round2.getWinnersFromPreviousRound(heat6Round1);
    		heat4Round2.getWinnersFromPreviousRound(heat7Round1);
    		heat4Round2.getWinnersFromPreviousRound(heat8Round1);
    		
    		//running race with combined winners of first heats from round 1
    		heat1Round2.runRace();
    		heat2Round2.runRace();
    		heat3Round2.runRace();
    		heat4Round2.runRace();
    		
    		//getting winners
    		heat1Round2.getWinnersFromHeat();
    		heat2Round2.getWinnersFromHeat();
    		heat3Round2.getWinnersFromHeat();
    		heat4Round2.getWinnersFromHeat();
    		
    		//creating round 2
    		Round round2 = new Round2();
    		//adding heat 1 to round 2
    		round2.addHeat(heat1Round2);
    		round2.addHeat(heat2Round2);
    		round2.addHeat(heat3Round2);
    		round2.addHeat(heat4Round2);
    		//displaying heat 1 round 2 
    		round2.displayHeats();
    
    		Heat semiFinal1 =new Heat1();
    		Heat semiFinal2 =new Heat2();
    		
    		semiFinal1.getWinnersFromPreviousRound(heat1Round2);
    		semiFinal1.getWinnersFromPreviousRound(heat2Round2);
    		semiFinal2.getWinnersFromPreviousRound(heat3Round2);
    		semiFinal2.getWinnersFromPreviousRound(heat4Round2);
    	
    		semiFinal1.runRace();
    		semiFinal2.runRace();
    		
    		semiFinal1.getWinnersFromHeat();
    		semiFinal2.getWinnersFromHeat();
    		
    		Round semiFinal = new semiFinal();
    	
    		semiFinal.addHeat(semiFinal1);
    		semiFinal.addHeat(semiFinal2);
    		
    		
    		semiFinal.displayHeats();
    		
    		
    		Heat Final =new Heat1();
    	
    		
    		Final.getWinnersFromPreviousRound(semiFinal1);
    		Final.getWinnersFromPreviousRound(semiFinal2);
    		
    		Final.runRace();
    		
    		
    		Final.getWinnersFromFinal();
    		
    		Round round4 = new Round4();
    		
    		round4.addHeat(Final);
    				
    		round4.displayHeats();
    		
    		}
    }
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    
    public abstract class Round {
    
    	private ArrayList heats = new ArrayList();
    
    	protected String name;
    
    	public String getName() {
    		return name;
    	}
    	
    	public Round() {}
    	
    	public void addHeat(Heat heat) {
    		heats.add(heat);
    	}
    
    	public String displayHeats(){
    		
    		Iterator iter =heats.iterator();
    		
    		String result="";
    		
    		while(iter.hasNext()){
    			
    			Heat aHeat=(Heat)iter.next();
    			
    		
    			System.out.println(aHeat.name +this.name +"\n"+ aHeat.getRunners().toString());
    
    
    			System.out.println(aHeat.name+this.name +"\nWinners"+ aHeat.getWinners());
    		}
    		return result;
    	}
    	
    public String displayRunners(){
    		
    		Iterator iter =heats.iterator();
    		
    		String result="";
    		
    		while(iter.hasNext()){
    			
    			Heat aHeat=(Heat)iter.next();
    			
    		
    			System.out.println(aHeat.name +this.name +"\n"+ aHeat.getRunners().toString());
    		}
    		return result;
    	}
    
    
    	
    	public String toString(){
    		return name;
    	}
    }
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.Random;
    
    public abstract class Heat {
    
    	private ArrayList runners = new ArrayList();
    	private  ArrayList  winners = new ArrayList();
    	
    	
    	public  ArrayList getRunners(){
    		
    		return runners;
    	}
    	public ArrayList getWinners(){
    		
    		return winners;
    	}
    	
    	protected  String name;
    	
    	public String getName(){
    		
    		return name;
    	}
    
    	public Heat(){}
    		
    	public void addRunner(Runner runner){
    		
    		runners.add(runner);
    	}
    		
     	public String toString(){
    		return name;
    	}
    	
    	public static double createRandomTime() {
    		Random ranGen = new Random();
    		return ranGen.nextDouble() + 9;
    	}
    	
    	public void runRace() {
    		Iterator iter = runners.iterator();
    		
    		while(iter.hasNext()){
    		
    			Runner aRunner =(Runner)iter.next();
    			
    			aRunner.setTime(createRandomTime());
    		}
    	}
    	
    public void getWinnersFromHeat(){
    				
    		int count=0;
    		Collections.sort(runners);
    		
    		Iterator iter = runners.iterator();
                 while(iter.hasNext()){
                	 Runner  aRunner= (Runner)iter.next();
                	 
                	 if(count!=4){
                	 winners.add(aRunner);
                	 }else
                	 {
                		 break;
                	 }
    	count+=1;
    }
    }             
                 
    public void getWinnersFromFinal(){
     				
        int count=0;
        Collections.sort(runners);
             		
        Iterator iter = runners.iterator();
                while(iter.hasNext()){
                    Runner  aRunner= (Runner)iter.next();
                         	 
                    if(count!=1){
                       winners.add(aRunner);
                     }else
                     {
                      break;
                     }
      count+=1;
             }             
    }
    public void getWinnersFromPreviousRound(Heat oldHeat){
    		
    	this.addRunner((Runner) oldHeat.getWinners().get(0));
    	this.addRunner((Runner) oldHeat.getWinners().get(1));
    	this.addRunner((Runner) oldHeat.getWinners().get(2));
    	this.addRunner((Runner) oldHeat.getWinners().get(3));
    	
    }
    	
    }
    public class Runner  implements Comparable{
    	
    	    private String fName;
    	    private String sName;
    		private double time;
    	 
    	    public Runner(){
    	    	
    	    }
    	    
    	    public Runner(String fName, String sName, double time){
    	        this.fName = fName;
    	        this.sName = sName;
    	        this.time = time;
    	    }
    	 
    	    public double getTime(){
    	    	return time;
    	    }
    	 
    	    public void setTime(double time){
    	    	
    	    	
    	    	double aTime =(double)time*100;
    	    	
    	    	aTime =(int)aTime;
    	    	aTime=aTime/100;
    	    	this.time= aTime;
    	   
    	    	
    	    }
    
    		public String getfName() {
    			
    			return fName;
    		}
    		
    	public String getsName() {
    			
    			return sName;
    		}
    
    	
    public String toString(){
    	
    	return fName+" "+sName+" "+time;
    }
    	
    	 public int compareTo(Object obj) {
    			// TODO Auto-generated method stub
    	    	
    	    	Runner r = (Runner)obj;
    	    	
    	    	
    	    	if(this.time<r.getTime()){
    	    		return 0;
    	    	}else if(this.time>r.getTime()){
    	    		
    	    		return 1;
    	    	}else{
    	    		
    	    		return -1;
    	    	}
    	    	
    		}
    	   
    }
    public final class Heat1 extends Heat{
    
    	public Heat1() {
    		this.name="Heat1";
    		// TODO Auto-generated constructor stub
    	}
    
    }
    public final class Round1 extends Round{
    
    	
    	public Round1(){
    		
    		this.name="Round1";
    	}
    	
    	
    }

  • #2
    As you are more aware of the functionality, your application is supposed to provide, I would recommend reading some theory stuff about under which circumstances aspects pay off:

    http://en.wikipedia.org/wiki/Aspect-...basic_concepts

    Comment

    Working...
    X