For my Java Programing class, in a lab assignment I have to modify the following
ID: 3788716 • Letter: F
Question
For my Java Programing class, in a lab assignment I have to modify
the following code by not just displaying the maximum distrance traveled but displaying both maximum projectile height and the time of flight as well. The new methods
have to be within the class definition but outside the main() method. The part I am having trouble with is where in the code do I add the new formula methods?
The formulas are:
maximum heightattained is h = v2sin2 () / ( 2 g )
time of flightist = 2 vsin () / g
// the packages
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.Math;
// the class definition
publicclass Projectile
{
// the global variables are declared
staticdouble angle, distance, height, time, velocity;
finalstaticdouble gravity = 32.2;
// the class constructor ( initialize values )
public Projectile()
{ angle = 0; distance = 0; height = 0; time = 0; velocity = 0; }
// the method to show object instantiation
publicvoid ObjectInstantiated()
{
// display a message
JOptionPane.showMessageDialog(null, "object created!",
"Result", JOptionPane.PLAIN_MESSAGE);
}
// the method to obtain data
publicvoid getData()
{
// local variables declared and assigned initial values
String firstNum = "", secondNum = "";
// local variables updated via prompt boxes
firstNum = JOptionPane.showInputDialog("Enter initial velocity");
secondNum = JOptionPane.showInputDialog("Enter angle (degrees)");
// update the global variables
velocity = Double.parseDouble(firstNum);
// convert the angle from degrees to radians
angle = Double.parseDouble(secondNum) * Math.PI / 180.0;
}
// method to compute maximum distance
publicdouble computeMaxDistance(double v, double theta)
{
// declare some local variables
double maxDist = 0.0;
// assign values to the global variables
velocity = v;
angle = theta;
// perform the computations
maxDist = Math.pow(velocity, 2.0);
maxDist *= Math.sin(2 * angle) / gravity;
// return a value
return maxDist;
}
// the main() method
publicstaticvoid main(String args[])
{
// introduce a DecimalFormat object
DecimalFormat twoPlace = new DecimalFormat("0.00");
// declare some variables that are local to main
double myVelocity = 0, myDistance = 0, myAngle = 0;
//introduce a class object
Projectile p = new Projectile();
// announce the object
p.ObjectInstantiated();
// object calls the getData() method
p.getData();
// assign globals to the locals
myVelocity = velocity;
myAngle = angle;
// object calls the computeMaxDistance() method
myDistance = p.computeMaxDistance(myVelocity, myAngle);
//display the result in a message box
JOptionPane.showMessageDialog(null, "maximum distance: " +
twoPlace.format(myDistance) + " feet ", "Result",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}// end the main() method
}// end the class definition
// the packages
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.Math;
// the class definition
publicclass Projectile
{
// the global variables are declared
staticdouble angle, distance, height, time, velocity;
finalstaticdouble gravity = 32.2;
// the class constructor ( initialize values )
public Projectile()
{ angle = 0; distance = 0; height = 0; time = 0; velocity = 0; }
// the method to show object instantiation
publicvoid ObjectInstantiated()
{
// display a message
JOptionPane.showMessageDialog(null, "object created!",
"Result", JOptionPane.PLAIN_MESSAGE);
}
// the method to obtain data
publicvoid getData()
{
// local variables declared and assigned initial values
String firstNum = "", secondNum = "";
// local variables updated via prompt boxes
firstNum = JOptionPane.showInputDialog("Enter initial velocity");
secondNum = JOptionPane.showInputDialog("Enter angle (degrees)");
// update the global variables
velocity = Double.parseDouble(firstNum);
// convert the angle from degrees to radians
angle = Double.parseDouble(secondNum) * Math.PI / 180.0;
}
// method to compute maximum distance
publicdouble computeMaxDistance(double v, double theta)
{
// declare some local variables
double maxDist = 0.0;
// assign values to the global variables
velocity = v;
angle = theta;
// perform the computations
maxDist = Math.pow(velocity, 2.0);
maxDist *= Math.sin(2 * angle) / gravity;
// return a value
return maxDist;
}
// the main() method
publicstaticvoid main(String args[])
{
// introduce a DecimalFormat object
DecimalFormat twoPlace = new DecimalFormat("0.00");
// declare some variables that are local to main
double myVelocity = 0, myDistance = 0, myAngle = 0;
//introduce a class object
Projectile p = new Projectile();
// announce the object
p.ObjectInstantiated();
// object calls the getData() method
p.getData();
// assign globals to the locals
myVelocity = velocity;
myAngle = angle;
// object calls the computeMaxDistance() method
myDistance = p.computeMaxDistance(myVelocity, myAngle);
//display the result in a message box
JOptionPane.showMessageDialog(null, "maximum distance: " +
twoPlace.format(myDistance) + " feet ", "Result",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}// end the main() method
}// end the class definition
Explanation / Answer
Hi, you don't even need to add separate methods for that and create overheads.For fast execution of program, declare two static variables and modify them in the display method. And,directly print them in the main method.By this,you will overcome the over-heads of calling functions and time saved.
Code: (modified statements are in bolded format ) :
This is the modified. It is optimized. If still you want to keep methods only .Then please inform me, I will again modifying using methods.Thank you friend
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.