6 Replies - 641 Views - Last Post: 13 November 2009 - 09:51 AM Rate Topic: -----

#1 mandangalo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-July 09

Why won't my array remember the data my constructor gives it?

Post icon  Posted 12 November 2009 - 04:35 PM

Here is the code
private void addElement(int atomicNum)
		{
			string atomicName;
			string atomicSym;
			double atomicWeight;
			Element e;
			if (m_theTable.ElementHasData(atomicNum - 1))
			{
				Console.WriteLine("This element has already been defined");
				Console.WriteLine("Would you like to edit that element?");
			}
			else
			{
				atomicNum -= 1;
				Console.WriteLine("atomic num: {0}", atomicNum);
				Console.WriteLine("What is the atomic name of the element you would like to add?");
				atomicName = Console.ReadLine();
				Console.WriteLine("What is the atomic symbol of that element?");
				atomicSym = Console.ReadLine();
				Console.WriteLine("What is the atomic weight of that element?");
				atomicWeight = Double.Parse(Console.ReadLine());
				e = new Element(atomicNum, atomicName, atomicSym, atomicWeight);
				m_theTable.setElement(atomicNum, e);
			}
		}

using System;

namespace StarterFiles
{
	class PeriodicTable
	{
		Element[] m_theElements;

		public PeriodicTable()
		{
			m_theElements = new Element[118]; // I think there are 113....changed to 118 to reflect more accurate periodic table

			// create new elements here, as needed/desired:
			m_theElements[0] = new Element(1, "Hydrogen", "H", 1.008);
			m_theElements[26] = new Element(27, "Cobalt", "Co", 58.9332);
			m_theElements[70] = new Element(71, "Lutetium", "Lu", 174.96);
			m_theElements[89] = new Element(90, "Protactinium", "Pa", 231.0359);

			// You'll need (at least) 6 more.
			// Pick them so that you can test your program

			// Don't worry about filling in more than 10, total, unless you
			// need more to test the program.
		}

		/// <summary>
		/// You might find this useful, since you're only required to fill 
		/// in 10 elements, and yet there are many more in table...
		/// </summary>
		/// <param name="atomicNum"></param>
		/// <returns>
		/// Return true if the table has data for a given element;
		/// return false otherwise
		/// </returns>
		public bool ElementHasData(int atomicNum)
		{
			// STUDENTS: YOU NEED TO WRITE YOUR CODE BELOW:
			if (m_theElements[atomicNum] != null)
				return true;
			else
				return false;

			// STUDENTS: YOU NEED TO WRITE YOUR CODE ABOVE:
		}

		public int MaxAtomicNumber
		{
			get
			{
				// STUDENTS: YOU NEED TO WRITE YOUR CODE BELOW:

				return m_theElements.Length;

				// STUDENTS: YOU NEED TO WRITE YOUR CODE ABOVE:
			}
		}

		/// <summary>
		/// This is an "indexer"
		/// </summary>
		/// <param name="atomicNum">The atomic number of the element that we're
		/// accessing.  Note that Hydrogen, the first element, is 1 (not zero).
		/// This method will take care of any 're-mapping' that needs to happen</param>
		/// <returns></returns>
		public Element getElement(int atomicNum)
		{
			// STUDENTS: YOU NEED TO WRITE YOUR CODE BELOW:

			foreach (Element e in m_theElements)
			{
				if (e != null)
				{
					if (e.getAtomicNumber() == atomicNum)
					{
						return e;
					}
				}
			}
			return null;

			// STUDENTS: YOU NEED TO WRITE YOUR CODE ABOVE:
		}

		public void setElement(int atomicNum, Element e)
		{
			if(m_theElements[atomicNum] != null)
				m_theElements[atomicNum] = e;
			// STUDENTS: IMPLEMENT THIS!!
		}
	}
}


using System;

namespace StarterFiles
{
	class Element
	{
		private int atomicNum;
		private string atomicName;
		private string atomicSymbol;
		private double atomicWeight;

		public Element(int num, string name, string sym, double w)
		{
			atomicNum = num;
			atomicName = name;
			atomicSymbol = sym;
			atomicWeight = w;
		}


