C++ assignment. Please read the below carefully: For this assignment, we will wr
ID: 3766210 • Letter: C
Question
C++ assignment.
Please read the below carefully:
For this assignment, we will write an application that uses the three design patterns discussed in this weeks lecture. Specifically, you are a professor and you are writing an application to help you manage information about your students. The class of interest is called ClassRecord. It has a method called buildClassBook that takes an array of strings. This method, supposedly, does many things, including sorting the names in the array.
Download file firstClassRecord.cpp. It uses a bubble sort algorithm to sort and print the names. There are many ways to sort an array, and bubble sort is the slowest. Let’s design ClassRecord so that it could use different sorting algorithms for the students array.
Reify method sort. Use the Strategy Design Pattern to allow ClassRecord to seamlessly use one of several possible sort strategies including QuickSort, MergeSort, and HeapSort.
Add another class called CompositeSort. Use the Composite Design Pattern that somehow combines different sort methodologies based on some heuristic of the array. (No complex heuristic algorithm need be implemented)
Implement ClassRecord as a Singleton class.
**** Note: This exercise is a vehicle for experimenting with three design patterns, not producing an application. Do not spend time researching the above sorting algorithms. It is not required that you implement any sort code. All of your sort algorithms can be implemented as skeletons:
void sort (string names []) {
cout << “sorting with merge sort” << endl;
}
Your final source will include all the classes and a main program that acts as a small driver for the application. Submissions that do not compile will receive a grade of zero.
Here is the source code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class ClassRecord {
public:
void buildClassBook (string students[100]) {
// sort students alphabetically
sort (students) ;
// do other things to prep class book
}
void sort (string students[100]) {
int i;
for (i=0; i<100; i++)
if (students[i].length() == 0)
break;
int length = i;
bool clean ;
do {
clean = true;
for (int j=1; j<length; j++)
if (students[j] < students[j-1]) {
string temp = students[j];
students[j] = students[j-1];
students[j-1] = temp;
clean = false;
}
} while (!clean) ;
for (int k = 0; k< length; k++)
cout << students[k] << endl;
}
};
int main () {
ClassRecord record;
string students [100] = {"Bill", "Jose", "Philipe", "Chaing", "Louise",
"Mary", "Lisa"};
record.buildClassBook(students);
}
Explanation / Answer
The Singleton's purpose is to control object creation, limiting the number of obejcts to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.
For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.
Implementing Singletons
The easiest implementation consists of a private constructor and a field to hold its result,
and a static accessor method with a name like getInstance().
The private field can be assigned from within a static initializer block or, more simply, using an initializer.
The getInstance( ) method (which must be public) then simply returns this instance
// File Name: Singleton.java
public class Singleton {
private static Singleton singleton = new Singleton( );
/* A private Constructor prevents any other
* class from instantiating.
*/
private Singleton(){ }
/* Static 'instance' method */
public static Singleton getInstance( ) {
return singleton;
}
/* Other methods protected by singleton-ness */
protected static void demoMethod( ) {
System.out.println("demoMethod for singleton");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.