Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project 1: Chapter 6 (Atoms) Due: Wednesday, January 25, 2012 In chemistry, an e

ID: 3637090 • Letter: P

Question

Project 1: Chapter 6 (Atoms)
Due: Wednesday, January 25, 2012
In chemistry, an element is defined by the number of protons in the nucleus of the atom.
Within such an element, the other particles that make up the atom can vary, giving ions
(different numbers of electrons) or isotopes (different numbers of neutrons). You will
take a C++ program that computes and displays information on atoms and replace its use
of a structure with a class.
Code needed to complete this project is on the Quincy College Portal web page for this
course. From the Main Page section (on left of page), go to Handouts and find the
heading Programming Projects. Under that heading (specifically, below this write-up),
download the zipped folder: Project 1 (Atoms).zip.
Unzip that folder and open the program in VISUAL STUDIO 2008 (go into the folder and
double-click Atoms.sln). To view the code in VS2008, in the Solution Explorer,
expand Atoms, then Source Files, and open main.cpp.
Sample Run
If you build and run the provided code (Debug > Start Without Debugging or
Ctrl-F5), the output of the program will look like (user input in bold):
Enter protons for atom (0=exit): 35
Enter charge for atom (e.g., +2, -1 or 0 for none): 0
Enter atomic weight for element: 79.904
Atom has:
35 protons
35 electrons
45 neutrons
It is not an ion
Enter protons for atom (0=exit): 20
Enter charge for atom (e.g., +2, -1 or 0 for none): +2
Enter atomic weight for element: 40.078
Atom has:
20 protons
18 electrons
20 neutrons
It is an ion with charge +2
Enter protons for atom (0=exit): 0CSI108 – Advanced C++ - Spring 2012 Page 2 of 3
Code
Within the code, you’ll find a structure definition:
struct Atom
{
int numProtons;
int numElectrons;
};
There are also prototypes and definitions for two functions that operate on the structure:
setAtomCharge() and calcAtomNeutrons().
Task
Your task is to replace the structure and functions with a single class.
Class
When converting the code that uses a structure to use a class (also called it Atom), adhere
to the following constraints:
? Make all data members private.
? Add a mutator member function to set the number of protons in the atom—it should
also set the number of electrons to the same value.
? Add accessors to get the protons and the electrons from Atom objects.
? Turn function setAtomCharge() and into a public mutator member function. As
it is now part of a class named Atom, give it a shorter name.
? Change calcAtomNeutrons into a public accessor. Also shorten its name when it
becomes a member function.
? Add additional member functions to do the following:
? Return the charge of the atom. For example, an atom with 26 protons and 23
electrons has a charge of +3; with 11 protons and 12 electrons, a charge of -1.
? Return whether an atom is an ion (true/false), i.e., whether it has a non-zero
charge. Call the member function that computes the charge in this function.
Main Program
? Alter the code in main() to use objects (variables of class Atom). Since data
members are private, you must replace code that directly accesses the members of the
structure with calls to the class member functions (see “Class” above). Use all the
member functions of class Atom within main().
Standards
Adhere to our conventions for naming types, variables, constants, members, and
functions. Include descriptive comments in code where appropriate. Include your name,
project name, course name, date, and a description at the top of your source code.
Test your program thoroughly with different input values.CSI108 – Advanced C++ - Spring 2012 Page 3 of 3
Submission
You must complete the C++ program in VISUAL STUDIO 2008 or VISUAL C++ 2008
EXPRESS EDITION (the latter is downloadable from microsoft.com).
Name your top-level project folder Project 1 (Atoms) Your Name. Turn in the
entire project folder (bring on your flash drive) on the due date.
Any late submissions that are e-mailed must be compressed in RAR format (Quincy
College e-mail currently does not accept certain ZIP attachments). Delete any
“Debug” folders within the project before compressing in RAR forma

Explanation / Answer

As we have no access to the Portal, we can only give general advice.

Your class will look like:

class Atom
{
private:
int protons, electrons;
public:
Atom(_protons=0, _electrons=0) { protons=_protons; electrons=_electrons }

void setCharge(int charge)
{
    // contents of original function
}

int Neutrons()
{
    // contents of original funciton
}

void setProtons(int p) { protons = p; }
void setElectrons(int e) { electrons = e; }

int Electrons() { return electrons; }
int Protons() { return protons; }

int Charge() { return protons - electrons; }
bool IsIon() { return Charge() != 0; }
};

Now, everywhere you declare a variable such as "struct Atom anAtom" change it to "Atom anAtom".

Change references such as "anAtom.numElectrons = 0" to "anAtom.setElectrons(0)", and so on

The instructions are pretty clear on the changes to main(). You need to add comments, adapt the identifiers to your class's naming conventions, fill out the missing functions, test it, and so on.