Question
In c++ GIVEN CODE: Location.h #ifndef _LOCATION_H_ #define _LOCATION_H_ #include using namespace std; class Location { public : // Default constructor Location(); // Default constructor
In c++
GIVEN CODE:
Location.h
#ifndef _LOCATION_H_ #define _LOCATION_H_ #includeusing namespace std; class Location { public : // Default constructor Location(); // Default constructor Location(int _x,int _y); int getX(); // returns the x coordinate int getY(); // returns the y coordinate // computes the Euclidean distance float computeDistance(const Location& loc); // returns the location coordinates in the following // string format: (x,y) string toString(); private : int x; // variable for x coordinate int y; // variable for y coordinate }; // end of class definition #endif
Location.cpp
#include#include #include"Location.h" using namespace std; // Default constructor Location::Location(){ x = 0; y = 0; } // parameterized constructor Location::Location(int _x,int _y){ x = _x; y = _y; } int Location::getX() // returns the x coordinate { return x; } int Location::getY() // returns the y coordinate { return y; } // computes the Euclidean distance float Location::computeDistance(const Location& loc) { return sqrt(pow(x - loc.x, 2) + pow(y - loc.y, 2)); } // returns the location coordinates in the following // string format: (x,y) string Location::toString(){ string loc = "(" + to_string(x) + "," + to_string(y) + ")"; return loc; }
MovieFile.h
#ifndef _MOVIE_H_ #define _MOVIE_H_ #includeusing namespace std; class MovieFile { public : // the constructor should receive 3 parameters: // the name of a movie file, its duration in minutes, // and the file size with a default value of 0 MovieFile(string, int, int); // copy constructor that copies all information from the input object MovieFile(const MovieFile& other); // returns the filename string getName(); // returns duration of the movie int getDuration(); // returns file size int getFileSize(); // sets file size void setFileSize(int); // adds the input value to the current file size. void appendFileSize(int); private : string title; // title of movie long duration; // duration of movie int size; // size of a movie file (in MB) }; #endif
MovieFile.cpp
#include#include "MovieFile.h" using namespace std; // the constructor should receive 3 parameters: // the name of a movie file, its duration in minutes, // and the file size with a default value of 0 MovieFile::MovieFile(string _title, int _dur, int _size = 0){ title = _title; duration = _dur; size = _size; } // copy constructor that copies all information from the input object MovieFile::MovieFile(const MovieFile& other){ title = other.title; duration = other.duration; size = other.size; } // returns the filename string MovieFile::getName(){ return title; } // returns duration of the movie int MovieFile::getDuration(){ return duration; } // returns file size int MovieFile::getFileSize(){ return size; } // sets file size void MovieFile::setFileSize(int _size){ size = _size; } // adds the input value to the current file size. void MovieFile::appendFileSize(int _size){ size += _size; }
main.cpp
#include#include "Location.h" #include "MovieFile.h" using namespace std; int main() { //-------------------------------------------- // Example code: Location class //-------------------------------------------- Location* loc1 = new Location(30,10); Location* loc2 = new Location(20,20); cout << loc1->computeDistance(*loc2) << endl; //output: 14.1421 cout << loc2->toString() << endl; //output: (20,20) delete loc1; delete loc2; loc1 = NULL; loc2 = NULL; //-------------------------------------------- // Example code: MovieFile class //-------------------------------------------- MovieFile* movie1 = new MovieFile("Black Panther", 120, 350); cout << movie1->getFileSize() << endl; //output: 350 movie1->setFileSize(50) ; cout << movie1->getFileSize() << endl; //output: 50 delete movie1; movie1 = nullptr; return 0;
}
/UserRequest.h #ifndef _USERREQUEST_H_ #define _USERREQUEST_H_ #includeusing namespace std; class UserRequest { Location* location; MovieFile* movie; int index,requestId; int complete; public: //Default Constructor UserRequest(Location* loc, int fileIndex, int id); //Destructor ~UserRequest(); //Function to set object of location Location* getLocation(); //Function to get movie index int getMovieIndex(); //Function to get requestId int getRequestId(); //Function to check download is complete or not bool isComplete(); //Function to check is it is bool or not void setComplete(bool); //function to print size and copy content of video to userid void download(MovieFile video, int bytes); //Function to append new size data to existing void download(int data); //return size int getAmountDownloaded(); //check for downloaded file MovieFile* getDownloadedFile(); }; //end of class definition #endif
//UserRequest.cpp #include#include "moviefile.h" #include "location.h" #include "userrequest.h" using namespace std; //parameterized constructor UserRequest::UserRequest(Location* loc, int fileIndex, int id) { location=loc; index=fileIndex; requestId=id; } //destructor UserRequest::~UserRequest() { } //return location Location* UserRequest::getLocation() { return location; } //return movie index int UserRequest::getMovieIndex() { return index; } //return requestId int UserRequest::getRequestId() { return requestId; } //set request status bool UserRequest::isComplete() { if(complete!=0) return 1; return 0; } //get request status void UserRequest::setComplete(bool request) { complete=request; } //print bytes left and copy the content of video to movie void UserRequest::download(MovieFile video, int bytes) { cout<<"Size transferred in the initial download : "< appendFileSize(data); } //get size downloaded int UserRequest::getAmountDownloaded() { movie->getFileSize(); } //check for file already downloaded or not MovieFile* UserRequest::getDownloadedFile() { if(movie) return movie; else return NULL; }
Question:
A server is characterized by its name, location, download speed, maximum number
of user requests it can handle, and a collection of movies in its storage. Assume
that the download speed (MB/s) is consistent throughout all downloads of
a specific server. You have been given a class specification file, Server.h, that
is well-commented which you should implement in Server.cpp. Do not modify
the given Server.h file as it will be overwritten on the Automated Marking
Tool.
The following provides further information on selected functions:
Member Functions:
void assignRequest(UserRequest* userReq): makes a shallow copy of the
UserRequest object and adds it to the servers list of requests.
void runDownloads(): the server should loop through the list of its user requests,
and download a chunk of data according to the servers download
speed. Assume that the function will be called once per second. For example,
if a UserRequest object wishes to download a file of size 135MB, on
a server that has a download speed of 20MB/s, that means every time the
runDownloads function runs, 20MB of data will be added to that UserRequest
, until it has strictly transferred a total size of 135MB (and not a megabyte
more). The function should call the relevant UserRequests download function
depending on whether a download has been previously initiated or not.
At the end of transferring the chunk of data, the function should check if
the UserRequest has been fulfilled, and if so, it should set the necessary
flag.
void dropCompletedRequests(): The objective of this function is to drop all
user requests that have been fulfilled, and remove them from the servers
list of connnected users. This will require you to shift the elements in
the requests array (member variable) to make sure that the elements are
stored in contiguous locations (i.e. in sequential order with no empty elements
between them).
void printInfo(): This function prints out an abridged version of the servers
details. It should print out a single line with the following information: the
name and location of the server, and the number of currently connected
users/active requests. Here is an example:
Server_PTA, Location: (40,70), #Connections: 4
void printDetailedInfo(): This function prints out a detailed version of
the servers details. It should print out the name, location and number of
connected user requests in a single line. It should then print out the list of
all user requests and the amount of data already downloaded. Indent the
user request information (you can use a tab or any number of spaces). See
below for an example.
Server_PTA, Location: (40,70), #Connections: 4
Request 1, Downloaded data: 50MB
Request 2, Downloaded data: 30MB
Request 3, Downloaded data: 20MB
Request 4, Downloaded data: 10MB
Remember to properly discard allocated memory when the class ceases to exist.
GIVEN SERVER.H FILE:
#ifndef SERVER_H_
#define SERVER_H_
#include "UserRequest.h"
class Server {
private:
string name; //name of server
double downloadSpeed; //consistent speed at which server transfers data
int capacity; //maximum number of requests that the server can handle
int numConnectedUsers; //tracks number of requests that the server handling
int numMovies; //number of movies loaded on this server
UserRequest** requests; //array of requests that has been assigned to this server
Location* location; //location of server
MovieFile** movies; //array of movies that can be downloaded from this server
public:
//Creates a shallow copy of the movie files, and assigns all other relevant variables
Server(string name, int x, int y, double speed, int maxUsers, int movieCount, MovieFile** files);
//Should deallocate any dynamic memory
~Server();
//-- accessor functions - self-explanatory
int getConnectedUsersCount();
string getName();
int getCapacity();
int getDownloadSpeed();
Location* getLocation();
//Returns TRUE if the server can accept any further requests, and FALSE otherwise
bool canAcceptRequests();
//Makes a shallow copy of the UserRequest object and adds it to the server's list of requests
void assignRequest(UserRequest* request);
//Runs the dowload process for all requests where a chunk of data is sent to the respective requests
//(see spec for more details)
void runDownloads();
//Drops all user requests that have been fulfilled, and removes them from the request list.
//The resulting elements in the request list should be stored in contiguous locations
// (i.e. in sequential order with no empty elements between them).
void dropCompletedRequests();
//Prints out an abridged version of the server's details (see spec for more details)
void printInfo();
//Prints out the server's details and info about the current user requests (see spec for more details)
void printDetailedInfo();
};
#endif /* SERVER_H_ */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started