In main: create at least six Car instances, ensuring that at least three have model years less than 2018. create at least four Car instances using the parameterized constructor. create at least two more Car instances with the no-arg constructor use the Car class setter methods to assign values to all data members of these two cars. use the "array initializer" syntax to make an array to hold all Car objects. call the method named carsValue with the array as its sole argument. print the value returned by carsValue and the value in the unitsSold data member. In carsValue use a foreach loop to process the array passed into the method as follows: reduce the price of all vehicles with model years less than 2018 by 25%. print the state of each Car object using the toString method. calculate the total value of the Car objects in the array. return the total value to main.
I have the first program file. It's called Cars, I have 6 cars all set up. I'm missing the 25% off of 2018 year and older cars, and everything looks fine but I have alot of errors popping up. This is what my professor wants:
- code a main method and another value-returning method named carsValue
- In main:
- create at least six Car instances, ensuring that at least three have model years less than 2018.
- create at least four Car instances using the parameterized constructor.
- create at least two more Car instances with the no-arg constructor
- use the Car class setter methods to assign values to all data members of these two cars.
- use the "array initializer" syntax to make an array to hold all Car objects.
- call the method named carsValue with the array as its sole argument.
- print the value returned by carsValue and the value in the unitsSold data member.
- In carsValue
- use a foreach loop to process the array passed into the method as follows:
- reduce the price of all vehicles with model years less than 2018 by 25%.
- print the state of each Car object using the toString method.
- calculate the total value of the Car objects in the array.
- return the total value to main.
my code:
public static void main(String[] args) {
Cars car1 = new Cars ("Nissan", "GT-R", 2018, 80000.0, "Matte Black");
Cars car2 = new Cars ("Nissan", "350z", 2009, 14279.0, "Black");
Cars car3 = new Cars();
car3.setMake("Nissan");
car3.setModel("370z");
car3.setYear(2018);
car3.setPrice(43349.0);
car3.setColor("Black");
Cars car4 = new Cars();
car4.setMake("Nissan");
car4.setModel("Z");
car4.setYear(2023);
car4.setPrice(42990.0);
car4.setColor("Black");
Cars car5 = new Cars();
car5.setMake("Lamborghini");
car5.setModel("Aventador S");
car5.setYear(2018);
car5.setPrice(418000.0);
car5.setColor("Matte Black");
Cars car6 = new Cars();
car6.setMake("Chevrolet");
car6.setModel("Camaro");
car6.setYear(2022);
car6.setPrice(43845.0);
car6.setColor("Black");
Cars tot[] = {car1,car2,car3,car4,car5,car6};
carsValue(tot);
}
private static double carsValue(Cars[] tot) {
double totalValue = 0;
for (Cars tot2: tot) {
Cars.unitsSold++;
totalValue = totalValue + tot2.getPrice();
System.out.println(tot2);
}
System.out.println("Your " + Cars.unitsSold + "cars are worth " + String.format("$%.,2", totalValue));
return totalValue;
}
}
Step by step
Solved in 2 steps with 1 images