Question

In: Computer Science

In this project you will implement the DataFrame class along with the all the methods that...

In this project you will implement the DataFrame class along with the all the methods that are represented in the class
definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For
this project we will assume that all the values that are stored in the columns and rows are integers.
After you have tested your class, you have to execute the main program that is also given below.

DataFrame Class


#include
using namespace std;
class DataFrame {
protected:
int** table;
int noRows, noCols;
char** colNames;
char** rowNames;
public:

//Constructors
DataFrame ();
DataFrame (int rows, int cols);

//Output Method
void display();

//Setters
void setRowName(int row, char* name);
void setColName(int col, char* name);
int* operator[] (int i); //get row i;

//Getters
char** getRowNames();
char** getColNames();
int getNumberRows();
int getNumberCols();
DataFrame* getColumns(int* columns, int cLen);
DataFrame* getRows(int* rows, int rLen);
DataFrame* getRowsCols(int* rows, int rLen, int* cols, int cLen);

//Destructor
~DataFrame();
};

The main function


int main () {
int c, r;
int selectC[3];
int selectR[10];

// Read the dataframe from input
// First line: two numbers seperated by space;
// first number is the number of rows (r) and
// second number is the number of columns (c)

cin >> r >> c;
DataFrame* firstDF = new DataFrame(r,c);

// Second line: strings seperated by a comma (c of them); representing column names
// Third line: strings seperated by a comma (r of them); representing row names
// Fourth line and more: c number of integers in each of the r rows (seperated by)
// a space between integers in the same row.
// TODO: Student completes code for the above comment block to get them as input
// using the display method, print (in the same format as the input):
// - the column names of the dataframe
// - the row names of the dataframe
// - the contents of the table in dataframe
// TODO: Student completes code for the above comment block
// Execute the following code
// Read the column numbers that you want to extract

for (int i=0; i < 3; i++)
cin >> selectC[i];
DataFrame* tempColumns = (*firstDF).getColumns(selectC, 3);
(*tempColumns).display();

// Change the row names of select rows
(*tempColumns).setRowName(2, "Jack");
(*tempColumns).setRowName(3, "Peter");
(*tempColumns).display();

// Read the row numbers that you want to extract
for (int i=0; i < 10; i++)
cin >> selectR[i];
DataFrame* tempRows = (*firstDF).getRows(selectR, 10);
(*tempRows).display();

// Change the column names of selected columns
(*tempRows).setColName(2, "Scores");
(*tempRows).setColName(3, "Values");
(*tempRows).display();

// Extract the rows in SelectR and columns in SelectC
DataFrame* tempColsRows = (*firstDF).getRowsCols(selectR, 10, selectC, 3);
(*tempColsRows).display();
delete tempRows;
delete tempColumns;
delete tempColsRows;

// Sample Code for you and you must execute this
DataFrame* myTable = new DataFrame(5,5);
for (int i =0; i < 5; i++) {
for (int j=0; j < 5; j++) {
(*myTable)[i][j] = i*j;
}
}
(*myTable).display();
delete myTable;
}

Solutions

Expert Solution

CODE:

#include <iostream>
using namespace std;
class DataFrame {
        public:
        int** table;
        int noRows, noCols;
        char** colNames;
        char** rowNames;
        public:
        DataFrame () {
                table=NULL;
                noRows=0;
                noCols=0;
                colNames=NULL;
                rowNames=NULL;
        }

        DataFrame (int rows, int cols) {
                noRows=rows;
                noCols=cols;
        }
      
        void display() {
                cout<<noRows<<" "<<noCols<<endl;
                for(int i=0;i<noCols;i++)
                        cout<<colNames[i]<<" ";

                cout<<endl;

                for(int i=0;i < noRows;i++)
                        cout<<rowNames[i]<<"";

                cout<<endl;

                for(int i=0;i<noRows;i++)
                        for(int j=0;j<noCols;j++)
                                cout<<table[i][j]<<" ";

                cout<<endl;

        }
        void setRowName(int row, char* name) {
                rowNames[row]=name;
        }

        void setColName(int col, char* name) {
                colNames[col]=name;
        }
        int* operator[] (int i){
                return table[i];
        }

