Question

starting out with >>> C++ EARLY OBJECTS - Chapter 15 Programming Challenge #7 Write a program...

starting out with >>> C++ EARLY OBJECTS - Chapter 15 Programming Challenge #7

Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses courses and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle for the tenth time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: #include <1 stream> #include <string> #include <vector> #include <Windows . h> usina namespace std; /Iglobal consta

int Rw, Cl; // Direction of motion class drawShape public rect Shape public: virtual V ld draw shape() = 0; //get position vo

virtual void motion ); private: int color; int row Position, col Position; class rectangle public drawShape public: virtual v

draw 3hape o //compute new position getDirection (Rw, Cl); row Position + Rw; c r c r saved; draw shape (); //Constructor rec

SetconsoleTextAttribute (out_hand, getcolor )) getPosition(row Position, col Position); pos.xcol_Position; pos.x-row Position

recti.draw shape (); rect2.draw_shape ); dirl- -dirl; dir2 -dir2; rect1.set dir (O , dirl); ect2.set dir (0, dir2) for ( int

Gluserstemp.spidom.0151documentslvisual studio 20101Projects 1212Debug11212.exe Nunber of neetings at the middle: 1

Code:

#include <iostream>

#include <string>

#include <vector>

#include <Windows.h>

using namespace std;

//global constant
const HANDLE out_hand = GetStdHandle(STD_OUTPUT_HANDLE);


class rect_Shape

{

public:
  
   //set direction
virtual void set_dir(int rw, int cl)

{

Rw = rw;

Cl = cl;

}
   //get direction
void getDirection(int &rw, int &cl)

{

rw = Rw;

cl = Cl;

}

virtual void motion() = 0;

private:

int Rw, Cl; // Direction of motion

};


class drawShape : public rect_Shape

{

public:

virtual void draw_shape() = 0;
     
   //get position
void getPosition(int &row, int &col)

{

row = row_Position;

col = col_Position;

}
   //set position
void setPosition(int row, int col)

{

row_Position = row;

col_Position = col;

}
   //set color
void setColor(int col)

{

color = col;

}
   //get color
int getColor()

{

return color;

}

virtual void motion();


private:

int color;

int row_Position, col_Position;

};

class rectangle : public drawShape

{

public:

virtual void draw_shape();

rectangle(int row_Position, int col_Position, int wid, int hgt);

private:

int wid, hgt;

};

void drawShape::motion()

{
   //motion direction

int Rw, Cl;

int color_saved = color;

   //color 0 erases the shape
color = 0;   

draw_shape();

//compute new position

getDirection(Rw, Cl);

row_Position += Rw;

col_Position += Cl;


color = color_saved;

draw_shape();

}


//Constructor

rectangle::rectangle(int row_Position, int col_Position, int wid, int hgt)

{

setColor(14);

setPosition(row_Position, col_Position);

this->wid = wid;

this->hgt = hgt;

draw_shape();

}


//Draws a rectangle shape

void rectangle::draw_shape()

{

int row_Position, col_Position;

COORD pos;


SetConsoleTextAttribute(out_hand, getColor());

getPosition(row_Position, col_Position);

pos.X = col_Position;

pos.Y = row_Position;


for(int r = 0; r < hgt; r++)

{

SetConsoleCursorPosition(out_hand, pos);

for( int c = 0; c < wid; c++)

cout<< "*";

cout<<endl;

pos.Y++;

}


SetConsoleTextAttribute(out_hand, 7);

}

int main()

{

//Creates two rectangles

rectangle rect1(5, 0, 10, 5);

rectangle rect2(5, 70, 10, 5);

int meet = 0;   

int dir1 = -1, dir2 = 1;


rect1.draw_shape();

rect2.draw_shape();

do

{

dir1 = -dir1; dir2 = -dir2;

rect1.set_dir(0, dir1);

rect2.set_dir(0, dir2);   

for( int i = 0; i < 30; i++)

{

Sleep(100);

rect1.motion();

rect2.motion();

}

meet++;

COORD pos;

pos.X = 5;

pos.Y = 15;

SetConsoleCursorPosition(out_hand, pos);

cout<<"Number of meetings at the middle: "

<<(meet+1)/2<<endl;

}while(meet < 19);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
starting out with >>> C++ EARLY OBJECTS - Chapter 15 Programming Challenge #7 Write a program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Starting out with C++ Early Objects solution 8th edition, chapter 8 programming challenge 11... line case...

    Starting out with C++ Early Objects solution 8th edition, chapter 8 programming challenge 11... line case 2:cc.properWords(s) is giving me a build error... build error states "properWords" is not a char converter. How do I fix this?

  • C++ Programming Objects Problem Assignment: Programming challenge #1 on page 714: "File Head Program" Write a...

    C++ Programming Objects Problem Assignment: Programming challenge #1 on page 714: "File Head Program" Write a program that asks the user for the name of a file. The program should display the first ten lines of the file on the screen (the “head" of the file). If the file has fewer than ten lines, the entire file should be displayed, with a message indicating the entire file has been displayed. Lab Help: Use the following two files to test your...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • starting out with c++ early objects 7th ed. chapter 8 programming challenges #7

    Rainfall StatisticsWrite a modular program that analyzes a year's worth of rainfall data. In addition to main, the program should have a getData function that accepts the total rainfallfor each of 12 months from the user, and stores it in a double array. It should also have four value-returning functions that compute and return to main thetotalRainfall, averageRainfall, driestRainfall, and wettestMonth. These last two functions return the number of the month with the lowest and highest rainfall amounts,not the amount...

  • Programming Assignment 6: A Python Class, Attributes, Methods, and Objects Obiectives .Be able to...

    I need some help with programming this assignment. Programming Assignment 6: A Python Class, Attributes, Methods, and Objects Obiectives .Be able to write a Python class Be able to define class attributes Be able to define class methods .Be able to process input from a text file .Be able to write an application using objects The goal of this programming assignment is to develop a simple image processing application. The application will import a class that creates image objects with...

  • M01 PA C++ Classes and Objects INTRODUCTION: Write a C++ program that implements and utilizes a...

    M01 PA C++ Classes and Objects INTRODUCTION: Write a C++ program that implements and utilizes a Stereo Receiver Class/Object. INCLUDE IN YOUR ASSIGNMENT At the top of each of your C++ programs, you should have at least four lines of documentation: Program name (the C++ file name(s)), Author (your name), Date last updated, and Purpose (a brief description of what the program accomplishes). Here is an example: // Program name: tictactoe.cpp // Author: Rainbow Dash // Date last updated: 5/26/2016...

  • Number 16. Chapter 3 Problem Comes from "Starting out with >>> C++ From Control Structures through...

    Number 16. Chapter 3 Problem Comes from "Starting out with >>> C++ From Control Structures through Objects" Ninth Edition. Checking work Programming Challenges 147 16. Senior Citizen Property Tax Madison County provides a $5,000 homeowner exemption for its senior citizens. For example, if a senior's house is valued at $158,000, its assessed value would be $94,800, as explained above. However, he would only pay tax on $89,800. At last year's tax rate of $2.64 for cach $100 of assessed value,...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • Please Write the following program in c# You are to write an application which will create...

    Please Write the following program in c# You are to write an application which will create a company, add agents (their name, ID, and weekly sales) to that company, and printout the details of all agents in the company. The application will contain three classes: Agent.cs, Company.cs, and CompanyTest.cs (this class is already provided). Flow of the application: The CompanyTest class creates an object of the Company class and reserves space for five agents. At this point if you call...

  • RADICO’S CHALLENGE The executive staff at Radico Corporation was quite pleased with the one-day training program...

    RADICO’S CHALLENGE The executive staff at Radico Corporation was quite pleased with the one-day training program they attended on the benefits of using agile and Scrum on some of their projects. Radico provided products and services to both public and private sector clients, almost all of it through competitive bidding. IT was not required for any of the products and services Radico provided. Agile and Scrum had proven to be successful on internal IT projects, but there were some concerns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT