Question

In: Computer Science

In this lab, you will be completing a programming exercise through the point of view of...

In this lab, you will be completing a programming exercise through the point of view of both a

contracted library developer and the client that will use the developed code.

In the first part, you will be required to write a class in C++ that will be included in the client’s code.

Your class must be written with defensive programming in mind. It should allow the client to include or

leave out your defensive checks at compile-time.In the second part, you will use your defensively

programmed class methods to guide the revision of the provided user driver program. After

encountering failures from misuse of the library class methods, you will update the driver program,

according to the implementation guidelines in the CWE documentation for the appropriate error.

Note that the code you write does not have to be completely optimized and you will likely see better

ways to write the class and the driver to avoid the problems inherit in the client descriptions.

In Part 1 of the lab you will complete the following:

 Write a class called myArray in a file named “yourlastname_lab2.cpp” where you substitute your

own name

            o Constructor

                       two inputs: int size and string input

                       dynamically create a char* array of int size

                       parse string input (which should be a string of comma-separated characters)

                              and enter the characters in the array in order

           o Destructor should free the memory assigned to your char* array

          o ReadFromArray

                 one input: int index

                 return char at the given index for the array

       o WriteToArray

               two inputs: int index, char replace

               overwrite char at given index with new char replace

    o DeleteArray

                 free the memory for your char*

             set char* to NULL

o PrintArray

                output the contents of the char* array to stdout

o NewArray

               two inputs: int size and string input

              dynamically create a char* array of int size

              parse string input (which should be a string of comma-separated characters)

             and enter the characters in the array in order

               For each class method, provide the contract for proper usage of the method

o enter as comment lines directly after the definition

o List any preconditions (what has to be true immediately before executing the method)o List any postconditions (what has to be true immediately after executing the method)

               Utilize C standard assert() library calls from assert.h to test your preconditions

               Use macros to give the client the option on whether to include the asserts at compile-time

               Use the provided sample client driver program to test your class code

                Take screenshots of your assertions being invoked for each function

In Part 2 of the lab you will complete the following:

• Using the assertions you have placed into your class methods, update the driver code to ensure

calls made to the class methods are in-contract

• Identify what CWE errors, if applicable, are occurring with out-of-contract use of your class

methods

• Review the ‘Potential Mitigation” section for those CWE errors and use the “Phase:

Implementation” entries to guide your revision of the provided program driver.

• Take screenshots of the driver code working without hitting the assertions – be sure to explain

in your word document how you tested the preconditions of each method and what changes

you made to the driver to ensure in-contract calls were made to the methods.

Graduate students should also answer the following:

• Is there a Python equivalent to the C-standard assert() calls used in class with C++?

• How would you approach defensive programming from the point-of-view of python methods?

Submit a zip file to Blackboard which contains your class file and a word document which includes the

screenshots and other information described above.

For full credit your code should compile, run as described, and be appropriately commented. If I need to

know anything in particular about how I should compile your code, include that in your document.

GIVEN cpp code:

============

//Sample client code for interfacing with myArray class
//Use this driver program to test your class and defensive programming assertions

#include <iostream>
#include <string>
#include <stdlib.h>
#include "your_class_here.cpp"   //replace this with your own file

using namespace std;

int main(){
   int size, choice, read, write;
   string input;
   char replace, response;
   char * array;

   cout << "Welcome, please enter a maximum size for your array" << endl;
   cin >> size;

   cout << "Please enter a series of comma-separated characters for your array" << endl;
   cin >> input;

   //create object of class type which should dynamically allocate a char* array
   //of int size and fill it with the comma-separated values from string input

   Array myArray(size, input);

   while(1){

       cout << "Array Menu" << endl;
       cout << "1. Read by index" << endl;
       cout << "2. Write by index" << endl;
       cout << "3. Delete array" << endl;
       cout << "4. Print array" << endl;
       cout << "5. New Array" << endl;
       cout << "6. Exit" << endl;
       cin >> choice;

       switch(choice){
           case 1:
              cout << "Enter an index to read a value from the array" << endl;
              cin >> read;
              //call to library function ReadFromArray(int read)
              //this library call should read a single character from the array and return it
              response = myArray.ReadFromArray(read);
              cout << "The item in index[" << read << "] is " << response << endl;
              break;
           case 2:
              cout << "Enter an index to write a value to the array" << endl;
              cin >> write;
              cout << "What single character would you like to write to the array?" << endl;
              cin >> replace;
              //call to library function WriteToArray(int write, char replace)
              //this library call should write a single character to the array
              myArray.WriteToArray(write,replace);
              cout << "The item in index[" << write << "] is " << myArray.ReadFromArray(write) << endl;
              break;
           case 3:
              //call to library function DeleteArray() which should free the dynamically allocated array
              myArray.DeleteArray();
              break;
           case 4:
              //call to library function PrintArray() which will print the contents of the array to stdout
              myArray.PrintArray();
              break;
           case 5:
              //call to library function NewArray() which will dynamically allocate a new array
              cout << "Welcome, please enter a maximum size for your array" << endl;
              cin >> size;

              cout << "Please enter a series of comma-separated characters for your array" << endl;
              cin >> input;
              myArray.NewArray(size, input);
              break;
           case 6:
              exit(0);
              break;
       }

   }
   return 0;
}

Solutions

Expert Solution

yourclass:

class myArray{
char *arr;
public:
myArray(int size, char input[]){
arr = new char[size];
for(int i=0;i<size;i++){
arr[i] = '\0';
}
char *c;
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else arr[i++] = tok[0];
}
}
~myArray(){
delete []arr;
}
char ReadFromArray(int index){
return arr[index];
}
void WriteToArray(int index, char replace){
arr[index] = replace;
}
void DeleteArray(){
delete arr;
arr = NULL;
}
void PrintArray(){
cout<<arr<<endl;
}
void NewArray(int size, string input){
char *newarr = new char[size];
for(int i=0;i<size;i++){
newarr[i] = '\0';
}
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else newarr[i++] = tok[0];
}
}
};

Fullcode:

//Sample client code for interfacing with myArray class
//Use this driver program to test your class and defensive programming assertions

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include<sstream>

using namespace std;
class myArray{
char *arr;
public:
myArray(int size, char input[]){
arr = new char[size];
for(int i=0;i<size;i++){
arr[i] = '\0';
}
char *c;
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else arr[i++] = tok[0];
}
}
~myArray(){
delete []arr;
}
char ReadFromArray(int index){
return arr[index];
}
void WriteToArray(int index, char replace){
arr[index] = replace;
}
void DeleteArray(){
delete arr;
arr = NULL;
}
void PrintArray(){
cout<<arr<<endl;
}
void NewArray(int size, string input){
char *newarr = new char[size];
for(int i=0;i<size;i++){
newarr[i] = '\0';
}
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else newarr[i++] = tok[0];
}
}
};

int main(){
int size, choice, read, write;
string input;
char replace, response;

cout << "Welcome, please enter a maximum size for your array" << endl;
cin >> size;

cout << "Please enter a series of comma-separated characters for your array" << endl;
cin >> input;
char arr[input.size()+1];
for(int i=0;i<input.size();i++)
arr[i] = input[i];
//create object of class type which should dynamically allocate a char* array
//of int size and fill it with the comma-separated values from string input
myArray Array(size, arr);

while(1){

cout << "Array Menu" << endl;
cout << "1. Read by index" << endl;
cout << "2. Write by index" << endl;
cout << "3. Delete array" << endl;
cout << "4. Print array" << endl;
cout << "5. New Array" << endl;
cout << "6. Exit" << endl;
cin >> choice;

switch(choice){
case 1:
cout << "Enter an index to read a value from the array" << endl;
cin >> read;
//call to library function ReadFromArray(int read)
//this library call should read a single character from the array and return it
response = Array.ReadFromArray(read);
cout << "The item in index[" << read << "] is " << response << endl;
break;
case 2:
cout << "Enter an index to write a value to the array" << endl;
cin >> write;
cout << "What single character would you like to write to the array?" << endl;
cin >> replace;
//call to library function WriteToArray(int write, char replace)
//this library call should write a single character to the array
Array.WriteToArray(write,replace);
cout << "The item in index[" << write << "] is " << Array.ReadFromArray(write) << endl;
break;
case 3:
//call to library function DeleteArray() which should free the dynamically allocated array
Array.DeleteArray();
break;
case 4:
//call to library function PrintArray() which will print the contents of the array to stdout
Array.PrintArray();
break;
case 5:
//call to library function NewArray() which will dynamically allocate a new array
cout << "Welcome, please enter a maximum size for your array" << endl;
cin >> size;

cout << "Please enter a series of comma-separated characters for your array" << endl;
cin >> input;
Array.NewArray(size, input);
break;
case 6:
exit(0);
break;
}

}
return 0;
}

OUTPUT:


Related Solutions

Programming assignment (75 pts): The Lab 1 development assignment was largely an exercise in completing an...
Programming assignment (75 pts): The Lab 1 development assignment was largely an exercise in completing an already started implementation. The Lab 2 development assignment will call on you to implement a program from scratch. It’s an exercise in learning more about Java basics, core Java Classes and Class/ method-level modularity. Implement a ‘runnable’ Class called “NumericAnalyzer”. Here’s the functional behavior that must be implemented. NumericAnalyzer will accept a list of 1 or more numbers as command line arguments. These numeric...
Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
On Lab 10 you did Chapter 8’s Programming Exercise 13, using the name Lab10TestScores and assuming...
On Lab 10 you did Chapter 8’s Programming Exercise 13, using the name Lab10TestScores and assuming that the class has ten students and five tests. Now let’s rewrite it to use dynamic arrays so that it will work for any number of students and any number of tests. Call this new program Lab11TestScores. Modify the program so that it asks the user to enter the number of students and the number of tests before it reads the data from the...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink. The public methods of your class should already be the following: MyDynamicArray(); Default Constructor. The array should be of capacity 2. MyDynamicArray(int s); For this constructor the array should be of capacity and size s. int& operator[](int i); Traditional [] operator. Should print a message if i is out of...
Must be written in Java: After completing this lab, you should be able to: Write a...
Must be written in Java: After completing this lab, you should be able to: Write a Java class to represent Time. Create default and parameterized constructors. Create accessor and mutator methods. Write a toString method. This project will represent time in hours, minutes, and seconds. Part 1: Create a new Java class named Time that has the following instance fields in the parameterized constructor: hours minutes seconds Create a default constructor that sets the time to that would represent the...
Lab Practical #1 On-line Determination of Bacteria. In this lab exercise you will be given the...
Lab Practical #1 On-line Determination of Bacteria. In this lab exercise you will be given the bacteria and you will need to explain what it would look like on the different medias (NG is suitable for No growth). BA=blood agar, CNA = Columbia nutrient agar, MA=MacConkey agar, MSA=mannitol salt agar, HE=Hektoen enteric Agar Staphylococcus Aureus: Gram stain __________________. Growth on Media: BA _________ CNA__________ MA__________MSA_________HE__________ One place where the bacteria causes infection ________. Streptococcus pneumoniae: Gram stain __________________. Growth on...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference. …………………………...…….. …………………………...……. Instructions LAB5 Instructions Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT