Gravity, .h & .cpp

Hi everyone, I've got this assignment that I been stuck on for days. I've already tried to google some answers and asking friends but that haven't helped. Please help me, even one line is helpful.

The assignment:

One part of a bigger game is that the player should roll out balls at a certain speed from one
trampoline. When the ball leaves the trampoline, it continues at the same speed in
horizontal axis (x-axis) while, due to gravity, it accelerates downwards
(Y-axis). This results in the ball describing a path called a parable.
You can totally ignore the air resistance to which the ball is exposed.
For example, to see a clip describing the movement, you can look at, fast forward too 3:47 in th clip
https://www.youtube.com/watch?v=EulXDXc73QE

The horizontal velocity, for example, is unchanged throughout the movement.

The vertical speed view is accelerated downward and the speed at a certain time
is given by

vy = 9.82 * time

The total velocity is the result of vx and view. Use Pythagoras rate.

The time is specified in seconds and the speed in meters per second.

Class Ball

Through this system there should be the opportunity to create a ball by entering the horizontal speed of the ball's center is just has when it leaves the trampoline.
Furthermore, for a ball it should be possible to:
• Obtain the horizontal speed
• Obtain vertical speed for a certain time after leaving the trampoline
• Obtain total speed for a certain time after leaving the trampoline
• Obtain a string containing horizontal velocity, vertical velocity as well
total speed a certain time after leaving the trampoline
• Compare with another ball using the > operator.
In this case > the horizontal speed is bigger
• Compare with another ball using the == operator.
In this case == mean that the horizontal speed is equal


Requirements for implementation of the Ball class:
1. The class Ball should be divided into a header (.h) and a definition file (.cpp).
There must be no function definitions in the header file
2. Inclusion of the header, containing the declaration of the class for Ball, shall
work even if this happens from "multiple directions"
3. Hard encapsulation should be used
4. Member functions that may be constant should be constant
5. Parameters that should be constant should be constant

The test program

In the test program (TestBall.cpp), the following must be done in turn (not menu-driven):
1. Create a dynamically assigned array containing 4 "default" balls
2. Add 4 "real" balls to positions 0 to 3 in the array above.
The values ​​for the horizontal speeds are read from a text file (balls.txt)
which you have created
3. Expand the 3-position array
4. Add another ball. Check is made that the same ball is not entered
more than once using the == operator.
Test by adding a ball already, partly by adding a ball that is not already available
5. Show horizontal, vertical and total speed for all non-default balls
6. Remove a ball of user-entered values ​​at horizontal speed. If the ball is not available, this should be announced
7. Show horizontal, vertical and total speed for all non-default balls
8. View the total speed of all non-default balls at one of the users entered time
9. Sort all "non-default" balls falling based on the balls horizontal speed. Here's is the usage of the > operator
10. Show horizontal, vertical and total speed for all non-default balls


Test Program Requirements:

1. Variables may not be declared out of function
2. Any constants are declared globally advantageous
3. Separate subproblems are handled in separate functions
• Add balls from text file
• Add a ball "manually"
• Show all balls
• Remove a ball
• View the total speed of all balls at a certain time
• Sort
• Expand the array can, but must not, be done in a separate function
The above functions should be divided into prototype and definition
4. The solution should contain relevant examples of both implicit and explicit call-by-reference as well as call-by-value
5. There must be no "cavities" in the array after removal, ie all
ball objects that are registered must always be located from position 0 and forward
6. The sorting should be implemented by you, ie you may not use
finished sorting features. No input or output may be performed in this function
7. The program must not generate memory leaks


Other information:
You can assume that no incorrect entries are made, such as that the user is feeding
in a string at integer input
You can't use break (except in connection with switch/case),
continuous, multiple return or goto in the solution!
You must submit the following files:
* Ball.h
* Ball.cpp
* TestBall.cpp
* balls.txt


my codes so far, P.S there's a lot of unfinished coding

The header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <string>

using namespace std;

class Ball
{
private:
	int speedX;
	int speedY;

public:
	Ball(int speedX, int speedY);
	Ball();

	int getSpeedX();
	int getSpeedY();

	/*{
		this->x = x;
		this->y = y;
	}*/
};

The test cpp. file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include "Ball.h"

using namespace std;

const double vy = 9.82;

int main()
{
	int capacity = -1;
	Ball = ballArray = nullptr;
	ballArray = now Ball[capacity];
	int nrOfBalls = 0;
	int time = 0;

	fillBallDef(ballArray, nrOfBalls, capacity);

	for (int i = 0; i < capacity; i++)
	{
		cout << ballArray[i].toString(time) << endl;
	}

	cout << "How long time has the balls been traveling?: " << endl;
	cin >> time;
	cin.ignore();
	showAll(ballArray, nrOfBalls, capacity, time);

	capacity = capacity + 3;
	Balls = tempBallArray = new Balls[capacity];
	
	for (int i = 0; i < nrOfBalls; i++)
	{
		tempBallArray[i] = ballArray[i];
	}

	delete[] ballArray;

	ballArray = tempBallArray;

	system("pause");

	return 0;
}

The cpp. file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "Ball.h"
#include <cmath>

Ball::Ball(double speedX)
{
	this->speedX = speedX;
}

Ball::Ball()
{
	this->speedX = -1;

}
int Ball::getSpeedX() const
{
	return this->speedX;
}

double Ball::getSpeedY(int travelTime+acceleration)
{
	return travelTime+acceleration;
}

double Ball::get
{
	return 
}

thank you for your time!
Last edited on
You are being asked to do a lot in this assignment.

The Ball 

There are two “speed”s you need to be aware of: horizontal (which never changes) and vertical (which varies). (In your notes, ‘speed’ and ‘velocity’ are often confused: in all cases you really only care about speed, not direction.)

The vertical speed is not something you need to store: it is to be calculated every time you ask for it. Keep in mind that when you ask for it, the asker must also supply the time. This leads into your equation for acceleration:

    vy=vertical = 9.81 * time

(I have no idea why your HW asks you to use 9.82; G is 9.80665..., which rounds to 9.81. And again, it is not negative because we only care about speed, not the actual velocity.)

Once you compute the vertical speed you can combine that with the horizontal speed using Pythagoras’ triangle (assuming I understand what is wanted properly). The width of your triangle is the horizontal speed. The height of your triangle is the vertical speed. The total velocity is the hypotenuse of the triangle. Use the Pythagorean equation to get it.

So, your methods are:

  • double getSpeedX( double t ) const return the horizontal speed at time t
  • double getSpeedY( double t ) const return the vertical speed at time t
  • double getTotalSpeed( double t ) const return the total speed at time t

(Note that the last method will call the first two! Also note that the very first one ignores t, right?)

You are also asked to create a method to get this information as a string:

  • std::string toString( double t ) const 
    For that you will want to use an std::ostringstream.
    Your notes above do not give a specific format for the string.

Here’s a freebie:
1
2
#include <sstream>
#include <string> 
1
2
3
4
5
6
7
8
9
10
11
12
std::string Ball::toString( double t ) const
{
  double vx = getSpeedX( t );
  double vy = getSpeedY( t );
  double vt = getTotalSpeed( t );
  std::ostringstream ss;
  ss << "For time = " << t << "s:";
  ss << " vx = " << vx << ",";
  ss << " vy = " << vy << ",";
  ss << " vt = " << vt << ".";
  return ss.str();
}


Finally, you need to have a few comparison operators to compare the horizontal speed of *this Ball with another Ball:

  • bool operator == ( const Ball& that ) const
  • bool operator < ( const Ball& that ) const

Your Ball.h file should be sure to have include guards; make sure that all methods that do not modify the Ball object are const. It should #include <string> but nothing else.

The Test
Whenever you write a new object, you should also write code to test it.
The test you are being asked to write is not very comprehensive, but it is what it is.

A dynamic array
Basically, you are being asked to create a dynamic array of four Balls. The X speed of each ball is obtained from the user (from a file named "balls.txt").

After you do that, try to add a ball that already exists in the array. This effort should fail. (You have to make sure it fails in your "add ball" function!)

Now try to add a ball that is not already in the array. This should succeed.

Both times, use the toString() method to print all the balls... (probably. The requirements above contradict each other. Ask your professor for help.)

In all cases, dynamically resizing the array is going to be a pain, since you must new a new array, copy the contents of the old to the new, and delete[] the old. Hint: only bother if the ball to add is not in the array already.

You must also be able to sort the array and remove individual Balls from the array — again necessitating some reallocation. (So this assignment is as much about managing a dynamic array as it is handling Balls. I’m so sorry.)

Again, I recommend you visit with your professor to iron out some of the details about handling this array. I presume you have already done dynamic arrays in a previous assignment.

balls.txt
This needs only be a list of four speeds. Something like the following will do:
12.34
3.141592
7
6.1234


Time
It was not specified what time to use when asking for information about the balls. You can get a time from the user or you can just use one (like 10 seconds). Again, ask your professor.

Final note: I could be wrong about some of this stuff. This is just from a quick glance over your notes above, and should be helpful to get you started. You know your assignment better than I do.

Good luck!
Hello huncho,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

At first look I do not see anything wrong with the header file except:
1
2
3
#include <string>

using namespace std;

The include should not be in the header file and read this about the using statement:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

In the "Ball.cpp" file The use of "this->" is not needed except maybe in the overloaded ctor. Since "Ball.cpp" defines the public member functions of the class these function already have access to the private member variables of the class. In the overloaded ctor "this->speedX" may be needed because the functions parameter also defines "speedX". It would be better to change the parameter name and just say speedX=difParameterName;.

The "getSpeedY" function is all wrong. First in the class you declare the function as having no parameters then in the definition you are trying to use a parameter incorrectly. These do not match and they must. Second what looks like two parameters need separated by a comma and each parameter name is to be preceded by a type. Lastly The point of a get function is to return the value of a private member variable or do some math on two or more private variables and return the result. Otherwise a normal function would be used.

The last function Ball::get" should be commented out for right now since it is not used.

Just what I see wihout testing.

Hope that helps,

Andy

Edit: typo
Last edited on
thank you guys, but I don't understand what should be in the cpp. file, the header file and in the test file? whats the diffrence between them?
roughly:

the class definitions go in the .h file
the class member function code goes in the .cpp file
use of the class (tests) and main function go in the test file.

pretty much what you have already done.

Last edited on
Topic archived. No new replies allowed.