0% found this document useful (0 votes)
3 views7 pages

HM 4

The document contains Java code for three projects involving food items, gold shapes, and engine types. Each project includes abstract classes and concrete implementations to calculate calories for food, price for gold shapes, and maximum speed for engines. The main classes demonstrate the creation of objects and the calculation of respective properties.

Uploaded by

amirazat150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

HM 4

The document contains Java code for three projects involving food items, gold shapes, and engine types. Each project includes abstract classes and concrete implementations to calculate calories for food, price for gold shapes, and maximum speed for engines. The main classes demonstrate the creation of objects and the calculation of respective properties.

Uploaded by

amirazat150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Hm 4

1 ex

package projject.Zadanie_1;

public abstract class Food {


String name;

public Food(String name) {


this.name = name;
}
abstract double getCalories();
}

package projject.Zadanie_1;
public class Coke extends Food {
double volumeLiters;
boolean isSparkling; // Гази или нет

public Coke(String name, double volumeLiters, boolean isSparkling) {


super(name);
this.volumeLiters = volumeLiters;
this.isSparkling = isSparkling;
}
double getCalories() {
if (isSparkling) {
return volumeLiters * 400;
} else {
return volumeLiters * 100;
}
}
}

package projject.Zadanie_1;
public class Chocolate extends Food {
int weight;

public Chocolate(String name, int weight) {


super(name);
this.weight = weight;
}

double getCalories() {
return weight * 740;
}
}
package projject.Zadanie_1;
public class Burger extends Food {
int meatAmount; // количество мяса 1,2 или 3
int meatType; //тип мяса, 1 - это говядина, 2 - это Чикен
public Burger(String name, int meatAmount, int meatType) {
super(name);
this.meatAmount = meatAmount;
this.meatType = meatType;
}
double getCalories() {
if (meatType == 1) {
return meatAmount * 840;
} else {
return meatAmount * 560;
}
}
}

package projject.Zadanie_1;
public class Main {
public static void main(String[] args) {
Food snickers = new Chocolate("Snickers", 50);
Food twix = new Chocolate("Twix", 45);
Food burgerKing = new Burger("Burger King", 2, 1);
Food mcDonalds = new Burger("McDonalds", 1, 2);
Food cocaCola = new Coke("Coca Cola", 0.5, true);
Food fuseTea = new Coke("Fuse Tea", 0.5, false);

Food[] foods = {snickers, twix, burgerKing, mcDonalds, cocaCola, fuseTea};


Food highCal = foods[0];
for (Food food : foods) {
System.out.println(food.name + " у него " + food.getCalories());
if (food.getCalories() > highCal.getCalories()) {
highCal = food;
}
}
System.out.println( highCal.name + " столько" + highCal.getCalories());
}
}

2 ех
package projject.Zadanie_2;

public abstract class GoldShape {


public abstract double getVolume();

public double getPrice() {


return getVolume() * 250 + 1200;
}
}

package projject.Zadanie_2;

public class SphereGold extends GoldShape {


private double radius;
public SphereGold(double radius) {
this.radius = radius;
}
public double getVolume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}

package projject.Zadanie_2;

public class CubeGold extends GoldShape {


private double side;
public CubeGold(double side) {
this.side = side;
}
public double getVolume() {
return Math.pow(side, 3);
}
}
package projject.Zadanie_2;

public class Main {


public static void main(String[] args) {
GoldShape[] goldShapes = new GoldShape[6];
goldShapes[0] = new SphereGold(2.5);
goldShapes[1] = new SphereGold(3.0);
goldShapes[2] = new SphereGold(1.5);

goldShapes[3] = new CubeGold(2.0);


goldShapes[4] = new CubeGold(3.0);
goldShapes[5] = new CubeGold(1.0);

for (GoldShape goldShape : goldShapes) {


System.out.println("Price: " + goldShape.getPrice());
}
}
}

3 ех

Package project.Zadanie_3;

public abstract class Engine {

protected double engineVolume;


protected int cylinderAmount;
protected double engineWeight;

public Engine() {}

public Engine(double engineVolume, int cylinderAmount, double engineWeight) {


this.engineVolume = engineVolume;
this.cylinderAmount = cylinderAmount;
this.engineWeight = engineWeight;
}
public double getEngineVolume() {
return engineVolume;
}
public void setEngineVolume(double engineVolume) {
this.engineVolume = engineVolume;
}
public int getCylinderAmount() {
return cylinderAmount;
}
public void setCylinderAmount(int cylinderAmount) {
this.cylinderAmount = cylinderAmount;
}
public double getEngineWeight() {
return engineWeight;
}
public void setEngineWeight(double engineWeight) {
this.engineWeight = engineWeight;
}
public abstract double efficiency();
public abstract double throttleEnergy();
public abstract double breakEnergy();
public double getMaxSpeed() {
return (throttleEnergy() - breakEnergy()) * efficiency();
}
}

Package project.Zadanie_3;

public class FerrariEngine extends Engine {


private static double efficiencyy = 0.25;

public FerrariEngine() {}

public FerrariEngine(double engineVolume, int cylinderAmount, double engineWeight) {


super(engineVolume, cylinderAmount, engineWeight);
}
public double efficiency() {
return efficiencyy;
}
public double throttleEnergy() {
return engineVolume * cylinderAmount * 100;
}
public double breakEnergy() {
return engineWeight * 2;
}
}
Package project.Zadanie_3;

public class RenaultEngine extends Engine {


private double extraTurboEnergy;
private static final double efficiencyy = 0.27;
public RenaultEngine() {}
public RenaultEngine(double engineVolume, int cylinderAmount, double engineWeight, double
extraTurboEnergy) {
super(engineVolume, cylinderAmount, engineWeight);
this.extraTurboEnergy = extraTurboEnergy;
}
public double getExtraTurboEnergy() {
return extraTurboEnergy;
}
public void setExtraTurboEnergy(double extraTurboEnergy) {
this.extraTurboEnergy = extraTurboEnergy;
}
public double efficiency() {
return efficiencyy;
}
public double throttleEnergy() {
return engineVolume * cylinderAmount * 110 + extraTurboEnergy;
}
public double breakEnergy() {
return engineWeight * 2.1;
}
}
Package project.Zadanie_3;

public class Main {


public static void main(String[] args) {
Engine[] engines = new Engine[10];

engines[0] = new FerrariEngine(4.5, 8, 250);


engines[1] = new FerrariEngine(3.9, 6, 200);
engines[2] = new FerrariEngine(5.2, 12, 300);
engines[3] = new FerrariEngine(4.0, 8, 220);
engines[4] = new FerrariEngine(3.5, 6, 180);

engines[5] = new RenaultEngine(4.5, 8, 250, 50);


engines[6] = new RenaultEngine(3.9, 6, 200, 40);
engines[7] = new RenaultEngine(5.2, 12, 300, 60);
engines[8] = new RenaultEngine(4.0, 8, 220, 55);
engines[9] = new RenaultEngine(3.5, 6, 180, 35);

for (Engine engine : engines) {


System.out.println("Max Speed: " + engine.getMaxSpeed());
}
}
}

You might also like