Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Each car can be described by the following pieces of data: • model type (integer

ID: 3768107 • Letter: E

Question

Each car can be described by the following pieces of data: • model type (integer) • cost – the cost should be within the range 5,000.00 to 30,000.00. The car class should have the following methods: 1. Getter methods for each variable 2. Setter methods for each variable 3. toString method – This will return the string representation of the object. The file Car.java contains an incomplete defintion for the car class. Save it to your directory and complete the class definition as follows: • Declare the instance data (model type and cost) • Add the missing method headers (aka signatures) • Add the missing method bodies

Explanation / Answer

Here is the Car class for you, as per your specifications:

import java.text.NumberFormat;
import java.util.Random;
public class Car
{
static Random gen = new Random(System.currentTimeMillis());
//private variables
int modelType;
double cost;
//Constructor with a given type as a parameter
Car(int m, double c)
{
this.setModelType(m);
if(c < 5000 || c > 30000)
throw new IllegalArgumentException("Cost out of range. Should be within the range 5000 - 30000");
else
this.setCost(c);
}
//getters
int getModelType()
{
return modelType;
}
double getCost()
{
return cost;
}
//setters
void setModelType(int m)
{
modelType = m;
}
void setCost(double c)
{
cost = c;
}
//Override the toString method
public String toString()
{
return String.format("ModelType: "+modelType+" Cost: "+cost);
}
}//end class

If you have any further queries, just revert here.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote