Question
Please use C++ for this program. Please read the problem and follow the instruction in main.cpp for this program. Problem: - Design an abstract class
Please use C++ for this program.
Please read the problem and follow the instruction in main.cpp for this program.
Problem:
- Design an abstract class called Shape:
- Private variables double x and y, this is x and y position in postscript file
- Constructor shape (x, y) that assign x and y values
- function area take no parameters return double const equal zero, pure virtual function
- function draw take ostream file return void and equal zero, pure virtual function
- make deconstruct ~Shape
- Hint: you need to use virtualthe question is okay plz
- Design class Circle that inherit class shape functions
- private variable x, y, radius
- Make the following functions:
- Circle constructor that take x, y, radius, Note that x and y are assigned to Shape(x,y)
- for example constructor_name(double x, double y) : Shape(x,y)
- draw that take ostream file to write postscript instruction the function return void
- area take no parameters and return double area of the circle
- ~Circle deconstruction
- Design class Rect that inherit class shape with same functions as class Circle
- private variables are x, y, weight, height
- Design class FilledRect that inherit class shape with same functions as class Circle
- private variables are x, y, weight, height
- Hint for Circle, Rect, and FilledRect: you will need to use override keyword
- e.g.: data_return_type function_name(inputs) override {}
- Design class Drawing that take all shapes created and save it in vectors. Then use draw function for each Shape to write test.ps file with postscripts instructions
- it has private variables of type: ofstream and vector
- Drawing constructor that take location to open and create new "test.ps" file and write postscript line in it
- void add function that push_back new shapes to vector. It takes "Polymorphism" shape
- void draw function take no parameter that loop through all shapes and write postscript instruction for each shape
- void showArea take no parameters and loop inside vector to run area function for each shape class
- void setrgb function take three double for red, green, and blue. It writes postscript to change the color for all shapes
- All postscript line to draw shapes are given in main.cpp comments
## Postscript language
Check Wiki. for some background about Postscript (https://en.wikipedia.org/wiki/PostScript) \
You can use online website to view .ps file (https://products.aspose.app/page/viewer/ps) \
Postscript line you need to use to draw shapes are given in the code.
main.cpp:
#include#include #include using namespace std; // -------------- Design Shape class here --------------- // ----------------------------------------------------------- // Shape abstract class with private double variable x and y that indicate shape position // make the following functions // 1. Constructor shape (x, y) // 2. function area take no parameters return double const equal zero - pure virtual function // 3. function draw take ostream file return void equal zero - pure virtual function // 4. make deconstruct ~Shape // -------------- Design your Circle class here --------------- // ----------------------------------------------------------- // Class Circle inherit Class Shape // private variable x, y, radius // Make the following functions: // Circle constructor that take x, y, radius, Note that x and y are assigned to Shape(x,y) // for example constructor_name(double x, double y) : Shape(x,y) // draw that take ostream file to write postscript instruction the function return void // postscript: // x y radius 0 360 arc \n // stroke \n // area take no parameters and return double // ~Circle deconstruction // -------------- Design your Rect class here --------------- // ----------------------------------------------------------- // Class Rect inherit Class Shape // private variable x, y, width, and height // Make the following functions: // Rect constructor that take x, y, width, height // draw that take ostream file to write postscript instruction the function return void // draw that take ostream file to write postscript instruction the function return void // postscript: // x y moveto \n // x+w y lineto \n // x+w y+h lineto \n // x y+h lineto \n // closepath \n // stroke \n // area take no parameters and return double // ~Rect deconstruction // -------------- Design your FilledRect class here --------------- // ----------------------------------------------------------- // Class FilledRect inherit Class Shape // private variable x, y, width, and height // Make the following functions: // FilledRect constructor that take x, y, width, height // draw that take ostream file to write postscript instruction the function return void // postscript: // x y moveto \n // x+w y lineto \n // x+w y+h lineto \n // x y+h lineto \n // closepath \n // fill \n // area take no parameters and return double // ~FilledRect deconstruction // -------------- Design your Drawing class here --------------- // ----------------------------------------------------------- // private variables ofstream and vector // Make the following functions: // Drawing constructor as following (is given): // Drawing(string filename) : f(filename.c_str()), shapes() {} // where f is ofstream // void add that take shape and push_back to vector // void draw take no parameters and loop inside vector to rum draw function for each shape class // void showArea take no parameters and loop inside vector to run area function for each shape class // void setrgb take three double for red, green, and blue and write postscript line to change color // postscript: // r_value g_value b_value setrgbcolor \n int main() { cout << "########" << endl; cout << "Main Problem" << endl; cout << "########" << endl; // set path to create .ps file to write postscript instructions Drawing d("Write_full_path/test.ps"); // set color d.setrgb(1,0,0); // set drawing color to be bright red: 1 0 0 setrgbcolor // add FilledRect d.add(new FilledRect(100.0, 550.0, 200.0, 50)); // x y moveto x y lineto ... fill // add Rect d.add(new Rect(200.0, 650.0, 200.0, 50)); // x y moveto x y lineto ... stroke // add Circle d.add(new Circle(300,300, 100)); // centered in 300 , 300 pixel with radius // draw all shapes added to d d.draw(); // print out all shapes area d.showArea(); cout << "====[ end ]====" << endl; cout << " " << endl; return 0; } State the conditions for a relation to be a partial order. [3 marks] A partition of a natural number n is a collection of natural numbers (possibly including duplicates and in any order) whose sum is n. Let Pn be the set of partitions of n; for example, P4 = {(4),(3, 1),(2, 2),(2, 1, 1),(1, 1, 1, 1)}. Order the partitions in Pn as follows: (a1, a2, . . . ar) 6 (b1, b2, . . . bs) if the (ai) and (bj ) can be rearranged so that b1 = a1 + a2 + · · · + ak1 b2 = ak1+1 + ak1+2 + · · · + ak2 bs−1 = aks−2+1 + aks−2+2 + · · · + aks−1 bs = aks−1+1 + aks−1+2 + · · · + ar Note that (2, 1, 1) 6 (3, 1), and (2, 1, 1) 6 (2, 2) but (3, 1) and (2, 2) cannot be compared. Show that 6 is a partial order on Pn. [4 marks] P5 has seven elements; draw the Hasse diagram for (P5, 6). [3 marks] 3 Operating Systems Operating systems need to be able to prevent applications from crashing or locking up the system, or from interfering with other applications. Which three kinds of hardware support do we require to accomplish this? Justify your answer. [6 marks] How do applications request that the operating system performs tasks on their behalf? [2 marks] What could we do if we did not have the requisite hardware support? Answer all five parts. (a) Prove that there are no integer solutions to x 5 − 3x 2 + 2x − 1 = 0. [4 marks] (b) Draw a picture of a deterministic finite automaton which accepts the language of strings matching a*ba. [4 marks] (c) Describe the operation of the Unix shell with reference to the process management system calls it makes use of. You might like to use pseudo-code or a diagram to aid your description. [4 marks] (d) What is the purpose of a formal specification language such as Z? [4 marks] (e) Describe ML's datatype declaration. Show an example of a function declaration that uses pattern matching on possible values of the data type. What is the maximum number of terms there can be in a minimal sum of products form of a function of n boolean variables? [2 marks] Consider a two-bit multiplier with inputs x1, x0, y1, y0 and outputs z3, z2, z1, z0 such that Z = Y × X where Z, Y, X are the positive integers represented by z3z2z1z0, y1y0 and x1x0 using the obvious representation. Find a minimal sum of products expression for each of z3, z2, z1 and z0. [10 marks] Comment on the complexity of building an eight-bit multiplier using a minimal sum of products form. [3 marks] Describe another way of building an eight-bit multiplier. You are required to build a circuit whose inputs are a data input D and a clock C. The data input D is used to pass a stream of bits into the circuit. Every 64 bits, an 8-bit synchronisation pattern 01100110 appears in the stream, followed by 56 bits of changing information. (a) Design a combinational circuit incorporated with an 8-bit shift register that recognises the synchronisation pattern and asserts a signal r for 1-bit time whenever the pattern appears. The following attempt at Java code might have been written by a beginner. Identify (but do not correct) as many of the mistakes as you can. Explain how each oddity you identify is either something that would prevent the program from compiling, something that would cause it to stop abruptly reporting failure at runtime, a reason why the program might not do anything sensible or just a stylistic oddity. //* A comment to start with: Exam:2000 */ public Class mycode.java { void static public fun main(String [junk]) begin Leaf tr = null; for (i=1; i>10; ++i) tr = new Node(i, tr) tr.print(); end; } class Leaf { integer value; Leaf(int value) { this = value; } public void print() { System.out.println(value); } } class Node extends Leaf { Leaf left, right; Node(leaf l, Leaf r) { left = l, right = r; } void print() { left.print(); System.out.println("val=" @ value); right.print(); } } [10 marks] 3 [TURN OVER CST.2000.1.4 SECTION B 5 Foundations of Computer Science State the time complexity of the lookup and update operations for each of the following: (a) association lists (b) binary search trees (c) functional arrays (implemented as binary trees) Use O-notation and include both the average-case and worst-case complexity. [6 marks] You are provided with the ML code for binary search trees, including the lookup and update operations. Use these operations to code a sorting function that works by repeatedly inserting elements of a list into a binary search tree, then converting the final binary search tree back into a list. [4 marks] Consider the following methods of sorting a list: (a) Locate the smallest element of the input. The output is this element followed by the result of recursively sorting the remaining elements. (b) Take the first 16 elements of the input and sort them using special hardware. Sort the remaining elements recursively. The output is the result of merging the two sorted lists. (c) Take the first 20% of the input elements. Sort them and the remaining 80% recursively. The output is the result of merging the two sorted lists. For each of these methods, state with justification the worst-case complexity in terms of the number of comparisons. An ordinary fluorescent light tube exhibits a lack of memory property in that its life expectancy does not depend on how long it has been working. A typical tube may be expected to work for another 5040 hours no matter how long it has been working so far. If a room contains eight such tubes and all are working, one may expect the first failure after 630 hours (5040/8). If the dud tube is not replaced, after how many more hours may one expect the second failure and (again assuming no replacement) after how many more may one expect the third failure? [2 marks] Consider a meeting room which is equipped with four light fittings, each equipped with two such tubes. The management has decided that the room lighting is acceptable provided at least one tube in each pair is working. As soon as a second tube fails in any one fitting, a maintenance crew replaces all dud tubes in the room. Starting with eight working tubes, the crew may be called out as early as the second failure or as late as the fifth failure. Let X be a random variable whose value r is the number of dud tubes at the moment the maintenance crew is called out. Clearly P(X = 0) = P(X = 1) = 0. Determine the values of P(X = 2), P(X = 3), P(X = 4) and P(X = 5). Express all four results as fractions. It may be assumed that all fittings are permanently switched on and that tubes fail independently. [12 marks] What is the expectation, E(X)? [4 marks] The management rounds the value of E(X) down to the nearest integer and uses the derived value for estimating the number of tubes that have to fail between successive call-outs of the maintenance crew and the time interval between such call-outs. What is this time interval in hours? [2 marks] 5 Probability An arbitrary value is loaded into an n-bit register. Let un be the probability that there are no instances of two (or more) adjacent bits being 0s. A hardware designer suspects (correctly) that un is related to un−1 and un−2. Write down the second-order difference equation which relates these terms. [8 marks] Justify the use of u0 = u1 = 1 as initial conditions. [2 marks] Solve the difference equation using these initial conditions. [10 marks] 4 CST.2000.2.5 SECTION D 6 Professional Practice and Ethics What moral obligations are incurred through becoming a member of a professional body such as the British Computer Society, and how would you justify these moral obligations? [20 marks] 7 Regular Languages and Finite Automata For each kind of regular expression over an alphabet Σ, define the language L(r) of strings matching a regular expression r of that kind. [4 marks] Define the language L(M) accepted by a deterministic finite automaton M. [2 marks] Prove that for every deterministic finite automaton M with alphabet of input symbols Σ it is possible to construct a regular expression r over Σ satisfying L(r) = L(M). [10 marks] Illustrate your proof by constructing such an r for the deterministic finite automaton with state set {0, 1, 2}, alphabet of input symbols {a, b}, initial state 0, accepting states 1 and 2, and next-state function (0, a) 7→ 2, (1, a) 7→ 1, (2, a) 7→ 0, (0, b) 7→ 1, (1, b) 7→ 0, (2, b) 7→ 2. I need help implementing the following public methods in the header file, language is c++: (Hashtable.h) #ifndef ORDEREDLINKEDLIST_H #define ORDEREDLINKEDLIST_H #include class Hashtable { private: int hash(int v) const; public: Hashtable(); //Constructs and empty hash table with a default capacity of 17 and a default load factor threshold of .65 Hashtable(int size); //Constructs and empty hash table with the given capacity and a default load factor threshold of .65 Hashtable(int, double); //Constructs and empty hash table with the given capacity and load factor threshold values Hashtable(const Hashtable& other); //creates a deep-copy of the given hashtable Hashtable& operator=(const Hashtable& other); //replaces the current hashtable with a deep-copy of the given hash table ~Hashtable(); //cleans up all allocated memory of the hashtable int size() const; //returns the number of items currently in the hashtable int capacity() const; //returns the capacity of the hashtable (i.e. the size of the array) double getLoadFactorThreshold() const; //returns the load factor threshold used to determine when to resize the hashtable. bool empty() const; //returns true if the table is empty or false otherwise void insert(const int); //Inserts the given value into the hashtable. Automatically resizes the table as necessary. void remove(int); //Removes the given value from the hashtable. If the value is not present it takes no action and throws no errors. bool contains(int) const; //Returns true if the given value is contained in the hashtable or false if the value is not in the hashtable. int indexOf(int) const; //Returns the index of the given value. // If the value is not in the hashtable, returns -1. This is not a normal mehtod for a hashtable and is here solely to test that your hashtable does quadratic probing. void clear(); //Removes all values from the hashtable, resetting it to an empty state static bool isPrime(int n); //Returns true if the value is a prime number. //One way to implement this method is to check if the number is divisible by 2-sqrt(n). // If it is then it is not prime. If it is not then it is prime. 2 and 3 must be tested separately. static int nextPrime(int n); //Returns the next prime number greater than or equal to n //One way to implement this method is to test if n is prime and then repeatedly increment n and test again if it is prime. }; #endif //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// (Hashtable.cpp) #include "Hashtable.h" int Hashtable::hash(int v) const{ return v % capacity(); } Hashtable::Hashtable() { } Hashtable::Hashtable(int capacity) { } Hashtable::Hashtable(int capacity, double threshold) { } Hashtable::Hashtable(const Hashtable& other) { } Hashtable& Hashtable::operator=(const Hashtable& other) { return *this; } Hashtable::~Hashtable() { } int Hashtable::size() const { return -1; } int Hashtable::capacity() const { return -1; } double Hashtable::getLoadFactorThreshold() const { return 0; } bool Hashtable::empty() const { return -1; } void Hashtable::insert(int value) { } void Hashtable::remove(int value) { } bool Hashtable::contains(int value) const{ return false; } int Hashtable::indexOf(int) const { return -2; } void Hashtable::clear()
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