In: Computer Science
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed. Create a class TestCar that instantiates a car and prints the default speed as well as a modified speed.
Please Save the following class as TestCar.Java
public class TestCar
{
public static void main(String[] args) {
//initializing the car class
Car car = new Car(10);
//displaying default and modified
speed of the car
System.out.println("Default Speed
of the car "+car.speed());
System.out.println("Modified Speed
of the car "+car.speed(80));
}
}
Save the following code as Car.java
public class Car
{
//intitializing the parameters
int make;
float speed;
static int defaultSpeed = 50;
//initializing constructor
Car(int make)
{
this.make = make;
}
//initializing speed method
public int speed()
{
return this.defaultSpeed;
}
//initializing overload speed method
public float speed(float newSpeed)
{
return newSpeed;
}
//initializing getSpeed method
public float getSpeed(float newSpeed)
{
return speed(newSpeed);
}
}
Output:
Default Speed of the car 50
Modified Speed of the car 80.0