In: Computer Science
Please give me an example of this java code.
Vehicle
- number: int
- color: String
- price: double
- horsepower: double
+ Vehicle ( )
+ Vehicle (int number)
+ Vehicle (int number, String color)
+ Vehicle (int number, String color, double price)
+ Vehicle (int number, String color, double price, double horsepower)
+getnumber( ):int
+setnumber(int number):void
+getcolor( ):String
+setcolor(String color):void
+getprice( ):double
+setprice(double price):void
+gethorsePower( ):double
+sethorsePower(double horsePower):void
toString( ):String
public String toString( )
{
return "\n\n number = " + number
+ "\n color = " + color
+ "\n price = " + price
+ "\n horsePower = " + horsePower;
public class VehicleTester
{
public static void main(String[] args)
{
Vehicle nissan1 = new Vehicle( );
nissan1.vehicleInformation( );
System.out.println(nissan1.toString( ));
}
}
class Vehicle {
private int number;
private String color;
private double price;
private double horsepower;
Vehicle() {
}
public Vehicle(int aNumber) {
super();
number = aNumber;
}
public Vehicle(int aNumber, String aColor) {
super();
number = aNumber;
color = aColor;
}
public Vehicle(int aNumber, String aColor, double
aPrice) {
super();
number = aNumber;
color = aColor;
price = aPrice;
}
public Vehicle(int aNumber, String aColor, double
aPrice, double aHorsepower) {
super();
number = aNumber;
color = aColor;
price = aPrice;
horsepower = aHorsepower;
}
public int getNumber() {
return number;
}
public void setNumber(int aNumber) {
number = aNumber;
}
public String getColor() {
return color;
}
public void setColor(String aColor) {
color = aColor;
}
public double getPrice() {
return price;
}
public void setPrice(double aPrice) {
price = aPrice;
}
public double getHorsepower() {
return horsepower;
}
public void setHorsepower(double aHorsepower)
{
horsepower = aHorsepower;
}
public String toString()
{
return "\n\n number = " + number
+ "\n color = " + color
+ "\n price = " + price
+ "\n horsePower = " + horsepower;
}
public void vehicleInformation() {
System.out.println(this);
}
}
public class VehicleTester
{
public static void main(String[] args)
{
Vehicle nissan1 = new Vehicle();
nissan1.vehicleInformation();
System.out.println(nissan1.toString());
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me