array of objects

trouble modifying data in an array of aobjects

Page 1 of 1

3 Replies - 1781 Views - Last Post: 27 November 2007 - 11:36 AM Rate Topic: -----

#1 saxontrooper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 07

array of objects

Post icon  Posted 25 November 2007 - 01:50 PM

Evening everyone,
I am having trouble getting the syntax down for adding data to and getting data from an array of objects.
I am focusing on the addStation methods at the moment, but I am getting confused, when I look at the write up for the assignment, it says array of objects, yet the commands in the StationsTest look like those for ArrayList.
I really would appreciate some guidance on what I am missing. I am also getting a null pointer exception, with ref... to missing exception msg. But when I added the import.java.io.IOException, and throws exception it didn't go away.


public class Stations
{
	// array size (and limit)
	private final static int MAX_ENTRIES = 10;
	
	// instance variables
	
	// array of Station objects
	
		private Station [] stationArray;

	// next available array element position

	private int nextPosition;
	
	/**
	 * Constructor for objects of class Stations.
	 */
	public Stations()
	{
		/* Initialize array and next position instance fields */

		Station [] stationArray = new Station [10]; // array with 10 indexes
		this.nextPosition = 0; // position instance to zero

	}
 
	/**
	 * addStation - add a Station to the array of Stations.
	 * 
	 * @param  inStation  Station object to be added
	 * @return  int  index of added store,
	 *	-1 if unable to add (array already full)
	 */
	public int addStation(Station inStation)
	{
		if (this.nextPosition >= MAX_ENTRIES)
		{
			return -1;
		}
		// Create a copy of the passed in Station object so we are not
		// storing the reference to that object in our array

		Station tmp = new Station();
		stationArray[nextPosition] = new Station();
		nextPosition++;

		/* Add object to the array using nextPosition.  Return array
		   position but remember that nextPosition must be incremented
		   BEFORE leaving method.  Hint: consider using a post-increment. */
			
		return nextPosition -1;
	}

	/**
	 * addStation - add a Station to the array of Stations.
	 * 
	 * @param  inLocation  Location  Location information
	 * @param  inInUse  boolean  Flag to tell if station in use
	 * @param  inConnectorType  String  Denotes connector type
	 * @param  inPowerAvailable  double  Power capacity
	 * @return  int  index of added store,
	 *	-1 if unable to add (array already full)
	 */
	public int addStation(Location inLocation, boolean inInUse,
		String inConnectorType, double inPowerAvailable)
	{
		if (this.nextPosition >= MAX_ENTRIES)
		{
			return -1;
		}
		// Create a Station object from the passed-in attributes

		Station tmp = new Station();
		stationArray[nextPosition] = tmp;
		nextPosition++;	

		/* Add object to the array using nextPosition.  Return array
		   position but remember that nextPosition must be incremented
		   BEFORE leaving method.  Hint: consider using a post-increment.
				*/
			
		return nextPosition -1;
	}
	
// I am going to attempt this once I figure out the addmethod problem :) 


	/** getStation - find and return Station object. 
	 * 
	 * @param  inIndex  int  Position of object in array 
	 * 
	 * @return  Station  if index valid, returns Station object,
	 *	if not, returns null reference
	 */
	public Station getStation(int inIndex)
	{
		  
		/* Make sure passed in index is within a valid range */
		if (this.nextPosition >= MAX_ENTRIES)
		{
			return null;
		}
			// Create a copy of the Station object so we are not passing back
			// a reference to that object

			Station tmp = null;
			
			//{	
				return tmp;
			//}
		//}		

		// we only reach here if index not valid
		//return null;
	}

	/** getStation - find and return Station object.
	 *
	 * @param  inTag  String  Location tag
	 *
	 * @return  Station  if tag found, returns Station object,
	 *	if not, returns null reference
	 */
	public Station getStation(String inTag)
	{
		String theTag = null;
		
		/* Hint: loop through the array and grab a Station
		   object and from that object get the Location,
		   and in turn get the tag.  Compare that to the passed
		   in tag using an appropriate String method. */
		
		//for (
		//{
			//
		
		// we only reach here if not found
		{
			return null;
		}
	}
	
