Could someone guide me how to make printsortedCars
line 32
Code: https://pastebin.com/QqbHtMGj
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Could someone guide me how to make printsortedCars
line 32
Code: https://pastebin.com/QqbHtMGj
Please post the code in question here on the forum. Be sure to wrap the code in code tags.
If you don't understand my answer, don't ignore it, ask a question.
package myPackage.exam.colections; import java.util.*; public class Task2 { static Car BMW = new Car(true, 2005, 2, 1, 22000.0D); static Car AUDI = new Car(false, 2020, 1, 2, 604900.0D); static Car MERCEDES_BENZ = new Car(true, 2015, 3, 3, 75000.0D); static Car BMW_2 = new Car(true, 2003, 2, 4, 150000.0D); static Car FERRARI = new Car(true, 2020, 3, 5, 1230000.0D); static Car KIA = new Car(false, 2008, 2, 6, 13000.0D); static Car FORD = new Car(false, 2011, 2, 7, 8599.99D); static Car MCLAREN = new Car(true, 2018, 3, 8, 1700000.0D); static Car SKODA = new Car(false, 2001, 1, 9, 14000.0D); static Car DACIA = new Car(false, 1998, 1, 10, 6000.0D); private static final List<Car> carlist; public Task2() { } public static List<Car> getCarList() { Iterator var0 = carlist.iterator(); if (var0.hasNext()) { Car c = (Car)var0.next(); return (List)c; } else { return null; } } public static void printsortedCars() { //Print a list of cars to the console (you have to make the list yourself, preparing GOOD test data is part of the task) //the list should be sorted by attributes in the following order: //- exclusive cars are to be at the beginning of the list //- the newest cars, with the highest year of production, are to be displayed before older cars //- cars with higher equipment level are to be displayed before cars with lower equipment level //- more expensive cars should be displayed before cheaper cars } public static void printCarsToValueAndLevelOfEquipment(double maxValue, int levelOfEquipment) { // Business context: a customer goes to a used car dealership website and wants to see cars up to a certain "maxValue" // and having a specific equipment level e.g.: level 2 = car has air conditioning, and level 1 car has no air conditioning etc. //TASK: Filter and then list the cars from the cheapest to the most expensive from a list of cars //todo here the task for(Car a : carlist){ System.out.println(a); } Collections.sort(carlist); } public static void addCardToList() { //todo } public static void removeCarFromList() { //todo } public static void printCarListAfterSomeModification() { //add 2-3 new cars to the list //remove 1-2 cars from the list // display the list } static { carlist = new LinkedList(Arrays.asList(BMW, AUDI, MERCEDES_BENZ, BMW_2, FERRARI, KIA, FORD, MCLAREN, SKODA, DACIA)); } public static void main(String[] args) { printCarsToValueAndLevelOfEquipment(1,2); System.out.println(carlist); } } //Car.java package myPackage.exam.colections; public class Car implements Comparable<Car> { boolean isExclusive; int yearOfProduction; int levelOfEquipment; int id; double value; public Car(boolean isExclusive, int yearOfProduction, int levelOfEquipment, int id, double value) { this.isExclusive = isExclusive; this.yearOfProduction = yearOfProduction; this.levelOfEquipment = levelOfEquipment; this.id = id; this.value = value; } public boolean getExclusive() { return this.isExclusive; } public Integer getYear() { return this.yearOfProduction; } public Integer getlevel() { return this.levelOfEquipment; } public Integer getId() { return this.id; } public double getvalue() { return this.value; } public int compareTo(Car o) { return 0; } }
What problems are you having with designing it?how to make printsortedCars
I see that The comments say what it is supposed to do.
If you don't understand my answer, don't ignore it, ask a question.
Yes, it is but I don't know how to go about it
Work through the problems one at a time.I don't know how to go about it
Post one of the problems you are having and describe what the code needs to do for that problem.
If your problem is doing a sort, google about how to use the Comparator.
For example: https://www.geeksforgeeks.org/compar...nterface-java/
If you don't understand my answer, don't ignore it, ask a question.
When I want the console to list my cars, this pops up, how do I fix it?
Console: myPackage.exam.colections.Car@3ac3fd8b myPackage.exam.colections.Car@5594a1b5 myPackage.exam.colections.Car@6a5fc7f7 myPackage.exam.colections.Car@3b6eb2ec myPackage.exam.colections.Car@1e643faf myPackage.exam.colections.Car@6e8dacdf myPackage.exam.colections.Car@7a79be86 myPackage.exam.colections.Car@34ce8af7 myPackage.exam.colections.Car@b684286 myPackage.exam.colections.Car@880ec60
Add a toString method to the Car class that returns the String you want to be shown on that print out.how do I fix it?
The Strings shown in that print out are what is returned by the default toString method for the Object class:
<the full classname>@hexadecimalNumber
If you don't understand my answer, don't ignore it, ask a question.
Now I have a problem with displaying cars according to price and according to equipment level. I made scanners, I give variables, but in class printCardToValueAndLevelOfEquipment I don't know how in if to add cars value, Car.value doesn't work
public static void printCarsToValueAndLevelOfEquipment(Scanner maxValue, Scanner levelOfEquipment) { // Business context: a customer goes to a used car dealership website and wants to see cars up to a certain "maxValue" // and having a specific equipment level e.g.: level 2 = car has air conditioning, and level 1 car has no air conditioning etc. //TASK: Filter and then list the cars from the cheapest to the most expensive from a list of cars //todo here the task for(Car a : carlist){ if() System.out.println(a); } Collections.sort(carlist); public static void main(String[] args) { System.out.println("Enter maxValue: "); Scanner maxValue = new Scanner(System.in); double m = maxValue.nextDouble(); System.out.println("Enter levelOfEquipment"); Scanner levelOfEquipment = new Scanner(System.in); int l = levelOfEquipment.nextInt(); System.out.println(); printCarsToValueAndLevelOfEquipment(maxValue,levelOfEquipment); } }
What does "doesn't work" mean?Car.value doesn't work
Is there an error message? Please copy the full text and paste it here.
Executing the statement does not give the desired result. Print out any evidence that shows what the result is.
What is the purpose of the two Scanner objects that are passed to the method?
What is the purpose of the for loop?
If you don't understand my answer, don't ignore it, ask a question.
The purpose of the scanner is to enter the MaxValue of the cars and then in the console pop up cars up to a given amount, if I enter the amount of 2000, only cars up to 2k will be displayed, the same with levelofequipment
--- Update ---
package myPackage.exam.colections; import java.util.*; public class Task2 { static Car BMW = new Car(true, 2005, 2, 1, 22000.0D); static Car AUDI = new Car(false, 2020, 1, 2, 604900.0D); static Car MERCEDES_BENZ = new Car(true, 2015, 3, 3, 75000.0D); static Car BMW_2 = new Car(true, 2003, 2, 4, 150000.0D); static Car FERRARI = new Car(true, 2020, 3, 5, 1230000.0D); static Car KIA = new Car(false, 2008, 2, 6, 13000.0D); static Car FORD = new Car(false, 2011, 2, 7, 8599.99D); static Car MCLAREN = new Car(true, 2018, 3, 8, 1700000.0D); static Car SKODA = new Car(false, 2001, 1, 9, 14000.0D); static Car DACIA = new Car(false, 1998, 1, 10, 6000.0D); private static final List<Car> carlist; public Task2() { } public String getCarList() { return toString(); } public static void printsortedCars() { //Wypisz na konsolę listę aut (listę należy zrobić samodzielnie, przygotowanie DOBRYCH danych testowych jest cześcią zadania) //lista ma być posotrowana po atrybutach w następującej kolejności: //- auta ekskluzywne mają znajdować się na początku listy //- auta najnowsze, o najwyższym roku produkcji, mają być wyświetlane przed autami starszymi //- auta o wyższym poziomie wyposażenia mają być przed autami o niższym poziomie wyposażenia //- auta droższe mają być wyświetlane przed tańszymi } public static void printCarsToValueAndLevelOfEquipment(Scanner maxValue, Scanner levelOfEquipment) { //Kontekst biznesowy: klient wchodzi na stronę salonu samochodów używanych i chce zobaczyć auta do pewnej kwoty "maxValue" // oraz mające konkretny poziom wyposażenia np: poziom 2 = auto ma klimatyzację, a poziom 1 auto klimatyzacji nie ma itp //ZADANIE: wyfiltruj a następnie wypisz auta od najtańszego do najdroższego z listy aut //todo tutaj wykonaj zadanie for(Car a : carlist){ if() System.out.println(a); } Collections.sort(carlist); } public static void addCardToList() { //todo } public static void removeCarFromList() { //todo } public static void printCarListAfterSomeModification() { //dodaj 2-3 nowe auta do listy //usuń z listy 1-2 auta // wyświetl listę } static { carlist = new LinkedList(Arrays.asList(BMW, AUDI, MERCEDES_BENZ, BMW_2, FERRARI, KIA, FORD, MCLAREN, SKODA, DACIA)); } public static void main(String[] args) { System.out.println("Enter maxValue: "); Scanner maxValue = new Scanner(System.in); double m = maxValue.nextDouble(); System.out.println("Enter levelOfEquipment"); Scanner levelOfEquipment = new Scanner(System.in); int l = levelOfEquipment.nextInt(); System.out.println(); printCarsToValueAndLevelOfEquipment(maxValue,levelOfEquipment); } } package myPackage.exam.colections; public class Car implements Comparable<Car> { boolean isExclusive; int yearOfProduction; int levelOfEquipment; int id; double value; public Car(boolean isExclusive, int yearOfProduction, int levelOfEquipment, int id, double value) { this.isExclusive = isExclusive; this.yearOfProduction = yearOfProduction; this.levelOfEquipment = levelOfEquipment; this.id = id; this.value = value; } public boolean getExclusive() { return this.isExclusive; } public Integer getYear() { return this.yearOfProduction; } public Integer getlevel() { return this.levelOfEquipment; } public Integer getId() { return this.id; } public double getvalue() { return this.value; } public int compareTo(Car o) { return 0; } // public String toString() { //return isExclusive + "" + yearOfProduction + "" + levelOfEquipment + "" + id + "" + value; // } public String toString() { return this.isExclusive + " " + this.yearOfProduction + " " + this.levelOfEquipment + " " + this.id + " " + this.value; } }
The values to test for should be acquired before the call to the method and then passed as a parameter to the method.purpose of the scanner is to enter the MaxValue
The method should not be asking the questions and getting the responses.
If you don't understand my answer, don't ignore it, ask a question.
The purpose of the loop is to output the data information about the cars of the list.
Unless I'm misunderstanding it and doing it wrong as you would. Because I don't understand your answer
--- Update ---
I wanted to make in if that if maxValue(e.g. we typed 10000 into the consola) > value (from the list) then the consola prints cars that cost up to 10000
--- Update ---
Do you understand? I enter 1000 in the console and it lists all cars that cost up to 1000, I enter equipment level 2 and it lists cars that have equipment level 2
You can use the Scanner class to read that number from the console outside of the printCarsToValueAndLevelOfEquipment method.I enter 1000 in the console
Then pass that number to the method as an argument. See your post#3
Note: It would be a better design to have separate print methods for the two items to search for:
printCarsToValue(int value) and printCarsLevelOfEquipment(int level)
On re-reading the method's comments I see that the method should select Cars that meet both criteria.
If you don't understand my answer, don't ignore it, ask a question.
What does that mean? If the code is designed and written incorrectly, it needs to be changed.But I have it made
If you don't understand my answer, don't ignore it, ask a question.
Then show me what you would change if you did
Look at post#9. It reads the values from the user in the main method into m and l.
It should pass those values to the method as was done in post#3 instead of the 1,2 that was passed.
If you don't understand my answer, don't ignore it, ask a question.
I keep trying but I don't know how to add value from cars list to if.
Because I don't know if you understand what he's trying to do.
Scanner I created in order to create a variable, which then in a loop will check the value of vehicles and if I enter the value of 20000 will write me only vehicles to this amount
The Car class needs to have get methods that return the value you want to test.how to add value from cars list to if.
In the foreach loop, the variable: a has the reference to the Car object.
Note: a is a poor name for a variable that refers to a Car object. Better would be: aCar
In the foreach loop, use the aCar variable to get the desired value from the Car object:... aCar.getDesiredValue() ...
The Scanner class usage should be like the code in post#9 - get the values to be passed to the method and then pass those values like was done on post#3.
If you don't understand my answer, don't ignore it, ask a question.
Now in the loop it gives an error and intelijj wants to change getDesiredvalue from double to boolean
Please copy the full text of the error message and paste it here.it gives an error
Also post the code where the error happens.
Note: the name: getDesiredValue was only meant as an example. You need to change it to reflect what value that you want it to return from the Car class. For example: getTankSize or getCost or getWeight
Look at the Car class's declaration, it has a get method for each value you might be interested in.
If you don't understand my answer, don't ignore it, ask a question.
Yes I know that getDesiredValue is as example
package myPackage.exam.colections; import java.util.*; public class Task2 { static Car BMW = new Car(true, 2005, 2, 1, 22000.0D); static Car AUDI = new Car(false, 2020, 1, 2, 604900.0D); static Car MERCEDES_BENZ = new Car(true, 2015, 3, 3, 75000.0D); static Car BMW_2 = new Car(true, 2003, 2, 4, 150000.0D); static Car FERRARI = new Car(true, 2020, 3, 5, 1230000.0D); static Car KIA = new Car(false, 2008, 2, 6, 13000.0D); static Car FORD = new Car(false, 2011, 2, 7, 8599.99D); static Car MCLAREN = new Car(true, 2018, 3, 8, 1700000.0D); static Car SKODA = new Car(false, 2001, 1, 9, 14000.0D); static Car DACIA = new Car(false, 1998, 1, 10, 6000.0D); private static final List<Car> carlist; public String getCarList() { return toString(); } public static void printsortedCars() { //Wypisz na konsolę listę aut (listę należy zrobić samodzielnie, przygotowanie DOBRYCH danych testowych jest cześcią zadania) //lista ma być posotrowana po atrybutach w następującej kolejności: //- auta ekskluzywne mają znajdować się na początku listy //- auta najnowsze, o najwyższym roku produkcji, mają być wyświetlane przed autami starszymi //- auta o wyższym poziomie wyposażenia mają być przed autami o niższym poziomie wyposażenia //- auta droższe mają być wyświetlane przed tańszymi } public static void printCarsToValueAndLevelOfEquipment(Scanner maxValue) { //Kontekst biznesowy: klient wchodzi na stronę salonu samochodów używanych i chce zobaczyć auta do pewnej kwoty "maxValue" // oraz mające konkretny poziom wyposażenia np: poziom 2 = auto ma klimatyzację, a poziom 1 auto klimatyzacji nie ma itp //ZADANIE: wyfiltruj a następnie wypisz auta od najtańszego do najdroższego z listy aut //todo tutaj wykonaj zadanie for(Car aCar : carlist){ if(aCar.getValue()) System.out.println(aCar); } Collections.sort(carlist); } public static void addCardToList() { } public static void removeCarFromList() { //todo } public static void printCarListAfterSomeModification() { //dodaj 2-3 nowe auta do listy //usuń z listy 1-2 auta // wyświetl listę } static { carlist = new LinkedList(Arrays.asList(BMW, AUDI, MERCEDES_BENZ, BMW_2, FERRARI, KIA, FORD, MCLAREN, SKODA, DACIA)); } public static void main(String[] args) { System.out.println("Enter maxValue: "); Scanner maxValue = new Scanner(System.in); double m = maxValue.nextDouble(); System.out.println(); System.out.println("isExclusive " + "" + "yearofProduction " + "" + "levelofEquipment " + " " + "id" + " " + "value "); printCarsToValueAndLevelOfEquipment(maxValue); System.out.println("Enter levelOfEquipment: "); Scanner levelOfEquipment = new Scanner(System.in); int l = levelOfEquipment.nextInt(); System.out.println(); System.out.println("isExclusive " + "" + "yearofProduction " + "" + "levelofEquipment " + " " + "id" + " " + "value "); printCarsToValueAndLevelOfEquipment(levelOfEquipment); } }
package myPackage.exam.colections; public class Car implements Comparable<Car> { boolean isExclusive; int yearOfProduction; int levelOfEquipment; int id; double value; public Car(boolean isExclusive, int yearOfProduction, int levelOfEquipment, int id, double value) { this.isExclusive = isExclusive; this.yearOfProduction = yearOfProduction; this.levelOfEquipment = levelOfEquipment; this.id = id; this.value = value; } public boolean getExclusive() { return this.isExclusive; } public Integer getYear() { return this.yearOfProduction; } public Integer getLevel() { return this.levelOfEquipment; } public Integer getId() { return this.id; } public double getValue() { return this.value; } public int compareTo(Car o) { return 0; } public String toString() { return this.isExclusive + " " + this.yearOfProduction + " " + this.levelOfEquipment + " " + this.id + " " + this.value; } }
Error:
java: incompatible types: double cannot be converted to boolean
The code is still passing the reference to the Scanner object instead of the value read in from the user into the variables: m and l
Note: m and l are poor names for variables that hold important values. variable names should describe what they contain.
For example: maxValue should hold the maximum value entered by the user.
The name of the Scanner should describe what it is used for: readUserInput not maxValue
An if statement requires a boolean value. getValue returns a doubleif(aCar.getValue()) //java: incompatible types: double cannot be converted to boolean
Use a comparison operator to create a boolean expression:
if(aCar.getValue() != 22.2) // a boolean expression now
If you don't understand my answer, don't ignore it, ask a question.