JAVA-Car details-Use set & get method-& use a method to calculate & display mileage

Q. Create a class called Car that includes the following: 
a. Instance variables – no_of_wheels, distance, oil_consumed, mileage 
b. A set and a get method for each instance variable except mileage
c. A method to calculate and display mileage of the Car 
Now create two objects of class Car and compare their mileage.

class car
{
private int  wheels,dis,oil,mil;
void setwheels(int a)
{
wheels=a;
}
int getwheels()
{
return wheels;
}
void setdis(int b)
{
dis=b;
}
int getdis()
{
return dis;
}
void setoil(int c)
{
oil=c;
}
int getoil()
{
return oil;
}
int getmil()
{
mil=dis/oil;
return mil;
}
}
class prog2
{
public static void main(String args[])
{
car ob=new car();
ob.setwheels(4);
ob.setdis(6000);
ob.setoil(12);
System.out.println("The 1st car information.............................");
System.out.println("number of wheels:"+ob.getwheels());
System.out.println("distance:"+ob.getdis());
System.out.println("oil consumed:"+ob.getoil());
System.out.println("mileage:"+ob.getmil());
car ob1=new car();
ob1.setwheels(16);
ob1.setdis(8000);
ob1.setoil(20);
System.out.println("The 2nd car information.............................");
System.out.println("number of wheels:"+ob1.getwheels());
System.out.println("distance:"+ob1.getdis());
System.out.println("oil consumed:"+ob1.getoil());
System.out.println("mileage:"+ob1.getmil());
if(ob.getmil()>ob1.getmil())
{
System.out.println("mileage of 1st car is greater ");
}
else
System.out.println("mileage of 2nd car is greater");
}
}

Output:
The 1st car information..............
number of wheels: 4
distance: 6000
oil consumed: 12
mileage: 500
The 2nd car information.............
number of wheels: 16
distance: 8000
oil consumed: 20
mileage: 400

//if you have any problem, comment below

Comments