Question

Homework-2 – Working with a class. Problem: Attached with this homework is code for a class...

Homework-2 – Working with a class. Problem: Attached with this homework is code for a class called DroppedObject. This class is used to to store the state of a falling object. The problem this class/object can be used to solve is detailed below. Your task for this homework is to read and understand the attached class. Note this class is formatted like all classes should be with private variables, constructors, a toString method, getter and setter methods, and other relevant methods. Write a main program that will use the class to solve the problem described below. Run your resulting program for a height of 100 feet. Upload to blackboard a copy of your main java class and a screen shot of your output. This is the problem: Write a program to simulate a falling object. The program should ask for the initial height of the object, in feet. The output of the program should be the time for the object to fall to the ground, and the impact velocity, in ft/s and miles/hour. Your program should use Euler’s method to numerically solve the differential equations describing the motion of a falling object. In Euler’s method, the state of the object at some small future time is calculated using the values at the present time. This small future time step is typically called delta time, or dt. Thus the position (p) and speed (v) of the object in the next timestep t + dt is written as a simple function of the position and speed at the current time step t (g is the acceleration due to gravity): v(t+dt) = v(t) + g * dt p(t+dt) = p(t) + v(t+dt) * dt You should actually start out with the velocity as zero, and the position at the initial height of the object. Then your position (above the ground) would be: p(t+dt) = p(t) - v(t+dt) * dt And you would integrate until the position becomes less than or equal to zero. The input/output formats for your program should look like this: Program to calculate fall time and impact speed of a falling object dropped from a specific height. Enter initial height in feet: 100 Falling time = 2.492224 seconds Impact speed = 80.249613 feet/sec Impact speed = 54.715645 mph Part of this assignment is to determine an optimum value for dt. Clearly, the value of the time step that you use in the simulation will be important. If the timestep (dt) is too large, then the results will not be accurate. However, if the time step is too small, then the program run time will be excessive. For this assignment, you must determine the value of delta time that is as large as possible but at the same time, results in an answer that is accurate to 5 significant figures – i.e., further decreases in the time step no longer change the result past the 5 th digit to the right of the decimal place. Turn in your program configured with this value of dt. Accuracy and precision is important for this assignment. You should use a double float data type for all real numbers. Also assume the effect of gravity does not vary with distance from the earth, the rate of acceleration is 32.2 feet per second per second, and the resistance of passing through the atmosphere is non-existent. Using symbolic constants for gravity G = 32.2 and delta time DT = .000??????? makes sense.

public class DroppedObject 
{
//
// Description: This class keeps the state of a falling object. The state means the height above the ground, the current 
// vertical velocity, the gravity constant , and the time differential (delta t).
// This class will contain methods to update the state of the object, print and set the state variables of the falling object
//
// Written by George Cardwell 
// Written as an example for CSC-202 Class Fall 2017.
        
        
// Private state variables

        private  double altitude;
        private double deltatime;
        private double velocity;
        private double gravityconst;
        private double TimeOfFlight = 0.0;
        
        
        
//  methods
        // consturctors
        
        public DroppedObject()
        {
                altitude = 0.0;
                deltatime = 0.0;
                velocity = 0.0;
                gravityconst = -32.2;
        }
        
        public DroppedObject(double h,double v, double dt, double g)
        {
                altitude = h;
                velocity = v;
                deltatime = dt;
                gravityconst = -g;
        }
        
//
// state Methods
//
        public void UpdateState()
        {
                velocity = velocity + gravityconst*deltatime;
                altitude = altitude + velocity*deltatime;
                TimeOfFlight = TimeOfFlight + deltatime;
        }
//
//      Getters and setters
//
        public double getAltitude() {
                return altitude;
        }

        public void setAltitude(double altitude) {
                this.altitude = altitude;
        }

        public double getDeltatime() {
                return deltatime;
        }

        public void setDeltatime(double deltatime) {
                this.deltatime = deltatime;
        }

        public double getVelocity() {
                return velocity;
        }

        public double getVelocityMPH(){
                return velocity * (3600.00/5280.00);
        }
        public void setVelocity(double velocity) {
                this.velocity = velocity;
        }

        public double getGravityconst() {
                return gravityconst;
        }

        public void setGravityconst(double gravityconst) {
                this.gravityconst = gravityconst;
        }
        
        public double getTimeOfFlight()
        {
                return TimeOfFlight;
        }

        @Override
        public String toString() {
                return "DroppedObject [altitude=" + altitude + ", deltatime=" + deltatime + ", velocity=" + velocity
                                + ", gravityconst=" + gravityconst + ", TimeOfFlight=" + TimeOfFlight + "]";
        }
        
}
public class DroppedObject 
{
//
// Description: This class keeps the state of a falling object. The state means the height above the ground, the current 
// vertical velocity, the gravity constant , and the time differential (delta t).
// This class will contain methods to update the state of the object, print and set the state variables of the falling object
//
// Written by George Cardwell 
// Written as an example for CSC-202 Class Fall 2017.
        
        
// Private state variables

        private  double altitude;
        private double deltatime;
        private double velocity;
        private double gravityconst;
        private double TimeOfFlight = 0.0;
        
        
        
//  methods
        // consturctors
        
        public DroppedObject()
        {
                altitude = 0.0;
                deltatime = 0.0;
                velocity = 0.0;
                gravityconst = -32.2;
        }
        
        public DroppedObject(double h,double v, double dt, double g)
        {
                altitude = h;
                velocity = v;
                deltatime = dt;
                gravityconst = -g;
        }
        
//
// state Methods
//
        public void UpdateState()
        {
                velocity = velocity + gravityconst*deltatime;
                altitude = altitude + velocity*deltatime;
                TimeOfFlight = TimeOfFlight + deltatime;
        }
//
//      Getters and setters
//
        public double getAltitude() {
                return altitude;
        }

        public void setAltitude(double altitude) {
                this.altitude = altitude;
        }

        public double getDeltatime() {
                return deltatime;
        }

        public void setDeltatime(double deltatime) {
                this.deltatime = deltatime;
        }

        public double getVelocity() {
                return velocity;
        }

        public double getVelocityMPH(){
                return velocity * (3600.00/5280.00);
        }
        public void setVelocity(double velocity) {
                this.velocity = velocity;
        }

        public double getGravityconst() {
                return gravityconst;
        }

        public void setGravityconst(double gravityconst) {
                this.gravityconst = gravityconst;
        }
        
        public double getTimeOfFlight()
        {
                return TimeOfFlight;
        }

        @Override
        public String toString() {
                return "DroppedObject [altitude=" + altitude + ", deltatime=" + deltatime + ", velocity=" + velocity
                                + ", gravityconst=" + gravityconst + ", TimeOfFlight=" + TimeOfFlight + "]";
        }
        
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//DropeedObject.java

package com.test;

import java.util.Scanner;

public class DroppedObject
{
//
// Description: This class keeps the state of a falling object. The state means the height above the ground, the current
// vertical velocity, the gravity constant , and the time differential (delta t).
// This class will contain methods to update the state of the object, print and set the state variables of the falling object
//
// Written by George Cardwell
// Written as an example for CSC-202 Class Fall 2017.
  
  
// Private state variables

private double altitude;
private double deltatime;
private double velocity;
private double gravityconst;
private double TimeOfFlight = 0.0;
  
  
  
// methods
// consturctors
  
public DroppedObject()
{
altitude = 0.0;
deltatime = 0.0;
velocity = 0.0;
gravityconst = -32.2;
}
  
public DroppedObject(double h,double v, double dt, double g)
{
altitude = h;
velocity = v;
deltatime = dt;
gravityconst = -g;
}
  
//
// state Methods
//
public void UpdateState()
{
velocity = velocity + gravityconst*deltatime;
altitude = altitude + velocity*deltatime;
TimeOfFlight = TimeOfFlight + deltatime;
}
//
// Getters and setters
//
public double getAltitude() {
return altitude;
}

public void setAltitude(double altitude) {
this.altitude = altitude;
}

public double getDeltatime() {
return deltatime;
}

public void setDeltatime(double deltatime) {
this.deltatime = deltatime;
}

public double getVelocity() {
return velocity;
}

public double getVelocityMPH(){
return velocity * (3600.00/5280.00);
}
public void setVelocity(double velocity) {
this.velocity = velocity;
}

public double getGravityconst() {
return gravityconst;
}

public void setGravityconst(double gravityconst) {
this.gravityconst = gravityconst;
}
  
public double getTimeOfFlight()
{
return TimeOfFlight;
}
public void setTimeOfFlight(double time)
{
TimeOfFlight += time;
}

@Override
public String toString() {
return "DroppedObject [altitude=" + altitude + ", deltatime=" + deltatime + ", velocity=" + velocity
+ ", gravityconst=" + gravityconst + ", TimeOfFlight=" + TimeOfFlight + "]";
}
  
}

//Main.java

package com.test;

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter initial height in feet:");

int height = sc.nextInt();

//v(t+dt) = v(t) + g * dt p(t+dt) = p(t) - v(t+dt) * dt

DroppedObject dp = new DroppedObject(0.0, 0.0, 0.4142, 32.2);

while(Math.abs(dp.getAltitude()) < height)

{

dp.UpdateState();

//System.out.println("Altitude is :"+dp.getAltitude());

}

System.out.println("Falling time = "+Math.abs(dp.getTimeOfFlight()) + " seconds Impact Speeed = "+Math.abs(dp.getVelocity()) + " feet/sec Impace Speed = "+ Math.abs(dp.getVelocityMPH()) + "MPH");

//Falling time = 2.492224 seconds Impact speed = 80.249613 feet/sec Impact speed = 54.715645 mph

}

}

//output:

Add a comment
Know the answer?
Add Answer to:
Homework-2 – Working with a class. Problem: Attached with this homework is code for a class...
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
  • Question 1 1 pts Which of the following is not a valid class name in Java?...

    Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide....

    Copy the program AmusementRide.java to your computer and add your own class that extends class AmusementRide. Note: AmusementRide.java contains two classes (class FerrisWheel and class RollerCoaster) that provide examples for the class you must create. Your class must include the following. Implementations for all of the abstract methods defined in abstract class AmusementRide. At least one static class variable and at least one instance variable that are not defined in abstract class AmusementRide. Override the inherited repair() method following the...

  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • CSC110 EXAM06A Objects NAME: class meters2feet private double meters; public void setMeters (double m) {meters=m ;...

    CSC110 EXAM06A Objects NAME: class meters2feet private double meters; public void setMeters (double m) {meters=m ; } public double getMeters ){ return meters;} pubic double convert2feet ( ) { return (meters*3.2808) ; } NOTE: This class cannot be changed must be used as is C class meters2inches private double meters; public void setMeters (double m) {mete rs=m ; } public double getMeters () { return meters ; } pubic double convert2inches ( ) { return (meters*39.3701) ; NOTE: This class...

  • We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private...

    We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private int x = 0; // constructor public Class1(int x) { this.x = x; } // instance methods public double myMethod1(double x, double y) { return x - y; } public void myMethod2(int x) { System.out.println(x); } public String myMethod3(String x) { return "hi " + x; } } We have also written the following Class2 class: class Class2 implements SecretInterface { // instance variable...

  • Consider the following declaration for a class that will be used to represent rectangles. public class...

    Consider the following declaration for a class that will be used to represent rectangles. public class Rectangle { private double height; private double width; public Rectangle() { height = 2.0; width = 1.0; } public Rectangle(double w, double h) { height = h; width = w; } public double getHeight() { return height; } public double getWidth() { return width; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } //Other...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double...

    Room Class....... in JAVA!!!!! Write a class named Room that has the following fields: Length: Double variable that holds the rooms length(in feet). Breadth: The double variable that holds the rooms breadth(in feet). Height: Double variable that holds rooms height in feet. Color: String object that holds the color you want the room to be painted with. The class should have a constructor to initialize the fields for a particular instance (or object) of the class as given: All double...

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