Java Programming Assignment: Loops
Problem Summary and Equations
When an object is dropped or thrown from an initial position (height), formulas can be used to determine the object’s position (height) and velocity at each second, as it drops.
The position of a falling object can be calculated using the equation:
P(t) = –16t2 + V0t + H0
t is time in seconds
P(t) is the position of the object at after t seconds,
V0 is the initial vertical velocity of the object in feet/sec, and
H0 is the initial height of the object in feet.
Likewise, the velocity of the object at any given time can be calculated from the equation:
V(t) = –32t + V0
t is time in seconds
V(t) is the velocity of the object at after t seconds, and
V0 is the initial vertical velocity of the object in feet/sec
Example: Assume we drop a ball from an initial height of 1000 ft. (H0 = 1000.0) above the Earth. Since we dropped the ball and didn’t throw it, its initial velocity is 0 ft/sec (V0 = 0.0). At zero, the ball is not moving up or down.
Now we can ask:
How high above the ground and how fast is the ball traveling after 5 seconds (t = 5)?
To determine these values, simply plug in all the numbers to the previous two equations:
P(5.0) = -16(5.0)2 + (0.0)(5.0) + 1000.0 P(5.0) = 600.0
V(5.0) = -32(5.0) + 0.0 V(5.0) = -160
Hence, after 5 seconds, the ball will be 600 feet above the ground traveling downwards at 160 ft/sec (downwards because of the negative sign).
Write a program that displays a countdown first, and then displays the position and velocity of an object for every second it drops, as long as it is above 500 feet
Required Control Structures
Within this program, you must use at least one of each of the following programming constructs:
· while loop
· do-while loop
· for loop
Required Classes and Methods
Two separate classes will be required for this program.
1. Define a class with properties and methods for a falling object.
The class will have the following private properties:
- constant initial position
- constant initial velocity
- current time
- current position
- current velocity
Note: As long as a value is known when an object is instantiated, and it will not change, the value can be defined as a constant (final).
You should also define a public static constant within the class to hold the value of the
- terminal velocity (-500 ft/sec)
Because this constant will be declared static, it will be created only once (instead of once for each object). And because it will be declared public, it can be used by both classes.
Within the class:
· Define a constructor, with parameters that pass in the user entered initial position and initial velocity, and which
o Initializes the current time to 0.
o Initializes the initial position and initial velocity constants to the passed in parameter values.
o Initializes the current position and current velocity to the passed in parameter values.
· Define two instance methods (getters) to:
o Get and return the current position
o Get and return the current time
· Define an instance method to reset the values for the falling object for the next second. This method will:
o Increment the current time by 1.
o Calculate and set the current velocity of the object at the updated current time.
§ § Air resistance affects the acceleration of falling objects, so they will eventually reach a maximum terminal velocity. The terminal velocity of an object will vary, depending on the object's mass.
· For this program we will not allow the velocity to ever pass -500 feet/second.
§ So if the calculated velocity is less than the terminal velocity of -500 feet/second, the program should use -500 feet/second as the velocity instead of the calculated value.
o Calculate and set the current position of the object at the updated current time.
§ If the object has reached the terminal velocity, the P(t) function cannot be used to calculate the position. Therefore, the program must check to see if terminal velocity was reached before calculating the position.
· If terminal velocity has been reached, subtract 500 feet from the previous position to get the new position, instead of calculating the position via the formula.
· Otherwise, use the position function to determine the current position of the object.
· Define an instance method to display the current time, current position, and current velocity of the object (see format in sample output below).
2. Define a second class with methods for tracking a falling object.
Within the class:
· Define a static method to read and validate the initial position of the object from the user.
o The initial position (height above ground) must be above 500 feet.
o If the user enters an invalid initial position, issue an error message and then re-prompt the user again, until the position entered is above 500 feet (prompts/error messages should specify the initial position requirements).
· Define a static method to read and validate the initial velocity of the object.
o The user may enter a positive velocity (indicating the object was thrown upward), a negative velocity (indicating the object was thrown downward), or zero (indicating the object was just dropped), but a negative velocity may not exceed a terminal velocity of –500 ft/sec.
o If the user enters a negative velocity smaller (i.e. more negative) than –500 ft/sec, issue an error message and then re-prompt the user again, until the velocity entered is at least –500 ft/sec (prompts/error messages should specify the velocity requirements).
· Define a main method to:
o Display a description of what the program will do to the user.
o Use the static methods to read the initial position and initial velocity values from the user
o Create a new object of the falling object class, using the initial position and initial velocity read from the user.
o Loop to display a countdown to the user, from 5 to 1.
o Display the release position and initial velocity.
o Loop to display the current time, position, and velocity of the falling object every second, formatted as shown in the example below.
§ Position and velocity figures should be displayed to one decimal place.
§ Cease displaying time, position, and velocity when the object reaches 500 feet above the ground.
o Display the stopping height and time reached in seconds, along with the object’s final position, as shown in the example below.
- 6 years ago
- Chapter 11 & 12 Microecon.
- Marketing Analysis
- Impact of the Sarbanes-Oxley Act (SOX Assume that you are a CEO of a medium-sized company that needs a significant influx...
- Earlier in the course you were asked to complete the Class Survey. The results are in! Findings from students who have taken NR439 can be found in Doc Sharing. Review these and describe what the results say about this sample of students.
- Demand Estimation
- Human Resource 4
- find the average speed of a person who swims 105 meters in 70 seconds
- Health care (11) Biology
- It is proposed at a stockholders’