carindustry

mail@pastecode.io avatar
unknown
java
2 years ago
1.9 kB
2
Indexable
Never
package midterm2;

public class midterm3task2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Diesel d = new Diesel(40, 50, 100);
		System.out.println(d.toString());
		d.cost1(100);
		Petrol p = new Petrol(40, 50, 98);
		System.out.println(p.toString());
		p.cost2(98);
		LPG l = new LPG(40, 50, 70);
		System.out.println(l.toString());
		l.cost3(70);
	}

}

class Cars{
	int consumption;
	int distance;
	//constructor
	Cars(int consumption, int distance){
		this.consumption=consumption;
		this.distance=distance;
	}
	public String toString() {
		return consumption + " " + distance;
	}
	public int cost(int c){
		c = (consumption*distance)/100;
		return c;
	}
}

class Diesel extends Cars{
	int fuelPrice;
	Diesel(int consumption, int distance, int fuelPrice){
		super(consumption, distance);
		this.fuelPrice = fuelPrice;
	}
	
	public String toString() {
		return consumption + " " + distance + " " + fuelPrice; 
	}
	public int cost1(int c1) {
		c1 = (consumption*distance)/100;
		System.out.println(c1*fuelPrice);
		return c1*fuelPrice;
		
	}
}

class Petrol extends Cars{
	int fuelPrice;
	Petrol(int consumption, int distance, int fuelPrice){
		super(consumption, distance);
		this.fuelPrice=fuelPrice;
	}
	public String toString() {
		return consumption + " " + distance + " " + fuelPrice;
	}
	public int cost2(int c2) {
		c2 = (consumption*distance)/100;
		System.out.println(c2*fuelPrice);
		return c2*fuelPrice;
	}
}

class LPG extends Cars{
	int fuelPrice;
	LPG(int consumption, int distance, int fuelPrice){
		super(consumption, distance);
		this.fuelPrice=fuelPrice;
	}
	public String toString() {
		return consumption + " " + distance + " " + fuelPrice;
	}
	public int cost3(int c3) {
		c3 = (consumption*distance)/100;
		System.out.println(c3*fuelPrice);
		return c3*fuelPrice;
	}
}