Car Agent Create a program that will classify then print the cars in tabular for
ID: 3692279 • Letter: C
Question
Car Agent
Create a program that will classify then print the cars in tabular form the latest to the oldest.
1) Your code should create a class called car with brand, year, and color as private members. Add whatever you need to the class but write your assumptions as comments.
2) Inside the main, you will create 8 cars of car class using array.
3) The program will read the information of the 8 cars from the user. 4) Use function called “print car” to print the cars in tabular format from the latest to the oldest
Explanation / Answer
Hello there ,
C++ CODE:
====
void sortcars(Cars carsarray[]) {
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(carsarray[j]>carsarray[j+1])
{
hold=carsarray[j];
carsarray[j]=carsarray[j+1];
carsarray[j+1]=hold;
}
}
for(i=1;i<n;++i){
carsarray[i].printcar();
}
}
Please find below code and its output.
Let me know if you have any queries.
Thanks.
/**
* It represents a Car class containing basic detial of car.
* @author dipal.prajapati
*
*/
public class Car implements Comparable<Car> {
private String brand;
private int year;
private String color;
public Car(String brand, int year, String color) {
super();
this.brand = brand;
this.year = year;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", year=" + year + ", color=" + color + "]";
}
@Override
public int compareTo(Car car) {
return Integer.valueOf(this.year).compareTo(Integer.valueOf(car.year));
}
}
import java.util.Arrays;
import java.util.Scanner;
/**
* Driver program.
* @author dipal.prajapati
*
*/
public class CarAgent {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("No of Car you want to enter:");
int year;
String brandName, color;
int noOfCars = scanner.nextInt();
Car car = null;
Car[] arrayOfCars = new Car[noOfCars];
for (int i = 0; i < noOfCars; i++) {
System.out.println("Enter Car " + (i + 1) + " Detail below :");
System.out.println("Car Brand Name: ");
brandName = scanner.next();
System.out.println("Year: ");
year = scanner.nextInt();
System.out.println("Color ");
color = scanner.next();
car = new Car(brandName, year, color);
arrayOfCars[i] = car;
}
printCars(arrayOfCars);
}
/**
* It will print all cars sorted by year.
* @param cars
*/
public static void printCars(Car[] cars) {
String format = "|%1$-20s|%2$-20s|%3$-20s| ";
// 1. sort using Comparable
Arrays.sort(cars);
System.out.println(" ");
System.out.format(format, "Car Brand", "Car Year", "Car Color");
System.out.println("-------------------------------------------------------------");
for (Car car : cars) {
System.out.format(format, car.getBrand(), car.getYear(), car.getColor());
}
}
}
=====O/P===
No of Car you want to enter:
3
Enter Car 1 Detail below :
Car Brand Name:
BMW
Year:
1996
Color
Black
Enter Car 2 Detail below :
Car Brand Name:
Audi
Year:
1990
Color
White
Enter Car 3 Detail below :
Car Brand Name:
Fiat
Year:
1899
Color
Red
|Car Brand |Car Year |Car Color |
-------------------------------------------------------------
|Fiat |1899 |Red |
|Audi |1990 |White |
|BMW |1996 |Black |