Question
Hi I have problem with Java task Need to build a car list generator which will generate 100 random cars, the generator need to draw
Hi
I have problem with Java task
Need to build a car list generator which will generate 100 random cars, the generator need to draw cars with different engines and models
and do some stream method after generating 100 random cars :
- filter the stream so that they remain only the most powerful ones with a power of over 300 HP, with engines not less than 2.0 liters and not more than 4.0 liters of capacity;
- remove duplicates;
-return the car with the most efficient engine (the best power-to-capacity ratio, i.e. the highest power per liter)
They are two classes:
Class Car with fields :
- producer = BMW,
- String model,
-char segment,
-engine.
and ClassEngine with fields :
-power,
-torque,
-displacement,
-description
Below is an example of a java Animal generator, he generate 100 random animals. This example generator maybe it will help to build a car list generator.
Please see the code and it can help with the task
public class ListGenerator { public staticList generate(Supplier supplier, int listSize) { //5 return IntStream.range(0, listSize)////// 0, 1, 2, 3, 4, .mapToObj(x -> supplier.get()) .collect(Collectors.toList()); } }
public class ListGenerator { public staticList generate(Supplier supplier, int listSize) { //5 return IntStream.range(0, listSize)////// 0, 1, 2, 3, 4, .mapToObj(x -> supplier.get()) .collect(Collectors.toList()); } }
public class AnimalFactory { private final Random random = new Random(); public Animal createRandomAnimal() { String[] catTypes = {"europejski", "maine coon", "perski", "sfinks", "syberyjski"}; String[] dogTypes = {"Jamnik", "Owczarek niemiecki", "Kundel", "York"}; String[] names = {"Pimpek", "Puszek", "Ciapek", "Rekso", "Luna", "Roxi"}; String genCatType = catTypes[random.nextInt(catTypes.length)]; String genDogType = dogTypes[random.nextInt(dogTypes.length)]; String genName = names[random.nextInt(names.length)]; int genAge = random.nextInt(20) + 1; boolean sterilized = random.nextBoolean(); int randomNumber = random.nextInt(); return (randomNumber % 2 == 0) ? new Cat(genCatType, genName, genAge, sterilized) : new Dog(genDogType, genName, genAge, sterilized); } }
public class MyComparator implements Comparator{ @Override public int compare(Animal o1, Animal o2) { // TODO Auto-generated method stub if (o1.getAge() < o2.getAge()) return -1; else if (o1.getAge() > o2.getAge()) return 1; else return 0; } }
public class Animal { private String type; private String name; private int age; private boolean sterilized; public Animal(String type, String name, int age, boolean sterilized) { this.type = type; this.name = name; this.age = age; this.sterilized = sterilized; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isSterilized() { return sterilized; } public void setSterilized(boolean sterilized) { this.sterilized = sterilized; } @Override public String toString() { return getClass().getSimpleName() + "{" + "type='" + type + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sterilized=" + sterilized + '}'; }
public class Cat extends Animal { public Cat(String type, String name, int age, boolean sterilized) { super(type, name, age, sterilized); } }
public class Dog extends Animal { public Dog(String type, String name, int age, boolean sterilized) { super(type, name, age, sterilized); } }
public class Animals { public static void main(String[] args) { AnimalFactory factory = new AnimalFactory(); Listanimals = ListGenerator.generate(() -> factory.createRandomAnimal(), 100); animals.forEach(System.out::println);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started