	/**
	 * getNextPosition - get index for next available array element.
	 *
	 * @return  int  next available array element position
	 */
	public int getNextPosition()
	{
		return nextPosition;
	}
	
	
	/** toString - provides String containing object attributes.
	 * 
	 * @return  String  values stored in Station array
	 */
	public String toString()
	{
		String tmp = null;
		tmp = "Stations:\n\n";
		
		/* Hint: loop through the array to build the String, remembering
		   that there is a Station object toString method */
		//for ( 
		
		return tmp;
	}
	
	
}





As always any help is greatly appreciated :D
Below is the code for the tester class that i have so far.


public class StationsTest extends junit.framework.TestCase
{

	// Instance fields
	private Stations stations1;
	private Location tempLocation;
	private Station tempStation;
	
	/**
	 * Default constructor for test class LocatorTest.
	 */
	public StationsTest()
	{
		// not used for this lab
	}

	/**
	 * Sets up the test fixture.
	 *
	 * Called before every test case method.  Creates a
	 * Stations object and populates it with test data.
	 */
	protected void setUp()
	{
		stations1 = new Stations();
		tempLocation = new Location();
		tempStation = new Station();

		// load stations
		// latitude and longitude information from:
		//  http://www.multimap.com
		tempLocation.setLongitude(83.0005);
		tempLocation.setLatitude(39.9621);
		tempLocation.setTag("Broad and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(266.5);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(82.9902);
		tempLocation.setLatitude(39.9586);
		tempLocation.setTag("Grant and Rich");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(false);
		tempStation.setConnectorType("CBL-01");
		tempStation.setPowerAvailable(121.0);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(83.0193);
		tempLocation.setLatitude(40.0625);
		tempLocation.setTag("Morse and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(375.3);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(82.9671);
		tempLocation.setLatitude(40.014);
		tempLocation.setTag("Cleveland and Hudson");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(55.0);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(82.9133);
		tempLocation.setLatitude(39.972);
		tempLocation.setTag("Broad and James");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("QTek");
		tempStation.setPowerAvailable(1024.0);
		stations1.addStation(tempStation);
	}

	/**
	 * Tears down the test fixture.
	 *
	 * Called after every test case method.
	 */
	protected void tearDown()
	{
		// not used for this lab
	}

	/**
	 * Create object.
	 */
	public void testCreateObject()
	{
		/* create and test a local Stations object */

		stations1 = new Stations();
		assertNotNull(stations1);
	}
	
	/**
	 * Add station (to local Stations object).
	 * 
	 * Use overloaded method 1.
	 */
	public void testAddStation()
	{
		/* Hint: create a Stations object.  Create a Stations1 object.
		   Add the Stations object to the Stations1 object and verify
		   that correct element position is returned.  Do twice to
		   determine that postion increments correctly.  Alternatively,
		   you can add to the stations1 Station instance field object. */

		Stations [] listArray = new Stations [10];
		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		assertNotNull(stations1);
	}
	
	/**
	 * Add station (to local Stations object).
	 * 
	 * Use overloaded method 2.
	 */
	public void testAddStation2()
	{
		/* Hint: create a Stations object.  Create a Location object.
		   Add a Station object to the Stations object (by passing in
		   attribute parameters) and verify that correct element position
		   is returned.  Do twice to determine that postion increments
		   correctly.  Alternatively, you can add to the stations1
		   Station instance field object. */

		tempLocation = new Location();
		tempLocation.setLongitude(82.9671);
		tempLocation.setLatitude(40.014);
		tempLocation.setTag("Cleveland and Hudson");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(55.0);
		stations1.addStation(tempStation);
		tempLocation.setLongitude(83.0005);
		tempLocation.setLatitude(39.9621);
		tempLocation.setTag("Broad and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(266.5);
		stations1.addStation(tempStation);
		assertEquals("1", stations1.getNextPosition());
		
	}
	
	/**
	 * Get station (from instance Stations object).
	 * 
	 * Use overloaded method 1.
	 */
	public void testGetStation1()
	{
		/* Hint: get a Station object by passing in a valid index.  Test
		   that an object was created.  Then test that an attribute
		   exists as expected in the object. */
			 
		// To be thorough one would check all six attributes, but for this
		// exercise we will test power available value only
	  
		/* Now test by passing in an invalid index, and verify that an
		   object was NOT created */

		tempLocation = new Location();
		tempLocation.setLongitude(82.9902);
		tempLocation.setLatitude(39.9586);
		tempLocation.setTag("Grant and Rich");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(false);
		tempStation.setConnectorType("CBL-01");
		tempStation.setPowerAvailable(121.0);
		stations1.addStation(tempStation);
		assertNotNull(stations1);
		assertEquals("121.0", stations1.getPowerAvailable());
	}

	/**
	 * Get station (from instance Stations object).
	 * 
	 * Use overloaded method 2.
	 */
	public void testGetStation2()
	{
		/* Hint: get a Station object by passing in a valid tag.  Test
		   that an object was created.  Then test that an attribute
		   exists as expected in the object. */
			 
		// To be thorough one would check all six attributes, but for this
		// exercise we will test power available value only
	  
		/* Now test by passing in an invalid tag, and verify that an
		   object was NOT created */

		tempLocation = new Location();
		tempLocation.setLongitude(83.0193);
		tempLocation.setLatitude(40.0625);
		tempLocation.setTag("Morse and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(375.3);
		stations1.addStation(tempStation);
		assertEquals("375.3", stations1.getPowerAvailable());
	}

	/**
	 * Add station when at max (to instance Stations object).
	 * 
	 * Test overloaded method 1 when array is at max.
	 */
	public void testGetStationMax1()
	{
		/* Hint: create a Station object then verify that it was created.
		   Then loop and add the object to the station1 Stations object
		   until reaching max.  Finally, add Station object one last time,
		   verifying that "maxed out" flag is returned. */

		tempLocation = new Location();
		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		
		// Put array right at max elements
		/*
		for (nextPosition = 1; nextPosition < stations1.length(); i++)
		{
			Stations = stations1.get(i);
			if ( .
		}
		*/

		// Now add one more Station object, should return -1 as index
		//return -1;  
	}
	
	/**
	 * Add station when at max (to instance Stations object).
	 * 
	 * Test overloaded method 2 when array is at max.
	 */
	public void testGetStationMax2()
	{
		/* Hint: create a Station object then verify that it was created.
		   Then loop and add the object to the station1 Stations object
		   until reaching max.  Finally, add Station object one last time
		   (using overloaded method 2 this time), verifying that "maxed out"
		   flag is returned. */

		tempLocation = new Location();
		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		
		// Put array right at max elements
		/*
		for
		{
			
		}
		*/

		// Now add one more Station object, should return -1 as index
		//return -1;
	}
	
	/**
	 * Test get next position.
	 */
	public void testGetNextPosition()
	{
		/* test getNextPosition method for expected value against
		   the station1 Stations instance field object */

		tempLocation = new Location();
		tempLocation.setLongitude(83.0193);
		tempLocation.setLatitude(40.0625);
		tempLocation.setTag("Morse and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(375.3);
		stations1.addStation(tempStation);
		assertEquals(" ", stations1.getNextPosition());
	}
	
	/**
	 * Test toString.
	 */
	public void testToString()
	{ 
		tempLocation = new Location();
		tempLocation.setLongitude(82.9133);
		tempLocation.setLatitude(39.972);
		tempLocation.setTag("Broad and James");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("QTek");
		tempStation.setPowerAvailable(1024.0);
		stations1.addStation(tempStation);
		String s = stations1.toString();

		// To be thorough one would check all six attributes for each Station,
		// but for this exercise we will test power available value only.
		// Note that this assumes no rounding errors for the floating point
		// value, which is not a safe assumption.  More advanced techniques
		// may need to be employed if a test on a FPV in a String fails.
		
		
		/* Use the String contains method to test for existance of correct
		   power available values.  Note that contains will return a boolean
		   true or false. */
		
		assertTrue(stations1.toString().contains("82.9133"));
		assertTrue(stations1.toString().contains("39.972"));
		assertTrue(stations1.toString().contains("Broad and James"));
		assertTrue(stations1.toString().contains("true"));
		assertTrue(stations1.toString().contains("QTek"));
		assertTrue(stations1.toString().contains("1024.0"));
	}
}






Any suggestions will be appreciated, thanks.

This post has been edited by saxontrooper: 25 November 2007 - 01:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: array of objects

#2 hoho13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 07

Re: array of objects

Posted 25 November 2007 - 04:13 PM

View Postsaxontrooper, on 25 Nov, 2007 - 01:50 PM, said:

Evening everyone,
I am having trouble getting the syntax down for adding data to and getting data from an array of objects.
I am focusing on the addStation methods at the moment, but I am getting confused, when I look at the write up for the assignment, it says array of objects, yet the commands in the StationsTest look like those for ArrayList.
I really would appreciate some guidance on what I am missing. I am also getting a null pointer exception, with ref... to missing exception msg. But when I added the import.java.io.IOException, and throws exception it didn't go away.


public class Stations
{
	// array size (and limit)
	private final static int MAX_ENTRIES = 10;
	
	// instance variables
	
	// array of Station objects
	
		private Station [] stationArray;

	// next available array element position

	private int nextPosition;
	
	/**
	 * Constructor for objects of class Stations.
	 */
	public Stations()
	{
		/* Initialize array and next position instance fields */

		Station [] stationArray = new Station [10]; // array with 10 indexes
		this.nextPosition = 0; // position instance to zero

	}
 
	/**
	 * addStation - add a Station to the array of Stations.
	 * 
	 * @param  inStation  Station object to be added
	 * @return  int  index of added store,
	 *	-1 if unable to add (array already full)
	 */
	public int addStation(Station inStation)
	{
		if (this.nextPosition >= MAX_ENTRIES)
		{
			return -1;
		}
		// Create a copy of the passed in Station object so we are not
		// storing the reference to that object in our array

		Station tmp = new Station();
		stationArray[nextPosition] = new Station();
		nextPosition++;

		/* Add object to the array using nextPosition.  Return array
		   position but remember that nextPosition must be incremented
		   BEFORE leaving method.  Hint: consider using a post-increment. */
			
		return nextPosition -1;
	}

	/**
	 * addStation - add a Station to the array of Stations.
	 * 
	 * @param  inLocation  Location  Location information
	 * @param  inInUse  boolean  Flag to tell if station in use
	 * @param  inConnectorType  String  Denotes connector type
	 * @param  inPowerAvailable  double  Power capacity
	 * @return  int  index of added store,
	 *	-1 if unable to add (array already full)
	 */
	public int addStation(Location inLocation, boolean inInUse,
		String inConnectorType, double inPowerAvailable)
	{
		if (this.nextPosition >= MAX_ENTRIES)
		{
			return -1;
		}
		// Create a Station object from the passed-in attributes

		Station tmp = new Station();
		stationArray[nextPosition] = tmp;
		nextPosition++;	

		/* Add object to the array using nextPosition.  Return array
		   position but remember that nextPosition must be incremented
		   BEFORE leaving method.  Hint: consider using a post-increment.
				*/
			
		return nextPosition -1;
	}
	
// I am going to attempt this once I figure out the addmethod problem :) 


	/** getStation - find and return Station object. 
	 * 
	 * @param  inIndex  int  Position of object in array 
	 * 
	 * @return  Station  if index valid, returns Station object,
	 *	if not, returns null reference
	 */
	public Station getStation(int inIndex)
	{
		  
		/* Make sure passed in index is within a valid range */
		if (this.nextPosition >= MAX_ENTRIES)
		{
			return null;
		}
			// Create a copy of the Station object so we are not passing back
			// a reference to that object

			Station tmp = null;
			
			//{	
				return tmp;
			//}
		//}		

		// we only reach here if index not valid
		//return null;
	}

	/** getStation - find and return Station object.
	 *
	 * @param  inTag  String  Location tag
	 *
	 * @return  Station  if tag found, returns Station object,
	 *	if not, returns null reference
	 */
	public Station getStation(String inTag)
	{
		String theTag = null;
		
		/* Hint: loop through the array and grab a Station
		   object and from that object get the Location,
		   and in turn get the tag.  Compare that to the passed
		   in tag using an appropriate String method. */
		
		//for (
		//{
			//
		
		// we only reach here if not found
		{
			return null;
		}
	}
	
	/**
	 * getNextPosition - get index for next available array element.
	 *
	 * @return  int  next available array element position
	 */
	public int getNextPosition()
	{
		return nextPosition;
	}
	
	
	/** toString - provides String containing object attributes.
	 * 
	 * @return  String  values stored in Station array
	 */
	public String toString()
	{
		String tmp = null;
		tmp = "Stations:\n\n";
		
		/* Hint: loop through the array to build the String, remembering
		   that there is a Station object toString method */
		//for ( 
		
		return tmp;
	}
	
	
}





As always any help is greatly appreciated :D
Below is the code for the tester class that i have so far.


public class StationsTest extends junit.framework.TestCase
{

	// Instance fields
	private Stations stations1;
	private Location tempLocation;
	private Station tempStation;
	
	/**
	 * Default constructor for test class LocatorTest.
	 */
	public StationsTest()
	{
		// not used for this lab
	}

	/**
	 * Sets up the test fixture.
	 *
	 * Called before every test case method.  Creates a
	 * Stations object and populates it with test data.
	 */
	protected void setUp()
	{
		stations1 = new Stations();
		tempLocation = new Location();
		tempStation = new Station();

		// load stations
		// latitude and longitude information from:
		//  http://www.multimap.com
		tempLocation.setLongitude(83.0005);
		tempLocation.setLatitude(39.9621);
		tempLocation.setTag("Broad and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(266.5);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(82.9902);
		tempLocation.setLatitude(39.9586);
		tempLocation.setTag("Grant and Rich");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(false);
		tempStation.setConnectorType("CBL-01");
		tempStation.setPowerAvailable(121.0);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(83.0193);
		tempLocation.setLatitude(40.0625);
		tempLocation.setTag("Morse and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(375.3);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(82.9671);
		tempLocation.setLatitude(40.014);
		tempLocation.setTag("Cleveland and Hudson");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(55.0);
		stations1.addStation(tempStation);

		tempLocation = new Location();
		tempLocation.setLongitude(82.9133);
		tempLocation.setLatitude(39.972);
		tempLocation.setTag("Broad and James");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("QTek");
		tempStation.setPowerAvailable(1024.0);
		stations1.addStation(tempStation);
	}

	/**
	 * Tears down the test fixture.
	 *
	 * Called after every test case method.
	 */
	protected void tearDown()
	{
		// not used for this lab
	}

	/**
	 * Create object.
	 */
	public void testCreateObject()
	{
		/* create and test a local Stations object */

		stations1 = new Stations();
		assertNotNull(stations1);
	}
	
	/**
	 * Add station (to local Stations object).
	 * 
	 * Use overloaded method 1.
	 */
	public void testAddStation()
	{
		/* Hint: create a Stations object.  Create a Stations1 object.
		   Add the Stations object to the Stations1 object and verify
		   that correct element position is returned.  Do twice to
		   determine that postion increments correctly.  Alternatively,
		   you can add to the stations1 Station instance field object. */

		Stations [] listArray = new Stations [10];
		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		assertNotNull(stations1);
	}
	
	/**
	 * Add station (to local Stations object).
	 * 
	 * Use overloaded method 2.
	 */
	public void testAddStation2()
	{
		/* Hint: create a Stations object.  Create a Location object.
		   Add a Station object to the Stations object (by passing in
		   attribute parameters) and verify that correct element position
		   is returned.  Do twice to determine that postion increments
		   correctly.  Alternatively, you can add to the stations1
		   Station instance field object. */

		tempLocation = new Location();
		tempLocation.setLongitude(82.9671);
		tempLocation.setLatitude(40.014);
		tempLocation.setTag("Cleveland and Hudson");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(55.0);
		stations1.addStation(tempStation);
		tempLocation.setLongitude(83.0005);
		tempLocation.setLatitude(39.9621);
		tempLocation.setTag("Broad and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(266.5);
		stations1.addStation(tempStation);
		assertEquals("1", stations1.getNextPosition());
		
	}
	
	/**
	 * Get station (from instance Stations object).
	 * 
	 * Use overloaded method 1.
	 */
	public void testGetStation1()
	{
		/* Hint: get a Station object by passing in a valid index.  Test
		   that an object was created.  Then test that an attribute
		   exists as expected in the object. */
			 
		// To be thorough one would check all six attributes, but for this
		// exercise we will test power available value only
	  
		/* Now test by passing in an invalid index, and verify that an
		   object was NOT created */

		tempLocation = new Location();
		tempLocation.setLongitude(82.9902);
		tempLocation.setLatitude(39.9586);
		tempLocation.setTag("Grant and Rich");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(false);
		tempStation.setConnectorType("CBL-01");
		tempStation.setPowerAvailable(121.0);
		stations1.addStation(tempStation);
		assertNotNull(stations1);
		assertEquals("121.0", stations1.getPowerAvailable());
	}

	/**
	 * Get station (from instance Stations object).
	 * 
	 * Use overloaded method 2.
	 */
	public void testGetStation2()
	{
		/* Hint: get a Station object by passing in a valid tag.  Test
		   that an object was created.  Then test that an attribute
		   exists as expected in the object. */
			 
		// To be thorough one would check all six attributes, but for this
		// exercise we will test power available value only
	  
		/* Now test by passing in an invalid tag, and verify that an
		   object was NOT created */

		tempLocation = new Location();
		tempLocation.setLongitude(83.0193);
		tempLocation.setLatitude(40.0625);
		tempLocation.setTag("Morse and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(375.3);
		stations1.addStation(tempStation);
		assertEquals("375.3", stations1.getPowerAvailable());
	}

	/**
	 * Add station when at max (to instance Stations object).
	 * 
	 * Test overloaded method 1 when array is at max.
	 */
	public void testGetStationMax1()
	{
		/* Hint: create a Station object then verify that it was created.
		   Then loop and add the object to the station1 Stations object
		   until reaching max.  Finally, add Station object one last time,
		   verifying that "maxed out" flag is returned. */

		tempLocation = new Location();
		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		
		// Put array right at max elements
		/*
		for (nextPosition = 1; nextPosition < stations1.length(); i++)
		{
			Stations = stations1.get(i);
			if ( .
		}
		*/

		// Now add one more Station object, should return -1 as index
		//return -1;  
	}
	
	/**
	 * Add station when at max (to instance Stations object).
	 * 
	 * Test overloaded method 2 when array is at max.
	 */
	public void testGetStationMax2()
	{
		/* Hint: create a Station object then verify that it was created.
		   Then loop and add the object to the station1 Stations object
		   until reaching max.  Finally, add Station object one last time
		   (using overloaded method 2 this time), verifying that "maxed out"
		   flag is returned. */

		tempLocation = new Location();
		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		
		// Put array right at max elements
		/*
		for
		{
			
		}
		*/

		// Now add one more Station object, should return -1 as index
		//return -1;
	}
	
	/**
	 * Test get next position.
	 */
	public void testGetNextPosition()
	{
		/* test getNextPosition method for expected value against
		   the station1 Stations instance field object */

		tempLocation = new Location();
		tempLocation.setLongitude(83.0193);
		tempLocation.setLatitude(40.0625);
		tempLocation.setTag("Morse and High");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("UNI-32");
		tempStation.setPowerAvailable(375.3);
		stations1.addStation(tempStation);
		assertEquals(" ", stations1.getNextPosition());
	}
	
	/**
	 * Test toString.
	 */
	public void testToString()
	{ 
		tempLocation = new Location();
		tempLocation.setLongitude(82.9133);
		tempLocation.setLatitude(39.972);
		tempLocation.setTag("Broad and James");
		tempStation.setLocation(tempLocation);
		tempStation.setInUse(true);
		tempStation.setConnectorType("QTek");
		tempStation.setPowerAvailable(1024.0);
		stations1.addStation(tempStation);
		String s = stations1.toString();

		// To be thorough one would check all six attributes for each Station,
		// but for this exercise we will test power available value only.
		// Note that this assumes no rounding errors for the floating point
		// value, which is not a safe assumption.  More advanced techniques
		// may need to be employed if a test on a FPV in a String fails.
		
		
		/* Use the String contains method to test for existance of correct
		   power available values.  Note that contains will return a boolean
		   true or false. */
		
		assertTrue(stations1.toString().contains("82.9133"));
		assertTrue(stations1.toString().contains("39.972"));
		assertTrue(stations1.toString().contains("Broad and James"));
		assertTrue(stations1.toString().contains("true"));
		assertTrue(stations1.toString().contains("QTek"));
		assertTrue(stations1.toString().contains("1024.0"));
	}
}






Any suggestions will be appreciated, thanks.



SaxonTrooper,
Hello! I think we may be in the same class. I have this code finished and running for Station. E-mail me I may be able to help.
holly.clem@gmail.com
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: array of objects

Posted 26 November 2007 - 04:24 AM

Quote

Hello! I think we may be in the same class. I have this code finished and running for Station. E-mail me I may be able to help.


Please post your replies here, so others can benefit, too.

Saxontrooper: please post the full error message you receive with the full stacktrace if possible.
Was This Post Helpful? 0
  • +
  • -

#4 saxontrooper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 07

Re: array of objects

Posted 27 November 2007 - 11:36 AM

View Post1lacca, on 26 Nov, 2007 - 04:24 AM, said:

Quote

Hello! I think we may be in the same class. I have this code finished and running for Station. E-mail me I may be able to help.


Please post your replies here, so others can benefit, too.

Saxontrooper: please post the full error message you receive with the full stacktrace if possible.

Sorry 1lacca, the only error I receive is when I run the tester class to add the object and assertNotNull, it passes a null pointer exception and flags:

stationArray [nextPosition] = new Station(); as the source code
Below is a testerClass excerpt


/**
	 * Add station (to local Stations object).
	 * 
	 * Use overloaded method 1.
	 */
	public void testAddStation()
	{
		/* Hint: create a Stations object.  Create a Stations1 object.
		   Add the Stations object to the Stations1 object and verify
		   that correct element position is returned.  Do twice to
		   determine that postion increments correctly.  Alternatively,
		   you can add to the stations1 Station instance field object. */

		stations1 = new Stations();
		tempStation = new Station();
		stations1.addStation(tempStation);
		assertNotNull(stations1);
	}




As always any help is greatly appreciated :D

sxntrpr

This post has been edited by saxontrooper: 27 November 2007 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1