Computer Science 1xx – Assignment #N

DUE DATE: mmddyyyy  

Objects & Arrays of Object References:

The Periodic Table Assignment

 

Learning Objectives:
(This is a list of the major topics that you, as students, will learn in this assignment:)

  1. One-Dimensional ('Normal' ) Arrays of References To Objects:
    In this assignment, you will need to become proficient in manipulating arrays that contain references to objects.  You will need to keep track of a small subset of all the possible atomic elements, including writing code that is able to handle object references that are null.
     
  2. Basic object oriented programming:
    In this assignment, you will need to implement two, interacting classes.  One class, the Element class, has a minimum amount of code provided to you, and you'll need to implement most of it (this shouldn't be too tricky, though, since it's a fairly small class).  The other class, the PeriodicTable class, has more material provided to you, to get you started, but will require more advanced logic in order to complete the program.  The PeriodicTableUI class, which handles all interaction with the user, has been provided to you.  The goal here is to learn how to implement functionality that is spread across multiple classes, rather than putting everything into a single class.

Grading Rubric: This document is available for you use - you should use it to  to help guide your work.  The rubric is a document which explains how your work will be evaluated, and is located here.

Part 1: Writing the program

 

    For this assignment, you will create a C# program that begins to explore how to use object oriented programming to store and look up information. For this assignment, you'll be using the periodic table of the elements. Since the table is not reproduced here, you may need to search the web for it in order to find one. Also, since knowledge of chemistry isn't required for this class, feel free to ask the instructor questions about the table.

    The periodic table of the elements allows chemists and physicists to easily categorize and deal with the huge number of different atomic elements that exist. For each element, there is an atomic number, a name, a standard "symbol" (which consists of one or two letters), and the atomic weight of the element. You need to write a program that could keep track of all of this information for all 103 of the known elements that have agreed-upon names and symbols. For the actual assignment, you only need to actually fill in information about ANY 10 of these elements: remember though that you need to write your program so that you could easily expand this to 103, so I suggest that you pick the 10 elements that allow you to test the various capabilities of your program.
Your program needs to have the following features:

Examination:
    The user should be allowed to print out any one element, by atomic number, and be given all the information (listed above) about the element (it's name, symbol, and atomic weight out to at least 1, and no more than 4 decimals places, if possible).  If the requested element isn't present in the periodic table, you should inform the user that the program doesn't have any information for that element (and then give the user the option of adding information for that element (see the section on 'Adding An Element', below) )
    Further, if the user requests information about something that's considered to be a 'transition element', you should say that in your output.  Likewise, your program should notify the user if the requested element is an inner transition element (and if so, whether the element is within the Lanthanoid or Actinoid series)(sometimes the inner transition elements are also referred to as 'rare earth elements').  For the purposes of this program, transition/inner transition information can be determined based on the element's placement within the periodic table (i.e., you don't have to worry about someone creating an element with atomic number 6, which is normally neither a transition, nor inner transition element, yet trying to claim that it is in the Lanthanoid series).  For an element that isn't a member of any of these groups, you don't have to print anything out (i.e., you don't need to print a message saying that Hydrogen is NOT a member of the transition elements (etc)).


    The user should also be able to ask for all the elements to be printed out. Any element that you haven't actually implemented can simply be identified by it's atomic number as being 'missing'. (Inner) transition information must still be properly printed.  An example of output (including some missing elements) might be:

Atomic # 1: "Hydrogen" Symbol: H Atomic Weight:1.00008
Atomic # 2: <Missing>
Atomic # 3: <Missing>

etc.


Adding An Element:
    The user is allowed to add elements to the periodic table via two mechanisms

  1. If the user asks the program to display an item that is missing, then the program will give the user the option of adding that new element to the table (see the section on 'Examination', above)

  2. If the user tried to edit an element that the program doesn't have information about (see the section on 'Editing', below)

  3. From the main menu, the user may elect to add a new element to the table.  In this case, the program should indicate that an error has occurred if the specified atomic number is less than 1, higher than the maximum atomic number that the PeriodicTable tracks, or if the PeriodicTable already has data for that element (in which case, the program should give the user the option of editing the element, instead - see 'Editting', below)

    Regardless of how the user chooses to initiate this action, the program will behave the same way - it will ask the user for the name, symbol, and weight of the element.  As specified in the 'Examination' section, the program can infer which group(s) the element belongs to, based on the element's position in the periodic table.


Editing:
    The user should be allowed to select an element (again, it's sufficient to specify elements by atomic number), and they should be allowed to change any of the listed information about the element, except for the atomic number (in other words, the atomic number may be used to identify the element, regardless of any changes to the name, atomic weight, etc, etc).

    If the user attempts to edit an element that doesn't exist, the user will be given the option to instead add a new element (see the section on 'Adding An Element', above), if they'd like.  Otherwise, the user will be returned to the main menu.

   
Limitations of the program:
    At no point will the program interact with files in any way.  This program will neither save data to, nor read data from, files.  This does mean that any changes that the user makes to the program will be transient (non-persistent).

 

Other requirements: Fully Implement Starter Project
    Please note that there is are several 'starter files' up on the website – download this project and start your work from there. You are required to implement any of the provided methods/properties, even if you don't use them yourself, so that they provide the behavior listed in the comments.

    Note: You are allowed to modify code outside of those specific regions that are clearly identified for you to implement.  Keep in mind, however, that you should NOT start from scratch, and you should not find it necessary to re-write large blocks of the provided code.  Starting from scratch will be penalized, and excessive modification of provided code will also be penalized (the idea being that you should be developing the skill of examining existing code, and working with it, rather than trying to start over by yourself).
 

Other requirements: Modular Design (Functional Decomposition)
    There are several aspects of this assignment that can be put into a separate function/method, and reused from different points.  You should design your program so that it exhibits good modularity (good functional decomposition), rather than duplicating code.  For example, the user might elect to create new element from the main menu, OR when attempting to edit a non-existent element - you should be able to find a way to create a common method that executes the actions that those two tasks have in common.

    Similarly, before writing your own code, you should look and see if there is already a method that does what you need, so that you don't duplicate code that has been provided to you.

 

Summary of required code
    What follows is a summary of the methods that you'll need to implement, and what each method has to accomplish, in order to successfully complete this assignment.  Please note that there is also a transcript provided here, in order to demonstrate how the program should interact with the user.

 

Table 1: Methods That You Have To Implement on the PeriodicTableUI class

    A couple of quick notes on the PeriodicTableUI class: The PeriodicTableUI class is responsible for interacting with the user through the keyboard and console.  A sizable fraction of the 'structure' of the code has been laid out for you - the main menu, the basic methods that will be called to accomplish the tasks outlined above, and several 'input validation' routines that you can (and should!) use to finish implementing the program.  The PeriodicTableUI class is responsible ONLY for interacting with the user - any management of the collection of Element objects should be done by the PeriodicTable, and any element-specific information should be stored in a particular Element object.

   IMPORTANT NOTE: Other than for 'Print' methods, no other class is allowed to interact with the user.  This is done to force you to create reusable, modular code.  Imagine that you were to take this assignment, and rework it for WindowsForms, or for ASP.Net - if this class is the only one that interacts with the user, then it will be the only class that needs to be rewritten, while the other classes can be reused, (almost) without any changes.

Data Field Name

Type

Description:

You're free to add any data fields

that you need to, in order to accomplish

the objectives set forth in this assignment

Note: all data fields should be marked private (if you need to expose them, use Properties, or accessor/mutator methods)

Method Name

Returns

Description/Parameters:

printAllElements

void

Parameters: None

This method needs to iterate through all possible elements in the PeriodicTable object that the PeriodicTableUI class maintains, and if the PeriodicTable has an Element object for that atomic number, then this method should retrieve a reference to that Element object, and then invoke the Print method on that object.  If the program does not have information for a particular element, then this method should print out a message clearly stating that the element is missing.

printOneElement

void

Parameters: None

This method will ask the user which element the user would like to print (as identified by the element's atomic number), and if the PeriodicTable object has data for that element, it will print it.  If not, it will allow the user to add that element to the PeriodicTable, if the user so chooses.

addElement

void

Parameters:

  1. int atomicNum: The atomic number of the element that the user wishes to create. 

This method will be called once it's clear that the user does want to create a new element, but before it's known if that element exists, or not.  Thus, it will need to verify that the given element doesn't already exist.  If the element doesn't exist, then it will ask the user for all the information needed to create the new Element object.  It will then tell the PeriodicTable that this new Element object should be used to represent/store data about the particular element.

Note that you are required to 'code defensively' - make sure that no matter what the value of the parameter is, your code will correctly detect any errors that may occur, and respond reasonably (and informatively) to the user

editElement

void

Parameters: None

This method will start by asking the user which element the user wishes to edit (as identified by the element's atomic number).  Assuming that the element already exists, then the user will be directed through editing the element.

 

 

Table 2: Methods That You Have To Implement on the PeriodicTable class

    A couple of quick notes on the PeriodicTable class: The PeriodicTable class is responsible for maintaining the collection of Element objects for the program. 

    Since you will have as many as 113 possible elements, but you won't actually create 113 element objects, this class will need to be able to deal with null references, which it will use to represent a 'missing' element (i.e., an element that the program doesn't have information about).  Learning to manipulate null references in an array is one of the major learning objectives of this assignment.

Data Field Name

Type

Description:

You're free to add any data fields

that you need to, in order to accomplish

the objectives set forth in this assignment

Note: all data fields should be marked private (if you need to expose them, use Properties, or accessor/mutator methods)

Method Name

Returns

Description/Parameters:

<Constructor>

<none>

Parameters: None

As stated above, you will need to make sure that your program starts off with a total of 10 elements.  Since you need to be able to deal with missing elements, you are specifically PROHIBITED from filling in every single element (i.e., some must remain blank, so that your null-handling methods can be tested).

ElementHasData

bool

Returns true if the table has data for a given element; returns false otherwise

Parameters:

  1. int atomicNum: The atomic number of the element that is being checked. 

Note that you are required to 'code defensively' - make sure that no matter what the value of the parameter is, your code will correctly detect any errors that may occur, and respond reasonably (and informatively) to the user

MaxAtomicNumber

int

Returns the highest atomic number, amongst all the elements that the table stores, or might store.

This C# read-only property will return the highest atomic number that the PeriodicTable might keep track of (regardless of whether it currently has information for that particular element or not). 

It is intended that this property, in conjunction with the other methods on this class, would allow another class to traverse all possible Element objects in the periodic table.

getElement

Element

Returns the Element object whose atomic number is equal to the provided parameter ; returns null if the PeriodicTable doesn't have any information about the element.

Parameters:

  1. int atomicNum: The atomic number of the element that is being retrieved. 

Note that you are required to 'code defensively' - make sure that no matter what the value of the parameter is, your code will correctly detect any errors that may occur, and respond reasonably (and informatively) to the user

setElement

void

Parameters:

  1. int atomicNum: The atomic number of the element that is being set. 
     

  2. Element e: A reference to the Element object that stores the information about the object.

This method will take the atomicNum parameter, check to make sure that it's valid, and then remember that the object e now contains information about that element.  Note that it is perfectly ok for the PeriodicTable to allow someone to overwrite existing information about an element - it is the responsibility of the UI (user interface) to ensure that the user isn't allowed to add an element that already exists.

Note that you are required to 'code defensively' - make sure that no matter what the value of the parameter is, your code will correctly detect any errors that may occur, and respond reasonably (and informatively) to the user

 

Table 2: Methods That You Have To Implement on the Element class

    A couple of quick notes on the Element class: The Element class is responsible for maintaining information about a single element, including everything listed above.

   Note that you will find that in order to successfully complete this assignment, you will need to add several methods to this class, in order to properly encapsulate this (and the other) classes.

Data Field Name

Type

Description:

You're free to add any data fields

that you need to, in order to accomplish

the objectives set forth in this assignment

Note: all data fields should be marked private (if you need to expose them, use Properties, or accessor/mutator methods)

Method Name

Returns

Description/Parameters:

<Constructor>

<none>

Parameters:

  1. int atomicNum: The atomic number of the element that is being created.  Example: 1
     

  2. string name: The name of the element that is being created.  Example: "Hydrogen"
     

  3. string sym: The symbol of the element that is being created.  Examples: "H", or "Na"
     

  4. double w: The atomic weight of the element that is being created. Example: 1.0008

This method constructs a new object of the Element type.  You must complete the implementation of this, in order to successfully complete this assignment.

Print

void

Parameters: None

This method will print out all the information about this element.  This method is the only method (other than those on the PeriodicTableUI class) that is allowed to interact with the user, and it is only allowed to print out information to the user (it is not allowed to get information from the user)

getAtomicNumber

int

The atomic number of this element.

Parameters: None

The number returned must be identical to the atomicNum parameter that was passed into the constructor.

getWeight

double

The atomic weight of this element.

Parameters: None

The return value must be identical to either the w parameter that was passed into the constructor, or the value that was passed into the setWeight method - whichever was executed most recently.

setWeight

void

Parameters:

  1. double weight: The new atomic number of the element. 

 

<Other methods>

 

Varies, based on the exact method

Parameters: Varies, based on the exact method
 

You may need to implement other methods on the Element class, in order to fully complete this assignment.  Remember that all the data fields must be private, and that you must allow the user to edit an existing Element object via the Console user interface.
 

 

Group Work, Commenting:

 

            You are not allowed to work in groups for this assignment.  You should start, finish, and do all the work on your own.  If you have questions, please contact the instructor.

 

            Additionally, you should aggressively comment your code, paying particular attention to areas that are difficult to understand.  If you found something to be tricky when you wrote it, make sure to comment it so that the next person (the instructor, who's grading you) understands what your code is doing.  It is not necessary to comment every single line.

 

The purpose of new requirement is to both help you understand, and have you demonstrate, a thorough understanding of exactly how your program works.

 

Every file that you turn in should have:

·        At the top of each file that you normally edit, you should put your name (first and last), the name of this class (“BIT 142”), and the year and quarter, and the assignment number, including the revision number, which starts at 0 (“A2.0”).  If you’re handing this in again for a regrade, make sure to increase the minor version number by one (from “A2.0”, to “A2.1").
You normally edit the C# source code files (.CS files), and any Word documents that you're handing in (if any).
You do not normally edit the .SLN or .CSPROJ files, and so you should not try to put this identifying information in those files.

 

In general, you should make sure to do the following before handing in your project:

·        All variables used should have meaningful names.

·        The code should be formatted consistently, and in an easy to read format.   

 

What to turn in:

 

·        A single electronic folder (a directory).  This folder should contain:

o       The source code for the program – all the .CS files in your project.
I would prefer that you include the project files – stuff ending in .SLN and .VCPROJ, so I can build your project more easily.  If you can save these files (the .SLN / . VCPROJ) into a file format that can be opened by VS.Net 2003, that would be great.

o       You have to name the folder with your last name, then first name, then the assignment number (both the major version – 2, and the minor (revision) number – 0).  Example: "Panitz, Mike, A2.0"

 

·        You should not include the Debug directory, or anything from it.  I will dock you a couple points if you do.  Also, you don't need to include your .NCB file, if it's present.

 

How to electronically submit your homework:

 

There's a link on the homework page to the document that guides you through handing in your work, using the SourceGear Vault program.


This document and the related materials are developed with support from Microsoft Research Computer Gaming Initiative under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871.