        char** getRowNames() {
                return rowNames;
        }

        char** getColNames() {
                return colNames;
        }

        int getNumberRows() {
                return noRows;
        }

        int getNumberCols() {
                return noCols;
        }

        DataFrame* getColumns(int* columns, int cLen) {
                DataFrame *d=new
                DataFrame(cLen,sizeof(columns)/sizeof(*columns));
                for(int i=0;i<sizeof(columns)/sizeof(*columns);i++)
                        d->setColName(i,colNames[columns[i]]);

                for(int i=0;i<cLen;i++)
                        d->setRowName(i,rowNames[i]);

                for(int i=0;i<cLen;i++)
                        for(int j=0;j<sizeof(columns)/sizeof(*columns);j++)
                                d->table[i][j]=table[i][columns[j]];

                        return d;
        }

        DataFrame* getRows(int* rows, int rLen) {
                int len=sizeof(rows)/sizeof(* rows);
                DataFrame *d=new DataFrame(len,rLen);
              
                for(int i=0;i<noCols;i++)
                        d->setColName(i,colNames[i]);

                for(int i=0;i<rLen;i++)
                        d->setRowName(i,rowNames[rows[i]]);

                for(int i=0;i<noCols; i++)
                        for(int j=0;j<len;j++)
                                d->table[i][j]=table[rows[j]][i];

                return d;
        }

        DataFrame* getRowsCols(int* rows, int rLen, int* cols, int cLen) {
                DataFrame *d=new DataFrame(rLen,cLen);
                for(int i=0;i<cLen;i++)
                        d->setColName(i,colNames[cols[i]]);

                for(int i=0;i<rLen;i++)
                        d->setRowName(i,rowNames[rows[i]]);

                for(int i=0;i<rLen;i++)
                        for(int j=0;j<cLen;j++)
                                d->table[i][j]=table[rows[i]][cols[j]];

                return d;

        }

        ~DataFrame() {
                delete table;
                delete rowNames;
                delete colNames;
        }
};

int main () {
        int c, r;
        int selectC[3];
        int selectR[10];
        cin >> r >> c;

        DataFrame* firstDF = new DataFrame(r,c);
        char* name;
        int ele;
        for(int i=0;i<c;i++) {
                cin>> name;
                firstDF->setColName(i,name);
        }

        for(int i=0;i<r;i++) {
                cin>> name;
                firstDF->setRowName(i,name);
        }
        for(int i=0;i<r;i++){
                for(int j=0;j<c;j++) {
                        cin>>ele;
                        firstDF->table[i][j]=ele;
                }
        }

        return 0;

}


Related Solutions

Problem: Implement a class named StringParser, along with specified methods, that can read words from a...
Problem: Implement a class named StringParser, along with specified methods, that can read words from a text file, parse those words (such as finding palindromes), and write those words to a new file More specifically: 1. Write the StringParser class so that it has the following methods: a) public static void main(String[] args) -- The main driver of the program. Prompts the user for an input file, an output file, and a choice for what kinds of words to output....
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide the accessor and mutator methods for all instance variables, and provide/override the toString methods. -Create a Customer class that has the attributes of name and age. Provide a method named importanceLevel. Based on the requirements below, I would make this method abstract. -Extend Customer to two subclasses: FlightCustomer, and RetailCustomer -FlightCustomer attributes: ticketPrice, seatNumber (The seat number should be a randomly generated number between...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
The Objective is to create a Custom LinkList. Implement the following methods Class Name: CustomLinkList Methods...
The Objective is to create a Custom LinkList. Implement the following methods Class Name: CustomLinkList Methods void insert (String data) – insert it the item to the last row void delete() - Deletes the last row boolean exists() - returns a Boolean if found String[] toArray() String getTail() – returns the last record  String getHead () – return the 1st record Extra Credit  int delete (String data) – delete an item in the link by position.  If an item is successfully the delete method return the number 1, else return the number 0
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of the class. I have added the Javadoc comments but please feel free to format them if they are done incorrectly. public class Nickel implements Comparable { private int year; /** * The monetary value of a nickel in cents. */ public final int CENTS = 5; /** * Initializes this nickel to have the specified issue year. * * @param year * * @pre....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT