Write a Java Program for below instruction: We simplify our concept of a vehicle
ID: 3908300 • Letter: W
Question
Write a Java Program for below instruction:
We simplify our concept of a vehicle to three properties: its velocity; its position on an (x,y) coordinate grid; and its direction. From this we design a Vehicle class, with the following capabilities:
• create a new Vehicle by giving the velocity, the position as an x, y pair, and the direction
• get the current velocity
• get the current position
• get the current direction
• change the direction (by giving a new direction)
• change the velocity (by giving an acceleration and time)
• show the position the vehicle will be at in a given time at its current velocity and direction
• change the position of the vehicle after it travels for a given time at its current velocity and direction
• represent the vehicle properties as a Java string At the end of this document, I give a modified version of the Vehicle class framework that we looked at in class. I've added stubs for the changeDirection and toString methods. I've also added a stub for an overloading of toString that takes a two-element array of doubles (represented a vehicle's x, y position) and represents the values as a string. The direction of the vehicle should be represented internally as a double value holding a value in radians. This is because the Java Math class trigonometric methods take angles expressed in radians. However, the Vehicle constructor and the changeDirection parameters should accept a double value expressed in degrees (0 degrees is east, 90 degrees is north, 180 degrees is west, and 270 degrees is south) and convert to radians. The changeVelocity method, as mentioned above, takes a double value representing the acceleration and an integer value representing a time interval. The velocity is modified by adding the product of the acceleration and time to the current velocity.
The showNewPosition and changePosition take an integer value representing a time interval and calculate the distance traveled as velocity x time. To show the new position or change the vehicle's position, the distance traveled must be represented as a pair of distances: the distance traveled along the x-axis and the distance traveled along the y-axis. This is done using the trigonometric functions cos and sin as follows:
• distance traveled on x-axis = (velocity x time) x cos(direction)
• distance traveled on y-axis = (velocity x time) x sin(direction) These must be added to the current x and y values of the vehicle to get the new vehicle position.
Use below code as a template:
package vehicle;
public class Vehicle {
// should be private
private double velocity;
private double x;
private double y;
private double direction;
public Vehicle(double v, double x, double y, double d){
// d is given as degrees, you should store it internally as radians
}
public double getVelocity() {
}
public double getDirection() {
// return direction in degrees not radians
}
public double [] getPosition() {
// return an array of doubles as the x, y pair
}
public void changeVelocity(double accelartion, int time) {
// acceleration can be positive or negative
// velocity is changed based on duration of acceleration
}
public void changeDirection(double nd) {
// change the direction
// the parameter nd is in degrees
// store the result in radians
}
public double [] showNewPosition(int t) {
// return the new x,y pair based on travelling for time t
// based on current velocity, direction and position
// does not change the vehicle's position
}
public void changePosition(int t) {
// changes the x,y pair based on travelling for time t
// based on current velocity, direction and position.
}
public String toString() {
// override the Object toString method to produce a formatted string
// representation of a vehicle: // "<v,(x,y),d>"
}
public static String toString(double [] p) {
// overloading of toString that takes an array of two doubles
// and returns a position string: "(x,y)"
}
// You will note that the calculations you perform using toRadians, toDegrees, cos, sin
// and floating point arithmetic are not exact and the raw display of values is messy
// this method produces a floating value with no more than two decimal places.
private double rndTo2DP(double d) {
// rounds d to have two decimal places return Math.round(100*d)/100.0;
}
}
Explanation / Answer
Code:
import java.io.*;
class Vehicle {
// should be private
private double velocity;
private double x;
private double y;
private double direction;
public Vehicle(double v, double x, double y, double d){
// d is given as degrees, you should store it internally as radians
velocity = v;
this.x = x;
this.y = y;
direction = rndTo2DP(Math.toRadians(d));
}
public double getVelocity() {
return velocity;
}
public double getDirection() {
// return direction in degrees not radians
return rndTo2DP(Math.toDegrees(direction));
}
public double [] getPosition() {
// return an array of doubles as the x, y pair
double []pos = new double[2];
pos[0] = x;
pos[1] = y;
return pos;
}
public void changeVelocity(double accelartion, int time) {
// acceleration can be positive or negative
// velocity is changed based on duration of acceleration
velocity = velocity + accelartion*time;
}
public void changeDirection(double nd) {
// change the direction
direction = Math.toRadians(nd);
}
public double [] showNewPosition(int t) {
// return the new x,y pair based on travelling for time t
// based on current velocity, direction and position
// does not change the vehicle's position
double []pos = new double[2];
pos[0] = x + rndTo2DP(velocity*t*Math.cos(direction));
pos[1] = y + rndTo2DP(velocity*t*Math.sin(direction));
return pos;
}
public void changePosition(int t) {
// changes the x,y pair based on travelling for time t
// based on current velocity, direction and position.
x = x + rndTo2DP(velocity*t*Math.cos(direction));
y = y + rndTo2DP(velocity*t*Math.sin(direction));
}
public String toString() {
// override the Object toString method to produce a formatted string
// representation of a vehicle: // "<v,(x,y),d>"
return "<"+velocity+",("+x+","+y+"),"+direction+">";
}
public static String toString(double [] p) {
// overloading of toString that takes an array of two doubles
// and returns a position string: "(x,y)"
return "("+p[0]+" , "+p[1]+")";
}
// You will note that the calculations you perform using toRadians, toDegrees, cos, sin
// and floating point arithmetic are not exact and the raw display of values is messy
// this method produces a floating value with no more than two decimal places.
private double rndTo2DP(double d) {
// rounds d to have two decimal places return Math.round(100*d)/100.0;
return Math.round(100*d)/100.0;
}
}