Sale!

CSED 232 Assignment # 3 Inheritance & polymorphism Solved

Original price was: $35.00.Current price is: $29.00.

Download Details:

  • Name: assn3-b74vos.zip
  • Type: zip
  • Size: 782.39 KB

Category:

Description

5/5 - (1 vote)

 

2. Program Design and Implementation ● Are the variables and algorithms designed well to satisfy the requirements? ● Are all the detailed conditions presented in the problem satisfied? ● Has the design been properly implemented using the required language? 3. Program Readability ● Is the program written in a readable and understandable manner? ● Is it easy to understand what the variable names mean? ● Is the program source code well commented to make it easy to understand? 4. Report Structure, Content, and Format ● Is the report well-written, easy to understand, and easy to read? ● Did the report format follow the correct format? If you copy or simply modify someone else’s program or a program found on the Internet and submit it, you will definitely receive an ‘F’ grade. If such misconduct is discovered, you may be subject to additional penalties according to the standards set by the department. 2 ASCII Art ASCII art is an art form that creates pictures or shapes using only ASCII characters. For example, if you place complex letters in the dark areas of a random photograph and simple letters in the light areas, as shown in Figure 1, the picture made up of only letters will appear as shown below. The goal of this assignment is to create a program that generates simple ASCII art using class inheritance and polymorphism. [Figure 1. ASCII art example] The classes and inheritance relationships used to solve the problem are as shown in Figure 2 below. The arrows indicate inheritance, and the class with the direction indicator is the parent class. For example, the classic class inherits the artist class. [Figure 2. Class diagram] 3 The following restrictions apply to the implementation of the program. ● The member variables and functions specified in Figure 2 must be implemented and used. ● The arguments and return types of the member functions specified in Figure 2 must be observed. ● You cannot define and use classes other than those specified in Figure 2. ● You cannot define and use functions other than those provided. ● You can freely add member functions and member variables in addition to the member functions (methods) and member variables specified in the class diagram. ● The provided main function must never be modified. A detailed description of the classes and main functions depicted in Figure 2 is as follows. int main(int argc, char *argv[]) When int and char*[] are defined as arguments in the C++ main function, the first argument is the number of arguments received when the program is executed, and the second argument has the actual argument values. For example, if you run any compiled program hello with the command ./hello 2 world 1234, argc will be 4, argv[1] will be 2, argv[2] will be world, and argv[3] will be 1234. Note that the executable file path is automatically assigned to argv[0], so argc will be 4, not 3. The final compiled program in this assignment receives three arguments when executed. The first argument is the path of the input file, the second argument is the path of the configuration file, and the third argument is the path of the output file. The given main function is as follows and must not be modified under any circumstances. int main(int argc, char *argv[]) { if (argc != 4) { cout << “argc is not 4, but ” << argc << endl; throw; } // CREATE PARSER parser p; // LOAD IMAGE AND CONFIG vector tokens = p.load_image(argv[1]); vector configs = p.load_config(argv[2]); 4 string style_target = configs[0]; string drawer_target = configs[1]; char *path_output = argv[3]; int width = tokens[0]; int height = tokens[1]; vector vals = {tokens.begin() + 2, tokens.end()}; // CREATE ARTIST artist *style; if (style_target == “classic”) { style = new classic(width, height, vals); } else if (style_target == “iclassic”) { style = new iclassic(width, height, vals); } else if (style_target == “sobelx”) { style = new sobelx(width, height, vals); } else if (style_target == “sobely”) { style = new sobely(width, height, vals); } else if (style_target == “gradient”) { style = new gradient(width, height, vals); } else { throw; } // CREATE DRAWER drawer *d; if (drawer_target == “drawer”) { d = new drawer(style); } else if (drawer_target == “upsample”) { d = new upsample(style); } else if (drawer_target == “downsample”) { d = new downsample(style); } else if (drawer_target == “scale”) { int scale_x = stoi(configs[2]); int scale_y = stoi(configs[3]); d = new scale(style, scale_x, scale_y); } else { throw; } // PERFORM DRAWING string output = d->draw(); cout << output; // WRITE OUTPUT p.write_result(path_output, output); delete d; return 0; } 5 vector parser::load_image(const char*) 인자로 input file의 경로를 받고 해당 파일의 정보를 불러온다. input 파일의 내용은 일련의 숫자들이 delimiter ‘|’ 을 통해 나눠져 있다. 예를 들어서 input 파일의 내용이 3|2|101|102|103|104|105|106 와 같다면, 해당 함수의 return 값은 vector { 3 ,2, 101, 102, 103, 104, 105, 106 } 이 된다. 여기서 처음 두 값은 그려질 그림의 width와 height를 의미하고 나머지 값은 각 좌표에 해당되는 pixel 값이다. 예를 들어서 위 정보는 101 (0,0) 102 (1,0) 103 (2,0) 104 (0,1) 105 (1,1) 106 (2,1) 와 같이 시각화 될 수 있다. 여기서 괄호 안의 정보는 x,y 좌표를 의미한다. 이후 artist와 drawer class를 통해서 해당 값이 특정 ASCII 문자로 변환되며 그림을 그리게 된다. vector parser::load_config(const char*) 인자로 config file의 경로를 받고 해당 정보를 불러온다. config 파일의 내용은 일련의 text들이 delimiter ‘|’ 을 통해 나눠져 있다. 첫 번째 값은 사용될 artist class의 이름, 두 번째 값은 사용될 drawer class의 이름을 의미한다. 예를 들어서 config 파일의 내용이 iclassic|upsample 와 같다면, 해당 함수의 return 값은 vector { “iclassic”, “upsample”} 이 된다. void parser::write_result(const char*, const string&) 첫 번째 인자로 저장할 파일 경로, 두 번째 인자로 저장할 내용이 들어온다. 예를 들어서 p.write_result(“output.txt”,”hello world”) 를 호출하면 output.txt파일이 “hello world”라는 내용이 담겨서 생성된다. 해당 함수로 최종 결과를 저장하게 된다. 예시는 아래와 같으며 예시를 그대로 사용해도 무방하다. 6 void parser::write_result(const char *path, const string& contents) { ofstream myfile; myfile.open(path); myfile << contents; myfile.close(); } artist(int, int, const std::vector&) artist class는 그림의 스타일을 정의하는 class다. 다양한 스타일 class들이 artist class를 상속받아서 자신만의 스타일을 정의하게 될 것이다. 그리고 이후 설명할 drawer class가 artist class의 instance를 인자로 받아서 최종적인 그림을 그리게 된다. 생성자는 첫 번째 인자로 그림의 width, 두 번째 인자로 그림의 height, 세 번째 인자로 그림의 각 좌표에 해당하는 pixel 값을 받는다. 즉, input 파일 parsing 결과를 인자로 받는다. virtual char mapper(int, int) 첫 번째 인자로 x coordinate, 두 번째 인자로 y coordinate을 받고, 사용될 ASCII 문자를 return한다.Since it is a virtual method for implementing polymorphism, it is not implemented in the class itself but in the inherited class. char classic::mapper(int, int) The mapper of the classic class implements the most common ASCII Art mapping. When an image is displayed, the smaller the pixel value, the darker it appears. Based on this observation, the lower the pixel value, the more visually dense the ASCII character is substituted to express the image. Specifically, classic::mapper uses 15 ASCII characters ‘@’ ‘&’ ‘%’ ‘W’ ‘X’ ‘A’ ‘H’ ‘O’ ‘T’ ‘*’ ‘^’ ‘+’ ‘-‘ ‘.’ ‘ ‘ to represent pixels. It corresponds to the case where the pixel value is low from the left, and each character evenly covers 17 values ​​between [0, 254]. For example, ‘@’ covers [0, 16], and ‘&’ covers [17, 33]. As an exception, only the last 18 characters cover the range of [238, 255]. Examples of each pixel being converted to an ASCII character are 2 18 @ & 18 37 & %. For reference, the ASCII art in Figure 1 is the result drawn using classic::mapper. 7 char iclassic::mapper(int, int) iclassic is an abbreviation for inverted classic. iclassic::mapper uses the same logic as classic::mapper except that the order of ASCII characters is reversed. For example, ‘ ‘ covers [0, 16] and ‘@’ covers [238, 255]. char sobelx::mapper(int, int) The sobel operation is an operation used to detect edges in an image. In this assignment, sobelx::mapper returns ‘|’ if the difference from the adjacent pixel in the positive x-axis direction from any pixel is 50 or more, otherwise it returns the space character ‘ ‘. For example, for the pixel values ​​on the left, like 100 160 120 | 100 30 100 | | 100 100 170 | , the same ASCII character as on the right is drawn. char sobely::mapper(int, int) It differs in two details from the logic used in sobelx. First, it considers the y-axis direction, not the x-axis. And the returned character is ‘-‘, not ‘|’. char gradient::mapper(int, int) gradient::mapper mirrors the logic of sobelx::mapper and sobely::mapper. Additionally, it returns ‘+’ if the difference with the adjacent pixel in both the positive x and y directions is 50 or more. For example, 100 160 120 | – 100 30 100 | + – 100 100 170 | , the pixel values ​​on the left are drawn with the same ASCII character as on the right. drawer(artist *) 8 The drawer class uses the methods of the artist to create the final drawn string. Therefore, the drawer constructor receives an artist pointer instance as its first argument. string drawer::draw() Returns a string containing the final ASCII character image. string downsample::draw() Upsample and downsample are methods used to enlarge and reduce the size of images. In this assignment, we will mimic nearest-neighbor based upsample and downsample through downsample::draw and upsample::draw. downsample::draw() reduces the size of the image drawn by drawer::draw() by half. Specifically, the coordinate (0, 0) is always drawn, and characters with odd values ​​​​based on that coordinate are ignored. An example is @ & % & @ % & % & % & & % & % & % & %, where the left is the original and the right is the downsampled result. string upsample::draw() Doubles the size of the image, the opposite of downsample::draw(). Repeats each character during the enlargement process. For example, @ % @ @ % % & & @ @ % % & & & & & & & & &, where the left side is the original and the right side is the upsample result. 9 scale(artist *, int, int) The constructor of the scale class exceptionally receives two additional int arguments in addition to artist*. The two int arguments represent the values ​​to be expanded and reduced in the x and y axes, respectively. In this case, the config file must contain four pieces of information instead of two, and the third and fourth pieces of information are passed as arguments to the scale constructor. This content has already been written in the main function. string scale::draw() If downsample::draw() and upsample::draw() simply double the expansion and reduction without distinguishing the axes, scale::draw() performs expansion and reduction with a fixed magnification for each axis. If the scale is a natural number, the image is scaled by that value. If the scale is a negative integer, the negative reciprocal of that value is used as the scale. Lastly, 0 is not allowed. For example, if the scales for the x-axis and y-axis are 3 and -2, the image is scaled by 3 times in the x-axis and 1/2 times in the y-axis. The following examples show the program’s input and the corresponding output. @@@@%%%WWXXXHHHOOTTT***++—…. @@@@%%%WWXXXHHHOOTTT***++—…. @@@@%%%WWXXXHHHOOTTT***++—….@@@@%%%WWXXXHHHOOTTT***++—…. @@@@%%%WWXXXHHHOOTTT***++—…. @@@@%%%WWXXXHHHOOTTT***++—…. @@@@%%%WWXXXHHHOOTTT***++—…. @@@@%%%WWXXXHHHOOTTT***++—…. ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ ….—++***TTTOOHHHXXXWW%%%@@@@ output using input1.txt and confit1.txt 10 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@&^–+^..^.HHX@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@*–…–.–. .+.*^^^+-@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@.-^.-…-++^^-.+.-+-.—.-.@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@&-.*…… ….. ..+…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XAW%WX^TOHAAAAHHOOAWTHA@@@@@@@@@@@@@@ @@@@@@@@@@@@@AX%WOHOHAAXAHXAAW%WHAT*HAHHHHOTOXX*O@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@WHHHHAAXWWWXHO^^+-^TOHHHOOTOAX@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@HHHHAXXXXXWXXHAOOTOOOOOOOOHW@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@AHHHHAAXWWO**HT*^THOOHOOOAX@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@XOOHOAXXXWAXXXHHHTOAAAHTHTO@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@WOT^OOOAHHOT*^^*TOHHOOOOTTO@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@%AO*^-*OHHAAHAAAAAHOT****OH@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@AXAO*^^++*OTOTOTT^^^^^^TOHA@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@HXWWAHOT^+++++^^+++^^^*TOHHAX@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@OAAWWXXAHOO*^++^+++^^**TOOHHHXXA@@@@@@@@@@@@@@@@@ output using input2.txt and confit2.txt 최종적으로 풀어야하는 문제는 아래와 같다. 문제 1,2,3은 코드 구현 문제로 test case를 통해서 채점 되고, 문제 4,5는 보고서에 작성한다.Problem 1> Implement a parser class. Problem 2> Implement an artist class, a class that inherits from artist, and a drawer class. Problem 3> Implement classes that inherit from the drawer class. Problem 4> Explain why the drawer constructor takes artist* as an argument instead of artist. (Write in the report.) Problem 5> Inherit the artist class, define your own style, and print the image. (Write in the report.) Problem 6> Discuss how the presented classes can be improved from an object-oriented programming perspective. (Write in the report.) 11