In: Computer Science
This code in java:
Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car.
Kindly add some other functionalities like refuel
CODE:
In "Car" class:
package carproperties;
public class Car
{
//variables
private String color, model, company,
regNumber,owner;
private char gear;
//getters and setters
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner =
owner;
}
public String getCompany() {
return company;
}
public void setCompany(String company)
{
this.company =
company;
}
public char getGear() {
return gear;
}
//setGear function also prints the status of the
gearBox
// if incorrect input returns without changing
the data
public void setGear(char gear) {
if(gear == 'F')
System.out.println("Car is moving in Forward direction.");
else if (gear ==
'B')
System.out.println("Car is moving in Backward direction.");
else if(gear ==
'N')
System.out.println("Car is not moving.");
else
{
System.out.println("Incorrect Input");
return;
}
this.gear = gear;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color =
color;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model =
model;
}
public String getRegNumber() {
return regNumber;
}
public void setRegNumber(String regNumber)
{
this.regNumber =
regNumber;
}
//constructor
public Car(String color, String model, String
company, String regNumber, String owner, char gear) {
this.color =
color;
this.model =
model;
this.company =
company;
this.regNumber =
regNumber;
this.owner =
owner;
this.gear = gear;
}
//This function takes in steering direction
and prints it
public void stear(String direction)
{
System.out.println("Car
is being steared in "+direction+" direction");
}
//prints a message
public void refuel()
{
System.out.println("Car
is refueled");
}
}
IN Driver program, "CarProperties":
package carproperties;
import java.util.Scanner;
public class CarProperties
{
//driver code for example
public static void main(String[] args)
{
//car class variable for
example
Car c = new
Car("Red","Mustang", "Ford","US2345","Jeff", 'F');
Scanner scan = new
Scanner(System.in);
// to get user option
choice
int con;
do
{
// prints the choice and gets an input
System.out.print("Choose:"+
"\n1.\tStear the car"+
"\n2.\tChange Gear of the car"+
"\n3.\tRefuel the car"+
"\n4.\tExit"+
"\nChoice: ");
con = scan.nextInt();
// using switch case on the value of "con" till con is not 4
switch(con)
{
case 1 :
System.out.print("Enter the direction to stear(Left/Right):
");
String dir = scan.next();
c.stear(dir);
break;
case 2:
System.out.print("Enter the update gear(F/B/N) ");
char ch = scan.next().charAt(0);
c.setGear(ch);
break;
case 3:
c.refuel();
break;
case 4:
break;
default:
System.out.println("Incorrect Input. Please try again");
}
}while(con!=4);
}
}
OUTPUT:
Choose:
1. Stear the car
2. Change Gear of the car
3. Refuel the car
4. Exit
Choice: 1
Enter the direction to stear(Left/Right): Left
Car is being steared in Left direction
Choose:
1. Stear the car
2. Change Gear of the car
3. Refuel the car
4. Exit
Choice: 2
Enter the update gear(F/B/N) B
Car is moving in Backward direction.
Choose:
1. Stear the car
2. Change Gear of the car
3. Refuel the car
4. Exit
Choice: 3
Car is refueled
Choose:
1. Stear the car
2. Change Gear of the car
3. Refuel the car
4. Exit
Choice: 4
Ask any questions if you have in comment section below.
Please rate the answer.