Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: OOP

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default OOP

    Just need help getting this class started if anyone can help me out please

    • Object NaiveWorld that inherits from World:
    o Attributes: no more than in the parent class World
    o Methods:
     Constructors
     Accessors / mutators (inherits from parent class World)
     naiveEvolutionIndividual: takes a NaiveWorld and the location of an individual in this NaiveWorld (a pair (i,j)) and returns an Individual:
    What this method does is that:
    • If the sum S of levels of gentleness of the individual at (i,j) + of all its surrounding individuals is positive and the individual at (i,j), say P, is bad (i.e., gentleness < 0), then P becomes good (i.e., its gentleness level is now = S);
    • If the sum S of levels of gentleness of the individual at (i,j) + of all its surrounding individuals is negative and the individual at (i,j), say P, is good (i.e., gentleness > 0), then P becomes bad (i.e., its gentleness level is now = S);
    • Otherwise, P, the individual at (i,j), remains unchanged.
    Note: there can be between 3 – case of a corner, and 6 – case of an internal individual – surrounding individuals.
     naïveEvolutionRC: this method takes a NaiveWorld, NW, and an integer, r, (the number of times the evolution is going to repeat), and does the following:
    • For r repeats:
    o For all rows
     For each element of the current row
    • Calls naiveEvolutionIndividual and NW is now the current NW in which the current individual is the result of naiveEvolutionIndividual
    Note: this method ranges on rows first and then columns, hence the RC part of its name.
     naïveEvolutionCR: this method takes a NaiveWorld, NW, and an integer, r, (the number of times the evolution is going to repeat), and does the following:
    • For r repeats:
    o For all columns
     For each element of the current column
    • Calls naiveEvolutionIndividual and NW is now the current NW in which the current individual is the result of naiveEvolutionIndividual
    Note: this method ranges on columns first and then rows, hence the CR part of its name


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: OOP

    This will get you started:
    public class NaiveWorld extends World
    {
         // your code goes here
    }

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: OOP

    I started that and thats all I have i dont know what to put in for the code.
    this is the class world
    import java.io.*;
     
    public class World
    {
    	private int size;
    	private Individual[][] myWorld;
    	private Random numGen = new Random();
     
    	/**
    	* This is the default constructor that creates a World of one element holding one default Individual.
     	*
    	* @since	1.6
     	*/
    	public World()
    	{	
    		this.size = 1;
    		this.myWorld = new Individual[1][1];
    		for( i = 0; i < myWorld.length, i++)
    		{
    			for( j = 0, j < myWorld[0].length, j++)
    			{
    				this.myWorld[i][j] = Individual()
    			}
    		}
    	}//end constructor
     
    	/**
    	* This constructor creates a World to the given size filled with the given amount of good Individuals, or Individuals with a postive gentleness. 
    	* The remaining spaces left in the world are filled with bad Individuals, or Individuals with a negative gentleness.
     	*
    	* @param	n is the size of the world to create.
    	* @param	g is the number of good Individuals to put into the world.
    	* @since	1.6
     	*/
    	public World(int n, int g)
    	{
    		this.size = n;
    		this.myWorld = new Individual[sqrt(n)][sqrt(n)];
    		int b = (n^2) - g;
     
    		for(i = 0, i < g, i++)
    		{	
    			Individual goodGuy = new Indivdual(numGen.nextInt(100), numGen.nextInt(100));
    			myWorld[numGen.nextInt(sqrt(n))][numGen.nextInt(sqrt(n))] = goodGuy;
    		}
    		for(j = 0, j < b, j++)	
    		{
    			Individual badGuy = new Individual(numGen.nextInt(100), (numGen.nextInt(100) * -1));
    			myWorld[numGen.nextInt(sqrt(n))][numGen.nextInt(sqrt(n))] = badGuy;
    		}
    	}//end constructor
     
    	/**
    	* This constructor creates a World filled with Individuals based on the data in the given file.
     	*
    	* @param	fileName is the file to gather the information about the world to be created.
    	* @since	1.6
     	*/
    	public World(String fileName)
    	{
    		Scanner inFile = new Scanner(new FileReader("fileName"));
     
    		this.size = inFile.nextInt();
     
    		this.myWorld = new Individual[sqrt(this.size)][sqrt(this.size)];
     
    			for(i = 0, myWorld.length, i++)
    			{
    				for(j=0, myWorld[0].length, j++)
    				{
    					myWorld[i][j] = new Individual(numGen.nextInt(100), inFIle.next());
    				}
    			}
    		inFile.close()
    	}//end constructor
     
    	/**
    	* This accessor method returns the current size of the World.
     	*
    	* @since	1.6
     	*/	 
    	public void getSize()
    	{
    		return this.size;
    	}
    	end accessor
     
    	/**
    	* This accessor method returns a textual representation of the World.
     	*
    	* @since	1.6
     	*/
    	public void printWorld()
    	{
    		int counter = 0;
     
    		for(i = 0, i < sqrt(this.size), i++)
    		{
    			for (j = 0, j < sqrt(this.size), j++)
    			{
    				myWorld[i][j].printIndividual();
    				System.out.print(" ");
    				counter++;
    				if(counter > sqrt(this.size))
    				{
    					System.out.println();
    					counter = 0;
    				}
    	}//end method
     
    	/**
    	* This method returns the current number of good Individuals in the world.
     	*
    	* @since	1.6
     	*/
    	public int countsGood()
    	{
    		int goodCount = 0;
     
    		for(i = 0, i < sqrt(this.size), i++)
    		{
    			for (j = 0, j < sqrt(this.size), j++)
    			{
    				if(myWorld[i][j] > 0)
    					goodCount++;
    			}
    		return goodCount;
    	}//end method
     
    	/**
    	* This method returns the current number of bad Individuals in the world.
     	*
    	* @since	1.6
     	*/
    	public int countsBad()
    	{
    		int bad count = 0;
     
    		for(i = 0, i < sqrt(this.size), i++)
    		{
    			for (j = 0, j < sqrt(this.size), j++)
    			{
    				if(myWorld[i][j] < 0)
    					badCount++;
    			}
    		return badCount;
    	}//end method
    }//end World object