Question

In: Computer Science

Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...

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.

Solutions

Expert Solution

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


Related Solutions

Write anoyher overloaded constructor for this class. Thr constructor should accept an argumrny for each field....
Write anoyher overloaded constructor for this class. Thr constructor should accept an argumrny for each field. C++ I need help thanks.
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the...
Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the given text using the key. Use the following cipher: Take the key and mod it by 26. Example: a key of 30 becomes 4. If the character is a letter, shift it by the key, but 'wrap around' the alphabet if necessary. If the character is not a letter, then shift it by the key but do not wrap. Check the test cases for...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor overloading) for initializing objects with different parameters. The employee class should have different functions for saving and updating bio-data, educational background, and current status like experience, Salary, position & travel history. The employee class should have a function which asks user what do he likes to see about employee (.i.e., Bio-data, current status....) and only show the respective data. Create two objects of interns...
1, Create a class Car. The class has four instance fields: make (String - admissible values:...
1, Create a class Car. The class has four instance fields: make (String - admissible values: Chevy, Ford, Toyota, Nissan, Hyundai) size (String - admissible values: compact, intermediate, fullSized), weight (int - admissible values: 500 <= weight <= 4000) horsePower (int - admissible values: 30 <= horsePower <= 400) Provide a default constructor that sets String values to null and int values to 0 a parameterized constructor that sets all four instance fields to the parameter values setters and getters...
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT