Untitled

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

public class midterm3task2 {
static int consumption = 80;
static int distance = 33;
	public static void main(String[] args) {

		Disel d = new Disel(consumption, distance, 50 ); 
		d.cost1();
		d.toString1();
		Petrol p = new Petrol(consumption, distance, 30);
		p.cost2();
		p.toString2();
		LPG l = new LPG(consumption, distance, 48);
		l.cost3();
		l.toString3();
		
	}

}



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

class Disel extends Cars{
	int fuelPrice;
	Disel(int consumption, int distance, int fuelPrice){
		super(consumption, distance);
		this.fuelPrice = fuelPrice;
	}
	public String toString1() {
		System.out.println("Consumption is " + consumption + " and distance is " + distance);
		return fuelPrice + " " + consumption + " " + distance + " ";
	}
	public int cost1() {
		int c1=super.cost()*fuelPrice;
		System.out.println("Cost is: " + c1);
		return c1;
	}
}

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

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