Assignment 1: Gravity¶
Write a C++ program that asks the user to enter the names of two different
objects (as strings), their masses (as double
s, in kilograms), and the
distance between them (as double
s, in meters), and then prints the force
of gravitational attraction between the objects.
For instance, here is a sample run of how your program should work when it’s done:
What is the name of the first object? Earth
What is the name of the second object? Moon
What is the mass of Earth (in kilograms)? 5.97e24
What is the mass of Moon (in kilograms)? 7.35e22
What is the distance between Earth and Moon? (in meters) 384400000
The force of gravitational attraction between Earth and Moon is
1.98071e+20 newtons
More sample runs are given below.
The Force of Gravity Between Two Objects¶
Newton’s law of universal gravitation is used to determine the force of gravitational attraction between physical objects. The force of attraction between two objects is
where:
- \(F\) is the force of gravitational attraction between the objects. The units of \(F\) are newtons (N).
- \(G\) is the gravitational constant, which is defined as \(G = 6.67 \times 10^{-11}\). The units for \(G\) are \(\textrm{N} \textrm{m}^2 \textrm{kg}^{-2}\).
- \(m_1\) and \(m_2\) are the masses of the two objects. The units of \(m_1\) and \(m_2\) are kilograms (kg).
- \(r\) is the distance between the centers of the two objects. The units of \(r\) is meters (m).
Error Handling¶
Your program must check that the inputs the user types are valid. This is called input validation.
Your program should indicate errors using the error()
function in the
error.h
file we are using for this course. We often refer to calling the
error()
function as raising an error.
Your program should raise an error if the user enters:
- identical names for the objects; or
- a mass that is less than, or equal to, 0; or
- a radius that is less than, or equal to, 0.
Please use cin
to read in the names of the objects. Because of how cin
works, object names can only consist of a single word. You do not need to
worry about the case where the user types in an object name consisting of more
than one word.
Sample Runs¶
What is the name of the first object? Earth
What is the name of the second object? Earth
terminate called after throwing an instance of 'std::runtime_error'
what(): Sorry, the names must be different!
What is the name of the first object? Earth
What is the name of the second object? Moon
What is the mass of Earth (in kilograms)? 5.97e24
What is the mass of Moon (in kilograms)? -7.35e22
terminate called after throwing an instance of 'std::runtime_error'
what(): Sorry, that is not a valid mass!
What is the name of the first object? Moon1
What is the name of the second object? Moon2
What is the mass of Moon1 (in kilograms)? 7.35e22
What is the mass of Moon2 (in kilograms)? 7.35e22
What is the distance between Moon1 and Moon2? (in meters) 0
terminate called after throwing an instance of 'std::runtime_error'
what(): Sorry, that is not a valid distance!
Notice how the names of the objects are use in the later questions. Make sure your program does the same thing!
Submission¶
Put your program in a single file called a1.cpp
. It should include only
these files: iostream
, cmpt_error.h
, and string
. Do not
include any other files.
When it’s ready to be submitted, compress a1.cpp
into a zip-file archive
called a1_submit.zip
by typing the following Linux command in a console
window:
$ zip a1_submit.zip a1.cpp
Don’t type the $
— that’s the command prompt. Do not compress your
program in any other way — only .zip
please!
Include the name of the course (CMPT 130), the semester (Fall 2017), and your name and student number in comments at the top of the file.
If your program does not compile correctly, your mark for the entire assignment will be 0. You may also receive 0, or a very low mark, if your assignment compiles but runs in some way that is difficult to judge if it works correctly.
Citing Help¶
If you received help from any person (other than yourself), book (such as the textbook), website (such at the course notes), etc., then you must cite this help in a comment at the top of your program. If you do not include such a comment, we will assume the program is entirely your own work. Not citing help (even if you accidentally forget) is considered academic dishonesty and will be dealt with seriously.