Construct a class whose objects will each represent a single vehicle. Each vehic
ID: 3682806 • Letter: C
Question
Construct a class whose objects will each represent a single vehicle. Each vehicle object should contain the following data.
• The make (i.e, the name of the company that makes the vehicle; e.g., “Toyota”).
• The model (i.e., the name of the category of vehicles; e.g., “Prius”).
• The number of doors.
• The suggested retail price (in dollars).
In addition, the class should contain the following methods.
• A constructor with appropriate parameters that initializes an object.
• Four accessor methods, one for getting each of the object’s four data values.
• One mutator method that allows the suggested retail price of the object to be changed.
• A toString method that returns a user-friendly string for the object.
Your class should not contain or represent anything else. It is a simple class with four instance variables, six instance methods, and a constructor.
Your class should be neatly written and have good coding style, but it is not necessary to
include Javadoc comments. Furthermore, it is not necessary to include any import statements,
package statements, or code to handle exceptions.
Explanation / Answer
VechileDemo.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author KomalStar
*/
class vechile {
String make;
String Model;
int noofdoors;
float retailprice;
//construrctor arguments
vechile(String arg_make, String arg_Model, int arg_noofdoors, float arg_retailprice) {
make = arg_make;
Model = arg_Model;
noofdoors = arg_noofdoors;
retailprice = arg_retailprice;
System.out.println("make="+make+" "+"Model="+Model+" "+"noofdoors ="+noofdoors+"retailprice="+retailprice);
}
//getter and setter methods
public String getModel() {
return Model;
}
public void setModel(String Model) {
this.Model = Model;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getNoofdoors() {
return noofdoors;
}
public void setNoofdoors(int noofdoors) {
this.noofdoors = noofdoors;
}
public float getRetailprice() {
return retailprice;
}
public void setRetailprice(float retailprice) {
this.retailprice = retailprice;
}
}
public class VechileDemo {//main class
public static void main(String args[])
{
vechile v=new vechile("toyota","prius",4,1455677.23f);
}
}
output
run:
make=toyota Model=prius noofdoors =4retailprice=1455677.2
BUILD SUCCESSFUL (total time: 0 seconds)