BCA

AJAVA LAB

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6

PART B

PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10 PROGRAM 11 PROGRAM 12 PROGRAM 13 PROGRAM 14 PROGRAM 15 . . .

9. Write a generic program to sort numerical array and alphabetical array.

 
 

	 // 9. Write a generic program to sort numerical array and alphabetical array.

// Importing generic java files
import java.util.*;

// Implements comparable interface into custom class
class Car implements Comparable<Car> {
	int ModalNo;
	String name;
	int stock;

	// Parameterized constructor of the class
	Car(int ModalNo, String name, int stock)
	{
		this.ModalNo = ModalNo;
		this.name = name;
		this.stock = stock;
	}

	// Override the compareTo method
	public int compareTo(Car car)
	{
		if (stock == car.stock)
			return 0;
		else if (stock > car.stock)
			return 1;
		else
			return -1;
	}
}

// Main driver method
class Program9B {

	// Main driver method
	public static void main(String[] args)
	{
		// Create the ArrayList object
		ArrayList<Car> c = new ArrayList<Car>();
		c.add(new Car(2018, "Kia", 20));
		c.add(new Car(2020, "MG", 13));
		c.add(new Car(2013, "creta", 10));
		c.add(new Car(2015, "BMW", 50));
		c.add(new Car(2017, "Audi", 45));

		// Call the sort function
		Collections.sort(c);

		// Iterate over ArrayList using for each loop
		for (Car car : c) {

			// Print the sorted ArrayList
			System.out.println(car.ModalNo + " " + car.name
							+ " " + car.stock);
		}
	}
}