		// HINT: In terms of figuring out what's a transition metal, etc,
		// you should do a web search for 'Periodic table', and then see what
		// useful,scientifically relevant images you find.  Your instructor can help you
		// if you get stuck - keep in mind that while you may (or may not) find the 
		// programming-related aspects of this assignment challenging, you shouldn't be spending
		// much time on the chemistry-specific stuff.  So if you're confused about the chemistry,
		// ask!!
		public void Print()
		{
			Console.WriteLine("Name: {0}", atomicName);
			Console.WriteLine("Symbol: {0}", atomicSymbol);
			Console.WriteLine("Atomic weight: {0}", atomicWeight);
			if ((atomicNum > 20 && atomicNum < 31) || (atomicNum > 38 && atomicNum < 49) ||
				(atomicNum > 57 && atomicNum < 81) || (atomicNum > 88 && atomicNum < 113))
				Console.WriteLine("This element is a transition metal.");
			if (atomicNum > 57 && atomicNum < 81)
				Console.WriteLine("This element is a lanthanoid metal.");
			if (atomicNum > 88 && atomicNum < 113)
				Console.WriteLine("This element is an actinoid metal.");
			// STUDENTS - IMPLEMENT THIS!!
		}

		public int getAtomicNumber()
		{
			return atomicNum;
		}

		public double getWeight()
		{
			return atomicWeight;
		}
		public void setWeight(double weight)
		{
			atomicWeight = weight;
		}

		
		// Here is a great place to add any other methods that you'll need...
	}
}


I am trying to make it so that addElement() puts data into the m_theElements array at a specific element chosen by the user but it will not seem to remember that object (right terminology?) created by the Element() constructor. Why not?

Is This A Good Question/Topic? 0
  • +

Replies To: Why won't my array remember the data my constructor gives it?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Why won't my array remember the data my constructor gives it?

Posted 12 November 2009 - 05:00 PM

You have another logic flaw :)

public void setElement(int atomicNum, Element e)
{
	if(m_theElements[atomicNum] != null)
		m_theElements[atomicNum] = e;
	// STUDENTS: IMPLEMENT THIS!!
}



Again...read CAREFULLY. When is this inserting the value?
Was This Post Helpful? 0
  • +
  • -

#3 mandangalo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-July 09

Re: Why won't my array remember the data my constructor gives it?

Posted 12 November 2009 - 05:01 PM

HAHAHA wow. I really have no idea what's wrong with me because I am usually not this retarded I swear Jack...
Was This Post Helpful? 0
  • +
  • -

#4 mandangalo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-July 09

Re: Why won't my array remember the data my constructor gives it?

Posted 12 November 2009 - 05:17 PM

Unfortunately even though this logical error was removed now whenever I try to print the data from the element I just assigned data into it crashes with the same NullReferenceException error. I don't know how I am getting this yet again when I thought it was corrected in my ElementHasData() method. God this shit is frustrating.
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Why won't my array remember the data my constructor gives it?

Posted 12 November 2009 - 08:36 PM

I just copied the code you had posted above, created a new element, added it to the table, fetched the element and printed it with no issues.

Can you post the code where you are doing this? Maybe that will help.
Was This Post Helpful? 0
  • +
  • -

#6 mandangalo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-July 09

Re: Why won't my array remember the data my constructor gives it?

Posted 12 November 2009 - 11:18 PM

private void addElement(int atomicNum)
		{
			atomicNum = atomicNum - 1;
			string atomicName;
			string atomicSym;
			double atomicWeight;
			Element e;

			if (m_theTable.ElementHasData(atomicNum))
			{
				Console.WriteLine("This element has already been defined");
				Console.WriteLine("Would you like to edit that element?");
			}

			else
			{
				Console.WriteLine("atomic num: {0}", atomicNum);
				Console.WriteLine("What is the atomic name of the element you would like to add?");
				atomicName = Console.ReadLine();
				Console.WriteLine("What is the atomic symbol of that element?");
				atomicSym = Console.ReadLine();
				Console.WriteLine("What is the atomic weight of that element?");
				atomicWeight = Double.Parse(Console.ReadLine());
				e = new Element(atomicNum, atomicName, atomicSym, atomicWeight);
				m_theTable.setElement(atomicNum, e);
			}
		}



Here is the method used for adding a new element to the array. I won't repost code for the methods used to do the verification of whether there is data in the element in the array as it's been posted but this is what I am doing. I tried just hard coding a new element into the array by this means
 Element e = new Element(atomicNum, "Helium", "He", 2.001);
just to see if it might be the way i'm getting the user input and it didn't even pass that to my array. I can't wait for this class to be over.
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Why won't my array remember the data my constructor gives it?

Posted 13 November 2009 - 09:51 AM

View Postmandangalo, on 12 Nov, 2009 - 10:18 PM, said:

Here is the method used for adding a new element to the array.

And it worked fine for me. Of course I had to fill in all the code that you haven't posted.

View Postmandangalo, on 12 Nov, 2009 - 10:18 PM, said:

 Element e = new Element(atomicNum, "Helium", "He", 2.001);
just to see if it might be the way i'm getting the user input and it didn't even pass that to my array. I can't wait for this class to be over.

and that code doesn't add the element to the table, it just creates one. Did you call your setElement method after this?

I suspect there is an issue in the driver code that you are using to test this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1